Friday, May 8, 2015

How To use Matlab with examples

In the book Matlab An Introduction with Application good examples are provided for matlab beginners. Here few of the examples provided in the book are shown for review.

1. Left and Right Division

There are two division operators in Matlab. One is the left division() and the other is right division(/).

One application of left division() is in solving system of linear equation.

For example- AX = B, therefore X = BA

eg-
2x-4y+2z = 3;
6x+2y-4z = 5;
9y+8z = 7

then, we can implement and solve in matlab as,

>> A = [2 -4 2;6 2 -4;0 9 8];
>> B = [3;5;7];
>> X = AB

X =

    1.2129
    0.1881
    0.6634

On the other hand using right divider, we have-

>> A = [2 -4 2;6 2 -4;0 9 8];
>> B = [3 5 7];
>> X=B/A

X =

    1.2129    0.1881    0.6634

2. How to perform element wise multiplication in matlab?

A dot is used to perform element wise operation in matlab, for example-

>> a = [1 2 3];
>> b = [4 5 6];
>> c = a.*b

c =

     4    10    18

>> c = a./b

c =

    0.2500    0.4000    0.5000

>> c = a.^b

c =

     1    32   729

3. How to use disp command in matlab?

disp command is used to display variables or string to the command screen,

for example to display variable-

>> a = [1 2 3];
>> disp(a)
     1     2     3

to display string using

>> disp(text)
text

4. how to use fplot matlab command
fplot matlab command is used to plot function which can be either directly typed into the argument of the fplot commmand, or the function can be user-defined or can be matlab inbuild function.

for example, plotting y(x) = sin2x is- >> fplot(sin(2*x),[-4,4])


5. How to plot multiple functions on the same plot in matlab?

There are two ways to plot multiple functions on the same plot in matlab.

The first one is use multiple vectors inside the plot command and the other is use hold on command and plot multiple function on the same graph.

Using vectors:
>> t = [0:0.1:10];
>> x = sin(t);
>> y = sin(2*t);
>> z = sin (3 *t);
>> plot(t,x,-b,t,y,-r,t,z,-g)

Using hold on:

>> t = [0:0.1:10];
>> x = sin(t);
>> y = sin(2*t);
>> z = sin (3 *t);
>> plot(t,x,-b)
>> hold on
>> plot(t,y,-r)
>> plot(t,z,-g)
>> hold
Current plot released

both method gives the following graph:


See more in Matlab Tutorials

Related Posts by Categories

0 comments:

 
Software Download - Powered By Blogger