How to get Matlab to return answers with ten-digit accuracy?

Hi,
Answers returned by Matlab (e.g. from the ode45 solver, or from the wrapTo2Pi function that maps accumulating angles to [0, 2pi] ) seem to be within four digits of accuracy; that is, it rounds to four digits after the decimal place.
I'm currently studying convergence and root-finding and would like ten-digit accuracy -- so, a bit beyond the standard function tolerance of 1e-6 or 1e-7.
How can I get Matlab answers with ten-digit accuracy?
Thanks,

Answers (2)

Write this on start of your script
format long
If you're reading the values from the workspace, find tab 'View' and under 'Format', click 'Long Fixed Decimal'.
Edit: this only changes displayed output, see James' answer.

10 Comments

Hi Mario,
Could I write this in my script file after the ode45 solver is called, i.e. when I'm trying to view printouts of my solutions, or should I write this line at the very beginning, before even all of the modeling code and anonymous functions are defined? I guess I'm wondering at what point does Matlab start using numbers with only four digits after the decimal place -- is it after the ode45 solver is called?
Thanks,
The "format" command only changes the displayed output ... it does not change the precision of the underlying calculations which will still be double precision. You can issue the format command anytime you like, either manually at the command line before you run the script or put it as a line anywhere in your script. Changing the display format will not change the answers from ode45 ... it simply changes how they are displayed.
Hi James,
I see; I would very much like to keep the precision of the solutions and transfer them over to another file, where I write another function that uses the solutions as inputs. So yes, I don't simply want a display of more precision, I want to preserve it.
I'm guessing this should be relatively straightforward code to write? Maybe put the solutions in some sort of a loop ... ?
I'll think about it a bit. Thanks for clarifying what goes on in Matlab's underlying calculations vs. what is displayed.
Thanks,
How are you transferring your solutions to the other function? That step you need to be careful so you don't lose precision.
Hi James,
I transfer solutions over by composing the new function with the old function that embeds the ode45 solver, and the new function is a difference-mapping, so something like g(x) = f(x) - x.
So, perhaps there are no issues with the precision of the inputs f(x) into the function g, since I am calling f directly in the script file for g. However, I am inputting values of x manually, each time updating x = (x_1, x_2, ..., x_n), based on what the simulations are like as I run them for longer and longer (especially if I am close to some periodic-like behavior that I'm seeking).
So, I think the potential for issues lies in me manually inputting the vector x in the function g. I'm trying to solve g = (0,0, ... , 0) eventually, with fsolve, but not until I know that I am close to a root -- and can supply fsolve with a decent initial guess.
That's what I'm currently thinking about. I'd be happy to hear more of your thoughts.
Thanks,
"... perhaps there are no issues with the precision of the inputs f(x) into the function g, since I am calling f directly in the script file for g ..."
Correct. There are no precision loss issues with this approach.
"... the potential for issues lies in me manually inputting the vector x in the function g ..."
Yes. This is where you need to be careful. It would be best to feed the results directly, as above. E.g., saved in a variable. Or you could save the results in a mat file and then load them later on ... this will also preserve the full precision. Try to avoid manually copying numbers from the screen as this will be prone to losing precision if not done correctly.
format long g comes close to giving complete precision, but in some cases you may need two more digits beyond what that format gives you. It is safest to fprintf() your values with %.17g
Hi James,
Ok, will do - and thanks so much for your help.
Hi Walter,
Ok, thanks so much - have a good night.

Sign in to comment.

The answers returned by Matlab actually have the full available precision, but for display purposes they are printed in the command window with a much smaller precision. You can change this default behavior by typing 'format long' or 'format longEng' in the command window.

Categories

Products

Release

R2017a

Asked:

on 17 Sep 2020

Commented:

on 18 Sep 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!