Newton's Method for a polynomial equation

1 次查看(过去 30 天)
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?

采纳的回答

James Tursa
James Tursa 2017-9-12
编辑:James Tursa 2017-9-12
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;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by