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

24 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 5 Oct 2014
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 &

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!