9:18 AM

Introduction to simulink

That was more or less what we need to know for basic matlab operations.Hope it helped.I will keep posting more fun facts as and when I can remember them.Please feel free to suggest,comment or discuss.I know this isn't any blog with good piece of writings...

Matlab supports a software called simulink.Simulink is where you can design various models and check the working of the systems.

You will get a shortcut for simulink in the matlab shortcut.Click it.Or you can type simulink in the command window.

The window for simulink opens .To the left you can see libraries.At the bottom you can see the block description.To the right are the various blocks for the selected blockset from the libraries.

5:39 AM

Plotting a 3-D graph

So we've learnt how to plot a 2-D graph.Now here we are to plot a 3-D graph.In this case we require three variables x,y,z.

where x,y,z are mutually dependent variables or at least one of the depends on both the other variables.

1.surf(x,y,z)
plots the surface formed by x,y and z interdependent variables.

2.meshgrid()

A. This function is used to manipulate and arrange the coordinates in a matrix form.
If you want to specify the range of x only,where y and z depends on the value of x:

x=meshgrid(-2:0.1:2);
and this will give you the 5x5 matrix with the column elements of each row being -2:0.1:2.

let's draw a parabola out of this in 3-D.Since the general equation for a parabola is y=x^2 and this is not matrix multiplication but element-by-element multiplication of x co-ordinates.for more reference:

hence:
y=x.^2; %element-by-element multiplication
z=x.^2; %element-by-element multiplication

%plotting the parabola
surf(x,y,z);
%label the axes
xlabel('x');
ylabel('y');
zlabel('z');

and you get the 3-D plot.


B.If you want to specify the range of x and y and z depends on both of them then:

[x,y]=meshgrid(-2:0.1:2,-2:0.1:2);

let's try to draw this parabola:
z=x.^2+y.^2; %element-by-element multiplication

%plotting the parabola
surf(x,y,z);
%label the axes
xlabel('x');
ylabel('y');
zlabel('z');



2.Checking the plot and playing with them:

When you get the figure,go to tools->Rotate 3-D.You will find that the cursor has changed to a spiral one.Hold the left mouse button(right-handed person) on the picture and rotate your plot to see from various directions.


3.plotting a sphere:

sphere(n)

will plot a sphere of unit radius withn-by-n faces.
eg., sphere(20)


increase the number and see the effect.You can also try meshgrid to draw a sphere of desired radius in the desired location.
PLEASE LEAVE A COMMENT :)

1:24 PM

Working with workspace

Go to file->new->M-File

Write your function and programming here.And best of luck...
PLEASE LEAVE A COMMENT :)

1:20 PM

defining a Function

A function is a programming or a part that you build and store it in the matlab saved file location.Now this function can be called by other functions or can be run by the user to check for the output.

Thus a function has the following parts:

1.The reserved word 'function'
2.The function name
3.The input variables(optional)
4.The returned values(optional).

Hence the syntax is:
function [rvar1 rvar2...rvarn]=function_name (ivar1,ivar2...,ivarm)

%the programming

end

The input variables:

The input variables are the variables that you may require for computing and manipulating purposes.Here in the syntax they are assigned to as ivar1,ivar2,...,ivarm for m variables.

The returned variables:

Te returned variables are the variables that you want to get as the output after the execution of the function.Here in the syntax they are assigned to as rvar1 rvar 2 ...rvarn fo n returned variables.

**Calling a function

You can simply write the name of the function and type in the values for the input variables in correct order and of correct number of variables(if there are any).
1.You can use it in another function hence rite it in another function.
2.You can run the function in the command window by the same method.

Here is an example where the function takes a complex number as an input and gives the absolute value and the phase value or theta of the complex number as the output.


function [absolute,theta]=to_polar(n)

absolute=abs(n)
p=angle(n);
theta=(p/pi)*180

and when you type:
to_polar(9+5i)

in the command window and run it to see the answer.
PLEASE LEAVE A COMMENT :)

9:07 AM

Falling into infinity(loops and conditions)

Well this is an album by DREAM THEATER,my favourite.What I am going to discuss here is how to write loops.As you can see that matlab is quite fun and easy to use.The syntax is not much of a problem.And if you are wrong somewhere,matlab will tell the possible reasons.Te syntax is quite flexible and if you know C,C++ or the like,matlab is 'as easy as pie'.

1.Whenever you want to start a loop you need to write 'end' at the end to denote its 'end'.Same applies for conditions also.So if you want to check if x is equal to 5 then change it to 6:

if x==5
x=6;
end

so the condition is checked here.The condition ends with an 'end'.Since you are checking if it's equal or not you are using two equal(=) signs.(One equal sign(=) would mean that you are assigning the value of x already-True for C,C++ not for matlab).Matlab will show an error.lets do the above case again:

if x=5
x=6;
end  %this will show an error

and if you have more than one conditions so do it like this:

if x==5
%statement;
elseif x==6
%statement;
end

2.for loop:

the syntax is:

for var=i:j:k
%statements;
end

var stands for a variable name.
i=starting value
j=increment
k=stop value

If no value is given for j then by default it is taken to be 1.

eg., for i=1:2:10
i
end
gives the answer:

i =

     1


i =

     3


i =

     5


i =

     7


i =

     9
3.while loop:

the syntax is:
while (condition)
%statements;
end

while loop executes until the condition is false whereas for loop executes until the variable name reaches the stop value.
eg.,

while i<5
i=i+1
end

gives

i =

     2


i =

     3


i =

     4


i =

     5
PLEASE LEAVE A COMMENT :)

4:44 AM

Matrix operation

1.If you want to add/subtract two matrices just adding them will do provided they are of same dimensions.
eg.,

a=[1 2;3 4];
b=[5 6;7 8];

 c=a+b gives

c =

     6     8
    10    12

2.Multiplying two matrices can also be done in a similar way so is dividing one matrix by another.
eg.,
c=a*b %provided num_of_columns of a=num_of_rows of b
gives

c =

    19    22
    43    50
 and for division
c=a/b %this is actually a*inverse(b)
gives you
c =

    3.0000   -2.0000
    2.0000   -1.0000

3.In fact you can solve linear equations using matrix form.This can be done by Martin's rule.eg.,
if the three equations are-
6x+8y+z=8;
4x+7y-5z=5;
10x+6y-9z=14;

if the equation can be represented as AX=B where A=[6 8 1;4 7 -5;10 6 -9]

A =

     6     8     1
     4     7    -5
    10     6    -9
i.e.,the coefficients of the variables and X=x
                                                                y
                                                                z
and B=8
           5
           14
then the solution can be found out by Martin's rule:X=B.inverse(A)
so if you type X=B/A gives you the desired solution:

X =
    2.7472   -2.5843    0.1854

4.However if you want to do element-by-element operation you need to do a little variation.

Suppose you want to square each number of a matrix x=1 2
                                                                                      3 4

simply doing x^2 will multiply the whole matrix with itself.Hence for element-by-element operation you need to do the following

>> x.^2

The dot(.) signifies element-by-element operation.So if you want to divide one matrix by another element-by-element simply write
x./y.Therefore the dot(.) must appear before every operational sign.


5.Transpose of a matrix.
X' gives you the transpose of the matrix X.
PLEASE LEAVE A COMMENT :)

5:53 AM

THE MOST IMPORTANT OF ALL

It's good to see that everyday someone or the other person is having a look at my programmings.I am taking exams so I apologize for not posting any more programmings or tutorials.I will surely do that after my exams are over.But please don't stay silent.Say,suggest,comment or discuss anything you like.I would really be happy.I shall be posting more interesting stuffs I have learnt.But I need your help.

SUGGEST,COMMENT OR DISCUSS ANYTHING SO THAT I CAN KNOW THAT MY PROGRAMMINGS ARE HELPING YOU, OR MAY BE NOT...

10:26 AM

MID-POINT RULE

A friend from yahoo answers asked me to write this program.So here it is:


function mid_point_rule

F=input('enter the function: ','s');
F=inline(F);

a=input('Enter the lower limit: ');
b=input('Enter the upper limit: ');
n=input('Enter the number of rectangles to be used(even number): ');

del_x=(b-a)/n;

sum=0;
for i=1:n
    x1=a+i*del_x;
    x0=a+(i-1)*del_x;
    x_mean=(x1+x0)/2;
  
    sum=sum+F(x_mean);
end
integ=del_x*sum;
fprintf('\nHence the integration is %d\n',integ);


Main formula:


Midpoint Rule:
where,






PLEASE LEAVE A COMMENT :)