Lecture1 Notes
Lecture1 Notes
The Command History Window lists the commands that have been
previously entered. A command can be executed again in the Command
Window by double clicking it in the Command History. Alternatively, you
may drag a command from the Command History Window to the
Command Window for editing/execution. You can also copy and paste
(right click the command and check the popup menu for options.)
A recently executed command can also be brought back to the Command
Window at the >> prompt by scrolling through old commands using the
up/down arrows.
Graphing Window: Displays Plots
>> syms x
>> ezplot(sin(x))
You type the calculation to be performed next to the >> prompt, inside the
Command Window:
Multiple calculations can be typed on the same line, separated by commas. Matlab
evaluates them, independently and in the order typed:
The arithmetic operations for scalars:
Order of operations:
Examples:
1+2-3*4/2 is evaluated as if it was typed as: 1+2-12/2 then 1+2-6 then 3-6 then -3.
If you forget the above priority order, then use parenthesis to eliminate any
confusion: (1+2)-(3*4)/2.
Example:
The diary filename command is used to create (or open) a diary text file with the
name “filename.txt”. After this command is executed, any instructions you execute
(including errors) are saved. A diary is closed with the diary off command, or if you
exit Matlab.
For all assignments, you are required to record your complete Matlab session in a
diary file that has your name (example, diary Mary_Smith) and be ready to submit it
electronically when requested by your instructor.
If a command is too long to fit in one line, it can be continued to the next line by
typing three periods … and pressing the enter key. The continuation of the
command is then typed in the new line.
If a semicolon (;) is typed at the end of an instruction, the output of the instruction
is not displayed. But it is computed and saved in the variable ans. Typing ans then
pressing the RETURN key displays the value of the calculation.
When the symbol % is typed at the beginning of the line, the line is treated as a
comment and is ignored by Matlab. A short comment can also be used on the same
line after an instruction (in-line comment). Comments are very useful for
documenting programs for future reference.
Formatting of Matlab Results
Matlab skips a line between the label (ans = ) and the numerical answer. This extra
line can be supressed (as in the above examples) with the command format
compact. The command format loose reintroduces the blank line. Here is Matlab’s
output for the last example on the previous page with the loose format:
The default display for floating point numbers in Matlab is four decimal digits
(corresponds to format short). Executing the instruction format long changes the
display to 15 decimal digits. Here is an example showing the value of the build-in
constant pi () in the short and long format:
Available Matlab display formats are summarized in the following table: (Source for
the following Tables: Matlab, Introduction with Applications, Amos Gilat (2007, John Wiley).
Examples:
Matlab built-in elementary functions:
Example:
Using Matlab to evaluate the following expressions (with highest accuracy
available). Assume angles are in radians.
Solution
When evaluating functions/expressions using software the user is advised to reformulate the expression in order
to minimize computation time. For example, since evaluating a trigonometric function is much more
computationally expensive than performing basic algebraic operations of addition, subtraction, multiplication
and division, the following expression:
𝑧 = cos(0.3)
𝑦 = ((3 ∗ 𝑧 + 2) ∗ 𝑧 + 1) ∗ 𝑧 − 3
The help and doc instructions:
Anytime you are not sure how to use a Matlab built-in function or instruction, you
can use the help or doc command to learn about its use.