Professional Documents
Culture Documents
Computer Programming
Computer Programming
YOUSAF KHAN
ROLL #
2018-CE-01
MATLAB
MATLAB stands for matrix laboratory.
o High-performance language for
technical computing
o Integrates computation,
visualization, and programming in
an easy-to-use environment where
problems and solutions are
expressed in familiar mathematical
notation.
MATLAB
The MATLAB Application:
Optimization Toolbox
o Includes standard algorithms for optimization
o Including minimax, goal attainment, and semi-
infinite minimization problems
MATLAB Tool Box
Curve Fitting Toolbox
o Allows you to develop custom linear and nonlinear models in a graphical user interface.
o Calculates fits, residuals, confidence intervals, first derivative and integral of the fit.
Another Tool boxes :
o Communications Toolbox
o Control System Toolbox
o Data Acquisition Toolbox
o Database Toolbox
o Image Processing Toolbox
o Filter Design Toolbox
o Financial Toolbox
o Fixed-Point Toolbox
o Fuzzy Logic Toolbox
Introduction to Simulink
o Simulink is a graphical, “drag and drop” environment for building
simple and complex signal and system dynamic simulations.
o It allows users to concentrate on the structure of the problem,
rather than having to worry (too much) about a programming
language.
o The parameters of each signal and system block is configured by the
user (right click on block)
o Signals and systems are simulated over a particular time.
MATLAB File Extensions
• .fig
MATLAB Figure
• .m
MATLAB function, script, or class
• .mat
MATLAB binary file for storing variables
• .mex
MATLAB executable (platform specific, e.g. ".mexmac" for
the Mac, ".mexglx" for Linux)
Connecting to MATLAB
Deploying with MATLAB
MATLAB History
o In the mid-1970s, Cleve Moler and several colleagues
developed 2 FORTRAN libraries
• LINPACK for solving linear equations
• EISPACK for solving eigenvalue problems.
o In the late 1970s, Moler, “chairman of the computer
science at the University of New Mexico”, wanted to
teach students linear algebra courses using the LINPACK
and EISPACK software.
o He didn't want them to have to program in FORTRAN,
because this wasn't the purpose of the course.
MATLAB History
o He wrote a program that provide simple interactive
access to LINPACK and EISPACK.
o Over the next years, when he visit another university, he
leave a copy of his MATLAB.
o In 1983, second generation of MATLAB was devoloped
written in C and integrated with graphics.
o The MathWorks, Inc. was founded in 1984 to market and
continue development of MATLAB.
MATLAB Environment
• MATLAB Desktop
MATLAB Environment
Command window
• save filename % save data from workspace to a file
• load filename % loads data from file to a workspace
• who % list variables exist in the workspace
• whos % list variables in details
• clear % clear data stored in the workspace
• clc % clears the command window
• ctrl+c % To abort a command
• exit or quit % to quit MATLAB
MATLAB Environment
Command window
• Through Command window:
o help command
Ex: >> help plot
o lookfor anystring
Ex: >> lookfor matrix
• Through Menus: (Using help window
o doc command
Ex: >> doc plot
MATLAB Variables
• To create a variable, simply assign a value to a name:
»var1=3.14
»myString=‘hello world’
• Variable name must start with letter.
• It is case sensitive (var1 is different from var1).
• To Check the variable name validation “isvarname [name]”
o isvarname X_001
o isvarname if
• To check the Max length supported by current MATLAB version
“namelengthmax”
MATLAB Variables
o MATLAB is a weakly typed language
(No need to declare variables!)
o MATLAB supports various types, the most often used are
»3.84
64-bit double (default)
»‘a’
16-bit char
o Most variables are vectors or matrices of doubles or chars
o Other types are also supported:
complex, symbolic, 16-bit and 8 bit integers.
MATLAB Environment
VARIABLES
• Variable can’t have the same name of keyword
o Use “iskeyword” to list all keywords
• Built-in variables. Don’t use these names!
o i and j can be used to indicate complex numbers
o Pi has the value 3.1415
o ans stores the last unassigned value (like on a calculator)
o Inf and –Inf are positive and negative infinity
o NaN represents ‘Not a Number’
MATLAB Environment
VARIABLES
• Warning:
MATLAB allows usage of the names of the built in function.
This is dangerous since we can overwrite the meaning of a
function.
• To check that we can use:
>> which sin ...
C:MATLABtoolboxmatlabelfun...
>> which ans … ans is a variable.
MATLAB Variables
1-Scaler:
• A variable can be given a value explicitly
»a = 10
shows up in workspace!
• Or as a function of explicit values and existing
variables
»c = 1.3*45-2*a
• To suppress output, end the line with a semicolon
»cooldude = 13/3;
MATLAB Variables
2-Array:
• Like other programming languages, arrays are an important
part of MATLAB
• Two types of arrays
1) Matrix of numbers (either double or complex
2) Cell array of objects (more advanced data structure)
MATLAB Variables
Row Vector:
• comma or space separated values between brackets
»row = [1 2 5.4 -6.6]
»row = [1, 2, 5.4, -6.6];
• Command window:
>> step= 1;
>> x= startP : step : endP;
MATLAB Variables
Matrix:
• Make matrices like vectors
• Element by element
» a= [1 2;3 4];
• By concatenating vectors or matrices (dimension matters)
MATLAB Special matrices
• ones:
>> x = ones(1,7) % All elements are ones
• zeros:
>> x = zeros(1,7) % All elements are zeros
• eye:
>> Y = eye(3) % Create identity matrix 3X3
• diag:
>> x = diag([1 2 3 4],-1) % diagonal matrix with main diagonal
shift(-1)
MATLAB Special matrices
• magic:
>> Y = magic(3) %magic square matrix
• rand:
>> z = rand(1,4) % generate random numbers
from the period [0,1] in a vector 1x4
• randint:
>> x = randint(2,3, [5,7]) % generate random integer
numbers from (5-7) in a matrix 2x3
MATLAB Operations
• Arithmetic Operators: + - * / ^ ‘
• Relational Operators: < > <= >= == ~=
• Logical Operators: Element wise & | ~
• Logical Operators: Short-circuit && ||
• Colon: (:)
Operation Order :
MATLAB Operations
• Addition and subtraction are element-wise ‛ sizes must match‛:
• Picking submatrices:
Vectors & matrix operations
Indexing:
• To select rows or columns of a matrix: Indexing:
Vectors & matrix operations
Indexing:
• To get the minimum value and its index:
»[minVal , minInd] = min(vec);
maxworks the same way
• To find any the indices of specific values or ranges
»ind = find(vec == 9);
»[ind_R,ind_C] = find(vec == 9);
»ind = find(vec > 2 & vec < 6);
Vectors & matrix operations
Deleting Rows & Columns:
>> X =[ 16 3 2 13 ; 5 10 11 8 ; 9 6 7 12 ; 4 15 14 1 ]
>>X(:,2) = [] % delete the second column of X
X=
16 2 13
5 11 8
9 7 12
4 14 1
MATLAB Programming
Relational Operators:
•MATLAB uses mostly standard relational operators
equal ==
notequal ~=
greater than >
less than <
greater or equal >=
less or equal <=
• Logical operators element wise short-circuit (scalars)
And &&
Or ||
Not~
Xor xor
•Boolean values: zero is false, nonzero is true
MATLAB Programming
If / else / elseif :
• Basic flow-control, common to all languages
• MATLAB syntax is somewhat unique
• Unlike the C language switch statement, MATLAB switch does not fall
through.
If the first case statement is case statements do not execute.
A= input( ‘ Matrix = ‘ )
try
B = inv (A);
Catch
msgbox(‘Matrix is not square’)
end
MATLAB Programming
User-defined Functions:
• syms x t
• x = sin(t)*exp(-0.3*t);
• sym(2)/sym(5)
• ans =
• 2/5
• sym(2)/sym(5) + sym(1)/sym(3)
• ans =
• 11/15
Benefit of MATLAB
• The benefits of MATLAB is that everything is stored in
matrices , make our analysis easy.
• Easy syntax, practically the same as for tan mist MATLAB user
are previous for tan easies.