Newton Ralpson on matlab

4 Ansichten (letzte 30 Tage)
matt noman
matt noman am 25 Feb. 2020
Beantwortet: David Hill am 25 Feb. 2020
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 Kommentare
darova
darova am 25 Feb. 2020
matt noman
matt noman am 25 Feb. 2020
comments

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Hill
David Hill am 25 Feb. 2020
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';

Community Treasure Hunt

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

Start Hunting!

Translated by