I am working with numerical differentiation and I am approximating the first derivative of f(x)=sin^2(x) with a fourth order approximation of the form:
I have the following code to approximate f'(x) at a = pi/4
k = 1:15;
h = 10.^(-k);
a = pi/4;
D = (1/12.*h).*(-3.*sin(a-h).^2-10.*sin(a).^2+18.*sin(a+h).^2-...
6.*sin(a+2.*h).^2+sin(a+3.*h).^2);
As h gets smaller D should be getting closer to 1 but when I run this code D gets closer to zero. Am I imputing the sin term incorrectly?

 채택된 답변

Roger Stafford
Roger Stafford 2015년 2월 25일

1 개 추천

Your code for 'D' has an error. You have multiplied by 'h' instead of dividing by it. The code should read:
D = (1/12./h).*(-3.*sin(a-h).^2 ...........

댓글 수: 4

Roger Stafford
Roger Stafford 2015년 2월 25일
Out of curiosity, Jim, why have you used an offset polynomial for your fourth order derivative approximation - that is, from x = a-h to x = a+3*h instead of a centered version from x = a-2*h to x = a+2*h ?
John D'Errico
John D'Errico 2015년 2월 25일
My guess is a homework assignment?
Jim Oste
Jim Oste 2015년 2월 25일
Exactly, we were getting practice with numerical differentiation and using Taylor expansions to find approximations with varying offsetted polynomials.
I thought so. A worthwhile thing to do is to look at the centered difference to compute that same value, varying over -2h to +2h. Why would it be a better choice of method in general? Thus, something like this (assuming I did my back of the envelope computations properly)
((f(2h) - f(-2h)) - 8*(f(h) - f(-h)))/(12h)
Why might the above template be a better choice in general, if it is available?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2015년 2월 25일

댓글:

2015년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by