Newton Raphon's Method - Help

조회 수: 2 (최근 30일)
António Aragão
António Aragão 2016년 11월 23일
댓글: António Aragão 2016년 11월 24일
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 :)

답변 (1개)

James Tursa
James Tursa 2016년 11월 23일
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
James Tursa
James Tursa 2016년 11월 23일
편집: James Tursa 2016년 11월 23일
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".
António Aragão
António Aragão 2016년 11월 24일
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

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

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by