12
MATLAB basics Get to know Matlab Interact with Matlab Write and save a program Run and debug a program Simple 2D ploBng

MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

MATLAB  basics  

•  Get  to  know  Matlab  •  Interact  with  Matlab  •  Write  and  save  a  program  •  Run  and  debug  a  program  •  Simple  2D  ploBng  

Page 2: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

GeBng  help  in  Matlab  

•  FuncDon  help  >>  help  <funcDon  name>  

•  Topic  help  – Mathworks  website  

Page 3: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

MATLAB  Desktop  

Command Window

Workspace Window

Current Directory Window Command History Window

Start Button

clc – clears the command window clear – clears the workspace

Page 4: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

•  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  

Page 5: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

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.  

Page 6: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

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”  

Page 7: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

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.  

Page 8: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

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

Page 9: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

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’);  

Page 10: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

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.  

Page 11: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

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.  

Page 12: MATLAB&basics& · 2012-12-28 · matlab1.pptx Author: John Abatzoglou Created Date: 2/8/2012 6:54:49 PM

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’);