Pralay Mitra MATLAB
Pralay Mitra MATLAB
Derived from FORTRAN subroutines LINPACK and EISPACK, linear and eigenvalue
systems.
Gained its popularity through word of mouth, because it was not o±cially distributed.
Rewritten in C in the 1980's with more functionality, which include plotting routines.
The MathWorks Inc. was created (1984) to market and continue development of
MATLAB.
Introduction
MATLAB: MATrix LABoratory
Strengths
Weakness
Introduction
Starting MATLAB
Double click on the MATLAB icon
It will open and following elements will appear
The Command Window
The Command History
The Workspace
The Current Directory
The Help Browser
The Start button
Environment and
Workspace
Getting Started
>> 4*x
Use as a calculator ans =
>> 1+2*3 28.0000
ans = Closing MATLAB (File -> Exit)
7 >> quit
>> x = 1+2*3
x=
7
Variables
Variable = value or expression
>> x= 5;
>> x = x+10
x=
15
>> 5x
??? 5x
|
Error : Unexpected MATLAB expression
To correct hit the up-key and edit
Operator Precedence
Parentheses (starting from inner most)
Exponentials (associativity left to right)
Multiplications and Divisions (associativity left to right)
Additions and Subtractions (associativity left to right)
Try:
>> 1/(2+3^2)+4/5*6/7
>> 1/2+3^2+4/5*6/7
>>format short Commands
>>x=123.1234
Short is default
>>format long
>>x=1.231233333333333e+002
>> clear
>> who size, space allocation, and class of the variables
>>help elmat
>>doc elmat
Matrix Operations
M-file Scripts
M-file functions
Output commands
Next if I type
>>who
Programming in MATLAB (M-File function)
%{
Comments
%}
# Define the function name, and the number and order of input and output arguments
function f = factorial(n)
# A one line summary description of the program, displayed when you request Help
% FACTORIAL(N) returns the factorial of N.
# A more detailed description of the program
% Compute a factorial value.
# Program code that performs the actual computations
f = prod(1:n);
>> f = factorial(5)
f=
120
Programming in MATLAB
Scripts Functions
- Do not accept input arguments - Can accept input arguments and
or return output arguments. return output arguments.
- Store variables in a workspace - Store variables in a workspace
that is shared with other scripts internal to the function.
- Are useful for automating a - Are useful for extending the
series of commands MATLAB language for your
application
I/O arguments
disp
fprintf
Control Flow (if-else)
discr = b*b - 4*a*c; discr = b*b - 4*a*c;
if discr < 0 if discr < 0
disp('Warning: discriminant is negative'); disp('Warning: discriminant is negative');
end elseif discr == 0
disp('Discriminant is zero')
discr = b*b - 4*a*c; else
disp('Roots are real')
if discr < 0 end
disp('Warning: discriminant is negative');
else
disp('Roots are real, but may be repeated')
end
Control Flow (for and while)
for ii=1:5
x=ii*ii
end
n = 5; A = eye(n);
for j=2:n
for i=1:j-1
A(i,j)=i/j;
A(j,i)=i/j;
end
end
x=1
while x <= 10
x = 3*x
end
Saving output to a file
Read a set of N linear equations For an input value, generate Sine and Cosine
with M unknowns (where N>M). function from 0 to twice PI and plot it with an
Solve it by MATLAB equal interval of 30 degree.
Thank you