Calculate integral in Matlab [closed]

I am given the following data:

Data

and I am asked to calculate the integral of Cp/T dT from 113.7 to 264.4.

I am unsure of how I should solve this. If I want to use the integral command, I need a function, but I don't know how my function should be in that case.

I have tried:

function func = Cp./T T = [...] Cp=[...] end 

but that didn't work.

1

3 Answers

Use the cumtrapz function in MATLAB.

T = [...] Cp=[...] CpdivT = Cp./T I = cumtrapz(T, CpdivT) 

You can read more about the function at

3

A simple approach using interp1 and integral using plain vanilla settings.
Would only use more sophisticated numerical techniques if required for application. You can examine the 'RelTol' and 'AbsTol' options in the documentation for integral.

Numerical Integration: (w/ linear interpolation)

% MATLAB R2017a T = [15 20 30 40 50 70 90 110 130 140 160 180 200 220 240 260 270 275 285 298]'; Cp = [5.32 10.54 21.05 30.75 37.15 49.04 59.91 70.04 101.59 103.05 106.78 ... 110.88 114.35 118.70 124.31 129.70 88.56 90.07 93.05 96.82]'; fh =@(t) interp1(T,Cp./T,t,'linear'); t1 = 113.7; t2 = 264.4; integral(fh,t1,t2) 

ans = 91.9954

Alternate Methods of Interpolation:
Your results will depend on your method of interpolation (see code & graphic below).

% Results depend on method of interpolation Linear = integral(@(t) interp1(T,Cp./T,tTgts,'linear'),t1,t2) % = 91.9954 Spline = integral(@(t) interp1(T,Cp./T,tTgts,'spline'),t1,t2) % = 92.5332 Cubic = integral(@(t) interp1(T,Cp./T,tTgts,'pchip'),t1,t2) % = 92.0383 

Graphic show impact of interpolation method choice

Code for graphic:

tTgts = T(1):.01:T(end); figure, hold on, box on p(1) = plot(tTgts,interp1(T,Cp./T,tTgts,'linear'),'b-','DisplayName','Linear') p(2) = plot(tTgts,interp1(T,Cp./T,tTgts,'spline'),'r-','DisplayName','Spline') p(3) = plot(tTgts,interp1(T,Cp./T,tTgts,'pchip'),'g-','DisplayName','Cubic') p(4) = plot(T,Cp./T,'ks','DisplayName','Data') xlim([floor(t1) ceil(t2)]) legend('show') % Cosmetics xlabel('T') ylabel('Cp/T') for k = 1:4, p(k).LineWidth = 2; end 

A poor approximation: (to get rough order of magnitude estimate)

tspace = T(2:end)-T(1:end-1); midpt = mean([Cp(1:end-1) Cp(2:end)],2); sum(midpt.*tspace)./sum(tspace) 

And you can see we're in the ballpark (makes me feel more comfortable at least).


Other viable MATLAB Functions: quadgk | quad

% interpolation method affects answer if using `interp1()` quadgk(@(t) interp1(T,Cp./T,t,'linear'),t1,t2) quad(@(t) interp1(T,Cp./T,t,'linear'),t1,t2) 

Functions that would require more work: trapz | cumtrapz
Notice that trapz and cumtrapz both require unit spacing; to use these would require first interpolating with unit spacing.


Related Posts: (found after answer already completed)
Matlab numerical integration
How to numerically integrate vector data in Matlab?

This is probably better for your problem. Take note that I have assumed 2nd order polynomial fits your data well. You may want to get a better model structure if the fit is unsatisfactory.

% Data T = [15 20 30 40 50 70 90 110 130 140 160 180 200 220 240 260 270 275 285 298]; Cp = [5.32 10.54 21.05 30.75 37.15 49.04 59.91 70.04 101.59 103.05 106.78 110.88 114.35 118.70 124.31 129.70 88.56 90.07 93.05 96.82]; % Fit function using 2nd order polynomial f = fit(T',Cp'./T','poly2'); % Compare fit to actual data plot(f,T,Cp./T) % Create symbolic function syms x func = f.p1*x*x + f.p2*x + f.p3; % Integrate function I = int(func,113.7,264.4); % Convert solution from symbolic to numeric value V = double(I); 

The result is 92.7839

5

You Might Also Like