0% found this document useful (0 votes)
22 views4 pages

Lab Task NC

The document outlines a lab task for numeric computations involving various methods for solving non-linear equations, including the Bisection method, Secant method, Fixed Point method, and Newton-Raphson method. Each method is accompanied by MATLAB code that prompts the user for input and iteratively calculates approximations until a specified accuracy is achieved. The document serves as a guide for implementing these numerical techniques in a programming environment.

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)
22 views4 pages

Lab Task NC

The document outlines a lab task for numeric computations involving various methods for solving non-linear equations, including the Bisection method, Secant method, Fixed Point method, and Newton-Raphson method. Each method is accompanied by MATLAB code that prompts the user for input and iteratively calculates approximations until a specified accuracy is achieved. The document serves as a guide for implementing these numerical techniques in a programming environment.

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/ 4

Name: Zuha Zia subject: Numeric

Computations
Section: Power Roll no: FA21-BEE-098

LAB TASK
1. First Section (Non –Linear Equations)
1.1 Bisection method
Code
syms x f
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 Secant method
Code

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));
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
% Final result
fprintf('\nRESULT:\n x%d = %f\n f(%f) = %f\n', i, x2,
x2, double(subs(f, x, x2)));
1.3 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)

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

i = i + 1;

% Calculate new approximation

x1 = double (subs(g, x, x0));

fprintf('x%d = %f\n', i, x1);

% Check for convergence

if abs(x1 - x0) <= 10^(-n)

break;

end

% Update for the next iteration

x0 = x1;

end

% Final result

fprintf('\nRESULT:\n x%d = %f\n f(%f) = %f\n', i, x1, x1, double(subs(f,


x, x1)));
1.4 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);

% Check for convergence

if abs(x1 - x0) <= 10^(-n)

break;

end

% Update for the next iteration

x0 = x1;

end

% Final result fprintf('\nRESULT:\n x%d = %f\n f(%f) = %f\n', i, x1, x1,


double(subs(f, x, x1)));

You might also like