Pakistan Electric Lab
Pakistan Electric Lab
Pakistan Electric Lab
Introduction to MATLAB
Introduction to MATLAB:
MATLAB is widely used in all areas of applied mathematics, in education and research at
universities, and in the industry. MATLAB stands for MATrix LABoratory and the software is
built up around vectors and matrices. This makes the software particularly useful for linear
algebra but MATLAB is also a great tool for solving algebraic and differential equations and for
numerical integration. MATLAB has powerful graphic tools and can produce nice pictures in
both 2D and 3D. It is also a programming language, and is one of the easiest programming
languages for writing mathematical programs. MATLAB also has some tool boxes useful for
signal processing, image processing, optimization, etc.
PC: Choose the submenu "Programs" from the "Start" menu. From the "Programs" menu, open
the "MATLAB" submenu. From the "MATLAB" submenu, choose "MATLAB".
The MATLAB environment (on most computer systems) consists of menus, buttons and a
writing area similar to an ordinary word processor. There are plenty of help functions that you
are encouraged to use. The writing area that you will see when you start MATLAB, is called the
command window. In this window you give the commands to MATLAB. For example, when you
want to run a program you have written for MATLAB you start the program in the command
window by typing its name at the prompt. The command window is also useful if you just want
to use MATLAB as a scientific calculator or as a graphing tool. If you write longer programs, you
will find it more convenient to write the program code in a separate window, and then run it in
the command window (discussed in Intro to programming).
In the command window you will see a prompt that looks like >> . You type your commands
immediately after this prompt. Once you have typed the command you wish MATLAB to
perform, press <enter>. If you want to interupt a command that MATLAB is running, type <ctrl>
+ <c>.
The commands you type in the command window are stored by MATLAB and can be viewed in
the Command History window. To repeat a command you have already used, you can simply
double-click on the command in the history window, or use the <up arrow> at the command
prompt to iterate through the commands you have used until you reach the command you
desire to repeat.
5*sin(2.5^(3-pi))+1/75
at the prompt. Be careful with parantheses and don't forget to type * whenever you multiply!
Note that MATLAB is case sensitive. This means that MATLAB knows a difference between
letters written as lower and upper case letters. For example, MATLAB will understand sin(2) but
will not understand Sin(2).
We can easily define our own variables in MATLAB. Let's say we need to use the value of
3.5sin(2.9) repeatedly. Instead of typing 3.5*sin(2.9)over and over again, we can denote this
variable as x by typing the following:
x=3.5*sin(2.9)
x+1
and observe what happens. Note that we did not need to declare x as a variable that is
supposed to hold a floating point number as we would need to do in most programming
languages.
Often, we may not want to have the result of a calculation printed-out to the command
window. To supress this output, we put a semi-colon at the end of the command; MATLAB still
performs the command in "the background". If you defined x as above, now type
y=2*x;
In many cases we want to know what variables we have declared. We can do this by typing
whos. Alternatively, we can view the values by openning the "Workspace" window. This is done
by selecting the Workspace option from the View menu. If you want to erase all variables from
the MATLAB memory, type clear. To erase a specific variable, say x, type clear x. To clear two
specific variables, say x and y, type clear x y, that is separate the different variables with a
space. Variables can also be cleared by selecting them in the Workspace window and selecting
the delete option.
Operations in MATLAB:
MATLAB has two different types of arithmetic operations. Matrix arithmetic operations are
defined by the rules of linear algebra. Array arithmetic operations are carried out element by
element, and can be used with multidimensional arrays. The period character (.) distinguishes
the array operations from the matrix operations. However, since the matrix and array
operations are the same for addition and subtraction, the character pairs .+ and .- are not used.
Arithmetic Operations:
Arithmetic functions include operators for simple operations like addition and multiplication, as
well as functions for common calculations like summation, moving sums, modulo operations,
and rounding.
Addition:
a=10;
b=100;
c=b+a;
a=10
b=100
c=b+a
Subtraction:
j=150;
k=50;
l=j-k;
Multiplication:
d=25;
e=4;
f=d*e;
d=25
e=4
f=d*e
Division:
g=125;
h=25;
i=h/g;
g=100
h=25
i=h\g
Matrices addition
a=[1 2 4; 7 5 6];
c=b+a;
a=[1 2 4; 7 5 6]
c=b+a
Matrices Subtraction
j=[15 10 25; 26 44 40];
l=k-j;
i=k-j
Matrices multiplication
f=d*e;
d=[11 14; 9 2]
e=[3 2 ; 4 5]
f=d*e
x=A;
x(:,3)=[]
size(S)L=[0 2 0];
size(L)
Conclusion:
In this lab we learn about how to use MATLAB. This is the introductory lab about MATLAB.
MATLAB stands for MATrix LABoratory and the software is built up around vectors and
matrices. In MATLAB we can produce pictures in both 2D and 3D. There are different
commands and operation performs in MATLAB. The commands we type in the command
window are stored by MATLAB and can be viewed in the Command History window.