Lecture 2 - Modeling and Computing(2)_2
Lecture 2 - Modeling and Computing(2)_2
• Note that variable names must start with a letter, though they
can contain letters, numbers, and the underscore (_) symbol
1 5 9
2 6 10
3 7 11
4 8 12
>>1:0.6:3
ans =
1.0000 1.6000 2.2000 2.8000
• y = (−4) ^ 2 gives y = 16
since the negation operation on the 4 takes place first because of the
parentheses
• y = 8/2*6 gives y = 24
since the left-to-right rule implements the division first to give 4 and then the
multiplication to give 4*6 to yield 24
© McGraw Hill LLC
Complex Numbers
All the operations listed in the previous slides can be used with
complex quantities (that is, values containing an imaginary part
entered using i or j and displayed using i)
>> x = 2+i*4; (or 2+4i, or 2+j*4, or 2+4j)
>> y = 16;
>> 3 * x
ans =
6.0000 +12.0000i
>> x+y
ans =
18.0000 + 4.0000i
>> x' % Compute complex Conjugate transpose
ans =
2.0000 - 4.0000i
• The built-in lookfor command will search help files for occurrences of text and
can be useful if you know a function’s purpose but not its name
• Two of the primary functions are plot (for plotting 2-D data) and
plot3 (for plotting 3-D data).
• Scripts can be executed either by (make sure to save your file first):
• typing their name (without the .m) in the command window, by selecting the
Debug, Run (or Save and Run) command in the editing window, or
• by hitting the F5 key while in the editing window.
The easiest way to get a value from the user is the input
command:
• n = input('promptstring')
MATLAB will display the characters in promptstring, and whatever value is
typed is stored in n.
• For example, if you type pi, n will store 3.1416…
• n = input('promptstring', 's')
MATLAB will display the characters in promptstring, and whatever characters
are typed will be stored as a string in n.
• For example, if you type pi, n will store the letters p and i in a 21 char array.
• disp(value)
• will show the value on the screen.
• The values in the variables are formatted based on format codes or format
specifiers as highlighted in the next slide.
• To load a rectangular array from a text file, simply use the load
command and the file name. The data will be stored in a matrix with
the same name as the file (but without any extension).
• Note - though the condition may become false as the statements are
running, the only time it matters is after all the statements have run.
• will display all the numbers up to 100 that are evenly divisible by 17.
x v0 cos 0 t
y v0 sin 0 t 0.5 gt 2
where v0 initial velocity ( m s )
0 initial angle (radians)
g gravitational constant (= 9.81m s )
2
• Example: create a handle to an anonymous function that finds the square of a number:
• Variable sqr is a function handle. The @ operator creates the handle, and the parentheses () immediately after the @
operator include the function input arguments. This anonymous function accepts a single input x, and implicitly
returns a single output, an array the same size as x that contains the squared values.
• Find the square of a particular value (5) by passing the value to the function handle, just as you would pass an input
argument to a standard function.
myfun(@sin)
52
= −1 𝑠𝑖𝑔𝑛 1+ 𝑏52−𝑖 2−𝑖 × 2𝑒−1023 = ±(1 + 𝑓) × 2𝑒−1023
𝑖=1