Introduction To Matlab
Introduction To Matlab
Introduction To Matlab
The name “Matlab” stands for matrix laboratory. Matlab is a high performance
computing language. Matlab has many advantages compared to conventional
computer language for solving technical problems.
1. Starting Matlab: -
When you start Matlab a special window called the Matlab desktop appears. The
Desktop is a window that contains other windows, the major tools within or
accessible from the desktop are:
1. The Command window (>> prompt)
2. The Command History
3. The Workspace
4. The Current Directory
For example you want to calculate 1+2 x 3
>> 1+2*3
Ans.
=7
You will notice that if you don’t specify an output variable, Matlab uses a default
variable ans.
To avoid this, you may assign a value to a variable or output argument name e.g.
>> X = 1+2*3
X
=7
4. Error messages:-
If you enter an expression incorrectly, Matlab will return an error messages.
>> X = 10;
>> 5X (incorrect)
>> 5*X (correct)
To make corrections, we can retype the expressions. But if the expressions is
lengthy, you can recalled the command with the up – arrow key ↑. When command
is prompt, it can be modified if needed and executed.
Controlling the hierarchy (order) of operation:-
Ex: 1+2x3
>> (1+2)*3 Ans = 9
>> 1+2*3
>> Ans = 7.
The order in which Matlab performs arithmetic operations is exactly that taught in
high school algebra courses.
8. Getting help:-
Information about any command is available by typing
>> help command
The help command searches for an exact function name match
>> help inv
The lookfor command differs from the help command. The help command
searches for an exact function name match while the lookfor command searches
the quick summary information in each function for match.
>> help inverse (No result)
On the other hand the command lookfor inverse will produce detailed information
which includes the function of interest.
>> Lookfor inverse
The doc function opens the on – line version of the help manual.
>> Doc plot
9. Mathematical function:-
There is a long lost of mathematical function that are built into Matlab. These
functions are called built – ins. Many standard mathematical function such as
sin(x), cos(x), tan(x), 𝑒 𝑥 , ln(x) are evaluated by the functions sin, cos, tan, exp, log
respectively
Cos(x) cosine
Sin(x) sine
Tan(x) tangent
Acos(x) Arc cosine
Asin(x) Arc Sine
Exp(x) exponential
Sqrt(x) square Root
Log(x) Natural log
Log10(x) common logarithm
In addition to elementary function Matlab includes a No. of predefined constant
values e.g. pi, inf, Nan
Typing help elfun and help specfun calls up the lists of elementary and special
functions respectively.
e.g.
𝑦 = 𝑒 −𝑎 sin 𝑥 + 10√𝑦 for a = 5, x = 2, y = 8
>> a = 5 ; x = 2; y = 8;
Y = exp(-a)* sin(x)+10*sqrt(y)
Y = 28.2904
10. Ploting:-
In 2D, Matlab graph we have to take a vector of x coordinates x = (x1, ---------- xN)
and a vector of y – coordinates y = (y1 ---------- yN) you need to prepare x and y in
an identical array form, namely x f y are both row arrays and column arrays of
same length.
The Matlab command to plot a graph is
Plot (x , y)
Ex.
X = (1,2,3,4,5,6)
Y = (3,-1,2,4,5) To proceed
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot (x , y)
Plot (x , y) produces graph of y versus x.
Ex 2.
To plot the function sin(x) on the interval [0, 2π]. Use first create a vector of x
values ranging from o to 2π, the complete the sine of these values and finally plot
the result.
>> x = 0 : pi/2 : 2*pi;
>> y = sin(x);
>> plot (x,y)
0 : pi/100 : 2*pi yields a vector that
Starts at 0
Takes increment of π/100
Stops when 2π is reached
If you omit the increment, Matlab automatically increments by 1.
Adding titles, axis labels
MATLAB enables you to add axis labels and titles.
Note the character |pi creates the symbol π
>> xlabel (‘x = 0:2/pi’)
>> ylabel(‘sin of x’)
>> title (‘plot of the sine function’)
17. Dimensions:-
To determine the dimensions of a matrix or vector use the command size.
Example:-
>> Size (A)
Ans =
3 3
Means 3 rows and 3 columns
22. Determinant:-
The determinant of A is
>> det (A)
Ans =
27