# Matlab Question, need help

• MATLAB
Hi,
Im new to Matlab, and my lecturer asked me to do his question which is:
Find one root of equation 8x^3-36x^2+54x-12=0 using the bisection method.

Ive already read the Introduction to MATLAB 7, and Numerical Methods for Engineers. And the only clue that i get is:

Step 1: choose lower x1 and upper x2 guesses for the root such that the function changes sign over interval. This can be checked by ensuring that f(x1).f(x2)<0

Step 2: an estimate of the root xr is determined by xr=(x1+x2)/2

Step 3: Make the following evaluations to determine in which subinterval the root lies
a) if f(x1).f(x2)<0, the root lies in the lower subinterval. Therefore, set x2=xr and return to step 2
b) if f(x1).f(x2)>0, the root lies in upper subinterval. Therefore, set x1=xr and return to step 2
c) if f(x1).f(x2)=0, the root equals xr, terminate the computation.

This is what i did, and i know it is completely wrong.

%program question 1 Numerical analysis
%Find one root of equation 8x^3-36x^2+54x-4(P+Q)=0
%where P=1,Q=2
disp('8x^3-36x^2+54x-12=0')
disp('choose lower x1 and upper x2 guesses for the roots')
x1=input('x1=')
x2=input('x2=')
a=x1
b=x2
f(x1)=(8*x1^3)-(36*x1^2)+(54*x1)-12
f(x2)=(8*x2^3)-(36*x2^2)+(54*x2)-12
xr=(a+b)/2
iter=0
e=f(x1)*f(x2)
for e=0
while e<0
xr=(a+xr)/2
iter=iter+1
end
while e>0
xr=(xr+b)/2
iter=iter+1
end
end
disp('root')
disp(xr)
disp('number of iterations')
disp(iter)

Step 1: choose lower x1 and upper x2 guesses for the root such that the function changes sign over interval. This can be checked by ensuring that f(x1).f(x2)<0

Step 2: an estimate of the root xr is determined by xr=(x1+x2)/2

Step 3: Make the following evaluations to determine in which subinterval the root lies
a) if f(x1).f(x2)<0, the root lies in the lower subinterval. Therefore, set x2=xr and return to step 2
b) if f(x1).f(x2)>0, the root lies in upper subinterval. Therefore, set x1=xr and return to step 2
c) if f(x1).f(x2)=0, the root equals xr, terminate the computation.

I think there is a problem here with the algorithm. What you're supposed to do is start with an interval [x1,x2] on which you know f changes sign (therefore it crosses zero), then determine whether the zero is in the left half of the interval or the right half, and then cut the interval in half and keep the half which is known to contain the zero crossing. Repeat this and your interval should approach the root. To check which side the zero crossing is in, you need to check the sign of f(x1)*f(xr) or f(xr)*f(x2), NOT f(x1)*f(x2). You want to check one half of the interval. As you have it, it checks the entire interval, which is useless because you know it's in there, and eventually it will take the wrong half and you will be screwed.

Code:
%program question 1 Numerical analysis
%Find one root of equation 8x^3-36x^2+54x-4(P+Q)=0
%where P=1,Q=2
disp('8x^3-36x^2+54x-12=0')
disp('choose lower x1 and upper x2 guesses for the roots')
x1=input('x1=')
x2=input('x2=')
a=x1
b=x2
f(x1)=(8*x1^3)-(36*x1^2)+(54*x1)-12
f(x2)=(8*x2^3)-(36*x2^2)+(54*x2)-12
xr=(a+b)/2
iter=0
e=f(x1)*f(x2)
for e=0
while e<0
xr=(a+xr)/2
iter=iter+1
end
while e>0
xr=(xr+b)/2
iter=iter+1
end
end
disp('root')
disp(xr)
disp('number of iterations')
disp(iter)

You have a good start here, but there's a few things you need to change. To avoid having to use inline functions you should evaluate
fx1=(8*x1^3)-(36*x1^2)+(54*x1)-12
fxr=(8*xr^3)-(36*xr^2)+(54*xr)-12
at each iteration inside the loop. Also move the calculation of e inside the loop. And since I don't like while loops, I would change to
if e>0,
elseif e<0
end
And e probably won't ever be zero exactly, so add something like
if norm(e)<1e-12 to tell when e is very small
I don't know if it was intentional, but if you put a semicolon ; at the end of the line, it won't display the result at every line.
And to prevent getting stuck in an infiinite loop, change 'while e=0' to 'for iter = 1:100', then you can eliminate iter=iter+1.
And keeping track of a,b,x1,x2 seems repetitive, so I would eliminate a,b and only use x1,x2.

In the end you will have something like this
Code:
%program question 1 Numerical analysis
%Find one root of equation 8x^3-36x^2+54x-4(P+Q)=0
%where P=1,Q=2
disp('8x^3-36x^2+54x-12=0')
disp('choose lower x1 and upper x2 guesses for the roots')
x1=input('x1=')
x2=input('x2=')
xr=(a+b)/2;

for iter = 1:100
fx1=(8*x1^3)-(36*x1^2)+(54*x1)-12; % compute f at left end
fxr=(8*xr^3)-(36*xr^2)+(54*xr)-12; % compute f in middle
e=fx1*fxr;  % check if zero is in left half
if e<0  % root is in left half of interval
x2=xr;  % this cuts the interval in half, keeping the left half
xr=(x1+x2)/2; % compute center of new interval
elseif e>0 % root is in right half of interval
x1=xr; % this cuts interval in half, keeping right half
xr=(x1+x2)/2;  % compute center of new interval
end
if norm(e)<1e-12, break, end
end
disp(['root = ',num2str(xr)])
disp(['number of iterations = ', num2str(iter)])
Let me know if something I said doesn't make sense or seems wrong.