0% found this document useful (0 votes)
16 views6 pages

Lab Task

The document outlines various numerical methods for solving non-linear equations, including the Bisection method, Regula Falsi method, Secant method, Newton-Raphson method, and Fixed Point method. Each method is presented with corresponding MATLAB code snippets that guide the user through inputting functions, initial guesses, and desired accuracy. The document emphasizes iterative processes and convergence checks for each method.

Uploaded by

husnainghaffar87
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)
16 views6 pages

Lab Task

The document outlines various numerical methods for solving non-linear equations, including the Bisection method, Regula Falsi method, Secant method, Newton-Raphson method, and Fixed Point method. Each method is presented with corresponding MATLAB code snippets that guide the user through inputting functions, initial guesses, and desired accuracy. The document emphasizes iterative processes and convergence checks for each method.

Uploaded by

husnainghaffar87
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/ 6

LAB TASK

NAME: MOAZ RAZA SUBJECT:


NC
Roll no: FA21-BEE-112 SECTION: Power

1.Non –Linear Equations


1.1 Bisection method

syms x f
output
f(x)=input ('Enter Function: ');
a=input ('Enter 1st Interval value: ');
b=input ('Enter 2nd Interval value: ');
if (f (a) *f (b) >0)
fprintf (' Incorrect Interval\nEnter again...\n');
while f(a) * f(b) >0
a=input ('Enter 1st Interval value: ');
b=input ('Enter 2nd Interval value: ');
end
end
n=input ('Enter decimal point accuracy: ');
if (f (a) *f (b) <0)
c= (a+b) /2; for 1=1:100
x0=с;
c= (a+b) / 2;
if (f (c) *f (a) >0)
а=с;
else b=c;
end
fprintf ('x8d = 8f\n', i,c) ;
if c>x0&&abs (c-x0) <=10^-n
break
end end
end
1.2 Regula Falsie method

syms x f
Output
f = input('Enter Function (in terms of x): ');
% Get two initial guesses
x0 = input('Enter first guess: ');
x1 = input('Enter second guess: ');
% Set the desired decimal point accuracy
n = input('Enter decimal point accuracy: ');
% Iteration counter
i = 0;
% Begin the secant method
fprintf('\n regula falsi Method Iterations:\n');
while true
i = i + 1;
% Calculate the new approximation
f_x0 = double(subs(f, x, x0));
f_x1 = double(subs(f, x, x1));
% Secant method formula
x2 = x1 - f_x1 * ((x1 - x0) / (f_x1 - f_x0));
fprintf('x%d = %f\n', i, x2);
% Check for convergence
if abs(x2 - x1) <= 10^(-n)
break;
end
% Update for the next iteration
x0 = x1;
x1 = x2;
end
1.3 % Final result Secant method
fprintf('\nRESULT:\n x%d = %f\n f(%f) = %f\n', i, x2,
x2, double(subs(f, x, x2))); output
syms x f
f = input('Enter Function (in terms of x): ');
% Get two initial guesses
x0 = input('Enter first guess: ');
x1 = input('Enter second guess: ');
% Set the desired decimal point accuracy
n = input('Enter decimal point accuracy: ');
% Iteration counter
i = 0;
% Begin the secant method
fprintf('\nSecant Method Iterations:\n');
while true
i = i + 1;
% Calculate the new approximation
f_x0 = double(subs(f, x, x0));
f_x1 = double(subs(f, x, x1));
% Secant method formula
x2 = x1 - f_x1 * ((x1 - x0) / (f_x1 - f_x0));
Newton Raphson method
syms x f

f = input('Enter Function (in terms of x): ')

% Calculate the derivative of the function

f_prime = diff(f, x);

% Get the initial guess


Output

1.4 Fixed Point method


Code

syms x f

f = input('Enter Function (in terms of x): ');

% Rearranging the function to g(x) = x - f(x) (or another suitable form) Output
g = input('Enter the rearranged function g(x) (in terms of x): ');

% Get the initial guess

x0 = input('Enter initial guess: ');

% Set the desired decimal point accuracy

n = input('Enter decimal point accuracy: ');

% Iteration counter

i = 0;

% Begin the Fixed Point Iteration method

fprintf('\nFixed Point Iteration Method Iterations:\n');

while true
Newton Raphson method
Code
syms x f

f = input('Enter Function (in terms of x): ')

% Calculate the derivative of the function

f_prime = diff(f, x);

% Get the initial guess

x0 = input('Enter initial guess: ');

% Set the desired decimal point accuracy

n = input('Enter decimal point accuracy: ');

% Iteration counter

i = 0;

% Begin the Newton-Raphson method

fprintf('\nNewton-Raphson Method Iterations:\n');

while true

i = i + 1;

% Calculate function value and derivative value

f_x0 = double(subs(f, x, x0));

f_prime_x0 = double(subs(f_prime, x, x0));

% Check if the derivative is zero to avoid division by zero

if abs(f_prime_x0) < 1e-10

fprintf('Derivative is too close to zero. No solution found.\n');

break;

end

% Newton-Raphson formula

x1 = x0 - f_x0 / f_prime_x0;
fprintf('x%d = %f\n', i, x1);

Output

You might also like