Newton's Method for a polynomial equation

10 Ansichten (letzte 30 Tage)
Dorothy  Carter
Dorothy Carter am 12 Sep. 2017
Kommentiert: Dorothy Carter am 12 Sep. 2017
This is my code for the function:
function x=newton(x0,func,grad,xTOL)
%xo=initial guess
% func= function
% grad= derivative of function
% xTOL=tolerance
% solving f(x)=0 using the Newton's method with initial guess x0
%
xerror=xTOL*2;
while xerror>xTOL
x1 = x0 - feval(func,x0)/feval(grad,x0);
xerror = abs(x1-x0);
disp(x1);
x0=x1; % x1= x_k, x0=x_{k-1}
end
x= x1;
end
I called it here:
x0=1;
func=36*x^4-12*x^3+37*x^2-12*x+1;
grad=144*x^3-36*x^2+74*x-12;
xTOL=1*10^-6;
newton(x0,func,grad,xTOL)
But it keeps coming up as x is undefined. Can someone help me. Is there code I am missing from the file where I call the function?

Akzeptierte Antwort

James Tursa
James Tursa am 12 Sep. 2017
Bearbeitet: James Tursa am 12 Sep. 2017
You need to create function handles for your functions. As written, MATLAB thinks you are trying to evaluate the functions at a currently defined value of x. But x is not defined, hence the error. To fix this, create function handles:
func = @(x) 36*x^4-12*x^3+37*x^2-12*x+1;
grad = @(x) 144*x^3-36*x^2+74*x-12;

Weitere Antworten (0)

Kategorien

Mehr zu Polynomials finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by