Sunday, August 3, 2008

Matlab - First week

Let us start with Simple calculations

1. Addition
2. Multiplication
3. division
4. Exponential(exp)
5. trigonometric (degrees must be converted into radians)( normal and arc normal fns)
6. Logarithimic log and log10

Some of the common functions that may be handy in dealing with many signal applications.

abs Absolute value
sqrt Square root
sign signum fn
conj conjugate of a complex number
imag imaginary part of a complex number
real real part of the complex number
angle phase angle of Complex Number
cos,sin,tan
exp
cosh, sinh,tanh, asin,asinh
round
floor
fix
ceil
rem --> remainder of division


Variable Name rule

Must have letters and digits
Start with a letter.
Special characters are not to be part of the name.
pi,i,j and e are reserved words
names of functions and Matlab commands cannot be used as variable names
variables are case sensitive.

All commands are to be entered in lower case.

Whenever a help is needed type "help command_name" in the command window

Vectors

Row vectors

U=[1 2 3];
V=[4,5,6];
U+V

W=[U,3*V]

sort(W) à sorting in ascending order

Colon notation
a:b:c à sequence of numbers starting with a and end with c in steps of b
1:0.5:2
1:10 automatically unit step size is assumed
1:-1:10 à shoots an error message

Can operate on a section of a vector

W=[1:5, 7:9]
W(4)

W(5:-1:1) à to reverse the vector

[M,N] = Size(W)

Column Vectors

X=[1;2;3];
Y=[4;5;6];
X+Y;

Z=X’ would do a Transpose on X

C=[1+i, 1-i]
D= C’
E=C.’

To store all texts
“diary filename”
diary on/off
save filename
load filename

"who" command displays all the variables used so far

whos --> More details on variables


Elementary Plots and Graphs

To generate a sine wave

N=30;
step_size=pi/N;
x=0:step_size:pi;
y=sin(x);
plot(x,y);
title(‘Sine Wave’);
xlabel(‘x’);
ylabel(‘sin(x)’);
grid
plot(x,y,’m+’)

To plot cos and sine fns
N=30;
step_size=pi/N;
x=0:step_size:2*pi;
plot(x,sin(x),’b-‘,x,cos(x),’m-.’);
legend(‘sine’,’cosine’);
grid
xlabel(‘x’);
ylabel(‘functions’);
title(‘Cosine and Sine Function Plot’);
hold off

Subplots

samples=30;
step_size=2*pi/samples;
x=0:step_size:2*pi;
subplot(121);
plot(x,cos(x));
xlabel(‘x’);
ylabel(‘cosine’);
grid;
subplot(122);
plot(x,sin(x));
xlabel(‘x’);
ylabel(‘sine’);
grid;


axis([0 10 0 100]) à scale the graph with x-axis in [0,10] and y-axis in [0,100] range

Matlab script file
always has a *.m extension.


Please see you have added the directory to the Matlab search path in which you are saving your files


Working with vectors and matrices

Significance of “.” (Element by element operation)
.+, .*, ./

To square a Matrix of size 3 x 3
A=[1 2 3; 4 5 6; 7 8 9];
A.^2

Experiment with A^2

U might have already studied Linear algebra(I guess)
If u have a matrix say U and you want to find the norm of U or the euclidean distance

We would use sqrt(U*U’);
Other way of finding this is norm(U);

How to find the angle between two vectors

Let X=[1 2 3] and Y=[4 5 6]
Angle between X and Y is theta = acos((X.Y)/(||X||*||Y||));

To tabulate the results
X=[0:pi/10:2*pi]’;
[X sin(X) cos(X)];

Other useful functions

zeros à vector elements with 1 as value
ones à vector elements with 1 as value
rand à random matrix
eye à Identity matrix
diag à diagonal matrix

for loop
while
if else elseif
input

Writing UDF
function output_arguments = function_name(input_arguments)
%comments go here

semilogy to change the axis to logarithmic y axis
disp(‘Hai’);

simplify((xˆ3 - yˆ3)/(x - y))
differentiation : diff(x^3)
integration : int(x)

Last tip: Use ctrl C to stop the execution
Finally home work on right division and left division

This post is possible because if lot of books which are available, so many documents available on the internet and many university notes.

No comments: