Lab Task NC
Lab Task NC
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
% Iteration counter
i = 0;
while true
i = i + 1;
break;
end
x0 = x1;
end
% Final result
% Iteration counter
i = 0;
while true
i = i + 1;
break;
end
% Newton-Raphson formula
x1 = x0 - f_x0 / f_prime_x0;
break;
end
x0 = x1;
end