MATLAB: Am I getting error: Index must be a positive integer of logical

errornewton

Attached is the code. It is supposed to use the newton raphson method to find the root of a function. func is the function, dfunc is the derivative of the function, xr is the first "guess" (in this case it is .5), maxit is the maximum iterations to be performed.
The command I enter is: newtonraphson('@x -0.9*x^2 + 1.7*x +2.5', '@x -2*.9*x^2 + 1.7', 5, .01, 50)
but I cannot get back an answer that isn't erroneous.

Best Answer

You are providing a string as the parameter func, and you have func(xr) inside your code. That is an attempt to subscript the string.
'@x -0.9*x^2 + 1.7*x +2.5' is a string, not a function handle.
Hint:
@(x) -0.9*x^2 + 1.7*x +2.5