Uploaded by Ramrasai marma Reg:3138

numerical all method in

advertisement
function bisectionMethod
repeatCalculation = true;
while repeatCalculation
Input_function = input('Enter the function in terms of variable x: ' ,'s');
f = @(x) eval(Input_function);
rootPossible = false;
while ~rootPossible
a = input('Enter the lower value: ');
b = input('Enter the upper value: ');
if f(a) * f(b) > 0
fprintf('No root found within the interval [%f, %f] because f(a)*f(b)>0\n',
a, b);
continue;
else
rootPossible = true;
end
end
tolerance = input('Enter the tolerance: ');
maxIterations = input('Enter the maximum number of iterations: ' ); iteration = 0;
while iteration < maxIterations
midpoint = (a + b) / 2.0;
fprintf('iteration %d: a=%f, b=%f, f(a)=%f, f(b)=%f, midpoint=%f, f(midpoint)=%f\n',
iteration,a,b,f(a),f(b),midpoint,f(midpoint));
if f(midpoint) == 0
break;
end
if f(a) * f(midpoint) < 0
b = midpoint;
else
a = midpoint;
end
if abs(f(midpoint)) <= tolerance
break;
end
iteration = iteration + 1;
end
if abs(f(midpoint)) <= tolerance
fprintf('The root is: %f\n', midpoint);
fprintf('Number of iterations required: %d\n' , iteration);
else
fprintf('The root was not found within the given tolerance and iterations.\n' );
end
repeat = input('Do you want to repeat the calculation? (yes/no) ' , 's');
if strcmpi(repeat, 'no')
break;
end
end
end
function iterationmethod
repeatCalculation = true;
while repeatCalculation
Input_f_function = input('Enter the iteration function f(x) in terms of x: ' , 's');
syms x;
f_sym = str2sym(Input_f_function);
df_sym = diff(f_sym);
f = matlabFunction(f_sym);
df = matlabFunction(df_sym);
x0 = input('Enter the initial guess x0: ' );
if abs(df(x0)) >= 1
fprintf('Cannot use iteration method as the absolute value of the derivative of f
is greater or equal to 1.\n' );
repeat = input('Do you want to change the interval and try again? (yes/no) ' ,
's');
if strcmpi(repeat, 'no')
break;
else
continue;
end
end
tolerance = input('Enter the tolerance: ');
maxIterations = input('Enter the maximum number of iterations: ' );
iteration = 0;
while iteration < maxIterations
xn = f(x0);
fprintf('iteration %d: x0 = %f, xn = %f, df(x0) = %f\n' , iteration,x0,xn,df(x0));
if (xn - x0) == 0
break;
end
if abs(xn - x0) <= tolerance
break;
end
x0 = xn;
iteration = iteration + 1;
end
if abs(xn - x0) <= tolerance
fprintf('The root is: %f\n', xn);
fprintf('Number of iterations required: %d\n' , iteration);
else
fprintf('The root was not found within the given tolerance and iterations.\n' );
end
repeat = input('Do you want to repeat the calculation? (yes/no) ' , 's');
if strcmpi(repeat, 'no')
break;
end
end
end
function NewtonRaphsonMethod
repeatCalculation = true;
while repeatCalculation
inputFunction = input('Enter the function in terms of x: ' , 's');
syms x;
f_sym = str2sym(inputFunction);
df_sym = diff(f_sym);
f = matlabFunction(f_sym);
df = matlabFunction(df_sym);
x0 = input('Enter the initial guess: ' );
tolerance = input('Enter the tolerance: ');
maxIterations = input('Enter the maximum number of iterations: ' );
if df(x0) == 0
error('Division by zero detected due to derivative being zero at x = %f' , x0);
end
iteration = 0;
while iteration < maxIterations
xn = x0 - f(x0) / df(x0);
fprintf('iteration %d: x0 = %f, f(x0) = %f, df(x0) = %f, xn = %f\n' , iteration,
x0, f(x0), df(x0), xn);
if (xn - x0) == 0
break;
end
if abs(xn - x0) <= tolerance
break;
end
x0 = xn;
iteration = iteration + 1;
end
if abs(xn - x0) <= tolerance
fprintf('The root is: %f\n', xn);
fprintf('Number of iterations required: %d\n' , iteration);
else
fprintf('The root was not found within the given tolerance and maximum number of
iterations.\n');
end
repeat = input('Do you want to repeat the calculation? (yes/no) ' , 's');
if strcmpi(repeat, 'no')
break;
end
end
end
function regularFalsiMethod
repeatCalculation = true;
while repeatCalculation
Input_function = input('Enter the function in terms of variable x: ' ,'s');
f = @(x) eval(Input_function);
rootPossible = false;
while ~rootPossible
a = input('Enter the lower value: ');
b = input('Enter the upper value: ');
if f(a) * f(b) > 0
fprintf('No root found within the interval [%f, %f] because f(a)*f(b)>0\n',
a, b);
continue;
else
rootPossible = true;
end
end
tolerance = input('Enter the tolerance: ');
maxIterations = input('Enter the maximum number of iterations: ' );
iteration = 0;
while iteration < maxIterations
c = (b * f(a) - a * f(b)) / (f(a) - f(b));
fprintf('iteration %d: a=%f, b=%f, f(a)=%f, f(b)=%f, c=%f, f(c)=%f\n' , iteration,
a,b,f(a),f(b),c,f(c));
if f(c) == 0
break;
end
if f(a) * f(c) < 0
b = c;
else
a = c;
end
if abs(f(c)) <= tolerance
break;
end
iteration = iteration + 1;
end
if abs(f(c)) <= tolerance
fprintf('The root is: %f\n', c);
fprintf('Number of iterations required: %d\n' , iteration);
else
fprintf('The root was not found within the given tolerance and iterations.\n' );
end
repeat = input('Do you want to repeat the calculation? (yes/no) ' , 's');
if strcmpi(repeat, 'no')
break;
end
end
end
function secantMethod
repeatCalculation = true;
while repeatCalculation
Input_function = input('Enter the function in terms of variable x: ' ,'s');
f = @(x) eval(Input_function);
x0 = input('Enter the first initial guess: ' );
x1 = input('Enter the second initial guess: ' );
tolerance = input('Enter the tolerance: ');
maxIterations = input('Enter the maximum number of iterations: ' );
iteration = 0;
while iteration < maxIterations
f0 = f(x0);
f1 = f(x1);
x2 = (x0 * f1 - x1 * f0) / (f1 - f0);
fprintf('iteration %d: x0 = %f, x1 = %f, f(x0)=%f, f(x1)=%f, x2=%f\n' , iteration,
x0,x1,f(x0),f(x1), x2);
if f(x2 - x1) == 0
break;
end
if abs(x2 - x1) <= tolerance
break;
end
x0 = x1;
x1 = x2;
iteration = iteration + 1;
end
if abs(x2 - x1) <= tolerance
fprintf('The root is: %f\n', x2);
fprintf('Number of iterations required: %d\n' , iteration);
else
fprintf('The root was not found within the given tolerance and maximum number of
iterations.\n');
end
repeat = input('Do you want to repeat the calculation? (yes/no) ' , 's');
if strcmpi(repeat, 'no')
break;
end
end
end
function LagrangesInterpolation
repeatCalculation = true;
while repeatCalculation
xa = input('Enter the x data points like [x1 x2 x3 ...]: ' );
ya = input('Enter the y data points like [y1 y2 y3 ...]: ' );
x = input('Enter the value at which to interpolate: ' );
n = length(xa);
y = 0;
fprintf('Performing Lagrange''s Interpolation for x = %.3f\n' , x);
for i = 1:n
L = 1;
fprintf('L%d(x) = ', i-1);
for j = 1:n
if i ~= j
L = L * (x - xa(j)) / (xa(i) - xa(j));
fprintf('((%0.3f - %0.3f) / (%0.3f - %0.3f))' , x, xa(j), xa(i), xa(j));
end
end
y = y + L * ya(i);
fprintf(('for ya%d = %0.3f, Result = %0.3f\n' ), i-1, ya(i), (L * ya(i)));
end
fprintf('The interpolated value at x = %f is y = %.3f.\n' , x, y);
repeat = input('Do you want to perform another calculation? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break;
end
end
end
function newtonBackwardInterpolation
repeatCalculation = true;
while repeatCalculation
difference = false;
while ~difference
xa = input('Enter the x data points like [x1 x2 x3 ...]: ' );
h = diff(xa);
if any(abs(diff(h)) > 0)
fprintf('Error: x data points are not equally spaced!' );
else
difference = true;
h = h(1);
end
end
ya = input('Enter the y data points corresponding to x like [y1 y2 y3 ...]:
');
x = input('Enter the value at which to interpolate: ' );
n = length(xa);
v = (x - xa(n)) / h;
backward_diff_table = zeros(n, n);
backward_diff_table(:,1) = ya;
for j = 2:n
for i = n:-1:j
backward_diff_table(i,j) = backward_diff_table(i,j-1) backward_diff_table(i-1,j-1);
end
end
fprintf('Backward Difference Table:\n' );
for i = 1:n
for j = 1:i
fprintf('% 0.3f ', backward_diff_table(i,j));
end
fprintf('\n');
end
y = ya(n);
mult_factor = v;
for i = 1:n-1
y = y + (mult_factor * backward_diff_table(n,i+1)) / factorial(i);
mult_factor = mult_factor * (v + i);
end
fprintf('The interpolated value at x = %f is y = %0.3f.\n' , x, y);
repeat = input('Do you want to perform another calculation? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break;
end
end
end
function newtonForwardInterpolation
repeatCalculation = true;
while repeatCalculation
difference = false;
while ~difference
xa = input('Enter the x data points like [x1 x2 x3]: ' );
h = diff(xa);
if any(abs(diff(h)) > 0)
fprintf('Error: x data points are not equally spaced!' );
continue;
else
difference = true;
h = h(1);
end
end
ya = input('Enter the y data points corresponding to x like [y1 y2 y3]: ' );
x = input('Enter the value at which to interpolate: ' );
n = length(xa);
u = (x - xa(1))/h;
forward_diff_table = zeros(n, n);
forward_diff_table(:,1) = ya;
for i = 2:n
for j = 2:i
forward_diff_table(i,j) = forward_diff_table(i,j-1) forward_diff_table(i-1,j-1);
end
end
fprintf('Forward Difference Table:\n' );
for i = 1:n
for j = 1:i
fprintf('% 0.3f ', forward_diff_table(i,j));
end
fprintf('\n');
end
y = ya(1);
for i = 1:n-1
prod = 1;
for j = 0:i-1
prod = prod * (u - j);
end
y = y + (prod * forward_diff_table(i+1,i+1)) / factorial(i);
end
fprintf('The interpolated value at x = %f is y = %0.3f.\n' , x, y);
repeat = input('Do you want to perform another calculation? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break;
end
end
end
function simpson_one_third
repeatCalculation = true;
while repeatCalculation
Input_function = input('Enter the function in terms of variable x: ' ,'s');
f = @(x) eval(Input_function); % Evaluet the input function
a = input('Enter the lower limit: ');
b = input('Enter the upper limit: ');
n = input('Enter the number of intervals: ' );
if rem(n,2)~=0
fprintf('n is not even number!!!')
n = input('Enter a even number of n: ')
end
h = (b - a) / n;
s = f(a) + f(b);
so = 0;
for i = 1: 2: n-1 %for odd part
so = so + f (a + i*h);
end
se = 0;
for i = 2: 2: n-2 %for even part
se = se + f (a + i*h);
end
Integral = h/3*[s + 4*so + 2*se];
fprintf('The Integral of the function is: %f\n' , Integral)
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
function simpson_three_eight
repeatCalculation = true;
while repeatCalculation
Input_function = input('Enter the function in terms of variable x: ' ,'s');
f = @(x) eval(Input_function);
a = input('Enter the lower limit: ');
b = input('Enter the upper limit: ');
n = input('Enter the number of intervals: ' );
if rem(n,3)~=0
fprintf('n is not divisible by 3!!!' )
n = input('Enter a number of n which is divisible by 3: ' )
end
h = (b - a) / n;
s = f(a) + f(b);
s3 = 0;
for i = 3: 3: n-3
s3 = s3 + f (a + i*h);
end
soth = 0;
for i = 1: n-1
soth = soth + f (a + i*h);
end
s3not = soth - s3;
Integral = [(3*h/8)*[s + 2*s3 + 3*s3not]];
fprintf('The Integral of the function is: %f\n' , Integral)
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
function trapezoidal_rule
repeatCalculation = true;
while repeatCalculation
Input_function = input('Enter the function in terms of variable x: ' ,'s');
f = @(x) eval(Input_function);
a = input('Enter the lower limit: ');
b = input('Enter the upper limit: ');
n = input('Enter the number of intervals: ' );
h = (b - a) / n;
s = f(a) + f(b);
sn = 0;
for i = 1: 1: n-1
sn = sn + f (a + i*h);
end
Integral = h/2*[s + 2*sn];
fprintf('The Integral of the function is: %f\n' , Integral)
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
function [x, iterations] = gauss_seidel_input()
repeatCalculation = true;
while repeatCalculation
A = input('Enter the coefficient matrix A: ' );
b = input('Enter the constants b: ');
x0 = input('Enter the initial guess x0: ' );
tol = input('Enter the tolerance for convergence: ' );
max_iterations = input( 'Enter the maximum number of iterations: ' );
n = length(b);
x = x0;
iterations = 0;
format long;
fprintf('Iteration %d: %s\n', iterations, mat2str(x));
while true
x_old = x;
for i = 1:n
sum = 0;
for j = 1:n
if j ~= i
sum = sum + A(i, j) * x(j);
end
end
x(i) = (b(i) - sum) / A(i, i);
end
iterations = iterations + 1;
fprintf('Iteration %d: %s\n', iterations, mat2str(x));
if abs(x - x_old) < tol
break;
end
if iterations >= max_iterations
break;
end
end
repeat = input('Do you want to perform another interpolation method?
(yes/no): ', 's');
if strcmpi(repeat, 'no')
break
end
end
end
function [x, iterations] = jacobi_input()
repeatCalculation = true;
while repeatCalculation
A = input('Enter the coefficient matrix A: ' );
b = input('Enter the constants b: ');
x0 = input('Enter the initial guess x0: ' );
tol = input('Enter the tolerance for convergence: ' );
max_iterations = input('Enter the maximum number of iterations: ' );
n = length(b);
x = x0;
iterations = 0;
format long;
fprintf('Iteration\t x\n');
fprintf('%d\t\t %s\n', iterations, mat2str(x));
while true
x_old = x;
for i = 1:n
sum = 0;
for j = 1:n
if j ~= i
sum = sum + A(i, j) * x_old(j);
end
end
x(i) = (b(i) - sum) / A(i, i);
end
iterations = iterations + 1;
fprintf('%d\t\t %s\n', iterations, mat2str(x));
if abs(x - x_old) < tol
break;
end
if iterations >= max_iterations
break;
end
end
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
function RK1()
repeatCalculation = true;
while repeatCalculation
f_input = input('Enter the differential equation in the form @(x,y) expression: ' ,
's');
fd = str2func(['@(x,y)', f_input]);
xa_initial = input('Enter the initial value of x: ' );
ya_initial = input('Enter the initial value of y: ' );
xn = input('Enter the final value of x: ' );
n = input('Enter the number of steps: ' );
h = (xn - xa_initial) / n;
xa(1) = xa_initial;
ya(1) = ya_initial;
fprintf('Solution using RK1 (First-Order Runge-Kutta) Method:\n' );
for i = 1:n
xa(i+1) = xa(i) + h;
ya(i+1) = ya(i) + h * fd(xa(i), ya(i));
fprintf('x(%d) = %.6f, y(%d) = %.6f\n' , i, xa(i), i, ya(i));
end
fprintf('x(%d) = %.6f, y(%d) = %.6f\n' , n+1, xa(end), n+1, ya(end));
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
function RK2()
repeatCalculation = true;
while repeatCalculation
f_input = input('Enter the differential equation in the form @(x,y) expression:
', 's');
fd = str2func(['@(x,y)', f_input]);
xa_initial = input('Enter the initial value of x: ' );
ya_initial = input('Enter the initial value of y: ' );
xn = input('Enter the final value of x: ' );
n = input('Enter the number of steps: ' );
h = (xn - xa_initial) / n;
xa(1) = xa_initial;
ya(1) = ya_initial;
fprintf('Solution using RK2 (Second-Order Runge-Kutta) Method:\n' );
for i = 1:n
k1 = h * fd(xa(i), ya(i));
k2 = h * fd(xa(i) + 0.5 * h, ya(i) + 0.5 * k1);
ya(i+1) = ya(i) + k2;
xa(i+1) = xa(i) + h;
fprintf('x(%d) = %.6f, y(%d) = %.6f\n' , i, xa(i), i, ya(i));
end
fprintf('x(%d) = %.6f, y(%d) = %.6f\n' , n+1, xa(end), n+1, ya(end));
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
function RK4()
repeatCalculation = true;
while repeatCalculation
f_input = input('Enter the differential equation in the form @(x,y) expression:
', 's');
fd = str2func(['@(x,y)', f_input]);
xa_initial = input('Enter the initial value of x: ' );
ya_initial = input('Enter the initial value of y: ' );
xn = input('Enter the final value of x: ' );
n = input('Enter the number of steps: ' );
h = (xn - xa_initial) / n;
xa = zeros(1, n+1);
ya = zeros(1, n+1);
xa(1) = xa_initial;
ya(1) = ya_initial;
fprintf('Solution using RK4 (Fourth-Order Runge-Kutta) Method:\n' );
for i = 1:n
k1 = h * fd(xa(i), ya(i));
k2 = h * fd(xa(i) + 0.5 * h, ya(i) + 0.5 * k1);
k3 = h * fd(xa(i) + 0.5 * h, ya(i) + 0.5 * k2);
k4 = h * fd(xa(i) + h, ya(i) + k3);
ya(i+1) = ya(i) + (k1 + 2*k2 + 2*k3 + k4) / 6;
xa(i+1) = xa(i) + h;
fprintf('x(%d) = %.6f, y(%d) = %.6f\n' , i, xa(i), i, ya(i));
end
fprintf('x(%d) = %.6f, y(%d) = %.6f\n' , n+1, xa(end), n+1, ya(end));
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
function [] = laplace_gauss()
repeatCalculation = true;
while repeatCalculation
u = input('Enter the initial temperature distribution (u = matrix):' );
Nx = input('Enter the number of grid points in the x direction (Nx): ' );
Ny = input('Enter the number of grid points in the y direction (Ny): ' );
uf = u;
k = 0;
err = 1;
tol = input('Enter the tolerance for convergence (tol): ' );
while err > tol
k = k + 1;
for i = 2:Ny-1
for j = 2:Nx-1
uf(i,j) = 0.25*(uf(i-1,j)+uf(i+1,j)+uf(i,j-1)+uf(i,j+1));
end
end
err = sqrt(sum(sum((u - uf).^2)));
u = uf;
end
disp('Final temperature distribution (u):' );
disp(u);
disp(['Number of iterations required for convergence: ' , num2str(k)]);
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
function [] = laplace_jacobi()
repeatCalculation = true;
while repeatCalculation
u = input('Enter the initial temperature distribution (u = matrix):' );
Nx = input('Enter the number of grid points in the x direction (Nx): ' );
Ny = input('Enter the number of grid points in the y direction (Ny): ' );
uf = u;
k = 0;
err = 1;
tol = input('Enter the tolerance for convergence (tol): ' );
while err > tol
k = k + 1;
for i = 2:Ny-1
for j = 2:Nx-1
uf(i,j) = 0.25 * (u(i-1,j) + u(i+1,j) + u(i,j-1) + u(i,j+1));
end
end
err = sqrt(sum(sum((u - uf).^2)));
u = uf;
end
disp('Final temperature distribution (u):' );
disp(u);
disp(['Number of iterations required for convergence: ' , num2str(k)]);
repeat = input('Do you want to perform another interpolation method? (yes/no): ' ,
's');
if strcmpi(repeat, 'no')
break
end
end
end
Download