1 Student’s HandoutIntroduction to MATLAB What is MATLAB: MATLAB stands forMATrix LABoratory .MATLAB is a software package for computation in engineering, science, and applied mathematics. It offers a powerful programming language, excellent graphics, and a wide range of expert knowledge. MATLAB is published by and a trademark of The MathWorks, Inc. Standard Windows : When the MATLAB program starts, the following window opens which contains four smallerwindows .These windows are default and can be switched on or off if necessary. United International University (UIU) Dept. of Electrical and Electronic Engineering (EEE) Course No. EEE 110 Course Title : Simulation laboratory Command window Command History window Current Directory & Workspace window Figure 1.1 : The default view of MATLAB desktop The easiest and best way to learn MATLAB is to use MATLAB.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
This shows current variables and allows to edit variables by opening array editor (double
click), to load variables from files and to clear variables.
Current
Directory
window
This shows current directory and MATLAB files in current folder, provides with a handy
way to change folders and to load files.
History window This shows previously executed commands. Commands can be re-executed by double-
clicking.
Working on the command window :(A)Using MATLAB as a calculatorYou can use “command window” part for various calculations as if you would use a calculator: Typeexpressions containing numbers, parenthesis and mathematical operations and press “Enter” to get the result.
Example :
>> 2^sqrt(9)
ans =
8
>> a = 1.2;
>> b=2.3;
>> c=4.5;
>> d=4;
>> a^3+sqrt(b*d)-4*c
ans =
-13.2388 Note the semicolon after each variable assignment. If you omit the semicolon, then MATLAB echoes back on
the screen the variable value.
(B)Assigning Values to Variables
To create a variable just use it on the left hand side of an equal sign. The following examples show how to
assign values to variables, x and y.
>> x= 5x =
5>> y =log(2)
y =
0.6931
>> z = 10;<nothing is displayed. Why? Because of the semicolon used. But be confirmed that, a value of 10 is stored in
the variable z but does not display. >
Note that :
(1) You can use who command to list the currently active variables. For the preceding session this results in
>> who
Your variables are:
ans x y z
(2) Use clear command to delete variables from computer memory
MATLAB will print the result of every assignment operation unless the expression on the right hand side is
terminated with a semicolon.
(C)List of useful commands for managing variables :
Command Outcome
clear Removes all variables from memory.
clear x y z Removes only variables x,y,z from the memory .
who Displays a list the variables currently in the memory.
whos Displays a list the variables currently in the memory and their size together with
information about their bytes and class.
(D)Two important command to ensure fairness and readability
Command Outcome
clc Clears contents on the command window ensuring blank working environment
% If used before any line or statement, program ignores the line or statement.
So used to insert any comment in the program.
(E)Predefined variables :
A number of frequently used variables are already defined when MATLAB is started. Some
of the predefined variables are :
Predefined
Variables
Meaning
ans A variable that has the value of the last expression that has not assigned to a specific
variable. If the user does not assign the value of an expression to a variable, MATLAB
automatically stores the result in ans .
pi Value of the number π .
eps The smallest difference between two numbers. It’s value is 2.2204e-016 .
inf Used for infinity.
i or j Defined as 1−
NaN Stands for Not-a-Number.Used when MATLAB cannot determine a valid numeric
value.For example 0/0.
(F)Complex numbers :MATLAB also supports complex numbers. The imaginary number is denoted with the symbol i or j, assuming
that you did not use these symbols anywhere in your program (that is very important!). Try the following:>> z =3 + 4i % note that you do not need the ‘*’ after 4
>> conj(z) % computes the conjugate of z
>> angle(z) % computes the phase of z
>> real(z) % computes the real part of z>> imag(z) % computes the imaginary part of z
In the previous session, the commands were executed in the command window. Although every MATLAB
command can be executed in this way, there are some drawbacks of using command window .
(i) Using the command window to execute a series of commands especially if they are related to eachother is not convenient and may be difficult or even impossible.
(ii) The commands in the command window cannot be saved and executed again.
(iii) The command window is not interactive. This means that, every time the ENTER key is pressed ,only the last command is executed, and everything executed before is unchanged. If a change or
correction is needed in a previous command ,the commands have to be entered again.
A different way of executing commands with MATLAB is first to create a file with a list of commands,
save it and then run the file. The files that are used for this purpose is called script files.
Creating and saving a script file:
In MATLAB, script files are created and edited in the Editor/Debugger window. To do so, follow
File→ New → M-File .You will see the following window:
Before a script file can be executed it has to be saved. This is done by choosing save as from the file menu,selecting a location and entering a name for the file.
Running a script file :
A script file can be executed in two ways:
(i) Directly from the editor window by clicking on Run icon (see figure 2.1 ) or, debug → run<file_name.m> or simply press F5.
(ii) Type it’s name in the command window and press Enter.
One complete example:
The commands in the script file are
typed line by line. The lines arenumbered automatically. A new line
Problem description :Voltage divider : When several resistors are connected in series in an
electrical circuit , the voltage across each of them is given by the voltage divider rule: s
eq
nn v.
R
R v = ,
where vn and R n are the voltage across nth resistor R n is resistive value .R eq =∑ nR is the total resistance
. The power dissipated in the nth resistor is given by,2
s2
eq
nn v
R
R P =
The figure below shows for example a circuit with seven resistors connected in series .
Write program in calculates the a script file which calculates the voltage across each resistor and power dissipated in each resistor, in a circuit that has resistors connected in series. When the script file is
executed , it prompts the user to first enter the source voltage and then to enter the resistance of the
resistors in a vector . The program displays the result in a table. Following the table , the program displays
the current in the circuit, and the total power.Run the program and enter the following data for vs and the R’s.
vs = 24V, R 1=20Ω , R 2=14Ω , R 3=12Ω, R 4=18Ω, R 5=8Ω, R 6=15Ω, R 7=10Ω
Solution : Create the M-File as in the Editor window given below :
After executing the script file in the command window ,the output is as follows: