Array indices must be positive integers or logical values.

1 view (last 30 days)
y=input('thecomplexvalue of z=');
x=input('therealvalue of z=');
tol=input('the value tolerance tol=');
z=x+1i*y;
funcnew01=z^5+z^3-pi;
funcnew02=diff(funcnew01);
k=0;
if real(z)==0
y=z;
disp('y funcnew01(y) funcnew02(y) k')
disp('........................................')
while abs(funcnew01(y))>tol
y=y-(funcnew01(y)/funcnew02(y));
k=k+1;
end
else
x=z;
disp('x funcnew01(x) funcnew(x) k')
disp('......................................')
while abs(funcnew01(x))>tol
x=x-(funcnew01(x)/funcnew02(x));
k=k+1;
end
end
I get this error in line 20, I'm not shure why I'm trying to make a code for finding the roots of the complex function with the Newton-Raphson method

Accepted Answer

the cyclist
the cyclist on 5 Sep 2020
Edited: the cyclist on 5 Sep 2020
It seems that you have tried to use this line of code:
funcnew01=z^5+z^3-pi;
to define a general function on z. Then you try to use that "function", e.g. like this:
funcnew01(y)
But that is not what that line of code does. Rather that first line of code, simply defines another variable with the value
z^5+z^3-pi
for whatever the value of z is at the time it is executed.
The specific error you are getting is that your code
funcnew01(x)
is trying to index into a variable (which can only be done with positive integers or logicals).
If you want to define a function, you instead need
funcnew01 = @(z) z^5+z^3-pi;
which can then be called and evaluated the way I believe you intend. See the documentation on anonymous functions for details.
You similarly need to fix funcnew02. I'm not sure what you intend there. A derivative?

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!