Thursday, December 29, 2011

Programming in MATLAB for Beginners-5

Now move a step ahead towards high level programming in MATLAB:

Is it possible to make large programs & store our codeings on MATLAB?????
Answer is YES........
How???????
with the help of editor
Editors: editors are use for store the cods or set of instructions. we can run our code or program stored in editor any time. editor create a M-File with the extension " .m ".
to open editor just type " edit " on your command window, you will get a new window. 
There are two types of M-File:
  1. Script Files
  2. Function File
Script File: Script files are set of codes & instructions. we can say scripts are simplest kind of M-File. a script file contain a whole as well as a part of any program. we can run any program directly from editor. the output will shown on command window.
to call any script file just type the name of file on command window.

Function File: MATLAB has many inbuilt functions like sin, cos etc. but some time we need a function which is not inbuilt in MATLAB. to create such functions we also use editors. once we create a function, we can call it any time in our program.

always remember the name of any M-File must not be same as any inbuilt function or command in MATLAB.

Thursday, December 22, 2011

Programming in MATLAB for Beginners-4

Let's take some examples:


Create a vector of the even whole numbers between 45 & 98.:

to perform this operation use " : " operator
X=[46:2:98];

Compute the length of hypotenuse of a right triangle if the length of two other sides are given:
We can calculate the hypotenuse of a right triangle using this formula C2=A2+B2
A=input('enter the value of base ');
B=input('enter the value of perpendicular side ');
C=sqrt(A^2+B^2);
['length of hypotenuse']
disp(C)

Concatenation of two matrices:
Suppose we have two matrices A & B
we can conctenat both matrices as follow-
C=[A B];
or
C=[A ; B];

How to convert a number into a string:
use num2str(A) to perform above operation
consider previous example of right triangle once again
A=input('enter the value of base ');
B=input('enter the value of perpendicular side ');
C=sqrt(A^2+B^2);
disp(['length of hypotenuse ',num2str(C)])


Response of a 2nd order system

A simplest way of getting response of a second order system......

Program:
% transfar function & response of a 2nd order system
% zeta is tha damping factor
% wn is natural frequency
zeta=input('enter zeta ');
wn=input('enter wn in rad/sec ');
num=[wn^2];
den=[1 2*zeta*wn wn^2];
G=tf(num,den)                  %% get transfar function
subplot(2,2,1),step(G)      %% step response response of system
subplot(2,2,2),impulse(G)  %% impulse response of system
subplot(2,2,3),bode(G)     %% bode plot of system
subplot(2,2,4),nyquist(G)  %% nyquist plot of system

Output:

Thursday, December 8, 2011

Sorry...

friends right now I can't update new post due to my end semester examination.....you can get new updates after 29th Dec,2011 .....so sorry.....