Matlab - Tutorial - Part I

Matlab - Tutorial - Part I

Matlab – I

This is the first edition of a series of forthcoming tutorials in Matlab.

Objective
To introduce the beginner to Matlab, Discuss Applications of Matlab & to equip him with basic knowledge required to work with Matlab.

so what is Matlab?

Matlab stands for MATrix LABoratory. It is an application for performing mathematical calculations required in different domains. It has extreme capabilities to work with multi dimensional arrays making it the tool of choice in processing for a variety of applications.

Applications of Matlab

Matlab finds applications in every domain from accounting to aerospace engineering. Our interests being in engineering, lets see some of the things it can do for us
• Help process complex signal data
o Process Images
o Process Video
o Process Audio
• Help Perform Engineering Calculation
o Calculate Angles
o Calculate Dimensions
o Calculate Area of Complex Shapes
o Map Characteristics in a multi-axis graphs/ curves
Well that isn’t an exhaustive list but that’s some of the things we will do over a period of time in this tutorial.

Section I - Getting Started with Matlab

Launch Matlab by clicking on the appropriate start menu programs. Once Matlab is open, you should see a central window named command window,
with a >> prompt.
This is the Matlab command prompt. You will be typing your commands here.

Now let's try a simple command. Type

>> a =2;
>> b = 3;
>> a + b

and Matlab will show something like ans = 5

If you got that, Congrats!, that’s the first step into the world of Matlab.
Now, if you notice towards the Right Hand Side at the top, you will find a pane named Workspace. Here you will notice a,b and ans listed with their corresponding values. This pane shows all active variables.

Now let’s try to clear one of the values. Type
>>clear a

You will see that the variable a has disappeared.

Exercise

Try replacing the arithmetic operator in the above commands.

Section II – Working with Matrices

Let’s take our learning a bit further now and work with matrices

First, let’s create a matrix. Type

>>clear all – This will clear all the variables we had used earlier.
>>a = [1 2; 3 4] – This creates a 2x2 Matrix.

Let’s see how we can create a 2x3 Matrix now

>>a= [1 2; 3 4; 5 6;] – There you go! This will create a 2x3 Matrix

Let’s move on to Matrix Operations and see how we can multiply all the values in the matrix by a constant number
>> a * 5 – This will multiply all elements of a by 5
Good! Let’s try to multiply 2 matrices

>> b = [3 4; 5 6]
>> a + b
– Observe the result. The result is a pure matrix addition output.

Now we'll change this
>>a * b – This will produce pure matrix multiplication output

Now try this
>>a .* b – This will produce a multiplication result where every element in a specific position in matrix a is multiplied with another element in the same position in matrix b. This will work only with matrices of the same dimensions.

So, how do we invert a matrix, simple, type
>> a’ - this will create an inverse of the matrix

We’ll stop here for this edition of the commands. Here is some interesting stuff for you to try out



Commands for you to try:



Online help:


help


Lists topics on which help is available


helpwin


Opens the interactive help window


helpdesk


Opens the web browser based help facility


help topic


Provide help on topic. Try replacing ‘topic’ with a command
of your choice.


lookfor string


Lists help topic containing string


demo


Runs the demo program


 



Workspace information:


who


Lists variable currently in the workspace


whos


Lists variable currently in the workspace with their size


what


Lists M-,Mat- and max-files on the disk


clear


Clears the workspace ,all  variables are removed


clear x y z


Clears only variables x,y and z


mlock fun


Locks function fun so that clear cannot remove it


munlock fun


Unlocks function fun so that clear can remove it


clc


Clears command window, command history is lost


home


Same as clc


clf


Clears figure window

 



 Directory information:


pwd


Shows the current working directory


cd


Changes the current working directory


dir


Lists contents of the current directory


is


Lists contents of the current directory, same as dir


path


Gets or sets MATLAB search path


editpath


Modifies MATLAB search path


copyfile


Copies a file


mkdir


Creates a directory

 



General information:


computer


Tells you the computer type you are  using


clock


Gives you wall clock time and date as a vector


date


Tells you the date as a string


more


Controls the paged output according to the screen size


var


Gives you the license and the MATLAB version information


bench


Benchmarks your computer on running MATLAB compared to other
computers

 



Termination:       


^c(control-c)


Local abort, kills the current command execution


quit


MATLAB


exit


Same as quit

 

 Random
Matrix Generation:


zeros(M,N)


generates an M x N matrix of zeros


ones(M,N)


generates an M x N matrix of ones


rand(M,N)


generates an M x N matrix whose entries are uniformly
distributed random numbers in the interval [0:0; 1:0]