Mech 251 Slides
Mech 251 Slides
COMPUTATIONS
D. M. Ashigbi
1 / 139
Course Objective
To introduce students to available computational tools and equip them
with computer programming skills for engineering problem solving.
2 / 139
Content
Introduction to computers, programming language and associated
problem-solving skills; The elements of a computer programming language
including variable types, assignment statements, library functions, control
structures, functions and procedures, arrays and formatting; Introduction
to using Excel for engineering computations: fitting curves to experimental
measurements, creating publication-quality graphs, solving equations,
Excel and user-defined functions, selected topics on programming Excel for
numerical analysis and statistics; Introduction to forms, controls and basic
elements of a Graphics User Interface; Basic MATLAB commands, Arrays:
one-dimensional and multi-dimensional, Flow control, Selective execution,
Repetitive execution and iterations, Input and Output, Modular
Programming: Functions, Plotting, and Advanced data types.
3 / 139
Prerequisites
None
Assessment
Assignments - 13%, MATLAB Onramp - 13%
Mid-Semester Examination, Quizzes - 14%
Final Examination - 60%
References
S. Attaway, MATLAB a practical introduction to programming and problem
solving, 5th ed. Butterworth-Heinemann, 2019.
J. C. Musto, W. E. Howard, and R. R. Williams, Engineering computations :
An Introduction Using MATLAB and excel, McGraw-Hill Higher Education,
2009.
William Palm, MATLAB for Engineering Applications, 4th ed. McGraw-Hill
Higher Education, 2019.
4 / 139
Policies
All submissions will be identified and credited to index number(s).
Donot write your names.
The penalty for late submission is a reduction by 10% of the
maximum mark applicable for the assignment, for each university
business day or part business day that the assignment is late.
Saturdays, Sundays and holidays are excluded from the reckoning.
You may collaborate on assignments, but you are expected to write
your own work. Two or more exact submissions will not be accepted.
Note
Acquiring suffiecient knowledge and usage of computer programming
concepts takes time and effort. There is nothing clever about it.
Determination, hard-work and consistent hands-on practice is the only
way; as in most things.
5 / 139
OUTLINE
1 Introduction
6 / 139
Introduction
Computational tools
8 / 139
Introduction
9 / 139
Introduction
11 / 139
Introduction
12 / 139
Introduction
13 / 139
Working with MATLAB/Octave Environment and Commands Overview
14 / 139
Working with MATLAB/Octave Environment and Commands Overview
15 / 139
Working with MATLAB/Octave Environment and Commands Overview
16 / 139
Working with MATLAB/Octave Environment and Commands Overview
Octave Online
17 / 139
Working with MATLAB/Octave Environment and Commands Overview
Use the up/down arrow keys to scroll through the previous commands.
Use the left/right arrow keys to position cursor for editing
18 / 139
Working with MATLAB/Octave Environment and Commands Overview
MATLAB variable name must start with a letter and contain only
letters, numbers, and underscores
I To create a variable, assign a value to a name (using =)
I Example: y1 = 2
MATLAB variables are case sensitive.
I x1 is different from X1
I x_1 is different from X_1
Builtin variables and constants
√
I i , j: used to indicate complex number −1
I pi: the number π, has the value 3.1415...
I ans: stores the result of the last unassigned value
I Inf: infinity
I eps: Specifies the accuracy of floating point precision.
I NaN: ”Not a Number”, indicates an undefine numerical results
19 / 139
Working with MATLAB/Octave Environment and Commands Overview
>> x = 2
x = 2
>> strg = "Octave"
>> x_1 = 5
strg = Octave
x_1 = 5
>> C = 'Hello, world'
>> X_1 = 5
C = Hello, world
X_1 = 5
>> y = 33;
20 / 139
Working with MATLAB/Octave Environment and Commands Overview
21 / 139
Working with MATLAB/Octave Environment and Commands Overview
22 / 139
Working with MATLAB/Octave Environment and Commands Overview
Order of precedence
23 / 139
Working with MATLAB/Octave Environment and Commands Overview
1 − dc x+2 1-d*c^(x+2)
dc x + 2 d*c^x+2 or 2+d*c^x
2
c x+2 (2/d)*c^(x+2) or 2/d*c^(x+2) or 2*c^(x+2)
d
dc x + 2
(d*c^x+2)/g^2.7
g 2.7
√
dc x + 2 sqrt(d*c^x+2) or (d*c^x+2)^0.5
24 / 139
Working with MATLAB/Octave Environment and Commands Overview
MATLAB SESSION
26 / 139
Working with MATLAB/Octave Environment and Commands Overview
Example 1
The volume of a circular cylinder of height h and radius r is given by
V = πr 2 h. Aparticular cylindrical tank is 15 m tall and has a radius of 8
m. We want to construct another cylindrical tank with a volume 20
percent greater but having the same height. How large must its radius be?
Solution r
V
r=
πh
>>r = 8;
>>h = 15;
>>V = pi*r^2*h;
>>V = V + 0.2*V;
>>r = sqrt(V/(pi*h))
r =
8.7636
27 / 139
Working with MATLAB/Octave Environment and Commands Overview
>> y = (7/2)*i
y =
0.0000 + 3.5000i
>> x = 7/(2i)
x =
0.0000 - 3.5000i
28 / 139
Working with MATLAB/Octave Environment and Commands Overview
29 / 139
Working with MATLAB/Octave Environment and Commands Overview
30 / 139
Working with MATLAB/Octave Environment and Commands Overview
Mathematical Functions
31 / 139
Working with MATLAB/Octave Environment and Commands Overview
32 / 139
Working with MATLAB/Octave Environment and Commands Overview
Exercise
1 Suppose that x = 2 and y = −1. Evaluate the following expressions
using Octave/MATLAB to 16 significant digits
√
4
a
p 2x 3
4
b 2y 3
2 In an open channel of circular cross-section shown below, the flow
rate Q in m3 /s is given by:
5/2 √
23/2 Dc g (θ − 0.5 sin(2θ ))3/2
Q= √
8 sin θ (1 − cos θ )5/2
33 / 139
Working with MATLAB/Octave Environment and Commands Overview
Getting help
34 / 139
Working with MATLAB/Octave Environment and Commands Overview
Script Files
35 / 139
Working with MATLAB/Octave Environment and Commands Overview
36 / 139
Working with MATLAB/Octave Environment and Commands Overview
Example 2
Convertion of example 1 into a script file
1 On home menu tab, new −− > Script, then save
OR
2 At command terminal type edit filename
A new script file opens and named ”filename”.m and save at
ccurrent working directory. Type in it
1 r = 8;
2 h = 15;
3 V = pi*r^2*h;
4 V = V + 0.2*V;
5 r = sqrt(V/(pi*h))
37 / 139
Working with MATLAB/Octave Environment and Commands Overview
38 / 139
Working with MATLAB/Octave Numeric Arrays
Arrays
All MATLAB variables are multidimensional arrays, no matter what
type of data
I MATLAB is designed to operate primarily on whole arrays.
I Most programming languages mostly work with numbers one at a time
A matrix is a two-dimensional array.
We create an array with four elements in a single row as:
1 2 3 4
1 3 5
2 4 6
7 8 10
39 / 139
Working with MATLAB/Octave Numeric Arrays
There are several classes (data types) in MATLAB. Each of them is in the
form of a matrix or array. With the exception of function handles.
40 / 139
Working with MATLAB/Octave Numeric Arrays
>>p = [3,7,9]
p =
To create a row vector, 3 7 9
separate the elements with
either a comma or a space. >>g = [3;7;9]
Use square brackets. g =
3
Create a column vector by
7
separating the elements by
9
semicolons
Also create a column vector >>p = [3,7,9]'
by using the transpose p =
notation (’), on a row vector 3
7
9
41 / 139
Working with MATLAB/Octave Numeric Arrays
>> r = [2,4,20]
r =
You can create vectors by
”appending” one vector to 2 4 20
another
For example, to create the >> w = [9,-6,3]
row vector u whose first three w =
columns contain the values of
r = [2,4,20] and whose fourth, 9 -6 3
fifth, and sixth columns
contain the values of w = >> u = [r,w]
[9,-6,3] u =
2 4 20 9 -6
42 / 139
Working with MATLAB/Octave Numeric Arrays
43 / 139
Working with MATLAB/Octave Numeric Arrays
>> z = 5:0.1:8
z =
-3 -2 -1 0 1 2
44 / 139
Working with MATLAB/Octave Numeric Arrays
45 / 139
Working with MATLAB/Octave Numeric Arrays
46 / 139
Working with MATLAB/Octave Numeric Arrays
>> x = [2,-4,5]
x =
The length command gives
2 -4 5
the number of elements in the
vector
>> length(x)
The magnitude of a vector is ans = 3
a scalar, and is the same as
the vector’s geometric length >> M = sqrt(sum(x.*x))
The absolute value of a vector M = 6.7082
x is a vector whose elements
are the absolute values of the >> abs(x)
elements of x. ans =
2 4 5
47 / 139
Working with MATLAB/Octave Numeric Arrays
Matrices
48 / 139
Working with MATLAB/Octave Numeric Arrays
Creating Matrices
Suppose
>> c = [a b]
>> a = [1,3,5] c =
a =
1 3 5 7 9 11
1 3 5
>> D = [a;b]
>> b = [7,9,11] D =
b =
1 3 5
7 9 11 7 9 11
50 / 139
Working with MATLAB/Octave Numeric Arrays
>> M = [2,4,10;16,3,7;8,4,9;3,12,15]
M =
2 4 10
16 3 7
8 4 9
3 12 15
>> M(2,3)
ans = 7
>> M(3,2)
ans = 4
51 / 139
Working with MATLAB/Octave Numeric Arrays
Amending arrays
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> A(2,2) = -11
A =
A(end,:) denotes the last row 1 2 3
in A, and A(:,end) denotes 4 -11 6
the last column. 7 8 9
Assigning elements >> A(end,:)
ans =
7 8 9
>> A(:,end)
ans =
3 55 / 139
Working with MATLAB/Octave Numeric Arrays
Multidimensional Arrays
56 / 139
Working with MATLAB/Octave Numeric Arrays
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
Create a 2-D matrix first, and 7 8 9
then extend it.
>> A(:,:,2) = [10 11 12; 13 14
To do this, assign another
A =
matrix to the index value 2 in
ans(:,:,1) =
the third dimension.
1 2 3
Note its dimension in the 4 5 6
workspace 7 8 9
ans(:,:,2) =
10 11 12
13 14 15
16 17 18 57 / 139
Working with MATLAB/Octave Numeric Arrays
Accessing Elements
58 / 139
Working with MATLAB/Octave Numeric Arrays
Array Functions
59 / 139
Working with MATLAB/Octave Numeric Arrays
60 / 139
Working with MATLAB/Octave Numeric Arrays
61 / 139
Working with MATLAB/Octave Numeric Arrays
Element-by-element operations
62 / 139
Working with MATLAB/Octave Numeric Arrays
>> A = [6,-2;10,3];
>> B = [9,8;-12,14];
>> A+B
ans =
15 6
-2 17
6 27
15 -21
64 / 139
Working with MATLAB/Octave Numeric Arrays
For eample,
11 5 −7 8
A= B =
−9 4 6 2
then C = A.*B gives this result:
11(−7) 5(8) −77 40
C = =
−9(6) 4(2) −54 8
-77 40
-54 8
65 / 139
Working with MATLAB/Octave Numeric Arrays
66 / 139
Working with MATLAB/Octave Numeric Arrays
The code snippet below computes the sine of 1,001 values ranging
from 0 to 10
1 t = 0:.01:10;
2 y = sin(t);
67 / 139
Working with MATLAB/Octave Numeric Arrays
68 / 139
Working with MATLAB/Octave Numeric Arrays
Matrix operation
matrix operations follow the normal rules of linear algebra
Example matrix Multiplication:
6 −2 (6)(9) + (−2)(−5) (6)(8) + (−2)(12)
9 8
10
3
= (10)(9) + (3)(−5) (10)(8) + (3)(12)
−5 12
4 7 (4)(9) + (7)(−5) (4)(8) + (7)(12)
64 24
= 75 116
1 116
70 / 139
Working with MATLAB/Octave Numeric Arrays
Function Description
Example 3
>> c = [4 -1 0 0 0 -1;...
-1 4 -1 0 -1 0; ...
0 -1 4 -1 0 0; ...
Solve the following system of 0 0 -1 4 -1 0; ...
equations 0 -1 0 -1 4 -1; ...
-1 0 0 0 -1 4];
4T1 − T2 − T6 = 200
>> d = [200, 80, 140, 140,
−T1 + 4T2 − T3 − T5 = 80 ,→ 80, 200];
−T2 + 4T3 − T4 = 140 >> T = linsolve(c, d')
−T3 + 4T4 − T5 = 140 T =
−T2 − T4 + 4T5 − T6 = 80 94.286
82.857
−T1 − T5 + 4T6 = 200
74.286
74.286
82.857
94.286 73 / 139
Working with MATLAB/Octave Numeric Arrays
>> A = [5 -3 2; -3 8 4; 4 2 -9];
>> [v,d] = eig(A)
v =
0.172542 0.870606 -0.537542
0.238228 0.377390 0.842875
-0.955760 0.315629 -0.024708
d =
-10.2206 0 0
0 4.4246 0
0 0 9.7960
74 / 139
Working with MATLAB/Octave Plotting
To plot a data set, just create two vectors containing the x and y
values to be plotted, and use the plot function.
1 x = 0:1:10;
2 y = x.^2 - 10.*x + 15;
3 plot(x,y);
75 / 139
Working with MATLAB/Octave Plotting
1 x = 0:1:10;
2 y = x.^2 - 10.*x + 15;
3 plot(x,y);
4 title ('Plot of y = x.^2
,→ - 10.*x + 15');
5 xlabel ('x');
6 ylabel ('y');
7 grid on;
76 / 139
Working with MATLAB/Octave Plotting
Multiple plots
Including more than one set of (x, y ) values in the plot function.
1 x = 0:pi/100:2*pi;
2 y1 = sin(2*x);
3 y2 = 2*cos(2*x);
4 plot(x,y1,x,y2);
1 x = -pi:pi/20:pi;
2 y1 = sin(x);
3 y2 = cos(x);
4 plot(x,y1);
5 hold on;
6 plot(x,y2);
7 hold off; 77 / 139
Working with MATLAB/Octave Plotting
78 / 139
Working with MATLAB/Octave Plotting
x = 0:pi/30:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
plot(x,y1,'bx-',x,y2,'go--');
title ('Plot of f(x) = sin(2x)
,→ and its derivative');
xlabel ('x');
ylabel ('y');
legend ('f(x)','d/dx
,→ f(x)','Location','southeast')
grid on;
79 / 139
Working with MATLAB/Octave Plotting
1 x = 0:0.01:5;
2 y = exp(-1.2*x).*sin(10*x+5);
3 subplot(1,2,1)
4 plot(x,y);xlabel('x');ylabel('y');axis([0 5 -1 1])
5 x = -6:0.01:6;
6 y = abs(x.^3-100);
7 subplot(1,2,2)
8 plot(x,y);xlabel('x');ylabel('y');axis([-6 6 0 350])
80 / 139
Working with MATLAB/Octave Plotting
Example 5
The script above plots the function:
81 / 139
Working with MATLAB/Octave Plotting
82 / 139
Working with MATLAB/Octave Program flow control structures
83 / 139
Working with MATLAB/Octave Program flow control structures
84 / 139
Working with MATLAB/Octave Program flow control structures
Branching If Statement
Branches to different parts of its structure depending on the satisfaction of
conditional expressions.
Syntax: Example 6
In English, we could specify this
if logical expression procedure as follows: If x is
greater than or equal to zero,
statements √
end compute y from y = x
The statement implements this
procedure as:
For example Suppose that,
x is a scalar and that we want 1 if x >= 0
√
to compute y = x only if 2 y = sqrt(x)
x ≥ 0. 3 end
85 / 139
Working with MATLAB/Octave Program flow control structures
1 z = 0;w = 0;
2 if (x >= 0)&(y >= 0)
3 z = sqrt(x) + sqrt(y)
4 w = sqrt(x*y)
5 end
86 / 139
Working with MATLAB/Octave Program flow control structures
The else statement must come after the elseif statement to take care
of all conditions that might be unaccounted for.
The statements elseif and else are optional.
Also, there can be more than one elseif statement.
1 if logical expression 1
2 statement group 1
3 elseif logical expression 2
4 statement group 2
5 else
6 statement group 3
7 end
87 / 139
Working with MATLAB/Octave Program flow control structures
Note how the indenting of the expressions inside the various nested
structures makes the code more readable.
88 / 139
Working with MATLAB/Octave Program flow control structures
Example 7
90 / 139
Working with MATLAB/Octave Program flow control structures
Example 8
Suppose the quantity k has been assigned a value or had its value
computed prior to encountering this structure.
91 / 139
Working with MATLAB/Octave Program flow control structures
for loop
A for loop repeats a series of statements a specific number of times.
I Each repetition of the loop is a pass
I the number of passes is known ahead of time
Its general form is:
92 / 139
Working with MATLAB/Octave Program flow control structures
Example 9
for k = 5:10:35
x = k^2
end
Example 10
g = [2,4,6,7,8,2,10,45,75,4,88,98,4,1,5,7];
gr8t = g(1);
for n = 1:length(g)
if g(n) >= gr8t
gr8t = g(n);
end
end
disp(gr8t)
94 / 139
Working with MATLAB/Octave Program flow control structures
while loop
The while loop repeats one or more statements an indefinite number
of times, leaving the loop only when a specified condition has been
satisfied.
I thus the number of passes is not known in advance
Its general form is
while logical_expression
statements
end
95 / 139
Working with MATLAB/Octave Program flow control structures
Example 11
x = 5;
while x < 25
disp(x)
x = 2*x - 1; The loop variable x is initially
end assigned the value 5.
The value then changes to 9,
after x = 2*x - 1;
Before each pass through the
loop, x is checked to see
whether its value is less than 25.
If so, the pass is made.
If not, the loop is skipped and
the program continues to
execute any statements
following the end statement. 96 / 139
Working with MATLAB/Octave Program flow control structures
97 / 139
Working with MATLAB/Octave Program flow control structures
98 / 139
Working with MATLAB/Octave Program flow control structures
Infinite loops
1 x = 8;
2 while x ~= 0
3 x = x - 3;
4 end
99 / 139
Working with MATLAB/Octave Program flow control structures
Debugging
100 / 139
Working with MATLAB/Octave Functions
Input/output functions
Function Description
101 / 139
Working with MATLAB/Octave Functions
Example 12
Upon execution,
stores the numeric value 1.23 into in1, where the value 1.23 was
entered by the user.
102 / 139
Working with MATLAB/Octave Functions
fprintf(fileID,formatSpec,A1,...,An)
fprintf(formatSpec,A1,...,An)
Example 13
1 A1 = [0.9, 900];
2 A2 = [0.8, 0.7 ; 800, 700]; X is 0.90 km or 900.0 m
3 formatSpec = 'X is %4.2f km or X is 0.80 km or 800.0 m
,→ %5.1f m\n'; X is 0.70 km or 700.0 m
4 fprintf(formatSpec,A1,A2)
103 / 139
Working with MATLAB/Octave Functions
104 / 139
Working with MATLAB/Octave Functions
%s string of characters
\n New line.
\t Horizontal tab.
105 / 139
Working with MATLAB/Octave Functions
Function Description
lu LU matrix factorization
qr QR decomposition
User-define Functions
All of the M-files that we have seen so far have been script files.
Script files are just collections of MATLAB statements that are stored
in a file.
Another type of M-file is a function file.
Create by selecting New under the HOME tab on the Toolstrip, but
instead of selecting Script, select Function.
The first line in a function file begins with a function definition line
that has a list of inputs and outputs, i.e. function header
Or create a script and begin its first executable line with a function
definition
This line distinguishes a function M-file from a script M-file.
107 / 139
Working with MATLAB/Octave Functions
Function Syntax:
108 / 139
Working with MATLAB/Octave Functions
Example 14
Example 14
8 % Evaluate function
9 [dist,vel] = drop(g,v0,t);
10
110 / 139
Working with MATLAB/Octave Functions
6 function p = perm(n,r)
7 % perm Local Function that calculates the permutation
8 p = fact(n)/fact(n-r);
9 end
10
11 function f = fact(n)
12 % fact Local Function that calculates the factorial
13 f = prod(1:n);
14 end
111 / 139
Working with MATLAB/Octave Functions
Function handles
A function handle is an Octave/MATLAB data type that stores an
association to a function.
To create a handle for a function, precede the function name with an
@ sign.
Once we create the function handle, we can pass input arguments to
the function handle or pass the handle to another function
For example, consider the following user-defined function, which
computes: y = x + 2e −x − 3
To create a handle to this
function and name the handle
fh1;
1 function y = f1(x)
2 y = x + 2*exp(-x) - 3; >> fh1 = @f1;
3 end >> fh1(2)
ans = -0.7293
112 / 139
Working with MATLAB/Octave Functions
Anonymous Functions
A function that is not stored in a program file, but is associated with
a variable whose data type is function handle.
Anonymous functions can accept multiple inputs and return one
output. They can contain only a single executable statement.
For example,
f (x) = 5 sin(x) − x 2 + 3
g (y , z) = 25 − y 2 − z 2
113 / 139
Working with MATLAB/Octave Functions
114 / 139
Working with MATLAB/Octave Functions
fplot function
fplot(FN,interval)
Plots the function FN over the specified interval. Specify the interval as a
two-element vector of the form [tmin tmax].
115 / 139
Working with MATLAB/Octave Functions
x = fzero(FN,x0)
Examples:
116 / 139
Working with MATLAB/Octave Functions
fsolve() function
x = fsolve(fun,x0)
1
x1 cos (x2 ) + x2 sin (x1 ) =
2
117 / 139
Working with MATLAB/Octave Functions
1
x1 cos (x2 ) + x2 sin (x1 ) − = 0
2
Write a function that computes the left-hand side of these two equations
1 function F = root2D(x)
2 F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
3 F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;
4 end
fun = @root2D;
x0 = [0,0]; %Initial guess
x = fsolve(fun,x0) 118 / 139
Working with MATLAB/Octave Functions
Exercise
g (x, y ) = x 2 /4 + y 2 − 1
119 / 139
Working with MATLAB/Octave Functions
r = roots(p)
1 p = [1 -2 -4];
2 r = roots(p)
120 / 139
Working with MATLAB/Octave Functions
121 / 139
Working with MATLAB/Octave Functions
122 / 139
Working with MATLAB/Octave Functions
1 MATLAB only
123 / 139
Working with MATLAB/Octave Functions
124 / 139
Working with MATLAB/Octave Functions
vq = interp1(x,v,xq,method)
125 / 139
Working with MATLAB/Octave Functions
° °
need to use the table below, which is just a portion of the steam tables.
Notice that the table is spaced first at 50 C intervals and then at 100 C
intervals. Suppose you have a project that requires you to use this table
and you prefer not toperform a linear interpolation every time you use it.
°
Use Octave/MATLAB to create a table, employing linear interpolation,
with a temperature spacing of 25 C.
126 / 139
Working with MATLAB/Octave Functions
Example 15
1 clear, clc
2 T = [100, 150, 200, 250, 300, 400, 500]';
3 v = [1.6958, 1.9364, 2.172, 2.406, 2.639, 3.103, 3.565]';
4 u = [2506.7, 2582.8, 2658.1, 2733.7, 2810.4, 2967.9,
,→ 3131.6]';
5 h = [2676.2, 2776.4, 2875.3, 2974.3, 3074.3, 3278.2,
,→ 3488.1]';
6 props = [v,u,h];
7 newT = [100:25:500]';
8 newprop = interp1(T,props,newT);
9 disp('Steam Properties at 0.1 MPa')
10 disp('Temp Specific Volume Internal Energy Enthalpy')
11 disp(' C m^3/kg kJ/kg kJ/kg')
12 fprintf('%6.0f %10.4f %8.1f %8.1f
,→ \n',[newT,newprop]')
127 / 139
Working with MATLAB/Octave Functions
Example 15
128 / 139
Working with MATLAB/Octave Functions
[p,S] = polyfit(x,y,n)
returns the coefficients for a polynomial p(x) of degree n that is a best fit
(in a least-squares sense) for the data in y. The coefficients in p are in
descending powers, and the length of p is n+1
129 / 139
Working with MATLAB/Octave Functions
Example 16
Fit Polynomial to Trigonometric Function
130 / 139
Working with MATLAB/Octave Functions
Example 16
131 / 139
Working with MATLAB/Octave Functions
Example 17
Determining how much water will flow through a culvert is not as easy as
it might first seem. The channel could have a non-uniform shape,
obstructions might influence the flow, friction is important, and so on. A
numerical approach allows us to fold all those concerns into a model of
how the water actually behaves.
Compute a best-fit linear, quadratic, and cubic equation for the data, and
plot them on the same graph. Which model best represents the data?
(Linear is first order, quadratic is second order, and cubic is third order.)
132 / 139
Working with MATLAB/Octave Functions
Example 17
1 % Water in a Culvert
2 height = [1.7, 1.95, 2.6, 2.92, 4.04, 5.24];
3 flow = [2.6, 3.6, 4.03, 6.45, 11.22, 30.61];
4 new_height = 0:0.5:6;
5 newf1 = polyval(polyfit(height,flow,1),new_height);
6 newf2 = polyval(polyfit(height,flow,2),new_height);
7 newf3 = polyval(polyfit(height,flow,3),new_height);
8 plot(height,flow,'o',new_height,newf1,new_height,newf2,
9 new_height,newf3)
10 title('Fit of Water Flow')
11 xlabel('Water Height, ft')
12 ylabel('Flow Rate, CFS')
13 legend('Data','Linear Fit','Quadratic Fit', 'Cubic Fit')
133 / 139
Working with MATLAB/Octave Functions
Example 17
134 / 139
Working with MATLAB/Octave Add-on packages and toolboxes
Symbolic Computations
135 / 139
Working with MATLAB/Octave Add-on packages and toolboxes
Example functions
136 / 139
Working with MATLAB/Octave Add-on packages and toolboxes
137 / 139
Working with Excel
138 / 139
Engineering Problem Solving
139 / 139