Monday, September 9, 2013

MATLAB Basic Tutorial

Good morning everyone. Hope everyone is okay. Today I'm going to write about MATLAB, a very popular and handy application for doing a lot of works. It is a numerical computing environment.

So, lets dive in!

First of all how to get it. You can simply go to their site for downloading/buying it. Once you have done doing that, install it. And run it.

The interface will be like this.


In the picture you can see the MATLAB 2012b edition. It has File browser , Command window, Workspace, Command History from left to right. You will be writing codes in the command window area.

Let's start with the Basic Arithmetic Operators then.

Arithmetic Operators

1.   +   Addition

Simply add two or more digits by directly typing all the digits and pressing enter. Result can be directly stored in any variable 

12+23.5= 35.5000

d=1222+45, d=1267
[You can see the variable values in workspace]

2.   -   Subtraction

Simply subtract. Either it can be done directly by typing and hitting enter or can be stored in variable first and then apply operator.

23-5=18

b=23-5 , b=18 (stored in b variable)

3.   *   Multiplication

Same as before.

47*89= 4183

e=45*65, e=2925

4.   /   Right Division

Same as before.

5.   \   Left Division

Same as before.

6.   :   Colon 

This can be used for creating list.

list = -3 : 3 will result in a list from -3 to 3.
list = 1 : 2 : 20 will result in a list of odd numbers. This syntax means it will start from 1, with an increment of 2 it will end at 20.

7.   ^  Power

Same as before.

8.   '   Transpose

Check the wiki link I have added. It will give you a clear knowledge about Transpose.

a = [3 5 9]  will result in a Row vector of 3 5 9.
but a = [3 5 9]' will result in a column vector because of the Transpose Operator.


Let's look at the examples now.





Now, Let's do some Vector and Matrix Stuff!

1. Constructing Row Vector

a = [1 5 9 4 3 6]
Will result in a Row vector a with the following elements mentioned above.

Extracting a certain element-

a(3) = 9 
It will find the third element of a.

c = a(3)
Value of third element will be stored in c variable.

Change any certain element.

a(3)=8
Third element will be 8.

Deleting certain element.
a(5)=[]
it will delete the fifth element of the vector a.
[] is the empty matrix whose size is 0x0.



2. Constructing Column Vector

b = [5; 8; 7]
b = [5 8 7]' 
second line Transpose operator has been used.




3. Constructing Matrix.

A = [ 2 4 3 ; 5 2 9 ; 3 1 7]

Extracting elements from matrix.

A(3,2)
1

Results in A32 element.

Extracting Sub-Matrix

A( 1:2 , 2:3 )

Extracting Certain Row

A(3,:)
Here ":" means all elements in the array.

Appending.

Append a certain value to  the matrix.
Sizes of two arrays must be compatible otherwise will result in error.

Deleting certain Row or Column.

A( : , 3) = []
A( 2 , : ) = []

Note that earlier we have used A( 3,:) for extracting certain value.
Now we will be storing 0x0 in that location that is ultimately Zero.






4. Time for some Matrix Calculation.

Construct Matrices as shown earlier.

Assume they are A and B.

Using arithmetic operators like + - * we can compute A+B , A-B or A*B matrices.

Note :

1. ";" sign after a line will stop MATLAB to print on the command Window.


2. Anything written after "%" sign will be ignored.



No comments:

Post a Comment