Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Lecture1 Notes

This document provides an introduction to basic Matlab calculations, commands, and functions. It covers the Matlab environment and interface, basic arithmetic, command window formatting and control, built-in constants, elementary functions, and trigonometric functions. It also discusses using help and doc commands to get information on functions.

Uploaded by

Qamar Sultana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture1 Notes

This document provides an introduction to basic Matlab calculations, commands, and functions. It covers the Matlab environment and interface, basic arithmetic, command window formatting and control, built-in constants, elementary functions, and trigonometric functions. It also discusses using help and doc commands to get information on functions.

Uploaded by

Qamar Sultana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Matlab Basics-I

This lecture provides an introduction and overview of basic calculations. It


covers the following:

 The Matlab environment


 Basic arithmetic calculations
 Command Window control & formatting
 Built-in constants & elementary functions
The Matlab Environment

When you run Matlab, the following user interface is displayed:

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

Editor Window: Edits Programs


Basic Arithmetic Calculations

The Matlab “Command Prompt” is the symbol: >>

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:

Operation Symbol Example


Addition + 1+2=3
Subtraction - 1 - 2 = -1
Multiplication * 5*2 = 10
Right division / 4/2 = 2
Left division \ 4\2 = 2/4 = 0.5
Exponentiation ^ 2^3 = 8

Order of operations:

Priority of operation Mathematical Operation


First Calculations inside parenthesis, the innermost are
executed first.
Second Exponentiation
Third Multiplication and division (from left to right)
Fourth Addition and subtraction (from left to right)

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:

is entered as: ((1-3)/5)^4*8-(1+2)

Power(x,a) is a Matlab command that is equivalent to x^a:


Command Window Control and Formatting
The clc instruction clears the Command Window.

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).

Note: 4e+002 stands for 4*102 = 400.


Built-in Constants and Elementary Functions

Matlab built-in constants:

ans Default result name


pi 3.141592653589793
i or j sqrt(-1)
Inf or inf Infinity, e.g., 1/0 or 171!
NaN or nan Undefined or not a number 0/0
eps Floating point (machine) precision at 1
realmax Largest positive floating point number
realmin Smallest positive floating point number

Examples:
Matlab built-in elementary functions:

Note: The Matlab instruction power(x,1/n) is equivalent to nthroot(x,n),


when x is real. However, “power” also allows for complex values of “x”.
Built-in trigonometric functions:

Inverse trigonometric functions


Function Description
asin(x) Returns answer in radians
asind(x) Returns answer in degrees
acos(x) Returns answer in radians
acosd(x) Returns answer in degrees
atan(x) Returns answer in radians
atand(x) Returns answer in degrees
acot(x) Returns answer in radians
acotd(x) Returns answer in degrees

Hyperbolic trigonometric functions


Function Definition
sinh(x) (ex - e-x)/2
cosh(x) (ex + e-x)/2
tanh(x) (ex - e-x)/ (ex + e-x)
coth(x) (ex + e-x)/ (ex - e-x)
Rounding 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:

𝑦 = 3 cos3 (0.3) + 2 cos2 (0.3) + cos(0.3) − 3

should be implemented as the two successive evaluations,

𝑧 = 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.

On the other hand, typing doc tanh returns:

You might also like