0% found this document useful (0 votes)
40 views

MATLAB Source Code 2

The document contains MATLAB source code implementing three root-finding algorithms: the bisection method, Newton-Raphson method, and secant method. For each algorithm, the code defines the necessary variables, functions, and loops to iteratively find the root of a given nonlinear equation within a specified tolerance. The code also provides sample inputs and outputs to demonstrate the application of each method.

Uploaded by

Sardar Kamran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

MATLAB Source Code 2

The document contains MATLAB source code implementing three root-finding algorithms: the bisection method, Newton-Raphson method, and secant method. For each algorithm, the code defines the necessary variables, functions, and loops to iteratively find the root of a given nonlinear equation within a specified tolerance. The code also provides sample inputs and outputs to demonstrate the application of each method.

Uploaded by

Sardar Kamran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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
Bisection Method MATLAB Output

Enter non-linear equations: x^0.5-cos(x)

Enter first guess: 0

Enter second guess: 1

Tolerable error: 0.001

a b c f(c)

0.000000 1.000000 0.500000 -0.170476

0.500000 1.000000 0.750000 0.134337

0.500000 0.750000 0.625000 -0.020394

0.625000 0.750000 0.687500 0.056321

0.625000 0.687500 0.656250 0.017807

0.625000 0.656250 0.640625 -0.001332

0.640625 0.656250 0.648438 0.008228

0.640625 0.648438 0.644531 0.003446

0.640625 0.644531 0.642578 0.001056

Root is: 0.641602


MATLAB 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 abs(fa)> e
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: x-cos(x)

Enter initial guess: 1

Tolerable error: 0.0001

Enter maximum number of steps: 30

step=1 a=1.000000 f(a)=0.459698

step=2 a=0.750364 f(a)=0.018923

step=3 a=0.739113 f(a)=0.000046

Root is 0.739085
MATLAB Code: Secant Method

% Secant Algorithm
% Find the root of y = cos(x) from 0 to pi.

f = @(x) (cos(x));
p0 = input('Enter 1st approximation, p0: ');
p1 = input('Enter 2nd approximation, p1: ');
n = input('Enter no. of iterations, n: ');

tol = input('Enter tolerance, tol: ');

i = 2;
f0 = f(p0);
f1 = f(p1);
while i <= n

p = p1-f1*(p1-p0)/(f1-f0);
fp = f(p);
if abs(p-p1) < tol
fprintf('\nApproximate solution p = %11.8f\n\n',p);
break;
else
i = i+1;

p0 = p1;
f0 = f1;
p1 = p;
f1 = fp;
end

end
MATLAB Output

>> Untitled7

Enter 1st approximation, p0: -3

Enter 2nd approximation, p1: -2

Enter no. of iterations, n: 20

Enter tolerance, tol: 0.0001

Approximate solution p = -1.57079633


MATLAB Code: Secant Method

% Secant Algorithm
% Find the root of y = x^3+3x^2-1 from 0 to pi.

f = @(x) (x^3+3*x^2-1);
p0 = input('Enter 1st approximation, p0: ');
p1 = input('Enter 2nd approximation, p1: ');
n = input('Enter no. of iterations, n: ');

tol = input('Enter tolerance, tol: ');

i = 2;
f0 = f(p0);
f1 = f(p1);
while i <= n

p = p1-f1*(p1-p0)/(f1-f0);
fp = f(p);
if abs(p-p1) < tol
fprintf('\nApproximate solution p = %11.8f\n\n',p);
break;
else
i = i+1;

p0 = p1;
f0 = f1;
p1 = p;
f1 = fp;
end

end
MATLAB Output

>> Untitled7

Enter 1st approximation, p0: -3

Enter 2nd approximation, p1: -2

Enter no. of iterations, n: 20

Enter tolerance, tol: 0.0001

Approximate solution p = -2.87938519

You might also like