You are on page 1of 47

Introduction

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

1. Command Window: commands may be


entered

2. Graphic Windows: display plots and


graphs

3. Edit/Debug Windows: create and modify


MATLAB programs.
Environment of MATLAB
Getting Started

help – Gives general help about MATLAB


demo - To run built-in demonstrations.
clc - Clear Command Window.
clf - Clear figure window.
clear - Clear workspace/clear all command form the memory.
!- It send a command to the computer’s operating system
%- Comment
diary - A copy of all input and output stored in a diary file
who - A list of variable and arrays in the current workspace.
exit - Exits MATLAB
Getting Started

• The fundamental unit of in any MATLAB


program is the ARRAY.
• MATLAB variable are automatically created
when they are initialized.
• There are three way to initializing variable in
MATLAB
 Assign data to the variable in an assignment
statement.
 Input data into the variable from the keyboard.
 Read data from file.
Getting Started
Entering Matrix in MATLAB

>>A = [16, 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

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

Operation MATLAB Form


Array Addition (a + b)
Array subtraction (a - b)
Array Multiplication (a .* b)
Matrix Multiplication (a * b)
Array right division (a ./ b)
Array left division (a .\ b)
Matrix Right Division (a / b)
Matrix Left Division (a \ b)
Array Exponentiation (a .^ b)
Saving Options in MATLAB

• 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

• save Stores workspace values (variable names,


sizes, and values), in the binary file matlab.mat in
the present working directory.
• save data Stores all workspace values in the file
data.mat
• save data_1 x y Stores only the variables x and y in the
file data_1.mat
• load data_1 Loads the values of the workspace values
previously stored in the file data_1.mat
M Files

• Script, which do not accept input


arguments or return output arguments.
They operate on data in the workspace.
• Functions, which can accept input
arguments and return output
arguments. Internal variables are local
to the function.
Script M-Files

Group of MATLAB commands placed in a text file with a text


editor. MATLAB can open and execute the commands
exactly as if they were entered at the MATLAB prompt.

How to create it?

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]

Commands within the script file have access to all variables in


the MATLAB workspace, and all variables created in by the
script file become a part of the workspace. [mpe2]
mpe1
%This file will show how to create and run
any script file.
a=2;
b=4;
c=a+b
mpe2

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.

input Prompt user for input

disp Displays the statement as it is or displays the value


of variable without its name
keyboard Give control to keyboard temporarily. Type return,
to return control to executing script file.
pause or Pause until user presses any keyboard key, or
pause (n) pause for n seconds and then continue.
•Let’s have a look at the file using all these functions mpe3
mpe 3
%This script file will describe some special commands.
% they are echo, input, disp, keyboard, pause
disp('this file will give you the idea about different commands')
a=input('please enter the value of a: ');
keyboard;
b=3;
echo on
c=a+b;
d=m+n;
echo
disp('Strike any key to continue');
pause;
f=input('please enter the value of f: ');
e=(d*b) +f;
pause(10);
disp(e)
Effective Use of Script Files

• The name of a script file must follow the


MATLAB convention for naming
variables; that is, the name must begin
with a letter and may include digits and
the underscore character.
• Do not give a script file the same name
as a variable it computes
• Do not give a script file the same name
as a MATLAB command or function.
Errors and Debugging

• Syntax Errors
• Run-time and Logic Errors
Errors and Debugging

The following are suggestions to help


with the debugging of MATLAB scripts
or M-files:
• Check the robustness of the file with
available data
• Execute the intermediate data.
• Control the execution with keyboard
command.
Programming Style

• Comments and H1 line


• The first line should have the file name
and keywords
• The next several lines should contain
comments giving the author’s name, the
date the script was created, and a
detailed description of the script.
Function M-files

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

• function [output variables] = function_name(input


variables);
– function d=function_mpe1(m)
• Function name and the name with which we
store it should be identical.
• Function workspace and local variables
• Multiple output \ input variables
– function [distance, velocity, accel] = position(x,y)
• Semicolons suppresses the display of the value.
[mpe4]
mpe4
% This file will show
how to create and Command Window
run any script file.
function d=mpe4(m) >>j=3;
>>g=mpe4(j)
a=2;
b=4; g=
c=a+b; 9
d=c+m;
Features of Function M-files

• Function file and Script file can call each


other.
• Multiple functions can appear in a single
function M file. [Sub function]
• All the sub functions follow the rules of
M-files. [mpe5]
mpe 5
%This is a scriptfile which will provide the
inputs for global T
% the function file to calculate vapor
pressure. T=input('please
enter the
system temperature: ');
a=input('please enter contstan A: ');
b=input('please enter contstan B: ');
function Psat=mpe6(an1,an2,an3,T)
c=input('please enter contstan C: ');
mpe11 %mpe6: Calculates vapour pressure of a given
Psat=mpe6(a,b,c,T) no. of component using antonies const.
%The consts should be taken from Reid et al 3rd
edition.
%The equation is ln(Psat)=A-B/(T+C)
%The pressure is in mmHg. T in C
Psat=exp(an1-(an2/(an3+T)));%Psat will always
in terms of mmHg.
mpe 5 Command window
>>mpe5
>>please enter contstan A: 25
>>please enter contstan B: 2500
>>please enter contstan C: 204
>>please enter the system temperature: 30

Psat =

1.6499e+006
Function Workspace

• Local Variables
• Global Variables
Global Variable

• If you define a variable in a script, it will stay defined


in the workspace. Functions, on the other hand, do
not share the same workspace.
• A function will not know what a variable is unless it
gets the variable as an argument, or unless the
variable is defined as a variable that is shared by the
function and the MATLAB workspace, or a global
variable.
• To gain access to a global variable within a function
or the MATLAB workspace, the variable must be
declared global within each desired workspace.
[mpe7]
mpe 7 (Global)
global T
T=input('please enter the system Temperature ');
an1=input('please enter the Antoin constant A: ');
an2=input('please enter the Antoin constant B: ');
an3=input('please enter the Antoin constant C: ');
Psat=mpe8(an1,an2,an3)

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

• Functions are black boxes. They accept inputs,, act


on these inputs, and create outputs. Any and all
variables created within the function are hidden from
the MATLAB workspace.
• Each function has its own temporary workspace that
is created with each function call and deleted when
the function completes execution.
Special Functions
• inv
• det
• rank
• eig
• roots
• fzero
• fsolve
• polyfit
• Interp1
Use of Matrix related functions
Use of roots in MATLAB

Compare it with the


prgramme in C/C++ to
find roots of polynomial
Use of fzero in MATLAB
fsolve in MATLAB (mpe 9)
• Solve the following set of equation using fsolve.
Use of fsolve in MATLAB
(Continued..)
We can write two
equations in one
function

The calling syntax in command window is


Use of polyfit in MATLAB
Use of interp1 in MATLAB
Plots in MATLAB
How to plot t-x-y diagram
x1 Temp. y1

0 337.6673 0

0.1 338.7721 0.0601

0.2 339.9186 0.1261

0.3 341.1129 0.1988

0.35 341.7292 0.2378

0.4 342.3591 0.2789

0.45 343.0032 0.3221

0.5 343.6623 0.3675

0.6 345.0288 0.4659

0.7 346.4678 0.5755

0.8 347.993 0.6984

0.9 349.6279 0.8378

1 351.4 1
SUMMARY

• Developed in 1970 by Cleve Muler and has


the base of C and FORTRAN.
• Basic element for MATLAB is a MATRIX.
• The environment of MATLAB, consists of
three windows, Command, Edit/Debug and
Graphic Window (we have not touched upon)
• One can keep the record of the work done in
any MATLAB session by just using the
command diary, to keep the record of
variables we have the command save and to
retrieve them we have load.
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

You might also like