Thursday, June 9, 2011

Simple Plots in Matlab

Simple Plots in Matlab

This is a continuation of the earlier post Matlab Basics
Creating Plots in Matlab is probably the easiest in all possible options present in the Software Universe. We shall study only the most basic methods in this Post and then advance to comparatively complex features.

Create a Vector for representing the X - Axis
>> x=[-2*pi:.001:2*pi];
>>
>> y=sin(x);
>> plot(x,y)
>> 




>> plot(cos(x),sin(x))
>>

 


 
>> plot(cos(x),sin(x).*cos(x))
>> 
%Remember the .* represents the dot product, and is necessary because all variables in Matlab are vectors and by default it will try to multiply them as matrices.


Next, we shall advance to more complex formatting of Plots, and then 3D plots.


Creating Variables and Simple Expressions in MATLAB

Basics of Matlab
The easiest way to begin learning Matlab is to start with the Command Window under the Desktop Menu.

Executing expressions in Matlab is absolutely simple !!! Just write the expression and view the results.

1) >> 3+4/10

ans =

    3.4000

>> 


To create a Variable

Simply write the expression
>> x=10

x =

    10

>> x*x + x

ans =

   110

>>

The variable is created automatically and initialized


To view the created variables write
>> x=1,y=2

x =

     1


y =

     2

>> who

Your variables are:

ans  x    y   


Point to remember: There are no scalars in Matlab. Only vectors. Thus x would  actually be [x].
Point to remember: To suppress an output use ; at the statements end.
Point to remember: use clc to clear the display.

>> x=5;
>> x=5

x =

     5

>> 




Creating Vectors

Vectors can be created in the following manners :
>> a=[1,2,3,4,5]

a =

     1     2     3     4     5

>>



>> a=[1:1:10]

a =

     1     2     3     4     5     6     7     8     9    10

>>



The first case is a simple enumeration, the second case specifies the starting value, the increment/decrement and the last value.

>> a=[10:-1:1]

a =

    10     9     8     7     6     5     4     3     2     1

>>


We shall learn 2D and 3D plotting next.