Array indices must be positive integers or logical values.

1 回表示 (過去 30 日間)
Sergio Manuel Benhumea Villaseñor
Sergio Manuel Benhumea Villaseñor 2020 年 9 月 5 日
編集済み: the cyclist 2020 年 9 月 5 日
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

採用された回答

the cyclist
the cyclist 2020 年 9 月 5 日
編集済み: the cyclist 2020 年 9 月 5 日
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?

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by