Introduction To MATLAB
Introduction To MATLAB
to
MATLAB
M H Joshipura
Contents
• Features
• Environment of MATLAB
• Getting Started
• Saving Options in MATLAB
• Script files
• M-files
• Special Functions in MATLAB
Features
• MATLAB is an interactive system for doing numerical
computations.
• MATLAB stands for Matrix Laboratory
• A numerical analyst called Cleve Moler wrote the first version of
MATLAB in the 1970s. It has since evolved into a successful
commercial software package.
• Powerful operations can be performed using just one or two
commands. There are more than 1000 functions in the basic
MATLAB product alone. MATLAB is based on C, user-friendly
and to be used in application where the emphasis is on logic
and not on the coding. MATLAB is based on C, user-friendly and
to be used in application where the emphasis is on logic and not
on the coding.
• Modeling, simulation, Data analysis, exploration, and
visualization and many more.
• Application development, including Graphical User Interface
building.
Features
Easy to use
Platform independence
Predefine function
Features
What is the fun in MATLAB
C++ code : Equivalent MATLAB Code
[Y,I]=sort(X)
for (i=0;i<n;i++)
{
Y(i)=X(i);
I(i)=i;
}
for (i=0; i<n-1;i++)
{
for (j=i+1;j<n;j++)
{
if (Y(i)>Y(j))
{
temp=Y(j);temp2=I(j);
Y(j)=Y(i);I(j)=I(i);
Y(i)=temp;I(i)=temp2;}
}
}
Environment of MATLAB
A=
16 3 2 13 Separation of Row
5 10 11 8
9 6 7 12
4 15 14 1 Separation of Column
Array and Matrix Operations
• Diary Command
– diary file: Saves all text from the MATLAB
session, except for the prompts (>>), as
text in file, written to the present working
directory. If file is not specified, the
information is written to the file named
diary.
– diary off: Suspends diary operation.
– diary on: Turns diary operation back on.
– diary: Toggles diary state
Saving Options in MATLAB
• Saving and Retrieving MATLAB Variables
The term script symbolizes the fact that MATLAB simply reads
from the script found in the file. The term M-file recognizes the
fact that script filenames must end with the extension “.m”. [mpe1]
Command Window
% This file will show
that the script file >> m=2;
can access the >> n=3;
variables in the >>mpe2
command window.
d=
a=2; 11
b=4;
c=a+b;
d=c+m+n
Special Commands – M Files
Functio Description
n
echo Control Command window echoing of script file
contents as they are executed.
• Syntax Errors
• Run-time and Logic Errors
Errors and Debugging
• .m extension
• Created in Editor/Debugger window
• Function communicates with the
MATLAB workspace only through
variable passed to it and through the out
put variables it creates
Rules for M-files
Psat =
1.6499e+006
Function Workspace
• Local Variables
• Global Variables
Global Variable
function Psat=mpe8(an1,an2,an3)
global T
Psat=exp(an1-(an2/(an3+T)));%Psat will always in terms of
mmHg.
mpe 7 Command Window
>>mpe7
>>please enter the system Temperature 30
>>please enter the Antoin constant A: 25
>>please enter the Antoin constant B: 2500
>>please enter the Antoin constant C: 204
Psat =
1.6499e+006
Functions
0 337.6673 0
1 351.4 1
SUMMARY
• Script files
– Doesn’t require any input
– Communicates between MATLAB workspace and
M-files
• Function-files
– Can’t be run without input arguments
– Similar to other functions in MATLAB
– Global variables
• Special functions are the key and more we
know them less will be the programming
efforts