Higher order derivatives
显示 更早的评论
Question 1: Higher order derivatives Write a function numdiff5 that approximates the fifth derivative of an unknown function f . The function numdiff5 has to be written in the file numdiff5.m, and its first line looks like this: function y=numdiff5(f,x) For any x it should return (an approximation of) the fifth derivative y = f (v)(x) of the unknown function f . You can assume that the function f can be evaluated everywhere.
I have created this script:
function y=numdiff5(f,x)
h=0.1;
dy=(f(x+h)-f(x-h))/(2*h);
dy5=(-f(x-3*h)+4*f(x-2*h)-5*f(x-h)+5*f(x+h)-4*f(x+2*h)+f(x+3*h))/(2*h^5);
for a=h/2;
dya=(f(x+a)-f(x-a))/(2*a);
dyb=((dya-dy)/3);
end
y=dya+dyb+((h^4)/160)*dy5;
end
because I was told that I needed to use Richardson extrapolation in order to improve accuracy but how have been told I have used it wrong. So how would I correct the script for accuracy >=10^-9?
回答(2 个)
bym
2011-5-16
I am still trying to digest the Richardson extrapolation, but I can offer a couple of suggestions.
1) your definition of h is fixed. In order to approximate the derivative, h has to be "small" compared to x. Perhaps h=x/100? Also, maybe redefining:
dy = (f(x+h)-f(x))/h
2) is dy5 derived from the taylor expansion? If so, you might want to check that.
Also, doesn't
for a=h/2
give you an error?
2 个评论
Moya
2011-5-17
Andrew Newell
2011-5-17
a = h/2 doesn't give an error because it sets a equal to h/2 and goes through the loop once. That makes the loop pointless.
Andrew Newell
2011-5-17
There is a lot of stuff that doesn't belong there. This line
dy=(f(x+h)-f(x-h))/(2*h);
is a finite difference formula for the first derivative, which you're not calculating. This line
y=dya+dyb+((h^4)/160)*dy5;
seems to be incorporating an estimate for the error in the expression. And, as I pointed out in a comment, the loop is pointless.
The idea behind Richardson extrapolation is to make two estimates of the quantity you want (the fifth derivative) and combine them. You could define an anonymous function
dy5= @(h) (-f(x-3*h)+4*f(x-2*h)-5*f(x-h)+5*f(x+h)-4*f(x+2*h)+f(x+3*h))/(2*h^5);
and then the expression you need is
y = dy5(h/2) + (dy5(h/2)-dy5(h))/3;
This is the MATLAB part. I'll leave it to you to decide whether this is accurate enough. That's the math part.
2 个评论
Moya
2011-5-17
Andrew Newell
2011-5-17
Moya, I think I have said too much already. We try not to do your homework for you because, in the long run, it is better if you solve the problem yourself. Clearly, you do not understand Richardson extrapolation. I think you should go back, review what it is and look at an example of its use, and then you'll see why I have practically written down the solution for you.
类别
在 帮助中心 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!