Basics in Matlab-1

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

What is MATLAB?

MATLAB is a numerical computing environment


as well as programming tool

Matlab is a commercial Matrix Laboratory


Package and it operates as an interactive
programming environment with the graphical
output.
History
MATLAB was invented in the late 1970s by Cleve
Moler, then chairman of the computer science
department at the University of New Mexico.

The purpose was to use certain software packages


without any need to learn Fortran.

In 1984 MATLAB was rewritten in C language.

In Engineering MATLAB was first used by


Control System engineers.
Certain Features of MATLAB
It Allows:
Easy matrix manipulation
Plotting of functions and data
Implementation of algorithms
Creation of user interfaces
Interfacing with programs in other languages etc.
It can work with the UNIX Platform as well as
with Windows.
In Matlab almost every data object is assumed to
be an array.
ENTERING AND RUNNING MATLAB

On a system running Windows double click on the


MATLAB icon to launch and a command window
will appear with the prompt.

You are now in MATLAB. From this point on,


individual MATLAB commands may be given at
the program prompt.
Command Window
It is the primary user interface
type commands
Workspace
view program variables
clear to clear
double click on a variable to see it in the Array
Editor
Command History
view past commands
save a whole session using diary
Launch Pad
access tools, demos and documentation
The Command window is where you
can submit commands to MatLab and
run programs.
The workspace is a convenient way to
look at variables youve created.
LEAVING MATLAB
A MATLAB session may be terminated by
simply typing
>> quit
or by typing
>> exit
at the MATLAB prompt.
Or simply clicking the cross button
Matlab statements can be prepared in the Editor
Window and stored in a file called Script file or
M file". (since there should be a name extension
of the form filename.m)

Demonstrations of various Matlab commands can


be explored using demo.

Help command can also be used to explore the


Matlab functions in a more specific way.
ONLINE HELP
>> help
[a long list of help topics
follows]
For specific commands:
>> help demo
A demonstration of various MATLAB
functions can be explored with:
>> demo
A performance test of the machine running
MATLAB can be done with:
>> bench
VARIABLES
MATLAB has built-in variables like pi, ans etc.
You can learn their values from the
MATLAB interpreter.
>> pi
ans =
3.1416
>> help pi
PI 3.1415926535897....
The variable ans will keep track of the last output
which was not assigned to another variable.
Variable Assignment : The equality sign is
used to assign values to variables:
>> x = 3
x =
3
y =
9
Variables in MATLAB are case sensitive.
Hence, the variables "x" and "y" are distinct
from "X and "Y" (at this point, the latter
are in fact, undefined).
MATLAB variable name must be one word.
Underscore_ must be used if variable
name exceeds one word.
Variable name can contain up to 63
characters while beyond 63rd are ignored
If at any time you forget your variables, you
can use the command who (who my
variables are).
To remove a variable, use command
clear x (variable).
Semicolon
The semicolon (';') has somewhat unexpected use
in m; it is not required to terminate commands as
in Java, C++ and others.

Output can be suppressed by appending a


semicolon to the command lines.
>> x = 3;
>> y = x^2;
>> y
y =
9
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division (left and right)
^ power
VARIABLE ARITHMETIC
MATLAB uses some fairly standard
notation. More than one command may be
entered on a single line, if they are
separated by commas.
>> 2+3;
>> 3*4, 4^2;
Order of Precedence
Powers are performed before division and
multiplication, which are done before
subtraction and addition
Example
>> 2+3*4^2;
generates ans = 50. That is:
2+3*4^2 ==> 2 + 3*4^2 <==
exponent has the highest
precedence
==> 2 + 3*16 <== then
multiplication operator
==> 2 + 48 <== then addition
operator
==> 50
BUILT-IN MATHEMATICAL FUNCTIONS
There are more than 300 built in functions in
MATLAB out of which some mathematical
functions are:
sin ;sin(pi) = 0.0
cos ;cos(pi) = -1.0
tan ;tan(pi/4) = 1.0
Arcsine; asin(pi/2)= ???
Arccosine; acos(pi/2)= ???
arctangent; atan(pi/4)= ???
Exponential; exp(1.0) = 2.7183
natural logarithm; log(2.7183) =
1.0
logarithm base 10 log10(100.0) =
2.0
An example of built-in mathematical function is
taking a square root of any no and assign the result
to the variable. e.g. a=sqrt(2) or b=sin(pi/2) or
c=log(30)

Lets verify that sin(90)^2+cos(90)^2=1


Matrices
A Matrix is a rectangular array of numbers, every
Matlab variable we define refers to a matrix [ a
single no is a 1 by 1 matrix]
There are many applications of matrixes related to
the field of Engineering e.g. families of equations
representing the state of the system( Equilibrium
equations, Energy & momentum conservation
equations). These all can be easily analyzed using
matrixes.
Due to the above stated facts MATLAB helps us in
defining the matrices and their solution.
Example
Here is an example which shows how a
matrix is define in a MATLAB:
a=[1 2 4;5 8 2;7 4 1] (3 by 3 order)
similarly
b=[4 5;9 0] (2 by 2 order)
or in other way
c=[2 8 4;0 2 1] (2 by 3 order).
Operations on Matrices
Matrices can be added or subtracted using
Matlab but their order should be same ,
some examples using command window
In the same way we can multiply two
matrixes but the product should obey the
law of multiplication of matrixes , some
examples using command window

You might also like