Monday, June 12, 2017

Numerical method Codes simple MatLab implementation

Numerical method Codes simple MatLab implementation

In this tutorial, we;ll learn about following MAT LAB works of Numerical Methods.

  1. Numerical Method Simpson 1/3 MatLab Code implementation
  2. Numerical Method Simpson 3/8  MatLab Code implementation
  3. Numerical Method Gauss Elimination MatLab Code Implementation
  4. Numerical Method Gauss Elimination MatLab Code Implementation:
  5. Numerical Method Gauss Zordan MatLab Code Implementation
  6. Numerical Method Gauss Cramers Rule MatLab Code Implementation
  7. Numerical Method Newton Raphson MatLab Code Implementation
  8. Numerical Method Fixed Point Iteration MatLab Code Implementation
  9. Numerical Method False Position Method MatLab Code Implementation
  10. Numerical Method Bisection Method MatLab Code Implementation

Numerical Method Simpson 1/3 MatLab Code implementation:


f = @ (x)  .2+ 25*x - 200*x^2+675*x^3-900*x^4+400*x^5;
a = 0;
b = 0.8;
x0 = a; 
x1 = (b-a)/2; 
x2 = b; 

fx0 = feval(f,x0);
fx1 = feval(f,x1);
fx2 = feval(f,x2);
exact_integral = 1.640533;

I = ((fx0 + (4*fx1) + fx2)/6 * (b-a));

E = abs((exact_integral - I)/exact_integral) * 100;


fprintf('\nIntegral = %f\n', I);
fprintf('\nError = %f\n', E);


Numerical Method Simpson 3/8  MatLab Code implementation:

f = @ (x) 0.2 + 25*x - 200*(x^2) + 675*(x^3) -900*(x^4) + 400*(x^5);

a = 0;
b = .8;

x0 = a;
x1 = (b-a)/3;
x2 = 2 * x1;
x3 = b;

fx0 = feval(f, x0);
fx1 = feval(f, x1);
fx2 = feval(f, x2);
fx3 = feval(f, x3);

I = (b - a) * ((fx0 + 3*(fx1 + fx2) + fx3)/8);

exact_integral = input('Enter Exact Integral : ');
E = abs((exact_integral - I)/exact_integral) * 100;


fprintf('\nIntegral = %f\n', I);
fprintf('\nError = %f\n', E);



Numerical Method Gauss Elimination MatLab Code Implementation:

%% Gauss Elimination Problem

% x1   + x2   + x3  = 4
% 2x1  + x2   + 3x3 = 7
% 3x1  + 4x2  - 2x3 = 9


n = 3;   %Solve 3 equestion here

Ab = [1 1 1 4; 2 1 3 7; 3 4 -2 9];

%% Forward Substitution
% Rj = Rj - ai,j*Ri Where ai,j = A(j,i)/A(i,i)


% A(1, 1)
alpha = Ab(2, 1)/Ab(1, 1);
Ab(2, :) = Ab(2, :) - alpha*Ab(1, :);

alpha = Ab(3, 1)/Ab(1, 1);
Ab(3, :) = Ab(3, :) - alpha*Ab(1, :);

% A(2, 2)
alpha = Ab(3, 2)/Ab(2, 2);
Ab(3, :) = Ab(3, :) - alpha*Ab(2, :);


%% Backward Substitution

x = zeros(n, 1);
x(3) = Ab(3, end) / Ab(3,3);
x(2) = (Ab(2, end) - Ab(2, 3) * x(3)) / Ab(2, 2);
x(1) = (Ab(1, end) - (Ab(1, 2) * x(2) + Ab(1, 3)*x(3))) / Ab(1, 1);

% Different Solution using loop
% for i= n:-1:1
%     x(i) = (Ab(i, end) - Ab(1, i+1:n) * x(i+1:n)) / Ab(i, i);
% end

disp('Final Answer');
fprintf('\nx1 = %.4f\nx2 = %.4f\nx3 = %.4f\n', x(1), x(2), x(3));


Numerical Method Gauss Zordan MatLab Code Implementation:

M = [3 -.1 -.2 7.85; .1 7 -.3 -19.3; .3 -.2 10 71.4];

disp('Start Matrix :')
disp(M);
for r =1:3
    
    A = M(r, :);
    A = A/A(r);
    M(r, :) = A;    %Set matrix from A For row 1
    
    for s=1:3
        if r~=s
            M(s, :) =  M(s, :) -  M(r, :) * M(s, r);
        end
    end
end

disp('Final Matrix :')
disp(M);
disp('Solution :')

%disp(M(:,4))
fprintf('X = %.4f\n', M(1, 4))
fprintf('Y = %.4f\n', M(1, 4))
fprintf('Z = %.4f\n', M(1, 4))


Numerical Method Gauss Cramers Rule MatLab Code Implementation:

%Cramers Rule Problems
% Use cramer rules to solve the problem
% .3x1 + .52x2 + x3     = -.01
% .5x1 + x2    + 1.9x3  = .67
% .1x1 + .3x2  + .5x3   = -.44

det_M = [.3 .52 1; .5 1 1.9; .1 .3 .5 ];
determinant = det(det_M);

det_x1 = [-.01 .52 1; .67 1 1.9; -.44 .3 .5 ];
x1 = det(det_x1) / determinant;


det_x2 = [.3 -.01 1; .5 .67 1.9; .1 -.44 .5 ];
x2 = det(det_x2) / determinant;

det_x3 = [.3 .52 -.01; .5 1 .67; .1 .3 -.44 ];
x3 = det(det_x3) / determinant;


disp('Solutions');
fprintf('x1 = %.2f\n', x1);
fprintf('x2 = %.2f\n', x2);
fprintf('x3 = %.2f\n', x3);

%disp(determinant)


Numerical Method Newton Raphson MatLab Code Implementation:

%Function e^-x
f = @ (x)  exp(-x);
f1 = @ (x)  -exp(-x);

x0 = 0;         %Given

for i=1:10
    y = x0 - ((feval(f, x0))/(feval(f1, x0)));
    x0 = y;
    fprintf('\nStep %d Result %.4f\n', i, y);
end

fprintf('\nResult of Newton Raphson : %f', y);



Numerical Method Fixed Point Iteration MatLab Code Implementation:

%Function e^-x
f = @ (x)  exp(-x);

x0 = 0;
result = 0;

for i=1:10
    result = feval(f, x0);
    x0 = result;
    fprintf('Step %d Result %.4f\n', i, result);
end

fprintf('\nResult is : %f', result);


Numerical Method False Position Method MatLab Code Implementation:

%Function
f = @(x) 5 * x^4 - 2.7 * x^2 - 2*x + 0.5;
l = input('Enter Lower Limit : ');
u = input('Enter Upper Limit : ');
previous_approximation = 0;

f_l = feval(f, l);
f_u = feval(f, u);
root = u -((f_u * (l-u))/ (f_l - f_u));
given_absoulte_error = input('Enter absolute error : ');
absolute_error = abs((root - previous_approximation) / root ) * 100;
previous_approximation = root;

while(absolute_error > given_absoulte_error)
    f_l = feval(f, l);
    f_u = feval(f, u);
    f_root = feval(f, root);
    
    if(f_l * f_root > 0)
        l = root;
    else
        u = root;
    end;
    
    f_l = feval(f, l);
    f_u = feval(f, u);
    
    
    root = u -((f_u * (l-u))/ (f_l - f_u));
    absolute_error = abs((root - previous_approximation) / root ) * 100;
    previous_approximation = root;
    
    fprintf('Root %f Error %f\n', root, absolute_error);
    
end;

disp('-------------------------------');
fprintf('Final Root = %f and absolute error = %f', root, absolute_error);

Numerical Method Bisection Method MatLab Code Implementation:

f = @(x) x^2 - 3;
l = input('Enter Lower Limit : ');
u = input('Enter Upper Limit : ');
prev_approximation = 0;

root = (l + u)/2;
f_root = feval(f, root);
given_absoulte_error = input('Enter absolute error : ');
absolute_error = abs((root - prev_approximation) / root) * 100;
disp(absolute_error);

fprintf('\nStep a\tb\tf(a)\tf(b)\troot\tError_abs\n');
while(absolute_error > given_absoulte_error)
    f_root = feval(f, root);
    f_l = feval(f, l);
    f_u = feval(f, u);
    
    if(f_l * f_root > 0)
        l = root;
    else
        u = root;
    end;
    
    root = (l + u)/2;
    absolute_error = abs((root - prev_approximation) / root) * 100;
    prev_approximation = root;
    
    fprintf('Step %f Error %f\n', root, absolute_error);
    fprintf('\nStep a\tb\tf(a)\tf(b)\troot\tError_abs\n');
    
end;

disp('-------------------------------');
fprintf('Final Root = %f and absolute error = %f', root, absolute_error);



Tags: 

Numerical method Codes simple MatLab implementation, Numerical Method Gauss Elimination Matlab code, Numerical Method Gauss Zordan Matlab code, Numerical Method Newton Raphson code, Numerical Method Cramers Rules Matlab code, Numerical Method Simpson 1/3 MatLab Code implementation.Numerical Method Simpson 3/8  MatLab Code implementation.Numerical Method Gauss Elimination MatLab Code Implementation.Numerical Method Gauss Elimination MatLab Code Implementation:.Numerical Method Gauss Zordan MatLab Code Implementation.Numerical Method Gauss Cramers Rule MatLab Code Implementation.Numerical Method Newton Raphson MatLab Code Implementation.Numerical Method Fixed Point Iteration MatLab Code Implementation.Numerical,Method False Position Method MatLab Code Implementation,Numerical Method Bisection Method MatLab Code Implementation



No comments:

Post a Comment