Matlab Introduction - University of Massachusetts Lowell

Download Report

Transcript Matlab Introduction - University of Massachusetts Lowell

Introduction to MATLAB
Barbara Deschamp
NSF GK-12 Fellow
Vibes and Waves in Action
Center for Advanced Computation and Telecommunications
University of Massachusetts Lowell
What is MATLAB?
MATLAB is a software product.
Useful for
– 2D-3D plotting
– Computation/calculation
– Prototyping
(prototype: an experimental model)
– Creating models and simulations
– Algorithms
(algorithm: step-by-step procedure to solve a problem)
Makes it easier when calculations are on big
sets of numbers.
Launch Matlab
– MATLAB/R2009a
MATLAB Desktop
Command Window: a big calculator
Current
Directory
Workspace
Details
Command
History
Matlab Calculations
Basic operations: +, -, /, *
Try:
5+4
6-5+4*3
Question: Does Matlab follow the order of operations?
sqrt(121)
semicolon (;) stops the output display
sqrt(10); -> will not display the result.
abs(-10)
2^2
2^3
Question: What operation is ^ ?
Variables/Numbers
Variable is a place holder for numbers.
Example :
a = 20.2
b = 10.1
3*a+b
List
– Matlab allows us to perform operations on list of
numbers.
List of numbers -> an array
– One way to create a list of numbers:
T=begin:interval:end
Exp: T=300:10:350.
– operations on list
Exp: 2*T , T+T
(matlab is case sensitive t ≠T)
Question: What are the values of t?
Generating Plots
Plot two variables
– x=1:1:8;
– y=50:5:85;
– plot(x,y)
Add labels and titles
– xlabel(‘Time of the day (hr)’)
– ylabel(‘Temperature (F)’)
– title(‘Change of temperature during a day’)
Formulas
Formulas are equations.
In these equations, one variable is
expressed in terms of other(s).
Exp: F=ma
F: force, m: mass, a: acceleration
Formula Exercise
Can you plot the Celsius-Fahrenheit for the
range of -30 to 120 Fahrenheit degrees?
Note: you must first enter the values of Fahrenheit
before you can perform the calculation for Celsius
Celsius/Fahrenheit conversion formula:
Celsius = (5/9)*(Fahrenheit – 32)
Note: if you use C instead of Celsius you must you C in all
calculations
Arrays
Array is a space holder for a set of variables.
Arrays can be multi-dimensional.
– 2D array is a matrix.
Our focus today is on 1D arrays
– To enter values for an array:
X=[1,4,2,7,10,3,4]
– You can apply operations on arrays.
Y=X*2
– To reach to an element of array
X(N), where N is element index number
exp: X(4)
Measures of Central Tendency
X=[1,4,2,7,10,3,4]
-Sort(x)
Our sorted data : 1 2 3 4 4 7 10
Matlab functions:
– mean(X)
– median(X)
– mode(X)
– range(X)
Exercise
Total rain fall in Grand Canyon
How can we type in the x parameters easily?
x
–
–
–
–
–
–
–
–
–
–
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
y (inches)
16.5
15.2
16.8
4.2
17.0
15.9
17.1
14.9
17.1
16.5
Exercise cont’d
Create an array for rain fall, and plot it.
Compute mean, median, mode, range
Outlier?
To eliminate the outlier:
Y(4)=[ ]
Y= 16.5 15.2 16.8 17.0 15.9 17.1 14.9
17.1 16.5
mean(Y) = ?
answer is 16.33
mode(Y) = ?
answer is 16.5
median(Y) = ?
answer is 16.5
range(Y) = ?
answer is 2.2
To Re-plot
The number of x cordinates must match
the number of y cordinates
You must delete the year of the outlier
x(4)= [ ]
Then plot(x,y)
Acknowledgement
Author, Barbara Deschamp,
acknowledges support from GK-12
fellowship, through National Science
Foundation grant No. 0841392.