Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Republic of the Philippines

Laguna State Polytechnic University


Province of Laguna
College of Engineering

Student: Ron Alfred G. Flaviano

Course/Year and Section BSCpE-2B

Academic Year 2023-2024

PO addressed Understand the basic concepts of computer


software tools related to numerical analysis in
dealing with engineering problems

Intended Learning Outcome To learn how to implement in Matlab the


different Bracketing and Open Methods in
solving nonlinear transcendental and
polynomial function techniques

CPE7 – Numerical Methods


Laboratory Exercise No. 2

NON-LINEAR TRANSCENDENTAL AND POLYNOMIAL FUNCTION TECHNIQUES


(Bisection Method and Regula Falsi)

Part 1. Bisection Method

(1) function m = bisection_method_YourInitials(f,a,b,n)

(2) alpha = f(a);


(3) beta = f(b);

(4) if alpha*beta > 0


(5) disp(['The function has the same sign at both endpoints of the interval.'...
(6) 'Please change the endpoints and run again']);
(7) else
(8) for i = 1:n
(9) m = (a + b)/2;
(10) x = f(a);
(11) y = f(m);
(12) fprintf('iteration # %d a = %f b = %f m = %f, f(a) = %f f(m) = %f\n',i,a,b,m,x,y)

(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

a = the lower interval

b = the upper interval

n = number of iterations required

Let XLOWER = a;
Let XUPPER = b;

1. Check that f (x) is continuous and f (a) and f(b) have different signs.

Line (2) calculates f(a) for lower interval a


Line (3) calculates f(b) for upper interval b

Lines (4) – (6)

If f(a) * f(b) > 0,

Display that the function has the same sign at both endpoints of the interval.

Lines (7) – (24)

if not/else [f(a) * f(b) < 0, or f(a)*f(b) = 0]

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.

Lines (10) and (11) calculates f(a) and f(m)

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);

if alpha * beta > 0


error('The function has the same sign at both endpoints of the interval. Please change
the endpoints and run again.');
end

for i = 1:n

m = (a + b) / 2;

x = f(a);
y = f(m);

fprintf('iteration # %d a = %f b = %f m = %f, f(a) = %f f(m) = %f\n', i, a, b, m, x, y);

if y == 0
return;
elseif x * y < 0
b = m;
else
a = m;
end

end

end

Calculate the root of cos 2x + sin x = 0.

a. With 0 and 2 as initial intervals, in 1 iteration


We use the inline command in Matlab to display the entire function in a single line when we call
our bisection method:
f = inline('cos(2*x)+sin(x)','x')
a = 0, b = 2, n = 1

Type the following in the Command window


bisection_method(inline('cos(2*x)+sin(x)','x'),0,2,1)

Command Window

b. With -1 and 2 as initial intervals, in 1 iteration

Command Window

Questions for item b:

1. Is f(a) * f(m) < 0 or > 0 ? < 0


2. For the next iteration, the interval to use is a = a and b =m.
3. The estimated root for this iteration is midpoint m.

c. With -1 and 2 as intervals, in 15 iterations.


Command Window

Questions for item c:


1. What is the root at 15th iteration rounded off to 4 decimal places?-0.5237
2. What is the root at 4th iteration rounded off to 4 decimal places?-0.6250
3. True or False? The value of f(a)* f(m) at 13th iteration is >0. False
4. True or False? The value of f(a)* f(m) at 11th iteration is >0 that is why the interval to use
at the succeeding iteration is a = m, and b = b. True

Part 2. Regula Falsi Method


The formula for finding Xm (approximated root) for False Position Method is:

𝑋𝐿*𝑓(𝑋𝑈) − 𝑋𝑈*𝑓(𝑋𝐿)
Xm = 𝑓(𝑋𝑈) − 𝑓(𝑋𝐿)

Let XL = a and XU = b and Xm = m;

Exercise 2.
a. In the given code for Bisection Method, replace the formula for Xm using:

𝑎*𝑓(𝑏) − 𝑏*𝑓(𝑎)
m= 𝑓(𝑏) − 𝑓(𝑎)

Save your work as falseposition_method_YourInitials.m

falseposition_method_YourInitials.m

function m = falseposition_method_RAGF(f, a, b, n)

alpha = f(a);
beta = f(b);

if alpha * beta > 0


error('The function has the same sign at both endpoints of the interval. Please change
the endpoints and run again.');

end

for i = 1:n

m = a*f(b) - b*f(a)/(f(a) - f(b));

x = f(a);
y = f(m);

fprintf('iteration # %d a = %f b = %f m = %f, f(a) = %f f(m) = %f\n', i, a, b, m, x, y);

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

In the command window, type the following:


falseposition_method(inline('x^3-20','x'),1,4,30)
Command Window

Questions for b:

1. At what iteration did f(m) begin to equal to zero? 7


2. What is the root at that iteration rounded off to 4 decimal places?1.0000
c. Change the interval to 1 and 3.

Command Window

Questions for c:

1. At what iteration did f(m) begin to equal to zero? 8


2. What is the root at that iteration rounded off to 4 decimal places? 1.0000

d. Use Bisection Method to solve the same function.

bisection_method(inline('x^3-20','x'),1,3,30)
Command Window

Questions for d:

1. At what iteration did f(m) begin to equal to zero? 8


2. What is the root at that iteration rounded off to 4 decimal places?1.0000
3. For the same function and intervals, which method converged faster? False position method
typically converges faster than the bisection method

You might also like