Array indices must be positive integers or logical values.

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;

8 commentaires

Tommy
Tommy le 22 Avr 2020
Modifié(e) : Tommy le 22 Avr 2020
If you'd like f2 to be a function handle like f1, then use
f2 = @(x) 4*x.^3 - 2*225*x;
What should ea equal if x is 0?
hi tommy, thanks much for your assistance
I'm not sure what ea would be in this case. I tried ea as 100 and x = 0, but when i ran the program, I got an answer of 0, so I'm not sure. If possible, can you assist?
f1 = @(x) x.^4 - 225*x.^2 + 15625; %dfunc = str2sym('x.^4 - a*x.^2 + 1562'); f2 = diff(dfunc)
Please recheck your code. Your f1 has + 15625, but your commented dfunc has + 1562 . This makes a significant difference because with the + 15625 version, the four roots of the polynomial are all complex-valued, whereas with the + 1562 version, the four roots are all real-valued.
Hi Walter, thank you very much for commenting. That 15625 is the correct figure - essentially 125^2 from calculation and simplication of the polynomial. Tried to solve the file but only getting my answer as zero, so i'm not sure how to get a good guess for "ea" value and "x" so I can end up with a solution. Any help with this will be greatly appreciated, more than willing to learn to enhance my skills
WIth that code, after fixing f2 to be a function handle, you would get an error on the initial loop. You only define ea if x is non-zero (which really should be comparing x to its initial value whatever the initial value), but on the first loop x is 0, so you have not defined ea by the time you get to the next line comparing ea to eas.
In practice with code like that, if you start with any real-valued xr and with that equation, you will cycle around forever or until you give up on iterations, jumping all over the place. With that equation, to get anywhere you need to initialize xr to a complex value, and then it is able to find the solution. That equation has no real roots, so a newton type method restricted to real values can never find a root.
Okay no problem Walter, thank you very much for this information. Definitely helped A LOT. If you dont mind me asking for knowledge purposes, an optiion like bisection or fixed point iteration method would be useful in this case as well?
Consider any function, f(x) that returns a real result for real input, x. With f(x) always being real-valued, the difference between f(x) and f(x+delta) will alway be real-valued, so the derivative will always be real-valued.
Now consider any strategy that involves taking f(x) at different locations, and (with or without the aid of the derivative) uses real-valued expressions to project/decide a new location to test as being the root of f(x). With real-valued functions and real-valued operations, the result of the location projection would be real-valued. In such a configuration, if you start with a real-valued location, you end up with a real-valued location. It follows from this that such a scheme can never create a complex-valued location to probe at, and therefore such schemes can never find complex roots of a function. If the function has no real-valued roots, the scheme will not be able to proceed.
Bisection methods rely on having starting points of opposite sign, but in the case of a real-valued function that has no real roots, no such points exist in the reals, so bisection methods cannot improve the situation.
Hypothetically there could be projection schemes that can move into complex locations -- for example if the projection scheme involves taking the square root of a difference of values where the difference is not certain to be a non-negative real, then such a system could potentially discover complex roots.
However, if you have complex inputs then you risk complex values of the function, and figuring out which direction to head for a zero crossing is tricky if you are working from complex function values. There are even functions in which the real and imaginary components each cross zero but the function as a whole has no zero (I think...). Thus bisection approaches are at the very least difficult under complex conditions. The Newton type projections do not rely on detecting zero crossings and so do not have that concern.
Ahh I see, wow thank you so much for the thorough explanation and for the knowlegde on this Walter. This information alone is worthwhile and I will take it going forward to help with my coding involving such circumstances. I will share such information with my collegues as well as they seek to improve their skills in matlab when dealing with complex and real values. Stay safe out there!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by