Using the Bisection Method,calculating xr and approximate errors???

Hi, I tried to solve a question using the bisection method, trying to find out xr (root of eq.) and aprroximate error, but there is a problem with my program that I need to define xrold anyhow as the value of xr changes in every iteration. In addition, I need to find Ea=((xr-xrold)/xr))*100 using the old and new values for xr in each step once again. I don't know how to employ this circle for each values of xr. Could you possibly help?
That was the program I made where I got an error at xrold value that obviously, it hasn't been defined properly;
In the question we have the given values of Es, xl, xu and a polynomial function which is f(x)=26+85*x-91*x^2+44*x^3-8*x^4+x^5
%Solve the equation using the bisection method.
xl=0.5;
xu=1;
Es=10;
iter=0;
while(1)
xrold=xr;
f=@(x) -26+85*x-91*x^2+44*x^3-8*x^4+x^5;
xr=(xl+xu)/2;
iter=iter+1;
if xr~=0;
Ea=abs((xr-xrold)/xr)*100;
end
test=f(xl)*f(xr);
if test<0;
xu=xr;
elseif test >0;
xl=xr;
else
Ea=0;
end
if Ea<Es
break
end
end

5 Comments

Onur - what exactly are you trying to find using this method and the polynomial that you have defined?
well, I am taking Numerical Analysis courses, and this course's main objective is showing such alternative methods and approaches for solving equations, mainly the equations that are too complex to solve with ordinary methods we normally use. (The equation given in the question is not really complex to prefer these methods, but as a learner we are supposed to practice with such easy problems)
As for this question, I need to create a computer program to solve based on bisection method with iterations.
But what are you trying to solve for given the polynomial and the interval that you have defined?
Ohh, trying to find out xr (root of eq.) and aprroximate errors.
But the root we predict with our iterations doesn't give us the exact root since we just make use of approximations, recalculating xr in each turn, and finally finding a suitable value for xr after some iterations which is supposed to be so close to the real root.
And as you can see our approximated root must be determined based on the method we use and the iterations, and iterations are repeated based on the criteria that we must check for each iteration(step) that approximate error should be greater than Prespecified error (given in the problem).From the moment, they either start to be equal or prespecified error(Es) becomes greater than approximate error we halt iterating and setting the final value of xr as the alternative value from this iteration.

2. Make an octave code to integrate ex with respect to dx from 0 to 1, by Simpson’s ⅓ rule. Divide the limits into 6 equal parts.

Please solve this

Sign in to comment.

 Accepted Answer

Onur - if the problem is because you don't have an xr on the first iteration of the while loop, then just wait until you do. And so allow one iteration to pass without you calculating the Ea value. Try the following
f=@(x) -26+85*x-91*x^2+44*x^3-8*x^4+x^5;
while iter<1000
xr=(xl+xu)/2;
iter=iter+1;
test=f(xl)*f(xr);
if test<0;
xu=xr;
elseif test >0;
xl=xr;
end
if iter > 1
if abs(xr)>0
Ea=abs((xr-xrold)/xr)*100;
if abs(Ea)<Es
break
end
end
end
xrold=xr;
end
In the above, note the following:
  • define the anonymous function outside of the while loop (no need to do it on every iteration);
  • cap the number of iterations in the while loop to 1000 so that we don't get stuck in an infinite loop;
  • only calculate Ea on every iteration after the first one; and
  • initialize xold at the end of the iteration

4 Comments

oh yes, that's it. Thank you so much I always have problems with defining the former value as an unknown just like the xrold value in this program.
I am glad that prefectly works, and gives the same result I solved using iteration by hand... And my final question is how can we display all of Ea values calculated in each step?
You can keep track of your Ea values by storing them in an array at each iteration of the while loop.
3. Make an octave code to find the root of cos(x) – x * ex = 0 by using bisection method. The answer should be corrected up to four decimal places

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!