Anda di halaman 1dari 12

CMPSC 200 Programming for

Engineers with MatLab


Lecture 3:
C 015 Olmsted Building
Penn State Harrisburg
Dr. Adams

Chapter 1.3: Engineering


Problem Solving Methodology
There are commonly five steps that are
followed in problem solving especially
when
it involves engineering
State
Prototype or
Describe
the
solve the
Inputs and
Proble
problem BY HAND
Outputs
m
Develop the
computer
solution

Test Test
and Retest the
solution

Chapter 2: Getting Started with


MatLab
Matlab is an interpreted language.
A commonality of all interpreted
languages is that they require an
interpreter.
In the case
interpreter
application
both single
programs.

of Matlab, the
is a proprietary
that enables us to run
commands and entire

Chapter 2: Getting Started with


MatLab
When we run Matlab, the application has
several windows.
Main window is the command window. It is
here that we run Matlab commands and
programs.
Directory and Path window. This shows the
current directory within which our
program is stored on the drive.
Workspace window shows all of the
variables that are currently active.
History window is a scrollable list of
all commands that have been run.

Chapter 2: Getting Started with


MatLab
We have discussed the difference between
compiled and interpreted programming
languages. An interesting characteristic of
many interpreted languages is that they can
be used interactively.
What is interactive?
Interactive means that you can enter a
single statement into a command window and
run that line immediately.
The proprietary IDE that is Matlab comes
with a command window.

Chapter 2: Getting Started with


MatLab
The Matlab command line is indicated by
the double insertion prompt >>
If you type a command then the command
will be interpreted, and if correct, run
Example:
>> 3 + 2
ans =
5

You can edit the current


command by using the left
arrow (<-) and the right
arrow (->)

Matlab performed the command return the


answer

Chapter 2: Getting Started with


MatLab
While it appears that Matlab simply added the
two values together, it actually did quite a
bit more.
1.It parsed the entire line recognizing it as
an addition action of two values.
2.Recognized the two values as integers (as
compared to floating point values or
characters.)
3.Created a memory location that is large
enough to store an integer.
4.Performed the addition storing the result
in the newly instantiated memory location.
5.Printed the variable to the monitor

Chapter 2: Getting Started with


MatLab
The fact that a memory location was formed
for the result means that answer is still
available after the fact. But to access
this stored value we need to know the
address of the memory location.
Since it would impossible to track all the
addresses, we instead give the memory
address a name called a variable
identifier or more simply, a variable.
>> ans
ans =
5

In this case the variable


is called ans. If want to
use the value we simple
use ans.

Chapter 2: Getting Started with


MatLab
We can use the ans variable in further
calculations Warning: Since ans is the
default variable, any
additional calculations will
overwrite the memory location
and thus the variable ans.
We are not limited to the single
variable. We can create our own variables
and thus additional memory storage
locations.
Storing a value in a variable
is called an assignment. All
>> cost = 4
variable assignments must be
cost =
made from right to left. The
4
= character is called the
>> ans * 4
ans =
20

Chapter 2: Getting Started with


MatLab
We can also use the variable in an
assignment operation
Matlab follows
order of operation
that we first
learned in
mathematics
P: Parentheses
E: Exponents
M: Multiplication
D: Division
A: Addition
S: Subtraction

>>
>> a = 4 * (2 + 5)
a =
28
>>
>> b = 2^5 * 3
b =
96
>>

Chapter 2: Getting Started with


MatLab
We can also use the variable in an assignment
operation
This operation changes the
>> total = cost*1.06
memory location because the
total =
result is no longer an
4.24
integer, but is now a float
Variables have data types depending upon the
using more space
type of value that they store.
Integers: Numerical values with no fractional
part
Floating Point: Numerical values with a
fraction
Characters: not calculable values such as
letters and symbols.

Chapter 2: Getting Started with


MatLab
Can anything be used as a variable
identifier?

There are very


strict criteria
for what may or
may not be a
variable
identifier.

Note: Variable
names should be
descriptive. Use
milesPerGallon
instead of x17.

1. Must start with any


alphabetic letter
such as a, b, c, ...
2. Names are case
sensitive so
myVariable is
different from
Myvariable.
3. After the first
letter, may use any
alphanumeric
character or the
underscore.