Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
MATLAB basics
• Get to know Matlab • Interact with Matlab • Write and save a program • Run and debug a program • Simple 2D ploBng
GeBng help in Matlab
• FuncDon help >> help <funcDon name>
• Topic help – Mathworks website
MATLAB Desktop
Command Window
Workspace Window
Current Directory Window Command History Window
Start Button
clc – clears the command window clear – clears the workspace
• Type in the Command Window:
2+2 • The soluDon was stored in
the default variable ans. • Then type: clear • Now define C_as: C_as=0.6; • The semi-‐colon (;)
prevents the result from being printed to the screen
• Only leUers, numbers and “_” can be used.
• Case sensiDve.
InteracDng with MATLAB
Scalar variables matrices
• Define 1D matrix (vector) x x=1:0.2:4 or x=ones(4,1) • The first index is the number of
rows and the second index is the number of columns.
• The soluDon can also be viewed in workspace.
• Then define a 2D matrix: y=[1,2,3;4,5,6] • We can change the value of any
elements: y(2,3)=5 • The matrix dimension can also be
changed: y(4,4)=7
WriDng a Program
• A MATLAB program can be a collecDon of command lines and is in the form of an M-‐file
• An M-‐file is to MATLAB what a doc-‐file is to Microso[ Word. • An M-‐file is wriUen in text Editor. • Let’s make a simple program to convert between degrees
Fahrenheit and degrees Celsius.
WriDng an M-‐File
• Create a new M-‐file: – Type edit into the Command Window – or use the menu “File”
• Type the following code in the Editor: tempF=[0:1:100]; tempC=(tempF-‐32)/1.8;
• A[er you have typed the code, save it as “convertFtoC.m”
Running an M-‐file
• Make sure that your Current Directory is where your file is. Type convertFtoC into the Command Window and press enter.
• If you typed the code as wriUen on the previous slide you will get an error. What went wrong?
• The advantage of an M-‐file is that we can go make the change and run it again without having to type all the code again.
• You can see the current variables in your workspace window or by typing “whos”
• It is o[en useful to display your data to the terminal. To do this type the name of the variable tempC and press enter.
PloBng in 2D
n Use the plot() command:
plot(tempF,tempC)
n Also try the following variants
plot(tempF,tempC,’x’) plot(tempF,tempC,’rx’)
title(’Temperature (F vs. C)') xlabel(’Degrees F') ylabel(’Degrees C')
n Add labels/titles
Hold on; % this allow for overlays Axis tight; %remove white space Clf; % clears figure
n Others
Saving Figures
• There are several ways to save plots created in MATLAB: – Save the figure as a .fig file, which is MATLAB’s graphics format, or in any other standard graphic format.
– Best to save to format compaDble with other so[ware (*.png, *.jpeg, *.Dff)
– Copy the figure into another document: Edit>>Copy Figure. OR, in command line – set(gcf,'paperposiDonmode','auto'); – Print(gcf,’-‐dpng’,’figure1.png’);
Loading Data
Download the following file hUp://webpages.uidaho.edu/jabatzoglou/ACIS/test.html
COOPID: 042319
1. Edit the file in Excel (search & replace “M”) 2. Copy & Paste 3. What are the dimensions of the variables? 4. Reshape to proper dimensions, 12-‐months a year.
StaDsDcs
Useful funcDons • mean : average value, try it
– Create new variable which is the difference from the mean, or anomaly
• Let’s make a plot of monthly mean Maximum temperature for Death Valley.
Linear Regression
• A means to model a response (Y) given predictor (X) using staDsDcs – We can use matlab’s polynomial regression which is a least-‐squares fiBng tool
– For linear model we assume the form Y(i) = α X(i)+β
– Let’s try this using Dme as X and temperature as Y >>b=polyfit(Year(1:116),Tempanom(1:116),1); % Note 1 refers to linear Examine b – Using this model with the enDre period of record of Years >>est =polyval(b,Year(1:116)); – Now plot your original values >>plot(Year(1:116),Tempanom(1:116),’k’); hold on; – And your modeled values >>plot(Year(1:116),est,’r’);