Matlab derivative approximation - can anyone help?
Afficher commentaires plus anciens
I've been puzzling over this last problem in my assignments for the past week or so, and still with no end in sight. Some of the folks here on MathWorks have given me a few pointers, but I still can't make this code work.
function r=myderivative(f,a,tol)
y = 0;
maxIters = 500;
n = 1;
while n<=maxIters
h = 1/n;
while (1/n+1)-h<tol;
y = (f(a+h)-f(a))/h;
end
n = n + 1;
y = (f(a+h)-f(a))/h;
end
r=y;
end
The problem in question instructed me to make an m-file mimicking a derivative function, which would apply (f(a+h)−f(a))/h to f(a) again and again (with h turning into 1, then 1/2, then 1/3...) until the successive values dipped under the "tol" value.
However, this current code skips over pretty much all the stuff after "y=0" and spits out 0 as the value for every set of (f,a,tol).
Could anyone spare a bit of help? I would greatly appreciate it.
1 commentaire
dpb
le 22 Août 2014
Well, work thru the results of what you've written...for example, look at the following that displays the condition you've calculated for the comparison to tol in the second loop.
>> maxIters=3;
>> n=1;
>> while n<=maxIters
h = 1/n;
(1/n+1)-h
n = n + 1;
end
I just took out the computations and looked at the looping construct. You don't give a value for tol but note that your comparison value never changes as you've got it defined.
Réponses (1)
Star Strider
le 22 Août 2014
0 votes
You need to track the successive values for the derivative y and compare them with the current estimate. The derivative calculation has to go before the second while block, and the second while block has to compare the previous value of the derivative with the current value. As long as that difference is >=tol, replace the previous value of the derivative with the current value and continue.
16 commentaires
Chong
le 22 Août 2014
Star Strider
le 22 Août 2014
I actually got it to work with the changes I described, so I was confident in suggesting them. I’d have posted my solution, but I don’t want to deprive you of the satisfaction of getting it to work yourself! I’ll post it later if you like.
(This is relatively straightforward, and isn’t like the L’Hospital example, where you could not be expected to know that the anonymous functions lost their definitions as functions in the symbolic diff statement, and that you needed to use subs to make it work.)
Chong
le 22 Août 2014
Star Strider
le 22 Août 2014
OK. Here’s the ‘guts’ of the function:
yp = 0;
maxIters = 500;
n = 1;
while n<=maxIters
h = 1/n;
y = (f(a+h)-f(a))/h;
while yp - y > tol;
yp = y;
end
n = n + 1;
end
r=y;
You only needed to make a few changes.
Chong
le 22 Août 2014
Star Strider
le 22 Août 2014
If I remember correctly, the derivative of sin(x) is cos(x), and cos(pi/4)=0.7071...
The code seems to be correct.
Chong
le 22 Août 2014
Star Strider
le 22 Août 2014
It can be as rough as you want it to be, but has an appropriately low error at a tolerance of 1E-10 or so. For me, the whole idea of an iterative approximation scheme is to generate as accurate and precise an estimate as possible.
Star Strider
le 23 Août 2014
How did you come up with 0.577320817064314 as cos(pi/4)? That’s just never going to work because cos(pi/4)=0.7071...
If that’s the goal, I yield.
Chong
le 23 Août 2014
Image Analyst
le 23 Août 2014
I don't understand your challenge. It's your homework. If you can do it, then do it. Why should Star waste his time doing something that you're already going to do by yourself anyway? I'm sure he could have done it already instead of giving you hints, if had wanted to.
Chong
le 23 Août 2014
Star Strider
le 23 Août 2014
It actually appeared a week ago here, posted by a classmate. I solved it then for fun, essentially in the time it took to type it, but didn’t post it as an Answer to that Question. If the derivative of sin(pi/4) was supposed to be 0.57732..., it was a trick question, and no one could have gotten it.
Chong
le 23 Août 2014
Star Strider
le 23 Août 2014
Modifié(e) : Star Strider
le 23 Août 2014
My original code (from a week or so ago):
tol = 1E-12;
dfdx = @(f,a,h) (f(a+h)-f(a))./h;
dd = Inf;
dprv = 0;
k1 = 1;
while dd > tol
h = 1/k1;
df = dfdx(f,a,h);
dd = df-dprv;
dprv = df;
k1 = k1+1;
end
df % Result
produces:
df =
707.1039e-003
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
