Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views7 pages

Lab 1 in Numerical

Uploaded by

damslambino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Lab 1 in Numerical

Uploaded by

damslambino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

GRAPH OF THE FUNCTION

It may not look like a graph suitable to cubic function since we just set the x range into , - 2 , +2 , 1000
NAME: FELIX DAMIAN LAMBINO SECTION: BSEE – 3A
KYLE ANDRE C.MENDOZA
EARL WAYNE JAMES S.CORPUZ

The Bisection Method is a systematic technique that repeatedly halves an interval to find a root of a function.
If the function's sign changes within the interval, the midpoint is evaluated to determine the subinterval containing the
root. This process is repeated for greater accuracy.

Using the software MATLAB, we have here a code wherein it can solve any given function using the
BISECTION METHOD:

% Prompt the user to input the function


func_input = input('Enter the function of x (e.g., @(x) x.^3 - cos(x)): ');

% Prompt the user to input the interval [a, b]


a = input('Enter the initial lower bound (a): ');
b = input('Enter the initial upper bound (b): ');

% Prompt the user to input the tolerance for the method


tolerance = input('Enter the tolerance (e.g., 1e-6): ');

% Set the maximum number of iterations


max_iter = 100; % Maximum number of iterations

% Check if root exists in the interval


if func_input(a) * func_input(b) >= 0
disp('The function does not have a root in the given interval [a, b].');
return; % Exit if no root is found in the interval
end

% Initialize variables
xn = (a + b) / 2; % Midpoint (initial guess)
iter = 0; % Iteration counter
table_data = []; % Initialize table storage

% Bisection method loop


while abs(func_input(xn)) > tolerance && iter < max_iter
fa = func_input(a);
fb = func_input(b);

% Store the current iteration data


table_data = [table_data; a, xn, b, fa, func_input(xn), fb]; % Store a, xn, b,
f(a), f(xn), f(b)

% Update interval
if fa * func_input(xn) < 0
b = xn; % Root is in the left subinterval
else
a = xn; % Root is in the right subinterval
end

% Update the midpoint


xn = (a + b) / 2;
iter = iter + 1; % Increment iteration counter
end

% Final iteration data


table_data = [table_data; a, xn, b, func_input(a), func_input(xn), func_input(b)]; %
Add final values to the table
% Create and display the table
T = array2table(table_data, 'VariableNames', {'a', 'xn', 'b', 'f(a)', 'f(xn)',
'f(b)'});
disp('Table of iterations:');
disp(T);

% Display the root approximation


disp('Root approximation:');
disp(xn);

% Plot the function


x_range = linspace(min(a, b) - 2, max(a, b) + 2, 1000); % Adjusted range for better
visibility
figure;
plot(x_range, func_input(x_range), 'b-', 'LineWidth', 2); % Plot the function
hold on;
plot(xn, func_input(xn), 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r'); % Mark the
root
xlabel('x');
ylabel('f(x)');
title('Graph of the Function and its Root Approximation');
grid on;

% Adjust y-axis for better view


axis([min(x_range) max(x_range) -10 10]); % Adjust the y-axis scaling

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The process involves several steps to find the root of a mathematical function:

1. Function Input: The user defines the function as an anonymous function (e.g., @(x) x.^3 - cos(x)).
2. Interval Input: The user specifies the lower (a) and upper (b) bounds of the interval.
3. Tolerance Input: The user sets the tolerance level for accuracy.
4. Root Existence Check: The code checks for a root in the interval using the Intermediate Value
Theorem; if the function values at a and b share the same sign, no root exists.
5. Bisection Method Loop: The algorithm iteratively narrows down the interval until the midpoint
satisfies the tolerance or the maximum iterations are reached, based on function value signs.
6. Final Table and Root Approximation: After iterations, the results are displayed in a table, along
with the approximate root.
7. Function Plotting: The function is plotted over a specified range, highlighting the approximate root
for visual clarity.
EXAMPLE: In this example we use the default function whereas it is indicated in our code as the gudie on
what to enter:

f(x) = x^3 – cos (x)

We copy and paste the code into the MATLAB and after doin so we run the code and input the required
information such as the function , interval and the tolerance input and after doing so it will solve on its own
with a table and a graph.
The Regula Falsi Method, or False Position Method, is a numerical technique used to find the root of a
function by iteratively narrowing down an interval where the function changes sign. It estimates the root by
connecting two points on the function with a straight line and finding where this line intersects the x-axis

Using the software MATLAB, we have here a code wherein it can solve any given function using the
REGULA FALSI METHOD:

clc; clear all; close all;

% Regula-Falsi Method using MATLAB Codes


f = input('Enter the given function: '); % Input function as @(x) ...
a = input('Enter the value of a: '); % Lower bound
b = input('Enter the value of b: '); % Upper bound

% Allowable error in numerical solution and Maximum Iteration


eps = 0.0000001;
maxiter = 100;

% Display header for the output table


fprintf('%2s %10.7s %10.7s %10.7s %10.7s %10.7s %10.7s\n', 'i', 'a', 'x(n)', 'b',
'f(a)', 'f(xn)', 'f(b)');

% Regula-Falsi Method Formula


i = 1;
while true % Corrected the loop condition to run indefinitely
fa = f(a);
fb = f(b);

% Calculate the new point using the Regula-Falsi formula


xn = b - fb * (b - a) / (fb - fa);
fxn = f(xn);

% Check if the root is in the interval


if fa * fb > 0
disp('===================================');
disp('The root is not in the interval');
disp('===================================');
break
end

% Display the output


fprintf('%2d %10.7f %10.7f %10.7f %10.7f %10.7f %10.7f\n', i, a, xn, b, fa, fxn,
fb);

% Condition for stopping the iteration


if abs(fxn) < eps || i >= maxiter
break
end

% Update the interval


if fa * fxn < 0
b = xn; % The root is in the left subinterval
else
a = xn; % The root is in the right subinterval
end

i = i + 1; % Increment iteration counter


end
% Show the graph of the function
fplot(f, 'b-');
hold on;
fplot(f, [a, b], 'ro'); % Mark the interval endpoints
grid on;
title('Graph of the function');
txt = sprintf('with interval [%f, %f]', a, b);
subtitle(txt);
xlabel('x');
ylabel('f(x)');
hold off;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Steps to Find the Root Using the Regula Falsi Method

1. Input: Enter the function f(x) and interval endpoints a and b.


2. Set Parameters: Define error tolerance and maximum iterations .
3. Header: Prepare output format for iteration results.
4. Loop: Begin iterations until the root is found or maximum iterations are reached.
5. Estimate: Calculate new root estimate xnx_nxn.
6. Evaluate: Compute f(a), f(b), and f(xn).
7. Check Signs: If f(a) and f(b) have the same sign, terminate.
8. Display: Print iteration values.
9. Convergence: Exit loop if max iterations reached.
10. Update Interval: Adjust a or b based on f(xn).
11. Increment: Increase iteration counter.
12. Plot: Visualize the function and points.

EXAMPLE: In this example we use the function

f(x) = x^3 – x - 2
We copy and paste the code into the MATLAB and after doin so we run the code and input the required
information such as the function , interval and the tolerance input and after doing so it will solve on its own
with a table and a graph.

 TABLE OF THE FUNCTION

 GRAPH OF THE FUNCTION

You might also like