matlab programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/classes/met164/... · met 164...
TRANSCRIPT
1/24MET 164
Matlab Programming
2/24MET 164
What does MATLAB mean?
Contraction of Matrix Laboratory
• Matrices are rectangular arrays of
numerical values
7362
1944
8415
7213
3/24MET 164
What are the fundamental components of Matlab?
Computing system that accepts one instruction at a time in
text form
Large library of modules that provide high-level capabilities for
processing data
Graphical user interface (GUI) that lets users assemble and
implement programs to solve a problem
4/24MET 164
What are the advantages and the disadvantages?
Advantages
interpreted language
numerical calculator
built in graphics
Disadvantages
does not work well for large computing projects (many
developers)
professional GUIs are best written in compiled language
5/24MET 164
Programming Language Background
Abstraction
“expressing a quality apart from a particular
implementation”
Algorithm
“sequence of instructions for solving a sub-program”
Programming Paradigms
“a set of assumptions, concepts, values, and practices that
constitutes a way of viewing reality for the community that
shares them, especially in an intellectual discipline”
6/24MET 164
What does “expressing a quality apart from a particular
implementation” (abstraction) mean?
It is things that we use in everyday conversation
“He used his bicycle to ride home from campus”
Procedural abstraction: the details will be different based on
the user to instruct the ride home.
“To convert from lb to N, multiply the force by 4.448”
Data abstraction: details about the force will determine how
you do the conversion.
7/24MET 164
What does “sequence of instructions for solving a sub-
program” (algorithm) mean?
Instructions that we use to problem solve
Problem: The car needs to be fixed
1) Change the oil
2) Drain the old oil, remove the old oil filter, install a new
oil filter, fill the old pan with new oil
3) Place the vehicle on ramps, place a container under the
oil pan to catch the old oil, etc.
The algorithm is full of abstractions that are based on what is computing the algorithm
8/24MET 164
Programming Paradigms
You will have a better understanding of programming
paradigms after you have learned basic programming
9/24MET 164
Now, let’s start talking about “basic” data manipulation
within Matlab
z = 4x - y
In algebra, this is a two way relationship
In algebra this represents a constraint on the values, but in
programming, this is an assignment statement
z = x + y
In programming, this is the sum of two values associated to a
new name; it can be changed with the next statement
x + y = z
10/24MET 164
When we start to define data to variables in Matlab, we
need to use appropriate syntax
Variables
upper or lowercase letters, numbers, and “_”
not “$” or 64 unique characters
Variables may be words
no spaces
use underscore or caps
velocity_feet_per_second
VelocityFeetPerSecond
11/24MET 164
Matlab is an untyped language
Variables are given a specific “type” when assigned
Numeric (double)
x = 7
Character (char)
x = ‘I like programming’
Issues
misspelled variables may cause errors (without notice)
logic errors with incompatible data
Typed languages (weak or strong) allow the programmer to
assign types and interactions before running or compilation
12/24MET 164
Now, let’s look at Matlab and talk about some of the
things we see
Command Window
Current FolderWorkspace Tab
Command History
This layout may be adjusted to the user preference
13/24MET 164
One way to adjust the layout is to use the layout feature
on the top window
14/24MET 164
Matlab can be used in two modes: Command mode or
Edit mode
Command mode
appears as a scientific calculator
Edit mode
able to type in numerous commands before executing
15/24MET 164
Matlab can be used in two modes: Command mode or
Edit mode
Command mode
appears as a scientific calculator
Edit mode
able to type in numerous commands before executing (use
what are called scripts)
16/24MET 164
The workspace provides a list of variables always keeps
the variables in alphabetical order
The “clear” command allows you to clear which ever
workspace variable you want. Example “clear AA BB” to
clear variable AA and BB or “clear all” to clear all of the
variables (note: the quotes are not used)
“ans” is a special
variable
17/24MET 164
The workspace always keeps the variables in
alphabetical order
Double clicking on a variable in the workspace will allow you
to see the variable and change its value
You may also paste the data here from another source
18/24MET 164
The Command History pane will allow you to fix
mistakes that you have made
The “clc” command allows you to clear all of the stuff that is
in the command window, but leaves all of the history and
workspace variables to use later
“whos” will allow you to see all of the variable information in
the command window
19/24MET 164
The output will have specific formats in the Command
Window
A semicolon will suppress the presentation of the result
20/24MET 164
Matlab has built in figure windows that allows you to plot
data
close, figure
21/24MET 164
Matlab has a built in text editor that allows you save a
script for running later
22/24MET 164
Matlab has a built in text editor that allows you save a
script for running later
The result will show in the Command Window
23/24MET 164
Additional syntax
;
…
close
clear
clc
^
plot
who
whos
%
24/24MET 164
Use the text editor to write your scripts and start using
comments
Start using the text editor
Start using comments
Try different commands and see what happens