# Connexions

You are here: Home » Content » Numerical Integration with MATLAB | Problem Set

### Recently Viewed

This feature requires Javascript to be enabled.

# Numerical Integration with MATLAB | Problem Set

Module by: Serhat Beyenir. E-mail the author

Summary: Problem Set for Numerical Integration with MATLAB

## Exercise 1

Let the function y defined by y=cosx y x . Plot this function over the interval [-pi,pi]. Use numerical integration techniques to estimate the integral of y over [0, pi] and over [-pi,pi].

### Solution

1. Plotting:

x=-pi:pi/100:pi;
y=cos(x);
plot(x,y),title('Graph of y=cos(x)'),xlabel('x'),ylabel('y'),grid

2. Area calculation 1:

>> x=0:pi/100:pi;
>> y=cos(x);
>> area1=trapz(x,y)

area1 =

1.1796e-016

3. Area calculation 2:

>> x=-pi:pi/100:pi;
>> y=cos(x);
>> area2=trapz(x,y)

area2 =

1.5266e-016


## Exercise 2

Let the function y defined by y=0.04x22.13x+32.58 y 0.04 x 2 2.13 x 32.58 . Plot this function over the interval [3,30]. Use numerical integration techniques to estimate the integral of y over [3,30].

### Solution

1. Plotting:
>> x=3:.1:30;
>> y=0.04*(x.^2)-2.13.*x+32.58;
>> plot(x,y), title('Graph of ...
y=.04*(x^2)-2.13*x+32.58'),xlabel('x'),ylabel('y'),grid

2. Area calculation:

>> area=trapz(x,y)

area =

290.3868


## Exercise 3

A 2000-liter tank is full of lube oil. It is known that if lube oil is drained from the tank, the mass flow rate will decrease from the maximum when the tank level is at the highest. The following data were collected when the tank was drained.

Table 1: Data
Time [min] Mass Flow [kg/min]
0 50.00
5 48.25
10 46.00
15 42.50
20 37.50
25 30.50
30 19.00
35 9.00

Write a script to estimate the amount of oil drained in 35 minutes.

### Solution

clc
t=linspace(0,35,8)                  % Data entry for time [min]
m=[50 48.25 46 42.5 37.5 30.5 19 9] % Data entry for mass flow [kg/min]
% Calculate time intervals
dt=[t(2)-t(1),t(3)-t(2),t(4)-t(3),...
t(5)-t(4),t(6)-t(5),t(7)-t(6),t(8)-t(7)]
% Calculate mass out
dm=[0.5*(m(2)+m(1)),0.5*(m(3)+m(2)),0.5*(m(4)+m(3)),0.5*(m(5)+...
m(4)),0.5*(m(6)+m(5)),0.5*(m(7)+m(6)),0.5*(m(8)+m(7))]
% Calculate differential areas
da=dt.*dm;
% Tabulate time and mass flow
[t',m']
% Tabulate time intervals, mass out and differential areas
[dt',dm',da']
% Calculate the amount of oil drained [kg] in 35 minutes
Oil_Drained=sum(da)

The output is:

ans =

0   50.0000
5.0000   48.2500
10.0000   46.0000
15.0000   42.5000
20.0000   37.5000
25.0000   30.5000
30.0000   19.0000
35.0000    9.0000

ans =

5.0000   49.1250  245.6250
5.0000   47.1250  235.6250
5.0000   44.2500  221.2500
5.0000   40.0000  200.0000
5.0000   34.0000  170.0000
5.0000   24.7500  123.7500
5.0000   14.0000   70.0000

Oil_Drained =

1.2663e+003


## Exercise 4

A gas is expanded in an engine cylinder, following the law PV1.3=c. The initial pressure is 2550 kPa and the final pressure is 210 kPa. If the volume at the end of expansion is 0.75 m3, compute the work done by the gas. 1

### Solution


clc
disp('A gas is expanded in an engine cylinder, following the law PV^1.3=c')
disp('The initial pressure is 2550 kPa and the final pressure is 210 kPa.')
disp('If the volume at the end of expansion is 0.75 m3,')
disp('Compute the work done by the gas.')
disp(' ')               % Display blank line
n=1.3;
P_i=2550;               % Initial pressure
P_f=210;                % Final pressure
V_f=.75;                % Final volume
V_i=(P_f*(V_f^n)/P_i)^(1/n); % Initial volume
c=P_f*V_f^n;
v=V_i:.001:V_f;         % Creating a row vector for volume, v
p=c./(v.^n);            % Computing pressure for volume
WorkDone=trapz(v,p)     % Integrating p*dv

The output is:

A gas is expanded in an engine cylinder, following the law PV^1.3=c
The initial pressure is 2550 kPa and the final pressure is 210 kPa.
If the volume at the end of expansion is 0.75 m3,
Compute the work done by the gas.

WorkDone =

409.0666


## Exercise 5

A force F acting on a body at a distance s from a fixed point is given by F=3s+1s2 F 3 s 1 s 2 . Write a script to compute the work done when the body moves from the position where s=1 to that where s=10. 2

### Solution

clc
disp('A force F acting on a body at a distance s from a fixed point is given by')
disp('F=3*s+(1/(s^2)) where s is the distance in meters')
disp('Compute the total work done in moving')
disp('From the position where s=1 to that where s=10.')
disp(' ')                     % Display blank line
s=1:.001:10;     % Creating a row vector for distance, s
F=3.*s+(1./(s.^2));    % Computing Force for s
WorkDone=trapz(s,F) % Integrating F*ds over 1 to 10 meters.

The output is:
A force F acting on a body at a distance s from a fixed point is given by
F=3*s+(1/(s^2)) where s is the distance in meters
Compute the total work done in moving
From the position where s=1 to that where s=10.

WorkDone =

149.4000


## Exercise 6

The pressure p and volume v of a given mass of gas are connected by the relation (p+av2)(vb)=k p a v 2 v b k where a, b and k are constants. Express p in terms of v, and write a script to compute the work done by the gas in expanding from an initial volume to a final volume. 3

Test your solution with the following input:
a: 0.01
b: 0.001
The initial pressure [kPa]: 100
The initial volume [m3]: 1
The final volume [m3]: 2

### Solution

clc                           % Clear screen
disp('This script computes the work done by')
disp('The gas in expanding from volume v1 to v2')
disp(' ')                     % Display blank line
a=input('Enter the constant a: ');
b=input('Enter the constant b: ');
p_i=input('Enter the initial pressure [kPa]: ');
v_i=input('Enter the initial volume [m3]: ');
v_f=input('Enter the final volume [m3]: ');
k=(p_i+(a/(v_i^2))*(v_i-b)); % Calculating constant k
v=v_i:.001:v_f;              % Creating a row vector for volume
p=(k./(v-b))-(a./(v.^2));    % Computing pressure for volume
WorkDone=trapz(v,p);         % Integrating p*dv
disp(' ')                    % Display blank line
str = ['The work done by the gas in expanding from ', num2str(v_i),...
' m3 to ' num2str(v_f), ' m3 is ', num2str(WorkDone), ' kW.'];
disp(str);

The output is:
This script computes the work done by
The gas in expanding from volume v1 to v2

Enter the constant a: .01
Enter the constant b: .001
Enter the initial pressure [kPa]: 100
Enter the initial volume [m3]: 1
Enter the final volume [m3]: 2

The work done by the gas in expanding from 1 m3 to 2 m3 is 69.3667 kW.


## Footnotes

1. Applied Heat for Engineers by W. Embleton and L Jackson, Thomas Reed Publications. © 1999, (p. 80)
2. O. N. Mathematics: 2 by J. Dobinson, Penguin Library of Technology. © 1969, (p. 213)
3. O. N. Mathematics: 2 by J. Dobinson, Penguin Library of Technology. © 1969, (p. 212)

## Content actions

### Give feedback:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks