Showing posts with label Mathematics. Show all posts
Showing posts with label Mathematics. Show all posts

Monday, April 8, 2013

Numerical Integraion Using Trapezoidal Rule.

The process of evaluating a definite integral from a set of tabulated values of integrand f(t) is called numerical integration. The following MATLAB code gives a generalized illustration of numerical integration using Trapezoidal rule. The algorithm for the same is given in the comments at the beginning of the program. Try it for your own function.

% Integration by Trapezoidal method
% This example shows how you can do numerical integration by Trapizoidal
% method
% The formula for this method is given as ...
% $$ \int\limits_0^{\frac{2\pi}{\omega}}f(t)dt = \frac{h}{2}\left[f(a) +
% 2\sum_{i = 0}^{i = K - 1}f(x) + f(b)\right]$
%
% where a = lower limit of integration, b = upper limit of integration
%
% $h = \frac{a - b}{K}$, where K = no. intervals under consideration
% Here the function is $ f(t) = \frac{1}{1+x^2} $ Here we are integrating
% from 0 to 6, taking 6
% intervals in between.
 
clc;
clear all;
a = 0;
b = 6;
K = 6;
h = (b - a) / K;
t = 0 : .0001 :6;
f = inline('1./(1+t.^2)', 't')
plot(t, f(t),'r','linewidth',2.5)
hold;
t1 = a : h : b;
stem(t1, f(t1),'m','filled','linewidth',2.5)
grid;
out = 0;
for i = 1 : K - 1
    out = out + f(a+i*h);
end
result = (h / 2) * (f (a) + 2 * (out) + f(b))

Thank You

Sunday, February 10, 2013

Newton Raphson Method

Hello everybody...
The following is a sample program to understand finding solution of a non linear equation using Newton Raphson Method. This program is not a generalised one. But you can understand the basic idea of the method and how to implement it using MATLAB. For more information about this method please try this

% This programme is to find the solution of a non linear equation by Newton Raphson method.
% This is not a generalized program
clc;
clear all;
format long;
syms x;
e = 1e-5;                      % setting the tolerance value
dx = e + 1;
f = log(2-x) + x^2;               % enter your function here;
x = 7;                          % initially assumed value of x
count = 0;                      % setting counter to know the no of iterations taken
p = zeros(1,1);
while (abs(dx) > e)             % initialising the iteration and continue until the error is less than tolerance
    dx = eval(f/(diff(f)));     % calculating dx, diff is used for finding the differentiation of the fuction
    x = x - dx                 % updating the value of x
    count = count + 1;          % incrimenting the counter
    p(count) = x;
    drawnow();
    plot(abs(p),'r','linewidth',3);
    grid;
    if (count > 300)
        fprintf('Error...! Solution not converging !!! \n');  % printing the error message
        break;
    end
end
% plot(abs(p));
if (count < 300)
    fprintf('The solution = ');  %printing the result
    x
    fprintf('\nNumber of iteration taken = %d\n',count);
end

Try it for different functions
Thank you...