Docsity

Solving Simple Problems and Saving Files in MATLAB | BAE 200, Lab Reports of Agricultural engineering

Toggle sidebar
bg1
BAE 200 - WK2 Laboratory
9/2/08
Chapter 5 - Solving Simple problems and saving Files in MATLAB p 86-115
Goals: - Get comfortable with the MATLAB environment and the ability to save
files to your lockerspace/workspace.
Open MATLAB and set the Current Directory to your Lab Lockerspace
Variables
• Start with a letter
• CaSe seNsitiVe
• Can contain letters,digits, under_score up to 63 characters
• Careful not to name a variable a MATLAB keyword or function
• Can test – isvarname 1= true, 0=false
• which function
• Activity – Type in iskeyword that lists keywords in MATLAB, but not built-in
functions
Matrices and Scalars
• Matrix – set of numbers in a rectangular row or grid
• 1x1 = scalar
• Must have brackets
Arithmetic Operations
• See Table 1 p 87 for operations between 2 scalars
• Note how variable x can be changed by x=x+1.
• Activity - Type in
o a=1+2;
o b=5;
o x=a+b
o What is the result?
o How about if you type in x = x+1?
o = is an assignment operator which saves the variable value like
overwriting a file and saving it
• Precedence of Arithmetic operations – same as standard rules
• MATLAB does not read white space – so you can insert spaces to make your
equations more readable
• Large expressions or equations can be separated into several variables
(numerator/denominator example p 89)
• Scientific notation an exception – need to have number next to each other or
MATLAB will interpret it as 2 numbers
pf3
pf4

Partial preview of the text

Download Solving Simple Problems and Saving Files in MATLAB | BAE 200 and more Lab Reports Agricultural engineering in PDF only on Docsity!

BAE 200 - WK2 Laboratory 9/2/ Chapter 5 - Solving Simple problems and saving Files in MATLAB p 86-

Goals: - Get comfortable with the MATLAB environment and the ability to save files to your lockerspace/workspace.

Open MATLAB and set the Current Directory to your Lab Lockerspace

Variables

  • Start with a letter
  • CaSe seNsitiVe
  • Can contain letters,digits, under_score up to 63 characters
  • Careful not to name a variable a MATLAB keyword or function
  • Can test – isvarname 1= true, 0=false
  • which function
  • Activity – Type in iskeyword that lists keywords in MATLAB, but not built-in functions

Matrices and Scalars

  • Matrix – set of numbers in a rectangular row or grid
  • 1x1 = scalar
  • Must have brackets

Arithmetic Operations

  • See Table 1 p 87 for operations between 2 scalars
  • Note how variable x can be changed by x=x+1.
  • Activity - Type in o a=1+2; o b=5; o x=a+b o What is the result? o How about if you type in x = x+1? o = is an assignment operator which saves the variable value like overwriting a file and saving it
  • Precedence of Arithmetic operations – same as standard rules
  • MATLAB does not read white space – so you can insert spaces to make your equations more readable
  • Large expressions or equations can be separated into several variables (numerator/denominator example p 89)
  • Scientific notation an exception – need to have number next to each other or MATLAB will interpret it as 2 numbers

o 6.022e23 Avagadro’s number – (number of atoms or molecules in a gram mole of the substance)

Matrix operations

  • x=[1,2,3,4] or [1 2 3 4] returns columns

x =

1 2 3 4

  • y=[1;2;3;4] returns rows (; indicates rows)

y =

1 2 3 4

  • a=[1 2 3 4; 5 6 7 8;9 10 11 12] returns a =

1 2 3 4 5 6 7 8 9 10 11 12

  • shortcuts to entering evenly spaced matrices include :, linespace()
  • Addition of matrices straightforward A+B
  • Multiplication, Division, Exponentiation require a “.” o A.B* o A.\B o A.^
  • Transposition of matrix
  • a =

1 2 3 4 5 6 7 8 9 10 11 12

  • a' ans =

1 5 9 2 6 10 3 7 11 4 8 12

o diary on will save to your work from the command window to your current directory when you exit MATLAB to a file named diary that you can open with Notepad, Word, etc. o diary (‘filename’) will save the diary to the filename you have chosen in your current directory. o Will submit homeworks in this way at first

  • M-files – will be more important later for programming

Activities - 30 minutes Complete examples 1, 3 and 4. Save work as diary file (I will check this following class) Save example 4 as a M-file