Lab04_Intro_to_MATLAB

advertisement
Lab 04 – Introduction to MATLAB
Task 1
Lets begin by familiarizing ourselves with MATLAB.
1) Open MATLAB. In the computer labs, this is found in the start menu. If it isn’t there,
you can access it through ERNIE for now. As we get later in the semester, there are
times ERNIE’s version of MATLAB won’t be able to do what we are talking about and
you will have to use a computer lab that has MATLAB in it.
2) Familiarize yourself with the various areas of the MATLAB program. By default,
MATLAB has 4 windows (as discussed in the videos).
a. Current Folder (on the left, listing all the files)
b. Command Window (in the center, taking up the most space)
c. Workspace (blank when you start, but will be filled with variables as we
create them)
d. Command History (a list of previously entered commands)
3) In the command window, type x = 3
a. You’ve defined a variable named “x” and given it a value of 3.
b. Notice that x appears in the workspace with a value of 3.
4) In the command window, type y = x + 2
a. You’ve now created a second variable y and given it a value equal to the
current value of x plus the constant value 2. MATLAB should print out y =
5.
5) In the command window, type help clear
a. Notice that MATLAB gives you help information on the clear command.
6) Type clear
a. Notice that x and y disappeared from the workspace window. The clear
command tells MATLAB to delete all the variables that are defined.
7) Type y = x + 2 (or press the up arrow until y = x + 2 reappears)
a. Notice you now get an error message telling you that you are trying to use an
undefined function or variable ‘x’. Because you’ve cleared
your variables, MATLAB doesn’t have a value for x and therefore doesn’t
know what to do when you ask for x+2.
8) Type help clc
a. clc, unlike clear, has no effect on the workspace and variables. clc clears
the screen.
9) Type clc to clear the screen.
Lab 04 – Introduction to MATLAB
Task 2
As we start learning MATLAB, we will start writing longer and more complex pieces of
code. We will collect these pieces of code in what MATLAB refers to as scripts. While you
can run your code in the command window, it’s not very practical when your code becomes
more than 1 or 2 lines long.
At the top of each script, it is standard practice to have a header that describes what the
code does, who wrote it, and any other important information about the program. For this
task, you will be creating a template that you can use for all future programs.
1) With MATLAB open, in the “Current Folder” bar at the top center of the screen,
navigate to your P drive (or thumb drive, or wherever you want to save your files). If
you’ve created an “EGR115” folder, go there. (If you don’t have an EGR115 folder,
you are encouraged to make one in the Windows Explorer.) You can also get there
using the Current Folder window on the left side of the screen. This window will
also let you create new files and folders by click on the gear icon.
2) In the Command Window, type edit template
a. The edit command tells MATLAB you want to edit a MATLAB script. This
brings up the MATLAB editor as a new window.
3) MATLAB comments always begin with a percent sign (%). Anything that comes after
the percent sign is considered a comment until the end of the line.
4) In the editor window, create a template that you will use for all future MATLAB
scripts.
5) In comments at the top of the file, create a template that looks like the following (but
with the information for you and your section. Yes – include the comments too):
% Matthew Verleger
% Email: mverleger@gmail.com
% Date: January 1, 2014
% EGR 115 – Section 2
% Level 0 – Problem 2
% Program Description: This is a generic template file
% Worked with: I worked with the following people to complete
%
this task. (If you get help from anyone other than Dr. V, list
%
that person here, including the tutors or other professors – if
%
you don’t know their name, ask them.)
clear
% Always start with a clean slate of variables
clc
% Always clear the screen before you start
close all
% Close any open graphs/figures
commandwindow
%Make the command Window the active location
6) For every program you write, start by opening your template file and saving it with
an appropriate name for the new program.
7) Save this file as template.m somewhere where you will be able to access it for
every program you write.
Lab 04 – Introduction to MATLAB
Task 3
A function is a piece of code that has been written for doing a very specific task. MATLAB has approximately 1800 built-in
functions. You’ll only use about 100 of them in this class. You’ve already seen some examples in lecture, such as the clear and
clc commands. These are functions that take no inputs and have no outputs. We’ll learn more about functions (and how to
write your own functions) in the coming weeks, but for now, let’s familiarize ourselves with the most commonly used ones.
Using the help documents (either with the help and lookfor commands, the doc command, or by selecting the “Product Help”
from the “Help” menu), fill in the following table. The first 3 have already been done for you.
Command Does what
Example of its usage Result of example
clc
Clears the screen
clc
<Screen clears>
clear
Deletes a variable (or all variables)
clear
<Variables Deleted>
sin
Calculates the sine of a number in radians
sin(1.3)
ans =
0.9636
Calculates the cosine of a number in radians
Calculates the tangent of a number in radians
Calculates the sine of a number in degrees
Calculates the inverse sine of a number in radians
Calculates the inverse sine of a number in degrees
Provides MATLAB’s built-in value for 𝜋
Takes the absolute value of a number
Raises a number to a power (example: 47)
Takes the square root of a number
Calculates the exponential of a number (example: e1.3)
Takes the natural log of a number (example: ln 1.3)
Rounds a number toward the nearest integer
Rounds a number toward positive infinity
Rounds a number toward negative infinity
Gives the remainder after division (referred to as the “modulus”)
Lab 04 – Introduction to MATLAB
Task 4
Using the template you created in task 2 as a starting point, write a script to compute x
using the formula for the displacement of an under-damped vibration response (formula
given below) using the values listed on the next page. Vibrations are a problem common to
almost all fields of engineering; you are likely to become very familiar with formulas such
as these in your engineering classes.
 (v   x ) 2  ( x  ) 2
0
n 0
0 d
x

d


e ( nt ) sin(  t   )
n


Where:
x = Displacement as a function of time in meters
vo = Initial velocity in meters/sec
xo = Initial displacement in meters
ωn (omega_n) = Natural frequency in radians/sec
ωd (omega_d) = Damped frequency in radians/sec
ζ (zeta) = Damping ratio (has no units associated with it, which in engineering
terminology is referred to as “dimensionless”)
t = Time in seconds
φ (phi)= Phase shift in radians
For this problem, start with the following values:
vo = 15 m/s
xo = 5 cm
ωn = 90 rad/s
ωd = 87 rad/s
ζ = 0.25 (unitless)
t = 0.036 seconds
φ = 0.2635 radians
To complete this task:
1) Re-save template.m as lab04task4.m before you do any editing.
2) Be sure to adjust the initial comment lines for this problem.
3) Create comments at the top of the file listing the following
o
o
o
o
State the Problem
Identify the Inputs
Identify the Outputs
Manual Solution
Lab 04 – Introduction to MATLAB
You are encouraged to split the formula into multiple terms and
manually solve each of those sub-terms before solving the entire
problem.
4) Assign all values in the formula to variable names before doing the overall
calculation. Be sure to comment each variable assignment with a clear description of
the variable and its unit.

Example:
init_vel = 15 % Initial velocity in meters/sec
5) Use your variable names in the overall calculation. This allows any of the variables
to change for a different system without requiring you to re-code the entire solution.
6) Pay attention to units. The formula as given to compute x is only defined for the
variables with the units as stated. This is always the case for engineering formulas.
7) Save and run your script often. It is a good idea to run a script after adding or
editing no more than 5 lines of code. There are at least four ways to run a script:
o In the Command Window, type the name of the file.
o In the MATLAB window's Current Directory window, right click on the filename
and select run from the menu.
o From the Editor, select the 'Save and Run' option from the Debug menu.
o From the Editor, click on the taskbar button that looks like a play button next to
a page.
IMPORTANT! All run options require that the Current Directory be set to the
location of the file you are trying to run.
What to submit
 template.m from Task 2 containing your information
 A Word document containing only the completed table of commands from Task 3
(copy and paste into a new file or delete Tasks 1, 2, and 4 from around this file)
 Lab4_Task4.m containing your algorithm for solving the lake area problem in
comments.
Download