Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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 )
D2h=zeros(n,1);
x=pi;
for i=1:n
h=10^-i;
D2h(i,1)=(sin(x+2*h)-2*sin(x+h)+sin(x))/h^2;
end

share|improve this question
2  
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
4  
I'd try writing after the title of the function "format long;". –  Danis Fischer Apr 28 '13 at 14:44
1  
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

1 Answer 1

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|improve this answer

Your Answer

 
discard

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.