Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am making a function for finding the approximate value of the second derivative (using the forward difference formula) of $sin(x)$ in $x=\frac {\pi}{3}$ with step $h=10^{-n}$ for $n=1, 2, 3, 4, 5$

The problem is that MATLAB rounds all the results by 4 positions after the decimal which makes all approximations equal. How can I fix this?

Here is the code:

function [ D2h ] = Forward( n )
for i=1:n

share|cite|improve this question
When I did it Alpha I get $-0.8653$ for a step of $0.1$ and $-0.8660$ for $0.01$, which differ in the third decimal. You are right they are very close. I can't help with the Matlab question. – Ross Millikan Apr 28 '13 at 14:41
I'd try writing after the title of the function "format long;". – user65985 Apr 28 '13 at 14:44
yes, "format long" works, and I also found a function for rounding numbers. For those who are curious click here – Silviya Apr 28 '13 at 14:53

Something that many people run into is that by default, matlab only displays 4 decimals. Of course, the precision is not lost, it is just not shown.

If you try help format you can find ways to change this behavior.

Some interesting options:

format short % Default setting, 4 decimals
format long % Many decimals
format longG % Many decimals if available, won't show trailing zeros
share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.