0% found this document useful (0 votes)
3 views

Tutorial 1

The document describes several problems and solutions related to MATLAB programming, including calculating the volume of a cylindrical tank with a conical base, using Manning's equation for water velocity in open channels, and performing vector operations such as dot and cross products. It provides detailed M-file functions for each problem, along with example test cases and expected outputs. Additionally, it includes a sinusoidal function plotting task to illustrate the effects of various parameters on the function's behavior.

Uploaded by

stevenjovi13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Tutorial 1

The document describes several problems and solutions related to MATLAB programming, including calculating the volume of a cylindrical tank with a conical base, using Manning's equation for water velocity in open channels, and performing vector operations such as dot and cross products. It provides detailed M-file functions for each problem, along with example test cases and expected outputs. Additionally, it includes a sinusoidal function plotting task to illustrate the effects of various parameters on the function's behavior.

Uploaded by

stevenjovi13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Problem 3.

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:

Note that the tank’s radius is R.

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

The M-file can be written as

function Vol = tankvolume(R, d)


if d < R
Vol = pi * R ^ 2 * d / 3;
elseif d <= 3 * R
V1 = pi * R ^ 3 / 3;
V2 = pi * R ^ 2 * (d - R);
Vol = V1 + V2;
else
Vol = 'overtop';
end

This function can be used to evaluate the test cases with the following script:

clear, clc, format compact


tankvolume(0.9,1)
tankvolume(1.5,1.25)
tankvolume(1.3,3.8)
tankvolume(1.3,4)

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

The M-file can be written as given below:

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');

This function can be run to create the table.

>> A=[.036 .0001 10 2


.020 .0002 8 1
.015 .0012 20 1.5
.03 .0007 25 3
.022 .0003 15 2.6];
>> Manning(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:

(a) a = [6 4 2]; b = [2 6 4];


(b) a = [3 2 −6]; b = [4 −3 1];
(c) a = [2 −2 1]; b = [4 2 −4];
(d) a = [−1 0 0]; b = [0 −1 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.
Solution 3.20

Here is a function to solve this problem:

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');

Here is a script to run the three cases:

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)

When this is run, the following output is generated:

(a)
th =
38.2132
c =
4 -20 28
m =
34.6410

Solution continued on next page...

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

Solution continued on next page...

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

A general equation for a sinusoid can be written as

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.

You might also like