Newton's method

조회 수: 6 (최근 30일)
Kevser Cifci
Kevser Cifci 2021년 9월 12일
댓글: Kevser Cifci 2021년 9월 12일
Hello, I have to write a simple Newton function but it's not working:
%%file newton.m
function out = newton (f, df, x, eps)
k = 0;
h = f(x)/df(x); % error
while abs(h)>= eps && (k < 1000)
h = f(x)/df(x);
x = x - h;
k = k+1;
end
out = x;
end
Then I have to create a matrix which stocks the solutions of the newton's function and create a graph from this matrix but it's not working either:
%%file f1.m
function [out] = f1(x)
out = x.^3 - (6*x.^2)+ 11*x - 6;
end
%%file df1.m
function out=df1(x)
out=3*x.^2 - 12*x + 11;
end
Tab[]
for i = -1:0.05:5 %% I have to apply Newton on the values x = [-1:0.05:5]
Tab = [Tab [newton(i,f1,df1,10^(-8))]] %% error message: the variable Tab appears to change size on every loop iteration
end
plot(-1:0.05:5,Tab)
Can someone please help me?

채택된 답변

Matt J
Matt J 2021년 9월 12일
편집: Matt J 2021년 9월 12일
Tab=[];
for x = -1:0.05:5
Tab = [Tab , newton(@f1,@df1,x,10^(-8)) ]
end
  댓글 수: 1
Kevser Cifci
Kevser Cifci 2021년 9월 12일
Thank you for your answer!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by