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

4 次查看(过去 30 天)
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 &

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by