In: Mechanical Engineering
The pressure p and volume v of a given mass of gas are connected by the relation
k=(p+a\v^2)(v-b)
where a, b, and k are constants.
Using the composite trapezoidal method, write a MatLab script to approximate the work done by the gas in expanding from an initial volume to a final volume. Express p in terms of v.
Where W = Integral (Pdv)
Here is a MATLAB code that will implement the trapezoidal rule.
This code is located in a file called trapezoidal.m. It is executed
at the command line prompt by typing:
>> trapezoidal(a,b,nintervals)
where a and b are the lower and upper limits of integration.
nintervals is the number of intervals. The function you wish to
integrate is listed as the last line of the code. In this sample,
f(x) = x.%
% Trapezoidal Method
%function integral = trapezoidal(a,b,nintervals);
dx = (b-a)/nintervals;
npoints = nintervals + 1;
x_vec = [a:dx:b];
integral = funkeval(x_vec(1));
for i = 2:1:nintervals
integral = integral + 2*funkeval(x_vec(i));
end
integral = integral + funkeval(x_vec(npoints));
integral = 0.5*dx*integral;
fprintf(1,'\nUsing the Trapezoidal method \n');
fprintf(1,'to integrate from %f to %f with %i
nintervals,\n',a,b,nintervals);
fprintf(1,'the integral is %e \n \n',integral);
function f = funkeval(x)
f = x;