Tuesday, May 2, 2017

Simpsons 1/3 Code implementation in Matlab using dynamic parts

Simpsons 1/3 Code implementation in Matlab using dynamic parts



Solution of Simpson 1/3 using dynamic segment of the polynomial


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

n = input('Enter how many parts do you want? : ');
a = input('Enter lower limit : ');
b = input('Enter Upper Limit : ');

part = (b - a)/n;
array = [0,0,0,0,0,0,0,0,0,0,0];


for i=1:n
    if(i == 1)
        array(i) = a;
    else
        array(i) = array(i-1) + part;
    end;
end;




result = 0;
for i=1:n+1
    x = i-1;
    if(x==0 || x == n)
        result = result + feval(f,array(i));
    end
    if(x~=0 || x~=n)
       if(rem(x,2)==0)
           result = result + 2*feval(f,array(i));
       else
           result = result + 4*feval(f,array(i));
       end
    end
end



I = (b-a) * (result /(3 * n));

fprintf('\nIntegral is : %f\n', I);

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

fprintf('\nTrue Error = %f\n', E);


No comments:

Post a Comment