Invalid syntax at 'x'. Possibly a ), }, ] is missing.

11 ビュー (過去 30 日間)
Stefan
Stefan 2014 年 10 月 5 日
コメント済み: Stefan 2014 年 10 月 5 日
Hi everyone, I'm writing this code for a hw problem in class. for some reason, I'm getting a syntax error for "Unexpected Matlab expression". I cannot figure out what is wrong, but it seems to be a simple error that I'm just making a silly mistake on. It's giving me the error on the 'x' variable in line 9, column 15. Any help is greatly appreciated. Thank you!
x=0; %set starting value
nmax=10; %set max number of iterations
eps=1; %initialize error bound eps
xvals=x; %initialize array of iterates
n=0; %initialize n (counts iterations)
while eps>=1e-5&n<=nmax %set while-conditions
y=x-(x^3-3x^2+6x-30)/(3x^2-6x+6) %compute next iterate
xvals=[xvals;y]; %write next iterate in array
eps=abs(y-x); %compute error
x=y;n=n+1; %update x and n
end

採用された回答

Image Analyst
Image Analyst 2014 年 10 月 5 日
These two lines are fixed:
while eps>=1e-5 && n<=nmax %set while-conditions
y=x-(x^3-3*x^2+6*x-30)/(3*x^2-6*x+6) %compute next iterate
When you multiply numbers by anything, you need to use *, like 3*x, not just 3x. Also you need && in your while test, not &
  1 件のコメント
Stefan
Stefan 2014 年 10 月 5 日
Thank you very much! I knew it was a silly mistake...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by