0% found this document useful (0 votes)
101 views

1 Matlab

MAT LAB Lecture one provides an introduction to MATLAB for engineering students. It discusses starting MATLAB and describes the various windows and tools available, including the Command Window, Command History, Workspace, Current Directory, Help Browser, and Launch Pad. It also covers how to use MATLAB for calculations, create variables, view help documentation, and control the hierarchy of arithmetic operations.

Uploaded by

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

1 Matlab

MAT LAB Lecture one provides an introduction to MATLAB for engineering students. It discusses starting MATLAB and describes the various windows and tools available, including the Command Window, Command History, Workspace, Current Directory, Help Browser, and Launch Pad. It also covers how to use MATLAB for calculations, create variables, view help documentation, and control the hierarchy of arithmetic operations.

Uploaded by

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

MAT LAB

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

Use tab to go to current


directory folder
Run program Get help

Enter Matlab Function

Figure 1.1: The graphical interface to the MATLAB workspace

The Desktop
The major tools within or accessible from the desktop are:
Window Purpose

Command Window Main window: for running functions and entering variables

Command History Logs previously run commands in the Command Window

Launch Pad Provides access to tools, demos, and documentation

Help Browser Provides help for accessing documentation

Current Directory Browser for accessing files

Workspace Provides information about the variables that are used

2
Editor/Debugger Helpful in creating, modifying, and debugging MATLAB script and function
files (M-files)
Figure Window Contains output from graphic commands

Array Editor Browser for editing arrays

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

Notes on working with the Command Window


• To type a command, the cursor must be placed next to the command prompt “>>”
• The end of a command is when the Enter key is pressed. Once a command is entered, it is
executed. However, only the last command is executed. Previous commands are unaltered
and unexecuted.
• Several command may be typed on the same line by typing a comma “,” between the
commands. Again, when the Enter key is pressed, the commands are executed in order from
left to right.
• A previous command may be “recalled” to be the current command by using the up arrow.
• If a command is too long to fit on one line, it can be split between lines by typing three
periods “...”, pressing the Enter key, and continuing the command on the next line. A
command may
extended over several lines up to a total of 4096 characters

Continuation and Suppress Echo:


Statement: Terminated by an end-of-line <carriage return> unless you put a ... at the end of
the line to continue. Can also terminate with a semicolon ; which suppresses the echo of the
statement.
Example :
x1 = 1 + 1/2 + 1/3 + 1/4 ...
+ 1/5 + 1/6;
Suppress echo: ;
>> x = 3 %evaluation printed
x=
3
>> x = 3; %evaluation suppressed
>>
Comments:
% a single line comment
v = 1584; %Initial velocity (km/h)

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

Managing the workspace


The contents of the workspace persist between the executions of separate commands.
Therefore, it is possible for the results of one problem to have an effect on the next one. To
avoid this possibility, it is a good idea to issue a clear command at the start of each new
independent calculation.

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

Launch Pad Window


The Launch Pad gives easy access to the different tools within MATLAB. You may need to go
through the Start Button (lower left-hand corner) to get to the Launch Pad.

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.

Using MATLAB as a calculator


As an example of a simple interactive calculation, just type the expression you want to
evaluate. Let’s start at the very beginning. For example, let’s suppose you want to calculate
the expression, 1 + 2 × 3. You type it at the prompt command (>>) as follows,
Example
>> 1+2*3
ans =
7

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

Symbol Operation Example


+ Addition 2+3
− Subtraction 2−3
∗ Multiplication 2∗3
/ Division 2/3

Creating MATLAB variables

MATLAB variables are created with an assignment statement. The syntax of variable
assignment is

variable name = a value (or an expression)


For example,

>> x = expression

where expression is a combination of numerical values, mathematical operators, variables,


and function calls. On other words, expression can involve:
• manual entry
• built-in functions
• user-defined functions

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.

Controlling the hierarchy of operations or precedence

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

By adding parentheses, these two expressions give different results: 9 and 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.

Therefore, to make the evaluation of expressions unambiguous, MATLAB has established a


series of rules. The order in which the arithmetic operations are evaluated is given in Table
1.2. MATLAB arithmetic operators obey the same precedence rules as those in

Table 1.2: Hierarchy of arithmetic operations


Precedence Mathematical operations

First The contents of all parentheses are evaluated first, starting from the innermost
parentheses and working outward

Second All exponentials are evaluated, working from left to right

7
Third All multiplications and divisions are evaluated, working
from left to right

Fourth All additions and subtractions are evaluated, starting


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.

Entering multiple statements per line


It is possible to enter multiple statements per line. Use commas (, ) or semicolons (; ) to
enter more than one statement at once. Commas (, ) allow multiple statements per line
without suppressing output.

>> a=7; b=cos(a), c=cosh(a)


b=
0.6570
c=
548.3170

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

c- other Elementary functions


functions Built In Function
Exponential exp(x)
Square root sqrt(x)
Remainder after division rem(x)
Natural logarithm log(x)
Common logarithm log10(x)
Absolute value abs(x)

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

5log10(x)+2𝑥 2 sin(x)+√𝑥 lin(x)


f=--------------------------------------------------------------
3
𝑒 6𝑥 +3𝒙𝟒 +sin(lin(x))

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

You might also like