Tuesday, May 2, 2017

Traphezoidal rules code implementation in Matlab- Dynamic parts

Traphezoidal rules code implementation in Matlab- Dynamic parts

Find the integral for the above function get upper limit and lower limit from user--
f = @ (x) 0.2 + 25*x - 200*(x^2) + 675*(x^3) -900*(x^4) + 400*(x^5);

In this problem, you can take divide the polynomial in mutiple parts. In this solution, this upto in 10 parts in array. You can do it more as your need.


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];


if(n == 2)
    array(1) = a;
    array(2) = b;
else
    for i=1:n
        if(i == 1)
            array(i) = a;
        else
            array(i) = array(i-1) + part;
        end;
    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)
        result = result + 2*feval(f,array(i));
    end
end

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

fprintf('Integral 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