1 Matlab
1 Matlab
Lecture one
1
INTRODUCTION TO MATLAB FOR ENGINEERING STUDENTS
Starting MATLAB
After logging into your account, you can enter MATLAB by double-clicking on the MATLAB
shortcut icon (MATLAB ) on your Windows desktop. When you start MATLAB, a special
window called the MATLAB desktop appears. The desktop is a window that contains other
windows. The major tools within or accessible from the desktop are:
• The Command Window
• The Command History
• The Workspace
• The Current Directory
• The Help Browser
• The Start button
The Desktop
The major tools within or accessible from the desktop are:
Window Purpose
Command Window Main window: for running functions and entering variables
2
Editor/Debugger Helpful in creating, modifying, and debugging MATLAB script and function
files (M-files)
Figure Window Contains output from graphic commands
Command Window
Use the command window to enter variables and run functions and M-files. Type the functions
and variables at the MATLAB prompt (>>) and they will be executed on the spot
Quitting MATLAB
To end your MATLAB session, type quit in the Command Window, or select File −→ Exit
MATLAB in the desktop main menu.
3
The clc command:
The clc command clears the Command Window. After a period of time working in the
Command Window, the display may become long. The clc command puts it back to a blank
window.
Command History Window
The commands you enter in the Command Window are logged in the Command History
window. In Command History, you can view previously used functions, and copy and paste
to the Command Windows to execute your selected lines. You can erase your command
history by selecting Clear Command History from the Edit menu
>> clear
The command clear or clear all removes all variables from the workspace. This frees up
system memory. In order to display a list of the variables currently in the memory, type
>> who
while, whos will give more details which include size, space allocation, and class of the
variables.
4
Help
• Use the Help Browser to search for and view documentation. It is a web browser that
displays HTML documents. There are many ways to get to the Help Browser
• click the help button ? in the toolbar
• type helpbrowser in the Command Window
• pick Help from the Launch Pad choices
Current Directory
MATLAB file operations use the current directory and the search path as reference points. Any
file you want to run must either be in the current directory or on the search path. A quick way
to view or to change the current directory is by using the Current Directory field on the
desktop toolbar.
To search for, view, open, or make changes to MATLAB M-files related directories/files, use
the Current Directory browser.
You will have noticed that if you do not specify an output variable, MATLAB uses a default
variable ans, short for answer, to store the results of the current calculation. Note that the
variable ans is created (or overwritten, if it is already existed). To avoid this, you may assign a
value to a variable or output argument name. For example,
>> x = 1+2*3
x=
7
5
will result in x being given the value 1 + 2 × 3 = 7. This variable name can always be used to
refer to the results of the previous computations. Therefore, computing 4x will result in
>> 4*x
ans =
28.0000
Before we conclude this minimum session, Table 1.1 gives the partial list of arithmetic
operators.
Table 1.1: Basic arithmetic operators
MATLAB variables are created with an assignment statement. The syntax of variable
assignment is
>> x = expression
Overwriting variable
Once a variable has been created, it can be reassigned. In addition, if you do not wish to
see the intermediate results, you can suppress the numerical output by putting a semicolon
(; ) at the end of the line. Then the sequence of commands looks like this:
>> t = 5;
>> t = t+1
t=
6
Error messages
If we enter an expression incorrectly, MATLAB will return an error message. For example,
in the following, we left out the multiplication sign, *, in the following expression
6
>> x = 10;
>> 5x
??? 5x
|
Error: Unexpected MATLAB expression.
Making corrections
To make corrections, we can, of course retype the expressions. But if the expression is
lengthy, we make more mistakes by typing a second time. A previously typed command
can be recalled with the up-arrow key ↑. When the command is displayed at the command
prompt, it can be modified if needed and executed.
Let’s consider the previous arithmetic operation, but now we will include parentheses. For
example, 1 + 2 × 3 will become (1 + 2) × 3
>> (1+2)*3
ans =
9
and, from previous example
>> 1+2*3
ans =
7
The order in which MATLAB performs arithmetic operations is exactly that taught in high
school algebra courses. Exponentiations are done first, followed by multiplications and
divisions, and finally by additions and subtractions. However, the standard order of
precedence of arithmetic operations can be changed by inserting parentheses. For example,
the result of 1+2× 3 is quite different than the similar expression with parentheses (1+2) × 3.
The results are 7 and 9 respectively. Parentheses can always be used to overrule priority, and
their use is recommended in some complex expressions to avoid ambiguity.
First The contents of all parentheses are evaluated first, starting from the innermost
parentheses and working outward
7
Third All multiplications and divisions are evaluated, working
from left to right
most computer programs. For operators of equal precedence, evaluation is from left to right.
𝟏 𝟒 𝟔
Now, consider another example: 𝟐+𝟑𝟐
+ 𝟓𝑿𝟕
in MATLAB, it becomes
>> 1/(2+3^2)+4/5*6/7
ans =
0.7766
or, if parentheses are missing,
>> 1/2+3^2+4/5*6/7
ans =
10.1857
So here what we get: two different results. Therefore, we want to emphasize the importance
of precedence rule in order to avoid ambiguity.
1-Combining in MATLAB
clc
clear
a=4;
b=5;
c=7;
d=a+b+c
or
clc
clear
a=[2 3 4 6 7 8 9 10];
sum(a)
2- subtraction in MATLAB
clc
clear
a=4;
8
b=5;
c=7;
d=a-b-c
3- Multiplication in MATLAB
clc
clear
a=4;
b=5;
c=7;
d=a*b*c
4- Divisible in MATLAB
clc
clear
a=4;
b=5;
c=7;
d=a/b
f=d/c
Mathematical functions
MATLAB offers many predefined mathematical functions for technical computing which
contains a large set of mathematical functions.
Typing help elfun and help specfun calls up full lists of elementary and special functions
respectively.
There is a long list of mathematical functions that are built into MATLAB. These functions are
called built-ins. Many standard mathematical functions, such as sin(x), cos(x), tan(x), ex,
ln(x), are evaluated by the functions sin, cos, tan, exp, and log respectively in MATLAB.
a- Trigonometric functions
b- Inverse Trigonometric functions
c- other Elementary functions
a- Trigonometric functions
Trigonometric functions Built In Function
Sine sin
Cosine cos
Tangent tan
Secant sec
Cosecant csc
Cosecant cot
Note: The MATLAB measuring angles in radians
>> x=sin(pi/2)
x=
1
>> y=cos(2*pi)
y=
1
>> v=tan(pi/4)
v=
9
1.0000
>> a=sec(2*pi)
a=
1
>> b=csc(pi/2)
b=
1
>> c=cot(pi/4)
c=
1.0000
b- Inverse Trigonometric functions
Inverse Trigonometric functions Built In Function
Inverse Sine asin
Inverse Cosine acos
Inverse tangent atan
Inverse Secant asec
Inverse Cosecant acsc
Inverse Cotangen acot
>> a=asin(1)
a=
1.5708
We can define inverse trigonometric functions in the following way what is the angle that if
we take her Sin value we get the number (1) and will definitely be ) pi/ 2)
>> b=acos(1)
b=
0
We get the angle of 0 or 2pi if take Inverse Cosine for one
>> c=atan(1)
c=
0.7854 %pi/4
>> d=asec(1)
d=
0 % 2pi
>> e=acsc(1)
e=
1.5708 %pi/2
>> f=acot(1)
f=
0.7854 %pi/4
10
In addition to the elementary functions, MATLAB includes a number of predefined
constant values. A list of the most common values is given in below .
pi : The π number, π = 3.14159 . . .
i,j : The imaginary unit i, √−1
Inf : The infinity, ∞
NaN : Not a number
a- abs
Syntax: y=abs(x)
Description: Absolute value and complex magnitude
Abs(x) returns an array y such that each element of y is the absolute value of the
corresponding element of x
EX: >> abs(-5)
ans =
5
b- rem
Syntax:R=rem(X,Y)
Description:Remainder after division
EX: >> rem(19,15)
ans =
4
>> rem(9,15)
ans =
9
>> rem(-9,15)
ans =
-9
>> rem(9,-15)
ans =
9
>> rem(-9,-15)
ans =
-9
c- imag
Syntax:y=imag(Z)
Description:y=image(Z) returns the imaginary part of the complex number of array Z .
EX: >> imag(2+3i)
ans =
3
d- real
Syntax:X=real(Z)
Description:X=real(Z)returns the real part of the elements of the complex array Z
EX: >> real(2+3i)
ans =
2
e- sqrt
Syntax: B=sqrt(X)
11
Description: B=sqrt(X)returns the square root of each element of the array X. for the
elements of X that are negative or complex sqrt(X) produces complex results.
EX: >> sqrt(16)
ans =
4
>> sqrt(-16)
ans =
0 + 4.0000i
f- exp
Syntax: y=exp(X)
Description: the exp function is an elementary function that operates element on arrays. Its
domain includes complex numbers . Y=exp(X) returns the exponential for each element of X .
EX: >> exp(0)
ans =
1
>> exp(1)
ans =
2.7183
Representation (Root and the exponential function and natural logarithm and trigonometric
functions) in MATLAB
clc
clear
x=1;
f=deconv((5*log10(x)+2*x^2*sin(x)+sqrt(x)*log(x)),(exp(6*x^3)+3*x^4+s
in(log(x))))
clc
clear
x=1;
f=(5*log10(x)+2*x^2*sin(x)+sqrt(x)*log(x))/(exp(6*x^3)+3*x^4+sin(log (x)))
Examples
We illustrate here some typical examples which related to the elementary functions
previously
defined.
As a first example, the value of the expression y = e−a sin(x) + 10√y, for a = 5, x = 2, and
y = 8 is computed by
>> a = 5; x = 2; y = 8;
>> y = exp(-a)*sin(x)+10*sqrt(y)
y=
28.2904
12
The subsequent examples are
>> log(142)
ans =
4.9558
>> log10(142)
ans =
2.1523
Note the difference between the natural logarithm log(x) and the decimal logarithm (base
10) log10(x) .
To calculate sin(π/4) and e10, we enter the following commands in MATLAB,
>> sin(pi/4)
ans =
0.7071
>> exp(10)
ans =
2.2026e+004
13