Newton Ralpson on matlab

4 ビュー (過去 30 日間)
matt noman
matt noman 2020 年 2 月 25 日
回答済み: David Hill 2020 年 2 月 25 日
In matlab I am supposed to enter a starting guess ?0 to use as the first value for the Newton-Raphson method. If this guess causes the derivative of the temperature function to be zero (i.e. if ? ′ (?0 ) = 0 ), the method will immediately fail due to a division by zero. Check if the initial guess will cause the method to fail. If so, prompt the user to enter a new initial guess until the user enters a valid value. If the user does not enter an acceptable guess after 3 attempts, produce an error and terminate the program.
this is my code so far but somethings seems to be going can wrong can someone help me fix it
here is the commented code
f = @(x) (cos(x));
%
% fd = @(x) (-sin(x));
%
% n=10;
%
% i=1;
%
% p0=0;
%
% while(p0==0),
%
% p0 = input('Enter the intial approximation: = ');
%
% end
%
%
%
% tol = 0.0001;
%
% while(i<n),
%
% d = f(p0)/fd(p0);
%
% p0 = p0 - d;
%
% if(abs(d) < tol),
%
% break;
%
% else
%
% i = i+1;
%
% end
%
% end
%
% disp(['The Func. Value = ',num2str(p0)]);
  2 件のコメント
darova
darova 2020 年 2 月 25 日
matt noman
matt noman 2020 年 2 月 25 日
comments

サインインしてコメントする。

回答 (1 件)

David Hill
David Hill 2020 年 2 月 25 日
You could try something like this:
function root = newtonR(f,fp,tol)
for k=1:3
x=input('root guess');
if ~isinf(1/fp(x))
root=x-f(x)/fp(x);
while abs(root-x)>tol
x=root;
root=x-f(x)/fp(x);
end
return;
end
end
xf='error';

カテゴリ

Help Center および File ExchangeNewton-Raphson Method についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by