0% found this document useful (0 votes)
8 views8 pages

Numerical Analysis

The document contains an assignment submitted by Inam Kousar for the Numerical Analysis course, detailing MATLAB source code for three numerical methods: Bisection Method, Regula Falsi Method, and Newton-Raphson Method. Each method includes input sections for non-linear equations, initial guesses, and tolerable errors, along with example outputs demonstrating their usage. Additionally, it includes a graphical representation of the sine and cosine functions.

Uploaded by

nasira9545
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)
8 views8 pages

Numerical Analysis

The document contains an assignment submitted by Inam Kousar for the Numerical Analysis course, detailing MATLAB source code for three numerical methods: Bisection Method, Regula Falsi Method, and Newton-Raphson Method. Each method includes input sections for non-linear equations, initial guesses, and tolerable errors, along with example outputs demonstrating their usage. Additionally, it includes a graphical representation of the sine and cosine functions.

Uploaded by

nasira9545
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/ 8

ASSIGNMENT NO.

SUBMITTED TO :- DR.GUL SANA

SUBMITTED BY :- INAM KOUSAR (29)

ASSIGNMENT TOPIC:

COURSE TITLE :- NUMERICAL ANALYSIS

COURSE CODE :- MTH-494

SEMESTER :- Fall 2024

SECTION :- 7 MA

SESSION :- 2021 - 2025

DEPARTMENT :- Mathematics

GOVERNMENT COLLEGE WOMEN UNIVERSITY FAISALABAD


MATLAB Source Code: Bisection Method

% Clearing Screen
clc
% Setting x as symbolic variable
syms x;
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
% Finding Functional Value
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = (a+b)/2;
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
else
a =c;
end
c = (a+b)/2;
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c) end

MATLAB Output
Enter non-linear equations: cos(x)-x*exp(x)
Enter first guess: 2
Enter second guess:3
Tolerable error: 0.00000

MATLAB Source Code: Regula Falsi Method

% Clearing Screen
clc
% Setting x as symbolic variable
syms x;
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
% Finding Functional Value
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while
abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
fb = eval(subs(y,x,b));
else
a =c;
fa = eval(subs(y,x,a));
end
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end

MATLAB Output

Enter non-linear equations: sin(x)+cos(x)+exp(x)-8


Enter first guess: 2
Enter second guess:3
Tolerable error: 0.000001

MATLAB Source Code: Newton-Raphson Method

% Clearing Screen
Clc
% Setting x as symbolic variable
syms x;
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter initial guess: ');
e = input('Tolerable error: ');
N = input('Enter maximum number of steps: ');
% Initializing step counter
step = 1;
% Finding derivate of given function
g = diff(y,x);
% Finding Functional Value
fa = eval(subs(y,x,a));
while
fa = eval(subs(y,x,a));
ga = eval(subs(g,x,a));
if ga == 0
disp('Division by zero.');
break;
end
b = a - fa/ga;
fprintf('step=%d\ta=%f\tf(a)=%f\n',step,a,fa);
a = b;
if step>N
disp('Not convergent');
break;
end
step = step + 1;
end
fprintf('Root is %f\n', a);

MATLAB Output

Enter non-linear equations: cos(x)-x*exp(x)


Enter initial guess: 1
Tolerable error: 0.00001
Enter maximum number of steps: 20
step=1:- a=1.000000 f(a)=-2.177980
step=2:- a=0.653079 f(a)=-0.460642
step=3:- a=0.531343 f(a)=-0.041803
step=4:- a=0.517910 f(a)=-0.000464
step=5:- a=0.517757 f(a)=-0.000000
Root is 0.517757
GRAPHYICALY VIEW:
x = linspace(-2*pi,2*pi,100);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)

xlabel('x')
ylabel('sin(x)')
title('Plot of the Sine Function')

You might also like