Professional Documents
Culture Documents
CPE7 Labsheet 2
CPE7 Labsheet 2
(13) if y == 0
(14) a = m;
(15) b = y;
(16) break
(17) end
(18) if x*y < 0
(19) b = m;
(20) else
(21) a = m;
(22) end
(23) end
(24) end
Where f = the continuous function
Let XLOWER = a;
Let XUPPER = b;
1. Check that f (x) is continuous and f (a) and f(b) have different signs.
Display that the function has the same sign at both endpoints of the interval.
Proceed to Step 2 – 4 (use for….end loop to process the number of iterations specified by
the user
𝑎+𝑏
2. Calculate the midpoint, m, of a and b such that m = 2
Line (9)
3. Now the interval [a,b] is split into two subintervals: [a,m] and [m,b].
4. Test to see if either f (a) and f (m) or f (m) and f (b) have opposite signs. Pick one of the two
subintervals that satisfies the opposite sign condition.
Line (12) shows the iteration number, the interval used for that iteration, the calculated
estimated root m, and the values for f(a) and f(m) for that iteration.
If f(m) = 0, stop the algorithm, m is the exact solution. Lines (13) to (17)
If f(a) * f(m) < 0, the new interval [a, b] for next iteration is a = a, b = m. Lines (18) and (19)
If f(a) * f(m) > 0, the new interval [a, b] for next iteration is a = m, b = b. Lines (20) and (21)
5. Repeat steps 2 through 4 with the new subinterval until the entered number of iterations is
satisfied.
Exercise 1. Create a function file using the Matlab code for Bisection Method
bisection_method_YourInitials.m
function m = bisection_method_RAGF(f, a, b, n)
alpha = f(a);
beta = f(b);
for i = 1:n
m = (a + b) / 2;
x = f(a);
y = f(m);
if y == 0
return;
elseif x * y < 0
b = m;
else
a = m;
end
end
end
Command Window
Command Window
𝑋𝐿*𝑓(𝑋𝑈) − 𝑋𝑈*𝑓(𝑋𝐿)
Xm = 𝑓(𝑋𝑈) − 𝑓(𝑋𝐿)
Exercise 2.
a. In the given code for Bisection Method, replace the formula for Xm using:
𝑎*𝑓(𝑏) − 𝑏*𝑓(𝑎)
m= 𝑓(𝑏) − 𝑓(𝑎)
falseposition_method_YourInitials.m
function m = falseposition_method_RAGF(f, a, b, n)
alpha = f(a);
beta = f(b);
end
for i = 1:n
x = f(a);
y = f(m);
if y == 0
return;
elseif x * y < 0
b = m;
else
a = m;
end
end
end
b. Find the root of x3 – 20 = 0 using Regula Falsi Method in 30 iterations with 1 and 4 as initial
intervals
Questions for b:
Command Window
Questions for c:
bisection_method(inline('x^3-20','x'),1,3,30)
Command Window
Questions for d: