Newton Raphon's Method - Help

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 :)

Answers (1)

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

The derivative I had works, is the code you gave me an easier and simpler method of coding it?
James Tursa
James Tursa on 23 Nov 2016
Edited: James Tursa on 23 Nov 2016
Depends on how you are using this. If this is being called inside an iterative loop, why would you go through all of that symbolic stuff on each iteration? But if this is only called once at the start of your code just to get the symbolic expressions for the function and its derivative, which are then used downstream in your iterative code, then maybe this makes sense. Without seeing the rest of your code it is hard to advise.
What I am doing is probably easy but since I'm not familiar with programming it's hard for me to explain.
The exercise is to determine if the function converges for x0=2, doing the necessary iterations to satisfy the tolerance of 0.5e-3.
I've only defined the expression and the derivative, to call it on the script for the Newton's method itself.
I'm not sure if I'm making myself clear, and I apologize if not doing so
James Tursa
James Tursa on 23 Nov 2016
Edited: James Tursa on 23 Nov 2016
Do you have the iterative stuff coded yet? (i.e., the x(k+1)=xk-f(x)/f'(x) part). Or do you need help with that?
Sorry for the noob question, but is it just writing x(k+1)=xk-f(x)/f'(x) and the respective iterations (k)?
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".
I've coded this so far, not sure if this is correct, but we have tried to get here logically according to the problem. We assumed 10 iterations, but we can't run it correctly yet, and we don't really know why we aren't understanding why
for
k=0
xk=2
xa(k+1)=xk-(f(xk)./y(xk))
Ea(k)=NaN
for
k=1:10
xk=xa(k-1)
xa(k+1)=xk-(f(xk)./y(xk))
Ea(k)=abs(xa(k+1)-xa(k))
end
if
Ea<=tolx
break
end
end
Thanks in advance

Sign in to comment.

Categories

Asked:

on 23 Nov 2016

Commented:

on 24 Nov 2016

Community Treasure Hunt

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

Start Hunting!