# Array indices must be positive integers or logical values.

8 views (last 30 days)

Show older comments

Good day, I am trying to run this code but I cant seem to know what the issue is as to why I am constanting getting this error. I have cleared my workspace so its free but to no avail. Any help would be appreciated please.

**Error in Command Window**

>> nonlinear_equation

Array indices must be positive integers or logical values.

Error in nonlinear_equation (line 32)

xr = xr - f1(xr)/f2(xr);

................................................................................................................................................................................................................................................................................

**CODE IN Matlab**

>>

% An M-file to implement the Newton-Raphson method.

function [root, ea iter]=nonlinear_equation(f1, f2,xr,j)

% newtraph: Newton-Raphson root location zeroes

% [root, ea, iter] = nwetraph (func, dfunc, xr, es, maxit):

% Uses Newton-Raphson Method to find the root of a function

% Input:

% func = name of function

% dfunc = name of derivative of function

% xr = initial guess of the root

% es = desired relative error (default = 0.0001%)

% maxit = maximum allowable iterations (default = 50)

% Output:

% root = real root

% ea = approximate relative error (%)

% iter = number of iterations

% Setting coefficients of Equation

x=0;

% Making equations to be solved

f1 = @(x) x.^4 - 225*x.^2 + 15625; %dfunc = str2sym('x.^4 - a*x.^2 + 1562'); f2 = diff(dfunc)

f2 = 4*x.^3 - 2*225*x;

es = 0.0001;

maxit = 50;

iter = 100;

xr=x;

while (1)

xrold=xr;

xr = xr - f1(xr)/f2(xr);

iter = iter+1;

if x ~=0, ea = abs ((x - xold)/x)*100; end

if ea<=es || iter >= maxit, break, end

end

root = x;

### Answers (1)

Image Analyst
on 22 Apr 2020

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!