Freshman Engineering Problem Solving With MATLAB
Freshman Engineering Problem Solving With MATLAB
Freshman Engineering Problem Solving With MATLAB
This selection and arrangement of content as a collection is copyrighted by Darryl Morrell. It is licensed under the Creative Commons Attribution 2.0 license (http://creativecommons.org/licenses/by/2.0/). Collection structure revised: April 23, 2007 PDF generated: February 4, 2011 For copyright and attribution information for the modules contained in this collection, see p. 75.
Table of Contents
1 Introduction 1.1 Introduction to the M-le Connexions Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Finding Help for M-le Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 Problem Solving 2.1 Problem Solving Using M-le Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.2 Exercise for Problem Solving with M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.3 Visual Tools for Problem Solving with M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3 Basic Mathematical Computations 3.1 Basic Mathematical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.2 Variables in M-le Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.3 Exercises for Basic Mathematical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.4 Vectors and Arrays in M-File Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.5 Basic Complex and Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 16 4 Graphing 4.1 Introduction to Graphing in M-File Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 4.2 Graphical representation of data in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 5 Programming with M-le Scripts 5.1 A Very Brief Introduction to Programming with M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 5.2 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 5.3 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 42 5.4 While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Attributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
iv
Chapter 1
Introduction
1.1 Introduction to the M-le Connexions Modules1
MATLAB has emerged as a widely used computational tool in many elds of engineering. MATLAB consists of a programming language used in an interactive computing environment that supports the development of programs to solve complex problems. The MATLAB language has become a defacto standard that is also used by several other computational packages, including LabVIEW MathScript and Octave. Generically, we refer to these packages as m-le environments because the program les typically are identied by an extension of "m". The Connexions modules in this course2 are intended to introduce freshman engineering students to problem solving using an m-le environment. Most of the information in these modules applies to any m-le environment (MATLAB, LabVIEW MathScript, Octave, etc.). There are some dierences between environments, and occasionally some material will be specic to a given environment. This material is oset from the surrounding text and labeled with the appropriate environment. For example:
note: note: note:
Matlab is a commercial product of The MathWorks3 . LabVIEW MathScript is a commercial product of National Instruments4 .
Octave is an open source environment that is available without charge. Information about Octave is available at the Octave home page5 .
2
note:
CHAPTER 1. INTRODUCTION
Useful information is available at the National Instruments LabVIEW MathScript Portal9 , including an interactive demonstration of MathScript10 .
note:
The denitive source for information is The ocial GNU Octave Manual11 . Also, here12 is one of many good tutorials.
Chapter 2
Problem Solving
2.1 Problem Solving Using M-le Environments1
The purpose of this module is to introduce the engineering problem solving process in the context of using m-le environments to solve problems. Many variations of this process exist and no single variation is best for solving all problems. In this module we describe a variation of the engineering problem solving process that applies to m-le environments problem solving. Other variations are described in the reference at the end of this module. The following problem solving process is fairly involved and may be an excessive amount of work for simple problems. For problems where the solution is straight forward, simply solve the problem; for more complex problems, the solution will usually not be obvious and this process will aid in development of an appropriate solution. This specic process is divided into a set of seven steps. Each step includes questions that help move you successfully through the problem solving process. 1. Dene the Problem
What problem are you trying to solve? "What would success look like?" What should the program output? Computed values? A plot or series of plots?
2. Identify given information.
What constants or data are supplied? What theory, principles, models and equations have you been given?
3. Identify other available information.
What theory, principles, models and equations can you nd in other sources (text books, lecture notes, etc.)?
4. Identify further needed information.
What other information do you need? Where will you nd it?
5. Design and implement your solution to the problem.
1 This
How can you break the larger problem into smaller problems? Look at the problem from the top down or bottom up? What programming techniques might you use to convert input to output? What variables do you need? Vectors? Arrays?
Example 2.1
The ACME Manufacturing Company is planning to manufacture widgets5 . There are two dierent manufacturing processes: one cost $10,000 to implement and can manufacture up to 1000 widgets, while the other cost $100,000 to implement and can manufacture up to one million widgets. In addition to the manufacturing cost, there is a xed cost of $1 per widget (for packing and shipping
2 This content is available online at <http://cnx.org/content/m13865/1.1/>. 3 This content is available online at <http://cnx.org/content/m13820/1.3/>. 4 http://www.uml.org/ 5 http://en.wiktionary.org/wiki/widget
5 each widget to the customer). Consider the problem of calculating the cost per unit to manufacture and ship a given number of widgets. One way to solve the problem is to complete the following steps:
Get the number of widgets to be produced. Determine the total manufacturing costs. Determine the total xed costs. Determine the total cost. Compute the cost per unit.
Figure 2.1 (First Flow Chart) shows a ow chart that represents these steps. Figure 2.2 (First Pseudo Code) shows the pseudo code that represents these steps.
Having developed an initial solution, we can rene those elements whose implementation may not yet be fully dened. In this example, the manufacturing cost depend on the number of widgets to be made; if this number is less than or equal to 1000, the cost is $10,000, while if the number is greater than 1000, the cost is $100,000. We can represent this using the ow chart blocks in Figure 2.3 (Flow Chart for Manufacturing Costs). The diamond is a conditional; the branch of the ow chart that is actually executed depends on whether the conditional is true or false.
Figure 2.3:
Putting this conditional into the complete ow chart gives the ow chart in Figure 2.4 (Complete Flow Chart). The conditional is incorporated in the the pseudo code to give the pseudo code in Figure 2.5 (Complete Pseudo Code).
Determine the total xed costs. Determine the total cost. Compute the cost per unit.
Figure 2.4:
There are advantages and disadvantages for both ow charts and pseudo code. Advantages of using a ow chart include that it provides a strong visual representation of the program and that it is straightforward for novice programmers to use. The primary disadvantage of using a ow chart is that it is possible to create a ow chart that can only be implemented by "spaghetti code"6 . Spaghetti code is considered extremely bad form for many reasons; in particular, it is hard to understand, debug and modify. The primary advantage of pseudo code is that its structure is clearly related to the available control structures in modern computer languages. Pseudo code has several disadvantages: it is not a very strong visual representation, and it is less straightforward for novice programmers.
6 http://en.wikipedia.org/wiki/Spaghetti_code
Chapter 3
m-le
x-y x+y x*y x/y x^y exp(x) log10(x) log(x) log2(x) cos(x) sin(x)
continued on next page
1 This
10
sqrt(x)
Expressions are formed from numbers, variables, and these operations. The operations have dierent precedences. The ^ operation has the highest precedence; ^ operations are evaluated before any other operations. Multiplication and division have the next highest precedence, and addition and subtraction have the lowest precedence. Precedence is altered by parentheses; expressions within parentheses are evaluated before expressions outside parentheses. Example 3.1
The Table (Table 3.2: Example Expressions) below shows several mathematical formulas, the corresponding expressions, and the values that are computed for the expressions.
MATLAB Expression
5^2+4^2 (5+4)^2 (2 + 3)/(4 - 5) log10(100) log(4*(2+3))
Table 3.2
Computed Value
41 81 -5 2 2.9957
(5 + 4)
A semicolon added at the end of a line suppresses the output. Often it is useful to split input over multiple lines. To split a statement across multiple lines, enter three periods ... at the end of the line to indicate it continues on the next line.
Example 3.2
(2+3)... /(4-5)
2+3 45
11 command clear a. Once a variable is set to a particular value, we can get this value by using the variable name in an expression (e.g. a/2).
Example 3.3
Suppose we wish to compute the circumference of a circle of diameter 5 units using the formula c = d . We could rst set the variable d to a value of 5:
d = 5 d = 5.000
Then we could compute the circumference and assign its value to the variable c:
c = pi*d c = 15.708
In this command, the product of the value of d (which is known because we earlier set it to 5) and the value of pi (which is a pre dened variable) is computed and the value of the product is stored in the variable c. Variable names must begin with an upper- or lower-case letter. They may contain letters, digits, and underscores; they may not contain spaces or punctuation characters. Variable names are case sensitive, so A and a are dierent variables.
(Solution on p. 18.)
a B ecky_ecky_ecky_ecky_ptang_zoo_boing ecky ecky ecky ecky ptang zoo boing 2nd John-Bigboote
There are several predened variables. The most commonly used include
Scalar - a scalar is a single value (i.e. a number). c and d in Example 3.3 are scalar variables. Vector - a vector is an ordered series of numbers. Matrices - a matrix is a rectangular array of numbers. The ability to do computations on vectors and matrices gives MATLAB its name (MATrix LABoratory). strings - variables may also contain strings of characters.
12
Exercise 3.2
BasicX Processor
The distance sensor uses a beam of infrared light to measure the distance from the sensor to an object; the sensor provides an output voltage that has a fairly complicated relationship to this distance. The BasicX processor converts the voltage from the sensor into a number between zero and one. Let us denote this number as x, and the distance (measured in inches) between the sensor and object as d. The relationship between x and d is
d=
34.63 x
5.162 2.54
Distance
Voltage
(3.1)
Exercise 3.3
The terminal velocity reached by a sky diver depends on many factors, including their weight, their body position as they fall, and the density of the air through which they fall. The terminal velocity is given by6 2mg Vt = (3.2) rACd where
13
r is the atmospheric density A is the sky diver's eective area Cd is the sky diver's coecient of drag
Compute the terminal velocity of the sky diver for each of the following values of m:
a scalar is a single value (i.e. a number). a vector is an ordered series of numbers. a matrix is a rectangular array of numbers.
The ability to do computations on vectors and matrices gives MATLAB its name (MATrix LABoratory).
note:
v = [9 7 5 3 1] v = 9 7 5 3 1
A second way to create a vector of values is with the sequence notation start:end or start:inc:end. For example, 1:10 creates the vector of integers from 1 to 10:
7 This
14
ans = 1 2 3 4 5 6 7 8 9 10
1:0.1:2 ans = 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000
10:-1:1 ans = 10 9 8 7 6 5 4 3 2 1
Vector elements are accessed using numbers in parentheses. For example if the vector v is dened as v = [9 7 5 3 1], the second element of v can be accessed as
v(2) ans = 7
The fourth element of v can be changed as follows:
12
10
Subtraction -
15
v-5 ans = 4 2 0 -2 -4
25
15
Division -
25
A = [1 2 3 4 5 6 7 8 9 10
8 http://www.mathworks.com/academia/student_center/tutorials/performing_calculations.html
16
A = 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 5 10 15 20
An entire row of A can be obtained by specifying a single ":" as the column index:
A(2,:) ans = 6 7 8 9 10
Similarly, an entire column of A can be obtained by specifying a single ":" as the row index:
A(:,3) ans = 3 8 13 18
9 This
17 Re(z ) Im(z )
|z |
Angle(z )
m-le
M*N inv(M) M' det(M)
Table 3.4
det(M )
The functions length and size are used to nd the dimensions of vectors and matrices, respectively. Operations can also be performed on each element of a vector or matrix by proceeding the operator by ".", e.g .*, .^ and ./.
Example 3.4
Let A =
1 1
1 1
2 2
2 2
12 12
12 12
1 1
1 1
Example 3.5
1 Given a vector x, compute a vector y having elements y (n) = sin(x (n)) . This can be easily be done the command y=1./sin(x) Note that using / in place of ./ would result in the (common) error "Matrix dimensions must agree".
18
Chapter 4
Graphing
4.1 Introduction to Graphing in M-File Environments1
One of the reasons that m-le environments are extensively used by engineers is their capability to provide graphical representations of data and computed values. In this module, we introduced the basics of graphing data in m-le environments through a series of examples. This module uses some fundamental operations on vectors that are explained in Vectors and Arrays in M-File Environments (Section 3.4).
Example 4.1
note:
The table below shows speed as a function of distance for a braking Dodge Viper decelerating from 70MPH to 0MPH. This data was not measured; it was computed using the stopping distance reported for a Dodge Viper2 and assuming constant deceleration. Thus, it may not accurately reect the braking characteristics of a real Dodge Viper.
19
20
CHAPTER 4. GRAPHING
The following commands will create a graph of velocity as a function of distance:
dist = [0 29.1 55.1 78.0 97.9 114.7 128.5 139.2 146.9 151.5 153.0] vel = [102.7 92.4 82.1 71.9 61.6 51.3 41.1 30.8 20.5 10.3 0.0] plot(dist,vel)
Figure 4.1 shows the graph created by these commands.
120
100
80
60
40
20
20
40
60
80
100
120
140
160
Figure 4.1:
This graph shows the data, but violates several important conventions of engineering practice. The axes are not labeled with quantity and units, and the graph does not have a title. The following commands, when executed after the plot command, will label the axes and place a title on the graph.
xlabel('Distance (ft)') ylabel('Velocity (ft/s)') title('Velocity vs Distance for the Dodge Viper')
The results of these commands are shown in Figure 4.2.
21
100
80
Velocity (ft/s)
60
40
20
20
40
60
120
140
160
Figure 4.2:
on the axes.
Graph of the Viper's velocity as a function of distance. The graph has a title and labels
After creating a gure, you may wish to insert it into a document. The method to do this depends on the m-le environment, the document editor and the operating system you are using. To paste a gure into a Word document on Windows, pull down the Edit menu of the window of the MATLAB gure you wish to paste, and select Copy Figure. Then go to the Word document into which you wish to insert the gure and use the paste command.
note:
Exercise 4.1
note:
Repeat Example 4.1 using the following data for a Hummer H2:
(Solution on p. 30.)
As in Example 4.1, this data was not measured; it was computed using the stopping distance reported for a Hummer H23 and assuming constant deceleration.
3 http://www.caranddriver.com/article.asp?section_id=33&article_id=2420&page_number=2
22
CHAPTER 4. GRAPHING
Example 4.2
An m-le environment can also be used to plot functions. For example, the following commands plot cos (x) over one period.
23
Figure 4.3:
Exercise 4.2
The module Exercises for Basic Mathematical Operations (Section 3.3) describes how to compute the terminal velocity of a falling sky diver. Plot the terminal velocity as a function of the sky diver's weight; use weights from 40kg to 500kg.
Exercise 4.3
In electrical circuit analysis, the equivalent resistance Req of the parallel combination of two resistors R1 and R2 is given by the equation
Req =
1 R1
1 +
1 R2
(4.1)
Exercise 4.4
Set R2 = 1000Ohms and plot Req for values of R1 from 100 Ohms to 3000 Ohms.
In an experiment, a small steel ball is dropped and videoed against a checkered background. The video sequence is analyzed to determine the height of the ball as a function of time to give the data in the following table:
24
CHAPTER 4. GRAPHING
This experimental data is to be compared to the theoretically expected values given by the following equation: 1 h = 22in gt2 (4.2) 2
in where h is in inches, t is in seconds, and g = 386.4 s 2 . Create a graph that compares the measured data with the theoretically expected values; your graph should conform to good conventions for engineering graphics. Plot the measured data using red circles, and plot the theoretically expected values using a blue line.
plot- The plot and stem functions can take a large number of arguments, see help plot and help stem. For example the line type and color can easily be changed. plot(y) plots the values in vector yversus their index. plot(x,y) plots the values in vector yversus x. The plot function produces a piecewise linear graph between its data values. With enough data points it looks continuous. stem- Using stem(y)the data sequence yis plotted as stems from the x-axis terminated with circles for the data values. stem is the natural way of plotting sequences. stem(x,y) plots the data sequence y at the values specied in x.
4 This
25
xlabel('string')- Labels the x-axis with string. ylabel('string')- Labels the y-axis with string. title('string')- Gives the plot the title string.
To illustrate this consider the following example.
Example 4.3
y = x.^2; figure(1); plot(x,y); xlabel('x'); ylabel('y=x^2'); title('Simple plot'); figure(2); stem(x,y); xlabel('x'); ylabel('y=x^2'); title('Simple stem plot'); This code produces the following two gures.
Figure 4.4
26
CHAPTER 4. GRAPHING
Figure 4.5
Some more commands that can be helpful when working with plots:
hold on / o - Normally hold is o. This means that the plot command replaces the current plot with the new one. To add a new plot to an existing graph use hold on. If you want to overwrite the current plot again, use hold off. legend('plot1','plot2',...,'plot N')- The legend command provides an easy way to identify individual plots when there are more than one per gure. A legend box will be added with strings matched to the plots. axis([xmin xmax ymin ymax])- Use the axis command to set the axis as you wish. Use axis on/off to toggle the axis on and o respectively. subplot(m,n,p) -Divides the gure window into m rows, n columns and selects the pp'th subplot as the current plot, e.g subplot(2,1,1) divides the gure in two and selects the upper part. subplot(2,1,2) selects the lower part. grid on/off - This command adds or removes a rectangular grid to your plot.
Example 4.4
This example illustrates hold, legend and axis. x = -3:0.1:3; y1 = -x.^2; y2 = x.^2;
figure(1); plot(x,y1); hold on; plot(x,y2,''); hold off; xlabel('x'); ylabel('y_1=-x^2 and y_2=x^2'); legend('y_1=-x^2','y_2=x^2');
27
figure(2); plot(x,y1); hold on; plot(x,y2,''); hold off; xlabel('x'); ylabel('y_1=-x^2 and y_2=x^2'); legend('y_1=-x^2','y_2=x^2'); axis([-1 1 -10 10]); The result is shown below.
(a)
Figure 4.6
(b)
Example 4.5
28
CHAPTER 4. GRAPHING
plot(x,y2); xlabel('x'); ylabel('y_2=x^2'); Now, the result is shown below.
Figure 4.7
4.2.4 3D Graphics
We end this module on graphics with a sneak peek into 3D plots. The new functions here are meshgrid and mesh. In the example below we see that meshgridproduces xand yvectors suitable for 3D plotting and that mesh(x,y,z) plots z as a function of both x and y.
Example 4.6
z = x.^2+y.^2; mesh(x,y,z);
5 http://www.mathworks.com/matlabcentral/leexchange/loadFile.do?objectId=727
29
Figure 4.8
30
CHAPTER 4. GRAPHING
100
80
Velocity (ft/s)
60
40
20
50
200
250
Figure 4.9:
Chapter 5
Both MATLAB and LABVIEW MathScript have built-in editors with features that make editing m-le scripts easier. In both envrionments, the editor is integrated with a debugger which makes nding and correcting errors in your scripts easier. More detailed information about using the MATLAB editor be found at Mathworks Matlab Tutorial-Creating Scripts with MATLAB Editor/Debugger2 . More detailed information about using the LABVIEW MathScript editor be found at National Instruments LabVIEW MathScript Tutorial-Inside LabVIEW MathScript Tutorial.3 .
M-le scripts interact with the current executing environment. Variables set before the script is executed can aect what happens in the script. Variables set in the script remain after the script has nished execution. Comments document your code and help other users (and yourself several months down the road) understand how you have implemented your program. Comments begin with the character %; any text in a line after the % is ignored by the script interpreter. To correctly execute a script, the script le environment must know the directory in which the script resides. To instruct the MATLAB environment where to search for the m-le script, you can set the current working directory or set the search path. More detailed information can be found at Mathworks Matlab Tutorial-Working with Files, Directories and Paths4 .
note:
31
32
note:
to repeat a series of computations using dierent values. The for loop has the
end
In the for loop, array can be any vector or array of values. The for loop works like this: d is set to the rst value in array, and the sequence of commands (Command 1, Command 2, and so on) in the body of the for loop is executed with this value of d. Then d is set to the second value in array, and the sequence of commands in the body of the for loop is executed with this value of d. This process continues through all of the values in array. So a for loop that performs computations for values of d from 1.0 to 2.0 is:
end
(Recall that 1.0:0.05:2.0 creates a vector of values from 1.0 to 2.0.) Note that in all of the examples in this module, the commands inside the for loop are indented relative to the for and end statements. This is not required, but is common practice and makes the code much more readable. The ow of control through a for loop is represented by the ow chart in Figure 5.1. This ow chart graphically shows how the sequence of commands in the for loop is executed once for each value. The ow of control through the for loop is also represented by the pseudo code in Figure 5.2; note that the pseudo code looks very similar to the actual m-le code.
5 http://zone.ni.com/reference/en-XX/help/371361A-01/lvdialog/mathscript_preferences_db/ 6 This content is available online at <http://cnx.org/content/m13258/1.5/>.
33
Set d to the next number in the vector Command 1 Command 2. ... Y More elements in vector? N
Figure 5.1:
A useful type of for loop is one that steps a counter variable from 1 to some upper value:
end
For example, this type of loop can be used to compute a sequence of values that are stored in the elements of a vector. An example of this type of loop is
% Store the results of this loop computation in the vector v for j = 1:10 % Commands % More Commands to compute a complicated result v(j) = result; end
34
Using a for loop to access and manipulate elements of a vector (as in this example) may be the most natural approach, particularly when one has previous experience with other programming languages such as C or Java. However, many problems can be solved without for loops by using the built-in vector capabilities. Using these capabilities almost always improves computational speed and reduces the size of the program. Some would also claim that it is more elegant. For loops can also contain other for loops. For example, the following code performs the commands for each combination of d and c:
end
(Solution on p. 54.)
How many times will this program print "Guten Tag Welt"?
(Solution on p. 54.)
(Solution on p. 54.)
end
7 This
35
(Solution on p. 54.)
(Solution on p. 54.)
count = 0; for d = 1:7 for h = 1:24 for m = 1:60 for s = 1:60 count = count + 1; end end end end count
What is a simpler way to achieve the same results?
y = cos (2f t)
(5.1)
Create an m-le script to plot the cosine waveform with frequency f = 2 cycles/s for values of t between 0 and 4.
Exercise 5.8
Suppose that we wish to plot (on the same graph) the cosine waveform in Exercise 5.7 for the following frequencies: 0.7, 1, 1.5, and 2. Modify your solution to Exercise 5.7 to use a for-loop to create this plot.
(Solutions on p. 55.)
Exercise 5.9
Suppose that you are building a mobile robot, and are designing the size of the wheels on the robot to achieve a given travel speed. Denote the radius of the wheel (in inches) as r, and the rotations per second of the wheel as w. The robot speed s (in inches/s) is related to r and w by the equation
s = 2rw
8 This
(5.2)
36
(Solution on p. 56.)
c a b
Figure 5.3:
Consider the right triangle shown in Figure 5.3. Suppose you wish to nd the length of the hypotenuse c of this triangle for several combinations of side lengths a and b; the specic combinations of a and b are given in Table 5.1: Side Lengths. Write an m-le to do this.
Side Lengths
a
1 1 2 4 2
b
1 2 3 1 2
Table 5.1
37
(5.3)
The lower bound is found by dividing the volume of the cylinder by the volume of the cube enclosing a single ball. Vcyl NL = (5.5) Vcube
(Solution on p. 56.)
(Solution on p. 57.)
To complicate your problem, suppose that you have not been given values for d, b, and h. Instead you are required to estimate the number of ping pong balls for many dierent possible combinations of these variables (perhaps 50 or more combinations). How can you automate this computation? One way to automate the computation of NL for many dierent combinations of parameter values is to use a for loop. (Read Programming with M-Files: For Loops (Section 5.2.1) if you are not familiar with the use of for loops.) The following problems ask you to develop several dierent ways that for loops can be used to automate these computations.
Add a for loop to your m-le from Problem 5.12 (Using an M-File) to compute NL for b = 8in, h = 14in, and values of d ranging from 1.0 in to 2.0 in.
(Solution on p. 57.)
Exercise 5.14 Can you still use a for loop? Exercise 5.15 More loops?
(Solution on p. 58.)
Modify your m-le from Problem 5.13 (Use a for loop) to and h = 14in.
Modify your m-le from Problem 5.13 (Use a for loop) to compute NL for d = 1.54in and various values of b and h.
5.2.5 Programming with M-Files: A Rocket Trajectory Analysis Using For Loops10
note:
This example requires an understanding of the relationships between position, velocity, and acceleration of an object moving in a straight line. The Wikipedia article Motion Graphs and
content is available online at <http://cnx.org/content/m13277/1.7/>.
10 This
38
CHAPTER 5. PROGRAMMING WITH M-FILE SCRIPTS Derivatives 11 has a clear explanation of these relationships, as well as a discussion of average and instantaneous velocity and acceleration and the role derivatives play in these relationships. Also, in this example, we will approximate derivatives with forward, backward, and central dierences; Lecture 4.1 by Dr. Dmitry Pelinovsky at McMaster University 12 contains useful information about this approximation. We will also approximate integrals using the trapezoidal rule; The Wikipedia article Trapezium rule 13 has an explanation of the trapezoidal rule.
39
50
100
150
Write a script that uses a for loop to compute velocity and acceleration from the altitude data using forward dierences. Your script should also plot the computed velocity and acceleration as function of time.
(Solution on p. 60.)
(Solution on p. 61.)
Modify your script from Problem 5.17 (Forward Dierences) to compute velocity and acceleration using backward dierences. Remember to save your modied script with a dierent name than your script from Problem 5.17 (Forward Dierences).
(Solution on p. 62.)
Modify your script from Problem 5.17 (Forward Dierences) to compute velocity and acceleration using central dierences. Remember to save your modied script with a dierent name than your script from Problem 5.17 (Forward Dierences) and Problem 5.18 (Backward Dierences). Compare the velocity and acceleration values computed by the dierent approximations. What can you say about their accuracy?
(Solution on p. 63.)
Modify your script from Problem 5.17 (Forward Dierences) to compute velocity and acceleration without using a for loop.
40
Acceleration.txt19 onto your computer (right click on this link20 ). The rst column is time in seconds, and the second column is acceleration in g's. The following commands load the data and plot the acceleration as a function of time.
10
10
Figure 5.5:
50
100
150
(Solution on p. 64.)
Write a script that uses a for loop to compute velocity and altitude from the acceleration data using the trapezoidal rule. Your script should also plot the computed velocity and altitude as function of time.
Modify your script from Problem 5.21 (Trapezoidal Rule) to compute velocity and altitude without using a for loop.
(Solution on p. 66.)
5.2.6 Programming with M-les: Analyzing Railgun Data Using For Loops21
This example requires an understanding of the relationships between acceleration and velocity of an object moving in a straight line. A clear discussion of this relationship can be found
note:
41 in Acceleration22 ; the Wikipedia article Motion Graphs and Derivatives 23 also has an explanation of this relationship, as well as a discussion of average and instantaneous velocity and acceleration and the role derivatives play. Also, in this example, we will compute approximate integrals using the trapezoidal rule; The Wikipedia article Trapezium rule 24 has an explanation of the trapezoidal rule.
Download the current data set in the le Current.txt26 onto your computer (right click on this link27 ). The le is formatted as two columns: the rst column is time in mili-seconds, and the second column is current in kA.
The following sequence of commands will load the data, create a vector t of time values, create a vector c of current values, and plot the current as a function of time.
load Current.txt -ascii t = Current(:,1); c = Current(:,2); plot(t,c) xlabel('time (msec)') ylabel('current (kA)')
The plot should be similar to that in Figure 5.6.
22 "Acceleration" <http://cnx.org/content/m13769/latest/> 23 http://en.wikipedia.org/wiki/Motion_graphs_and_derivatives 24 http://en.wikipedia.org/wiki/Trapezoidal_rule 25 http://en.wikipedia.org/wiki/Railgun 26 http://cnx.org/content/m14031/latest/Current.txt 27 http://cnx.org/content/m14031/latest/Current.txt
42
Figure 5.6:
Exercise 5.24
Compute the projectile velocity as a function of time. Note that velocity is the integral of acceleration.
5.3 Conditionals
5.3.1 Programming with M-Files: If Statements28
5.3.1.1 The If Statement The if statement is one way to make the sequence of computations executed by in an m-le script depend
on variable values. The if statement has several dierent forms. The simplest form is
43
if expression % Commands to execute if expression is true else % Commands to execute if expression is false end
For example, the following if statement will print "v is negative" if the variable v is negative and "v is not negative" if v is not negative:
if expression1 % Commands to execute elseif expression2 % Commands to execute elseif expression3 % Commands to execute ... else % Commands to execute end
if expression1 is true if expression2 is true if expression3 is true if all expressions are false
if v < 0 disp('v is negative') elseif v > 0 disp('v is positive') else disp('v is zero') end
Note that in all of the examples in this module, the commands inside the if statement are indented relative to the if, else, elseif, and end statements. This is not required, but is common practice and makes the code much more readable.
44
note:
Logical expression are typically formed using the following relational operators:
Relation
Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to
Table 5.2
==
=
note:
== is not the same as =; they are treated very dierently in m-le scripting environments. == compares two values, while = assigns a value to a variable.
Complex logical expressions can be created by combining simpler logical expressions using the following logical operators:
Relation
Not And Or
Table 5.3
&& ||
Exercise 5.26
30 This
(Solution on p. 66.)
45
Exercise 5.27
(Solution on p. 66.)
Exercise 5.28
Suppose the code in Problem 5.26 is modied by adding parentheses around a > 0. What will it print?
(Solution on p. 66.)
Exercise 5.29
(Solution on p. 66.)
Exercise 5.30
p1 = 3.14; p2 = 3.14159;
(Solution on p. 66.)
46
if p1 == p2 disp('p1 and p2 are equal') else disp('p1 and p2 are not equal') end
Exercise 5.31
(Solution on p. 66.)
a = 5; b = 10; if a = b disp('a and b are equal') else disp('a and b are not equal') end
Exercise 5.32
For what values of the variable a will the following MATLAB code print 'Hello world' ?
(Solution on p. 67.)
Exercise 5.33
For what values of the variable a will the following code print 'Hello world' ?
(Solution on p. 67.)
Exercise 5.34
For what values of the variable a will the following code print 'Hello world' ?
(Solution on p. 67.)
Exercise 5.35
For what values of the variable a will the following code print 'Hello world' ?
(Solution on p. 67.)
47
else end
disp('Goodbye world')
(Solution on p. 67.)
Exercise 5.36
Write an if statement that will print 'a is very close to zero' if the value of the variable a is between -0.01 and 0.01.
5.3.4 Programming with M-Files: An Engineering Cost Analysis Example Using If Statements31
5.3.4.1 An Engineering Cost Analysis Example
Suppose you are a design engineer for a company that manufactures consumer electronic devices and you are estimating the cost of producing a new product. The product has four components that are purchased from electronic parts suppliers and assembled in your factory. You have received cost information from your suppliers for each of the parts; as is typical in the electronics industry, the cost of a part depends on the number of parts you order from the supplier. Your assembly cost for each unit include the cost of labor and your assembly plant. You have estimated that these costs are C0=$45.00/unit. The cost of each part depends on the number of parts purchased; we will use the variable n to represent the number of parts, and the variables CA, CB, CC, and CD to represent the unit cost of each type of part. These cost are given in the following tables.
CA
$16.00 $14.00 $12.70 $11.00
CB
$24.64 $24.32 $24.07 $23.33
31 This
48
CD
$12.50 $10.42 $9.62
The unit cost is Cunit = C0 + CA + CB + CC + CD. To nd the unit cost to build one unit, we look in the above tables with a value of n=1; the unit cost is
$45.00+$16.00+$24.64+$17.98+$12.50 = $116.12
To nd the unit cost to build 20 units, we look in the above tables with a value of n=20 and get
$45.00+$14.00+$24.32+$17.98+$10.42 = $109.72
As expected, the unit cost for 20 units is lower than the unit cost for one unit. Create an if statement that will assign the proper cost to the variable CA based on the value of the variable n.
Exercise 5.37
(Solution on p. 67.)
Exercise 5.38
Create a script that will compute the total unit cost Cunit for a given value of the variable n.
Create a m-le script that will compute and plot the total unit cost as a function of n for values of n from 1 to 150. Suppose that you decide to re your workers, close down your plant, and have the assembly done oshore; in this arrangement, C0 = Cx + Cs, where Cx is the cost of oshore assembly and Cs is the cost of shipping assembled units from the assembler to your warehouse. After some investigation, you nd an oshore assembler that gives you the following assembly costs as a function of the number of units to assemble:
You also nd a shipping company that will ship the units from the assembler to your warehouse and whose freight charges are the following function of the number of units shipped :
Cs
$20.00 $18.00 $16.00 $15.00
Update the m-le script in Exercise 5.39 to account for the changes in cost due to oshoring.
while % % % end
expression Command 1 Command 2 More commands to execute repeatedly until expression is not true
where expression is a logical expression that is either true or false. (Information about logical expressions is available in Programming with M-Files: Logical Expressions (Section 5.3.2).) For example, consider the following while loop:
50
n = 1 n = 2 n = 3
Note that in all of this example, the commands inside the while loop are indented relative to the while and end statements. This is not required, but is common practice and makes the code much more readable.
Exercise 5.42
(Solution on p. 70.)
Exercise 5.43
a = 1 while a < 100 a = a*2 end
(Solution on p. 70.)
Exercise 5.44
33 This
(Solution on p. 71.)
51
5.4.3 Programming with M-Files: A Personal Finance Example Using While Loops34
5.4.3.1 A Personal Finance Example
A student decides to nance their college education using a credit card. They charge one semester's tuition and then make the minimum monthly payment until the credit card balance is zero. How many months will it take to pay o the semester's tuition? How much will the student have spent to pay o the tuition? We can solve this problem using an m-le script. We dene the following variables:
r=
APR 1+ 100
1 12
(5.8)
More information on how to compute monthly rates can be found here35 . Credit cards usually have a minimum monthly payment. The minimum monthly payment is usually a xed percentage of the balance; the percentage is required by federal regulations to be at least 1% higher than the monthly interest rate. If this minimum payment would be below a given threshold (usually $10 to $20), the minimum payment is instead set to the threshold. For a threshold of $10, the relationship between the balance and the minimum payment can be shown in an equation as follows:
(5.9)
To compute the balance for one month (month n + 1) from the balance for the previous month (month n), we compute the nance charge on the balance in the previous month and add it to the previous balance, then subtract the payment for the previous month:
bn+1 = bn + fn pn
(5.10)
52
In the following exercises, we will develop the program to compute the number of months necessary to pay the debt. We will assume that the card APR is 14.9% (the average rate on a student credit card36 in mid February 2006) and that the initial balance charged to the card is $2203 (the in-state tuition at Arizona State University at the Polytechnic Campus for Spring 2006 semester37 ).
Exercise 5.45
Exercise 5.46
Write code to compute the monthly interest rate r from the APR using (5.8). Write code to compute the minimum monthly payment pn using (5.9).
Write code to compute the balance at month n + 1 in terms of the balance at month n using (5.10). Place the code developed for Exercise 5.47 into a while loop to determine how many months will be required to pay o the card. Modify your code from Exercise 5.48 to plot the monthly balance, monthly payment, and total cost-to-date for each month until the card is paid o.
(Solution on p. 71.)
Exercise 5.49
(Solution on p. 71.)
36 http://money.cnn.com/pf/informa/index.html 37 http://www.asu.edu/sbs/FallUndergradEastWest.htm
53
Solution to Exercise 5.3 (p. 34) Solution to Exercise 5.4 (p. 34)
The code -1:1:-50 creates an empty vector with no elements. "Bonjour Monde" would be printed once for each element in the vector, but since the vector is empty, it is never printed. The outer loop (the loop with a) will be executed ve times. Each time the outer loop is executed, the inner loop (the loop with b) will be executed eleven times, since 0:0.1:1 creates a vector with 11 elements. "Hola Mundo" will be printed 55 times.
In the rst line, the value of n is set to 10. The code 1:n creates a vector of integers from 1 to 10. Each iteration through the loop sets j to the next element of this vector, so j will be sent to each value 1 through 10 in succession, and this sequence of values will be printed. Note that each time through the loop, the value of n is decreased by 1; the nal value of n will be 0. Even though the value of n is changed in the loop, the number of iterations through the loop is not aected, because the vector of integers is computed once before the loop is executed and does not depend on subsequent values of n. The d loop will be executed seven times. In each iteration of the d loop, the h loop will be executed 24 times. In each iteration of the h loop, the m loop will be executed 60 times. In each iteration of the m loop, the s loop will be executed 60 times. So the variable count will be incremented 7 24 60 60 = 604800 times. A simpler way to achieve the same results is the command
7*24*60*60
When this code is run, it plots all of the cosine waveforms using the same line style and color, as shown in Figure 5.7. The next solution shows one rather complicated way to change the line style and color.
54
0.5
0.5
1
Figure 5.7:
The following code changes the line style of each of the cosine plots.
fs = ['r-';'b.';'go';'y*']; %Create an array of line style strings x=1; %Initialize the counter variable x t=0:.01:4; hold on for f=[0.7 1 1.5 2] y=cos(2*pi*f*t); plot(t,y,fs(x,1:end)); %Plot t vs y with the line style string indexed by x x=x+1; %Increment x by one end xlabel('t'); ylabel('cos(2 pi f t)') title('plots of cos(t)') legend('f=0.7','f=1','f=1.5','f=2')
This code produces the plot in Figure 5.8. Note that this plot follows appropriate engineering graphics conventions-axes are labeled, there is a title, and there is a legend to identify each plot.
55
0.5 cos(2 pi f t)
0.5
2 t
Figure 5.8:
A solution that does not use a for loop was also created by Heidi:
56
h = 14 vcyl = h*pi*(b/2)^2 vcyl = 703.7168 vcube = d^3 vcube = 3.6523 nl = vcyl/vcube nl = 192.6796
% PingPong.m - computes a lower bound on the number of % ping pong balls that fit into a cylinder % Note that most lines end with a ";", so they don't print % intermediate results d = 1.54; h = 14; b = 8; vcyl = h*pi*(b/2)^2; vcube = d^3; nl = vcyl/vcube
When run from the command line, this program produces the following output:
PingPong nl = 192.6796
57
This solution is by Wade Stevens. Note that it uses the command hold on to plot each point individually in the for loop.
clear all hold on for d=1.0:0.1:2.0; b=8; h=14; C=h*pi*(b/2)^2; %volume of cylinder c=d^3; %volume of cube N=C/c; %Lower bound floor(N) plot (d,N,'g*') end
This solution creates the plot in Figure 5.9.
1.2
1.4
1.6
1.8
This dierent solution is by Christopher Embrey. It uses the index variable j to step through the dv array to compute elements of the nlv array; the complete nlv array is computed, and then plotted outside the for loop.
58
clear dv=1.0:.05:2.0; [junk,dvsize] = size(dv) for j=1:dvsize d=dv(j) b=8; %in h=14; %in vcyl=h*pi*(b/2)^2; vcube=d^3; nl=vcyl/vcube; nlv(j)=nl; end plot (dv,nlv)
This solution creates the plot in Figure 5.10.
1.2
1.4
1.6
1.8
And nally, this solution by Travis Venson uses vector computations to perform the computation without a for loop.
%creates a vector for diameter dv=1:.02:2; b=5.5; h=12; %computes volume of cylinder vcyl=h*pi*(b/2)^2; %computes volume of cube
59
This solution is by AJ Smith. The height, h, ranges from 12 to 15 and the base, b, ranges from 8 to 12.
for h=12:15; %ranges of height for b=8:12; %ranges of the base d=1.54; %diameter of ping pong ball. Vcyl=h*pi*(b/2)^2; %Volume of cylinder Vcube=d^3; %volume of a cube that encloses a single ball Nl=Vcyl/Vcube %lower bound on the number of balls that fit in the cylinder end end
load Altitude.txt -ascii t=Altitude(:,1); s=Altitude(:,2); for n=1:180; v=((s(n+1))-s(n))/((t(n+1))-t(n)) hold on plot(t(n),v,'o') end
The plot produced by this code is shown in Figure 5.11.
60
600
400
200
200
Figure 5.11:
50
100
150
Plot of velocity computed with the forward dierence method versus time.
load altitude.txt -ascii t=altitude(:,1); s=altitude(:,2); hold on for x=2:181 v(x)=(s(x)-s(x-1))/(t(x)-t(x-1)); plot(t(x),v(x),'b.') end
The plot produced by this code is shown in Figure 5.12.
61
800
600
400
200
200
Figure 5.12:
50
100
150
Plot of velocity computed with the backward dierence method versus time.
This code computes the velocity using the central dierence formula.
load Altitude.txt -ascii t=Altitude(:,1); s=Altitude(:,2); for n=2:180 v(n-1)=(s(n+1)-s(n-1))/(t(n+1)-t(n-1)); end plot(t(2:180),v)
The plot produced by this code is shown in Figure 5.13.
62
50
100
150
Plot of velocity computed with the central dierence method versus time.
This code uses the diff function to compute the dierence between adjacient elements of t and s, and the ./ function to divide each element of the altitude dierences with the corresponding element of the time dierences:
63
800
600
400
200
200
50
100
150
Figure 5.14: Plot of velocity computed with the forward dierence method versus time. The values in this plot are the same as in Figure 5.11.
load Acceleration.txt -ascii t=Acceleration (:,1); a=Acceleration (:,2); v(1)=0; for n=1:181 v(n+1)=(t(n+1)-t(n))*(a(n+1)+a(n))/2+v(n); end plot(t,v)
This code creates the plot in Figure 5.15.
64
50
100
150
Plot of velocity versus time. The velocity is computed by numerically integrating the measured acceleration.
This code can be easily extended to also compute altitude while it is computing velocity:
load Acceleration.txt -ascii t=Acceleration (:,1); a=Acceleration (:,2); v(1)=0; % Initial velocity s(1)=0; % Initial altitude for n=1:181 v(n+1)=(t(n+1)-t(n))*(a(n+1)+a(n))/2+v(n); s(n+1)=(t(n+1)-t(n))*(v(n+1)+v(n))/2+s(n); end plot(t,s)
This code creates the plot in Figure 5.16.
65
50
100
150
This solution by Nicholas Gruman uses the cumtrapz function to compute velocity with the trapezoidal rule:
s=cumtrapz(t,v);
Solution to Exercise 5.25 (p. 44) Solution to Exercise 5.26 (p. 44) Solution to Exercise 5.27 (p. 45)
' a is positive' ' a is not equal to 0'
Solution to Exercise 5.28 (p. 45) Solution to Exercise 5.29 (p. 45) Solution to Exercise 5.30 (p. 45)
' p1 and p2 are not equal'
b*c gives a value of 6, and 5 < 6, so this code will print 'Hello world'.
The parentheses around the relational expression a > 0 will not change its validity, so this code will print 'a is positive'. The parentheses in this expression change its meaning completely. First, a < b is evaluated, and since it is false for the given values of a and b, it evaluates to zero. The zero is than multiplied by c, giving a value of zero which is interpreted as false. So this code prints 'Goodbye world'.
66
This code will generate an error message, since a = b assigns the value of b to a. To check if a and b are equal, use a == b.
Solution to Exercise 5.32 (p. 46) Solution to Exercise 5.33 (p. 46) Solution to Exercise 5.34 (p. 46) Solution to Exercise 5.35 (p. 46)
Any value that is not zero.
Any value greater than or equal to 0 and less than 7. Any value less than 3 or greater than 10. Every value of a will print 'Hello world'.
if a >= -0.01 && a <= 0.01 disp('a is very close to zero') end
= 11.00;
if n>=1 && n<=4; %if n=1 to 4, CA is $16.00 CA=16.00; elseif n>=5 && n<=24; %if n=5 to 24, CA is $14.00 CA=14.00; elseif n>=25 && n<=99; %if n=25 to 99, CA is $12.70 CA=12.70; elseif n>=100; %if n=100 or more, CA is $11.00 CA=11.00; end %this ends the if statement for CA if n>=1 && n<=9; %if n=1 to 9, CB is $24.64 CB=24.64; elseif n>=10 && n<=49; %if n=10 to 49, CB is $24.32 CB=24.32; elseif n>=50 && n<=99; %if n=50 to 99, CB is $24.07 CB=24.07; elseif n>=100; %if n=100 or more, CB is $23.33
67
CB=23.33; end %this ends the if statement for CB if n>=1 && n<=24; %if n=1 to 24, CC is $17.98 CC=17.98; elseif n>=25 && n<=49; %if n=25 to 49, CC is $16.78 CC=16.78; elseif n>=50; %if n=50 or more, CC is $15.78 CC=15.78; end %this ends the if statement for CC if n>=1 && n<=9; %if n=1 to 9, CD is $12.50 CD=12.50; elseif n>=10 && n<=99; %if n=10 to 99, CD is $10.42 CD=10.42; elseif n>=100; %if n=100 or more, CD is $9.62 CD=9.62; end %this ends the if statement CO=45.00; Cunit=CO + CA + CB + CC + CD;
This code was originally written by Bryson Hinton and then modied:
68
%compute price for part D if n >= 1 && n <= 9 cd=12.50; elseif n >= 10 && n <= 99 cd=10.42; else cd=9.62; end %sum cost for all parts cunit(n)= c0+ca+cb+cc+cd;
end
% Plot cost as a function of n plot(1:150,cunit); xlabel('n (units)'); ylabel('cost (dollars)'); title('Cost/unit as a function of number of units');
This code produces the plot in Figure 5.17.
69
116
114
cost (dollars)
112
110
108
106
104
50 n (units)
100
150
Figure 5.17:
Solution to Exercise 5.41 (p. 51) Solution to Exercise 5.42 (p. 51) Solution to Exercise 5.43 (p. 51)
a = 1 2 4 8 16 32
10 times. This loop will continue to print 'Hello World' until the user stops the program. You can stop a program by holding down the 'Ctrl' key and simultaneously pressing the 'c' key.
a = a = a = a = a = a =
70
64 a = 128
1 2 6
24 a = 120
Solution to Exercise 5.48 (p. 53) Solution to Exercise 5.49 (p. 53)
This space intentionally left blank. This space intentionally left blank.
Bibliography
[1] Anthony M. Stareld; Karl A. Smith; Andrew L. Bleloch. How Computer Age. Interaction Book Company, Edina, MN, 1994.
To Model It: Problem Solving for the
[2] Alexander E. Zielinski; Paul Weinacht. Eect of railgun electrodynamics on projectile launch dynamics. IEEE Transactions on Magnetics, 35(1):118123, 1999.
71
72
INDEX
A C E
Array, 3.4(13) Complex operations, 3.5(16) Credit Card Debt, 5.4.3(51) Engineering cost analysis, 5.3.4(47) Engineering data analysis, 5.2.5(37) Engineering modeling, 5.2.4(36), 5.2.6(40) Exercise, 2.2(4), 5.2.3(35) Exercises, 3.3(12), 5.2.2(34), 5.3.3(44), 5.4.2(51) Flow Chart, 2.3(4) for loop, 5.2.1(32), 5.2.2(34), 5.2.3(35), 5.2.4(36), 5.2.5(37), 5.2.6(40) Graphing, 4.1(19) Help resources, 1.2(1) if statement, 5.3.1(42), 5.3.3(44), 5.3.4(47) Introduction, 1.1(1) LabVIEW MathScript, 1.1(1), 1.2(1), 2.1(3), 3.1(9), 3.2(10), 3.5(16), 5.1(31) logical expression, 5.3.2(43) logical operator, 5.3.2(43) 5.2.2(34), 5.2.3(35), 5.2.4(36), 5.3.1(42), 5.3.2(43), 5.3.3(44), 5.3.4(47), 5.4.1(50), 5.4.2(51), 5.4.3(51) M-les, 2.3(4), 5.1(31) mathematical operations, 3.1(9), 3.3(12) MathScript, 2.2(4), 2.3(4), 3.3(12),
F G H I L
3.4(13), 4.1(19), 5.2.1(32), 5.2.2(34), 5.2.3(35), 5.2.4(36), 5.2.5(37), 5.2.6(40), 5.3.1(42), 5.3.2(43), 5.3.3(44), 5.3.4(47), 5.4.1(50), 5.4.2(51), 5.4.3(51) MATLAB, 1.1(1), 1.2(1), 2.1(3), 2.2(4), 2.3(4), 3.1(9), 3.2(10), 3.3(12), 3.4(13), 3.5(16), 4.1(19), 5.1(31), 5.2.1(32), 5.2.2(34), 5.2.3(35), 5.2.4(36), 5.2.5(37), 5.2.6(40), 5.3.1(42), 5.3.2(43), 5.3.3(44), 5.3.4(47), 5.4.1(50), 5.4.2(51), 5.4.3(51) Matrix operations, 3.5(16)
Octave, 1.1(1), 1.2(1), 2.1(3), 2.2(4), 2.3(4), 3.1(9), 3.2(10), 3.3(12), 3.4(13), 3.5(16), 4.1(19), 5.1(31), 5.2.1(32), 5.2.2(34), 5.2.3(35), 5.2.4(36), 5.2.5(37), 5.2.6(40), 5.3.1(42), 5.3.2(43), 5.3.3(44), 5.3.4(47), 5.4.1(50), 5.4.2(51), 5.4.3(51) Personal Finance, 5.4.3(51) problem solving, 2.1(3), 2.2(4) Programming, 5.1(31) Pseudocode, 2.3(4) relational operator, 5.3.2(43) Scripts, 5.1(31) Variables, 3.2(10) Vector, 3.4(13) While Loops, 5.4.1(50)
P R S V
ATTRIBUTIONS
73
Attributions
Collection: Freshman Engineering Problem Solving with MATLAB Edited by: Darryl Morrell URL: http://cnx.org/content/col10325/1.18/ License: http://creativecommons.org/licenses/by/2.0/ Module: "Introduction to the M-le Connexions Modules" By: Darryl Morrell URL: http://cnx.org/content/m13749/1.1/ Page: 1 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Finding Help for M-le Environments" By: Darryl Morrell URL: http://cnx.org/content/m13750/1.3/ Pages: 1-2 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Problem Solving Using M-le Environments" By: Darryl Morrell URL: http://cnx.org/content/m13694/1.4/ Pages: 3-4 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Exercise for Problem Solving with M-Files" By: Darryl Morrell URL: http://cnx.org/content/m13865/1.1/ Page: 4 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Visual Tools for Problem Solving with M-Files" By: Darryl Morrell URL: http://cnx.org/content/m13820/1.3/ Pages: 4-7 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Basic Mathematical Operations" By: Anders Gjendemsj, Darryl Morrell URL: http://cnx.org/content/m13439/1.7/ Pages: 9-10 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Based on: Using MATLAB By: Anders Gjendemsj URL: http://cnx.org/content/m13254/1.1/
74 Module: "Variables in M-le Environments" By: Darryl Morrell URL: http://cnx.org/content/m13354/1.3/ Pages: 10-11 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Exercises for Basic Mathematical Operations" By: Darryl Morrell URL: http://cnx.org/content/m13832/1.4/ Pages: 12-13 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Vectors and Arrays in M-File Environments" By: Darryl Morrell URL: http://cnx.org/content/m13355/1.2/ Pages: 13-16 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Basic Complex and Matrix Operations" By: Darryl Morrell URL: http://cnx.org/content/m13751/1.2/ Pages: 16-17 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Based on: Basic operations in MATLAB By: Anders Gjendemsj, Darryl Morrell URL: http://cnx.org/content/m13439/1.3/ Module: "Introduction to Graphing in M-File Environments" By: Darryl Morrell URL: http://cnx.org/content/m13836/1.6/ Pages: 19-24 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Graphical representation of data in MATLAB" By: Anders Gjendemsj URL: http://cnx.org/content/m13252/1.1/ Pages: 24-29 Copyright: Anders Gjendemsj License: http://creativecommons.org/licenses/by/2.0/ Module: "A Very Brief Introduction to Programming with M-Files" By: Darryl Morrell URL: http://cnx.org/content/m13259/1.6/ Pages: 31-32 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/
ATTRIBUTIONS
ATTRIBUTIONS
Module: "Programming with M-les: For Loops" By: Darryl Morrell URL: http://cnx.org/content/m13258/1.5/ Pages: 32-34 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-les: For-Loop Drill Exercises" By: Darryl Morrell URL: http://cnx.org/content/m13276/1.6/ Pages: 34-35 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming With M-Files: For-Loop Exercises" By: Darryl Morrell URL: http://cnx.org/content/m14013/1.4/ Pages: 35-36 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-les: A Modeling Example Using For Loops" By: Darryl Morrell URL: http://cnx.org/content/m13275/1.4/ Pages: 36-37 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-Files: A Rocket Trajectory Analysis Using For Loops" By: Darryl Morrell URL: http://cnx.org/content/m13277/1.7/ Pages: 37-40 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-les: Analyzing Railgun Data Using For Loops" By: Darryl Morrell URL: http://cnx.org/content/m14031/1.3/ Pages: 40-42 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-Files: If Statements" By: Darryl Morrell URL: http://cnx.org/content/m13356/1.3/ Pages: 42-43 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-Files: Logical Expressions" By: Darryl Morrell URL: http://cnx.org/content/m13357/1.5/ Pages: 43-44 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/
75
76 Module: "Programming with M-Files: If-Statement Drill Exercises" By: Darryl Morrell URL: http://cnx.org/content/m13432/1.4/ Pages: 44-47 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/
ATTRIBUTIONS
Module: "Programming with M-Files: An Engineering Cost Analysis Example Using If Statements" By: Darryl Morrell URL: http://cnx.org/content/m13433/1.6/ Pages: 47-50 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-Files: While Loops" By: Darryl Morrell URL: http://cnx.org/content/m13456/1.2/ Pages: 50-51 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-Files: While-Loop Drill Exercises" By: Darryl Morrell URL: http://cnx.org/content/m13457/1.4/ Page: 51 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/ Module: "Programming with M-Files: A Personal Finance Example Using While Loops" By: Darryl Morrell URL: http://cnx.org/content/m13461/1.4/ Pages: 51-53 Copyright: Darryl Morrell License: http://creativecommons.org/licenses/by/2.0/
The objective of this course is that students can use an m-le scripting environment (MATLAB, MathScript, Octave, etc.) to solve freshman engineering problems. This course is designed for the novice programmer.
About Connexions
Since 1999, Connexions has been pioneering a global system where anyone can create course materials and make them fully accessible and easily reusable free of charge. We are a Web-based authoring, teaching and learning environment open to anyone interested in education, including students, teachers, professors and lifelong learners. We connect ideas and facilitate educational communities. Connexions's modular, interactive courses are in use worldwide by universities, community colleges, K-12 schools, distance learners, and lifelong learners. Connexions materials are in many languages, including English, Spanish, Chinese, Japanese, Italian, Vietnamese, French, Portuguese, and Thai. Connexions is part of an exciting new information distribution system that allows for Print on Demand Books. Connexions has partnered with innovative on-demand publisher QOOP to accelerate the delivery of printed course materials and textbooks into classrooms worldwide at lower prices than traditional academic publishers.