matrix=math tricks ;)
Categories
- Basic Matlab Operations (19)
Blog Archive
Pages
Followers
About Me
- santrox
- Kolkata, West Bengal, India
- well hey this is me.i am here to kill time.thought of starting with sharing my matlab programmings-i am not an expert in this field...am learning in on the way but with your support i can reach o a greater height.feel free to share your views and queries :)
Search This Blog
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.
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:
Labels: Basic Matlab Operations
Write your function and programming here.And best of luck...
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
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 :)
Labels: Basic Matlab Operations
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 :)
Labels: Basic Matlab Operations
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
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 :)
Labels: Basic Matlab Operations