Lecture Notes 1
Lecture Notes 1
2 − 3 ∗ (1 + 2)/2 = 2 − 3 1+2
2
The workspace shows variables that have been defined in the current
session. In particular, ans is by default the value of the last arithmetic
computation we made. We can check the value of a variable by entering
its name in the command window.
>> ans
ans =
50.2655
>> x1 = 5.337
>> my variable = "howdy"
>> frodoBaggins33 = sqrt(2)*pi
>> radius = 4
>> pi*radius^2
ans =
50.2655
Do your best to choose informative names for your variables.
>> x = 5; y = 6; disp(x+y)
11
Use SHIFT-ENTER to start a new line. Use ellipses (...) and
SHIFT-ENTER to continue a line.
>> sqrt(5 + 7 + ...
13)
ans =
5
Use clear to clear all variables from the workspace. Use clear VAR1
VAR2 to clear specific variables VAR1 and VAR2.
>> x = 5; clear x;
>> x
Undefined function or variable ’x’.
Use clc to clear the command line.
Okay, it’s finally time to make a proper script! In the command line, enter
help (as well as doc) is also invaluable when learning how to use various
MATLAB functions.
The following statements will take value 0 (if false) or 1 (if true)
a < b: a less than b
a > b: a greater than b
a <= b: a less than or equal to b
a >= b: a greater than or equal to b
a == b: a equal to b (note the doubled equals sign!)
a ∼= b: a not equal to b
>> 5 + true
ans =
6
The order of operations is as follows:
1 negation
2 relations
3 and
4 or
This construct is used where the decision to execute one or another set of
computations depends on the value of a boolean expression.
if this boolean expression is true
execute these commands
elseif this second expression is true instead
then exectue these other commands
else
do this if those earlier conditions are false
end
Write a script that prompts the user for two numbers (call them x and y ).
Write a script that outputs The numbers are equal if x = y and The
numbers are not equal otherwise.