Tuesday, 15 May 2012

On 15:13 by Unknown in    1 comment
Matlab Basic Part 2

In the first Part We See Some Basic of Matlab. now in the Part 2we will see some advanced functions and methods of Matlab. then we go forward to our work on the Telecom And Networking side. In this section we will see about the loop statements and how to draw plots figure etc. 

if, if else, else Statement in Matlab

>> A=109;
>> B=104;
>> If (A > B)
>> fprintf('A is Greater than B');
>> elseif (A < B)
>> fprintf('B is greater than A');
>> elseif (A = = B)
>> fprintf('B is equal with A');
>> else 
>> fprintf('Unexpected situation');
>> end



In Matlab, loops can be implemented with a for ...end construct or a while ...end construct. In terms of their ability to repeat a series of calculations, for loops and while loops are equivalent.


for Loops
for loops are often used when a sequence of operations is to be performed a predetermined number of times. For example computing the Maximum or Minimum of a list of numbers.
Syntax
Loop counter incremented by one:
for i = startingValue : endingValue
x = Some Value
y = Some value
Do somthing
...
...
...
end

Exp1: Compute the sum of the fi rst n integers
>> n = 10; 
>> sum = 0;
>> for i = 1 : n
>> sum = sum + i;
>> end


Exp2: Compute the average of a list of numbers
>> n = 500;
>> x = rand(1,n);                      % generate a row vector of pseudo random number having length n
>> sum = 0;
>> for i=1:n
>> sum = sum + x(i);                % Add ith element of x to sum
>> end
>> avg = sum/n;

while loop
While Loop is same as for loop, only the difference is it first check the condition then do action. syntax is below

while(condition)
...
...
do something
...
...
end

Switch Statement
In Matlab switch statement is same as other languages like C/C++. but in Matlab no parenthesis are used. the syntax is below:

switch(y)
     case(1)
           do something
           break
     case (2)
          do something
          break
     ...
     ... 
     ...
     case(n)
          do something
          break
     default
          display default message/ do default work
end

Graphics
Graphics are used to see the graphical representation of a sequence/ signal etc. In Matlab two major types of plot are used. one is "plot" command used for continuous signal, second is "stem" command for discrete type signal.
"subplot" command is used if one want to display multipull plots in one figure at different location. if one wants to display one plot together with another plot the after first plot "hold on" command is used. Remember that "figure" command is used to create one figure. 

Exp 3: sine generation: A*sin(omega*n+theta)


>> n = 0: 1: 50;
>> A = 0.87;                                         % Amplitude of sin wave
>> theta = 0.4;                                      % Phase of sin wave
>> omega = 2*pi / 20;                           % frequency of sin wave
>> xn1 = A*sin(omega*n+theta);          % sin generation
>> figure (1);                                         % create first figure
>> subplot(2,1,1);                                 % there are two locations for plot in figure 1
>> plot(xn1);
>> title('first signal');
>> xn2 = A.^n;                                     % exp generation

>> subplot(2,1,2);                                 % xn2 will be plot on the 2nd row 1st column
>> plot(xn2);
>> title('Exponential signal');
>> figure (2);
>> plot(xn1);
>> hold on;
>> plot(xn2);
>> hold off;
>> figure (3);
>> stem (n, xn1);




1 comment:

  1. check it out for your help. if not contain info you need then write your problem in comments. you will be entertained, In Shah Allah

    ReplyDelete