Tutorial 1
Tutorial 1
Figure P3.1 shows a cylindrical tank with a conical base. If the liquid level is quite low, in the conical
part, the volume is simply the conical volume of liquid. If the liquid level is midrange in the
cylindrical part, the total volume of liquid includes the filled conical part and the partially filled
cylindrical part.
Use decisional structures to write an M-file to compute the tank’s volume as a function of given
values of R and d. Design the function so that it returns the volume for all cases
where the depth is less than 3R. Return an error message (“Overtop”) if you overtop the tank—that
is, d > 3R. Test it with the following data:
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
Solution 3.1
This function can be used to evaluate the test cases with the following script:
Results:
ans =
1.0179
ans =
2.9452
ans =
15.5739
ans =
overtop
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
Problem 3.9
Manning’s equation can be used to compute the velocity of water in a rectangular open channel:
2/3
S BH
U
n B 2H
where U = velocity (m/s), S = channel slope, n = roughness coefficient, B = width (m), and H = depth
(m). The following data are available for five channels:
Write an M-file that computes the velocity for each of these channels. Enter these values into a
matrix where each column represents a parameter and each row represents a channel. Have the M-
file display the input data along with the computed velocity in tabular form where velocity is the
fifth column. Include headings on the table to label the columns.
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
Solution 3.9
function Manning(A)
A(:,5)=sqrt(A(:,2))./A(:,1).*(A(:,3).*A(:,4)./(A(:,3)+2*A(:,4))).^(2/3);
fprintf('\n n S B H U\n');
fprintf('%8.3f %8.4f %10.2f %10.2f %10.4f\n',A');
n S B H U
0.036 0.0001 10.00 2.00 0.3523
0.020 0.0002 8.00 1.00 0.6094
0.015 0.0012 20.00 1.50 2.7569
0.030 0.0007 25.00 3.00 1.5894
0.022 0.0003 15.00 2.60 1.2207
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
Problem 3.20
A Cartesian vector can be thought of as representing magnitudes along the x-, y-, and z-axes
multiplied by a unit vector (i, j, k). For such cases, the dot product of two of these vectors {a} and {b}
corresponds to the product of their magnitudes and the cosine of the angle between their tails as in
{a}·{b} = ab cosθ
The cross product yields another vector, {c} = {a} × {b}, which is perpendicular to the plane defined
by {a} and {b} such that its direction is specified by the right-hand rule. Develop an M-file function
that is passed two such vectors and returns θ, {c} and the magnitude of {c}, and generates a three-
dimensional plot of the three vectors {a}, {b}, and {c) with their origins at zero. Use dashed lines for
{a} and {b} and a solid line for {c}. Test your function for the following cases:
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
Solution 3.20
function [theta,c,mag]=vector(a,b)
amag=norm(a);
bmag=norm(b);
adotb=dot(a,b);
theta=acos(adotb/amag/bmag)*180/pi;
c=cross(a,b);
mag=norm(c);
x1=[0 a(1)];y1=[0 a(2)];z1=[0 a(3)];
x2=[0 b(1)];y2=[0 b(2)];z2=[0 b(3)];
x3=[0 c(1)];y3=[0 c(2)];z3=[0 c(3)];
x4=[0 0];y4=[0 0];z4=[0 0];
plot3(x1,y1,z1,'--b',x2,y2,z2,'--r',x3,y3,z3,'-k',x4,y4,z4,'o')
xlabel('x');ylabel('y');zlabel('z');
b = [2 6 4]; a = [6 4 2];
[th,c,m]=vector(a,b)
pause
a = [3 2 -6]; b = [4 -3 1];
[th,c,m]=vector(a,b)
pause
a = [2 -2 1]; b = [4 2 -4];
[th,c,m]=vector(a,b)
pause
a = [-1 0 0]; b = [0 -1 0];
[th,c,m]=vector(a,b)
(a)
th =
38.2132
c =
4 -20 28
m =
34.6410
30
20
10
0
10
0 6
4
-10 2
-20 0
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
(b)
th =
90
c =
-16 -27 -17
m =
35.6931
10
-10
-20
10
0 5
-10 0
-5
-20 -10
-15
-30 -20
(c)
th =
90
c =
6 12 12
m =
18
15
10
-5
15
10 6
5 4
0 2
-5 0
(d)
th =
90
c =
0 0 1
m =
1
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
1
0.5
0
0
0
-0 . 5 -0 . 2
-0 . 4
-0 . 6
-0 . 8
-1 -1
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
Problem 3.25
t y ysin(2πft φ )
y()
where y = the dependent variable, y = the mean value, Δy = the amplitude, f = the ordinary
frequency (i.e., the number of oscillations that occur each unit of time), t = the independent variable
(in this case time), and = phase shift. Develop a MATLAB script to generate a 5 panel vertical plot
to illustrate how the function changes as the parameters change. On each plot display the simple
sine wave, y(t) = sin(2πt), as a red line. Then, add the following functions to each of the 5 panels as
black lines:
Employ a range of t = 0 to 2π and scale each subplot so that the abscissa goes from 0 to 2π and the
ordinate goes from −2 to 2. Include the Ftles with each subplot, label each subplot’s ordinate as
'f(t)', and the bottom plot’s abscissa as 't'.
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
Solution 3.25
clc,clf
t=[0:pi/64:2*pi]; f=1;
y1=sin(2*pi*f*t); y2=1+sin(2*pi*f*t);
subplot(5,1,1)
plot(t,y1,'r--',t,y2,'k');
xlim([min(t) max(t)]),ylim([-2 2]),ylabel('f(t)')
title('(a) Effect of mean')
y3=2*sin(2*pi*f*t);
subplot(5,1,2)
plot(t,y1,'r--',t,y3,'k')
xlim([min(t) max(t)]),ylim([-2 2]),ylabel('f(t)')
title('(b) Effect of amplitude')
y4=sin(4*pi*t);
subplot(5,1,3)
plot(t,y1,'r--',t,y4,'k')
xlim([min(t) max(t)]),ylim([-2 2]),ylabel('f(t)')
title('(c) Effect of frequency')
y5=sin(2*pi*f*t-pi/4);
subplot(5,1,4)
plot(t,y1,'r--',t,y5,'k')
xlim([min(t) max(t)]),ylim([-2 2]),ylabel('f(t)')
title('(d) Effect of phase shift')
y6=cos(2*pi*f*t-pi/2);
subplot(5,1,5)
plot(t,y1,'r--',t,y6,'k')
xlim([min(t) max(t)]),ylim([-2 2]),xlabel('t'),ylabel('f(t)')
title('(e) Relationship of sine to cosine')
f(t)
f(t)
f(t)
f(t)
f(t)
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.
Copyright © McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for
sale or distribution in any manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or
posted on a website, in whole or part.