MATLAB: Newton’s Method for a polynomial equation

newton's methodpolynomial function

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?

Best Answer

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;