Newton Raphon's Method - Help
2 views (last 30 days)
Show older comments
Hello all
I'm a student and recently acquainted with MatLab, and I have to program Newton's method on it for a work.
The thing is, I'm not sure how to code it in the program.
I have the following function and values: f(x)=e^x-4x^2, with a x0?2 and a max tolerance of 0.5*10^-3. However, I have no limit for max iterations (it says "Do the necessary number of iterations" to achieve that tolerance)
Knowing that Newton's expression is x(k+1)=xk-f(x)/f'(x), I've coded the derivative of that function symbolically (in a function file, not script).
Code:
function [ y,f,f2 ] = derivf2(x)
syms t
f2=exp(t)-4.*t.^2;
f=@(t)diff(f2);
y=eval(subs(f,t,x));
end
My question is. How do I code the actual method on Matlab itself?
I've tried several times, but I ran across some problems. My intention isn't to have the exercise completely solved, but to have a general idea on how to do it to help me solve this and other exercise.
Thanks in advance
EDIT: I'm a second year college student of Chemical Engineering, I've never faced programming in my entire life until now, so please be patient with me :)
0 Comments
Answers (1)
James Tursa
on 23 Nov 2016
I don't know why you would use all of the symbolic stuff inside your function (as opposed to just hard coding the derivative), but here is a way to get that part of it to work:
function [ y,f,f2 ] = derivf2(x)
syms t
f2 = exp(t) - 4.*t.^2;
f = diff(f2);
y = subs(f,t,x);
end
7 Comments
James Tursa
on 23 Nov 2016
Edited: James Tursa
on 23 Nov 2016
OK, just piecing parts of your code together (making use of your symbolic stuff), here is a start for you:
syms t
F = exp(t) - 4*t^2
FP = diff(F)
f = str2func(['@(t)' char(F)])
fp = str2func(['@(t)' char(FP)])
x = 2
tolerance = 0.5*10^-3
%
% You need to put your iterative code here for the x = x - f(x)/fp(x) part
%
So, for the code you need to insert above, you will need to create a loop of some kind with an appropriate test condition, and inside that loop will be the "Newton expression".
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!