I got "The expression to the left of the equals sign is not a valid target for an assignment."

5 ビュー (過去 30 日間)
Hello, when I run my code, i get error.
I can not see my fault. There is something wrong about i^2=-1 But I must use that expression.
Thank you.
clc
clear all
close all
fprintf('%10s %10s %10s %10s\n','k','real(xk)','imag(xk)','|f(xk)|')
k = 0 ;
i^2 = -1 ;
x = -2-2i;
f = @(x) x^3 - x - 3 ;
while abs(f(x)) > 10^(-5)
f = @(x) x^3 - x - 3 ;
diff = @(x) 3*(x^2) - 1 ;
xreal = real(x);
ximag = imag(x);
xnew = x - (f(x) / diff(x));
fprintf('%10.4f %10.4f %10.4f %10.4f\n',k,xreal,ximag,f(x))
x = xnew;
k = k +1;
end
  2 件のコメント
A. Sawas
A. Sawas 2019 年 4 月 16 日
編集済み: A. Sawas 2019 年 4 月 16 日
In Matlab you don't need to define the imaginary number (i) or (j).
i^2 = -1 ; % this is not a valid assignment
x = -2-2i;
hgrlk
hgrlk 2019 年 4 月 17 日
thank you!! i didnt know that

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2019 年 4 月 16 日
Just eliminate that line from your code. The way you've defined x on the next line will make it complex since i is already defined to be the imaginary unit.

その他の回答 (1 件)

Kelly Kearney
Kelly Kearney 2019 年 4 月 16 日
As others have pointed out, i is already assigned the value you want by default.
But assuming it weren't, the proper way to assign the value would be:
i = sqrt(-1);
The error is basically telling you that Matlab won't implictly do any algebra for you... you can only assign a value to a variable, not to a function or equation like you attempted to do.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by