Matrix Laboratory Manual
MATLAB (MATrix LABoratory)
MATLAB is a high-performance language for technical computing. Computation,
Visualization, and Programming in MATLAB has an easy-to-use environment
Typical uses include:
• Math and computation
• Algorithm development
• Modelling, simulation, and prototyping
• Data analysis, exploration, and visualization
• Scientific and engineering graphics
• Application development, including Graphical User Interface building
MATLAB consists of following:
The MATLAB language: a high-level matrix/array language with control
flow statements, functions, data structures, input/output, and object-
oriented programming features.
The MATLAB working environment : The set of tools and facilities that you
work with as the MATLAB user or programmer, including tools for
developing, managing, debugging, and profiling
• Handle Graphics: The MATLAB graphics system. It includes high-level
commands for two-dimensional and three-dimensional data visualization,
image processing, animation, and presentation graphics.
• The MATLAB function library. a vast collection of computational
algorithms ranging from elementary functions like sum, sine, cosine, and
complex arithmetic, to more sophisticated functions like matrix inverse,
matrix eigenvalues, Bessel functions, and fast Fourier transforms as well as
special image processing related functions
Symbols in MATLAB
>> Prmpt
... Continue statement on next line
, Separate statements and data
% Start comment which ends at end of line
; (1) suppress output (2) used as a row separator in a matrix
: specify range
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
Relational Operators:- Math & Assignment Operators
Less Than < Power ^ or .^
Less Than or Equal <= Multiplication * or .*
Greater Than > Division / or ./
Greater Than or Equal >=
or \ or .\
Equal To ==
Not Equal To ~=
Frequently Used Functions in MATLAB:-
>> abs(x); % absolute value of x
>> exp(x); % e to the x-th power
>> fix(x); % rounds x to integer towards 0
>> log10(x); % common logarithm of x to the base 10
>> rem(x,y); % remainder of x/y
>> mod(x, y); % modulus after division – unsigned rem
>> sqrt(x); % square root of x
>> sin(x); % sine of x; x in radians
>> acoth(x) % inversion hyperbolic cotangent of x
>> ans %Default variable name for results
>> pi %Value of π
>> inf %Infinity
>>NaN %Not a number e.g. 0/0
>> sum % Find sum of numbers
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
Basic functions in MATLAB:
I) syms : Create symbolic scalar variables and functions, and matrix variables and
functions.
Syntax: 1) syms x y Create Symbolic Scalar Variables
2) syms a [1 4] Create Vector of Symbolic Scalar Variables
3) syms x Create and Evaluate Symbolic Functions
4) syms f(x,y) Create and Evaluate Symbolic Matrices as
Functions of Two Variables
II) Diff : Differentiate symbolic expression or function
Syntax:
1) df = diff(f) Differentiates f with respect to the symbolic scalar variable
determined by symvar(f,1
2) df = diff(f,n) computes the nth derivative of f with respect to the symbolic
scalar variable determined by symvar.
3) df = diff(diff(f)) or df=diff(f,2) Second derivative of the expression
III) subs - Symbolic substitution
Syntax
1) snew = subs(s,old,new)
2) subs(function, [oldvar1,oldvar2..], (newvar,newvar2..))
IV) fprintf: Write data to text file
Syntax
fprintf(‘textMessage’)
fprintf(‘formatString’, listOfVariables) f
Code Conversion instruction
%d format with no fractional part (integer format)
%e format as a floating-point value in scientific notation
%f format as a floating-point value
%g format in the most compact form of either
%f or %e %s format as a string
\n insert newline in output string
\t insert tab in output string
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
PLOTTING:-
MATLAB will plot one vector vs. another. The first one will be treated as the
abscissa (or x) vector and the second as the ordinate (or y) vector. The vectors
have to be the same length.
MATLAB will also plot a vector vs. its own index. The index will be treated as the
abscissa vector.There are commands in MATLAB to "annotate" a plot to put on
axis labels, titles, and legends. For example:
To put a label on the axes we would use:
>> xlabel ('X-axis label')
>> ylabel ('Y-axis label')
To put a title on the plot, we would use:
>> title ('Title of the plot’)
To distinct between function in the graph use:
>> legend(legend_1, legend_2)
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
Department of Mathematics KLE Institute of Technology