Help understanding "Missing operator, comma, or semicolon" error.

12 views (last 30 days)
Hello I'm taking Numerical Methods course in my college. And am trying to write a program for a method called 'bisection method' and this is my program :
clear;
fx=inline('sin(0.01*x)+cos(0.75*x^2)');
xl=2;
xu=2.5;
n=5 % number of iteration
xr_old[];
for i=1:n
xr=(xl+xu)/2;
ea=abs((xr - xr_old)/xr)*100;
xr_old=xr;
% To display the results: Type the following command %
if i==1
dis('iteration Xl Xu Xr f(Xl)*f(Xu) ea');
end
dis([i xl xu xr fx(xl)*fx(xu) ea]);
if fx(xl)*fx(xr) < 0
xu=xr;
else if fx(xl)*fx(xu) > 0
xl=xr;
else
return;
end
end
end
When I want to run it, am keeping getting the following error:
>> bisection
??? Error: File: C:\MATLAB6p1\work\bisection.m Line: 6 Column: 7 Missing operator, comma, or semicolon.
So what is wrong with it?

Answers (1)

Andreas Goser
Andreas Goser on 1 Mar 2011
I get
??? Error: File: bisection.m Line: 6 Column: 7 Unbalanced or unexpected parenthesis or bracket.
Which explains better. Problaly you want to do
xr_old=[];
  4 Comments
Mubarak
Mubarak on 2 Mar 2011
Yes.
I've corrected it.
But still it is display only 1 row.
I think we must type i=i+1; before the last end, Right?
Andreas Goser
Andreas Goser on 2 Mar 2011
Well with i=i+1, your whole for i=1:n loop would be pointless, right?
After the first run of the loop "fx(xl)*fx(xu)" is <0, so the "return" command fires. I do not know what you intend to do, but the codes executes correctly.
Let me share my concern that your project will be succesful with this sequential progress. I encourage you to take the warning and error messages serious and look at programming tools like the MATLAB debugger and M-Lint.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!