MATLAB Develop and program the trapezoidal method of numeric
MATLAB: Develop and program the trapezoidal method of numerical integration for any function f(x) where x is the independent variable. Note: You may not use the Matlab built-in function “trapz”.
Solution
This Program for y=2*x.You can Change the function also.
function I = trapezoidal_Method ()
n = 10;
a= input( \' Enter the Lower integrations Limit [xmin] :\ \');
b= input( \' Enter the Upper integrations Limit [xmax] :\ \');
sum = 0.0; % to find the sum
dx = (b-a)/(n-1); % to find step size or height of trapezium
% Generating the samples
for i = 1:n
x(i) = a + (i-1)*dx;
end
% to generate the value of function at different values of x or sample
for i = 1:n
y(i) = 2*x(i);
end
% computation of area by using the technique of trapezium method
for i = 1:n
if ( i == 1 || i == n) % for finding the sum of fist and last ordinate
sum = sum + y(i)./2;
else
sum = sum + y(i); % for calculating the sum of other ordinates
end
end
I = sum * dx; % defining the area
end
