Friday, October 31, 2014

NS 2 - Tutorial for beginners 2

NS2 program for simulation of TCP packets in a network

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set nt [open out.tr w]
$ns trace-all $nt

proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
        }

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n1 10Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-down
$ns duplex-link-op $n1 $n2 orient right
$ns duplex-link-op $n3 $n1 orient right-up

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

set ftp [new Application/FTP]
#$ftp set packet_size_ 4.5Mb
$ftp set interval_ 0.05
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink

$ns connect $tcp $sink

$ns at 0.3 "$ftp start"
$ns at 3.0 "finish"

$ns run





NS2 program for simulation of UDP packets in a network

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set nt [open out.tr w]
$ns trace-all $nt

proc finish {} {
        global ns nf nt
        $ns flush-trace
        close $nf
        close $nt
        exec nam out.nam &
        exit 0
        }

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-up
$ns duplex-link-op $n0 $n2 orient right

set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]
$ns attach-agent $n2 $null0

$ns connect $udp0 $null0

$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"

$ns run


NS2 program for simulation of both  TCP and UDP packets

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set nt [open out.tr w]
$ns trace-all $nt

proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
        }

set h1 [$ns node]
set r1 [$ns node]
set h2 [$ns node]
set h3 [$ns node]
set r2 [$ns node]
set h4 [$ns node]

$ns duplex-link $h1 $r1 10Mb 20ms DropTail
$ns duplex-link $h2 $r1 10Mb 20ms DropTail
$ns duplex-link $r1 $r2 1.5Mb 20ms DropTail
$ns duplex-link $r2 $h3 10Mb 20ms DropTail
$ns duplex-link $r2 $h4 5Mb 20ms DropTail

$ns duplex-link-op $h1 $r1 orient right-down
$ns duplex-link-op $h2 $r1 orient right-up
$ns duplex-link-op $r1 $r2 orient right
$ns duplex-link-op $r2 $h3 orient right-up
$ns duplex-link-op $r2 $h4 orient right-down

set udp [new Agent/UDP]
$ns attach-agent $h2 $udp

set cbr [new Application/Traffic/CBR]
#$cbr set interval_ 0.0005
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $h4 $null

set tcp [new Agent/TCP]
$ns attach-agent $h1 $tcp

set ftp [new Application/FTP]
#$ftp set interval_ 0.0005
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $h3 $sink

$ns connect $udp $null
$ns connect $tcp $sink

$ns at 0.0 "$cbr start"
$ns at 0.0 "$ftp start"
$ns at 10.0 "finish"

$ns run

NS2 program for simulation of Distance Vector routing protocol

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set tr [open out.tr w]
$ns trace-all $tr

proc finish {} {
        global nf ns tr
        $ns flush-trace
        close $tr
        exec nam out.nam &
        exit 0
        }

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n3 10Mb 10ms DropTail
$ns duplex-link $n2 $n1 10Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-down
$ns duplex-link-op $n1 $n3 orient right
$ns duplex-link-op $n2 $n1 orient right-up

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

set ftp [new Application/FTP]
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink

set udp [new Agent/UDP]
$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $n3 $null

$ns connect $tcp $sink
$ns connect $udp $null

$ns rtmodel-at 1.0 down $n1 $n3
$ns rtmodel-at 2.0 up $n1 $n3

$ns rtproto DV

$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"

$ns at 5.0 "finish"

$ns run

 

NS2 program for simulation of Link State routing protocol

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set tr [open out.tr w]
$ns trace-all $tr

proc finish {} {
        global nf ns tr
        $ns flush-trace
        close $tr
        exec nam out.nam &
        exit 0
        }

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n3 10Mb 10ms DropTail
$ns duplex-link $n2 $n1 10Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-down
$ns duplex-link-op $n1 $n3 orient right
$ns duplex-link-op $n2 $n1 orient right-up

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

set ftp [new Application/FTP]
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink

set udp [new Agent/UDP]
$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $n3 $null

$ns connect $tcp $sink
$ns connect $udp $null

$ns rtmodel-at 1.0 down $n1 $n3
$ns rtmodel-at 2.0 up $n1 $n3

$ns rtproto LS

$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"

$ns at 5.0 "finish"

$ns run


NS 2 - Tutorial for beginners 1

Explanation for a Simple Script

Step 1: To start we have to set a few variables like simulator object, trace file and object, nam file and object:
 
set ns [new Simulator]

This would set the simulator with the simulator object which is to be accessed in the script.

Step 2: Then, to set the nam (network animation) file with that 'ns' object and associate with it:

set nf [open out.nam w]

$ns namtrace-all $nf

set tr [open out.tr w]

$ns trace-all $tr
This would set the trace file and would connect to the simulator. The trace file is required to analyze the various packets which are send, received type of application used etc.

Step 3: Now the nodes could be set as many as you want, for loop could be used if many nodes are to be made.

set n0 [$ns node]

Step 4: Creating connection for the various nodes with each other with the band width and rate.

$ns duplex-link $n0 $n1 10Mb 10ms DropTail

Step 5: The nodes could be given with various orientations with this option. right, right-up and right down could be used depending on the node.

$ns duplex-link-op $n0 $n1 orient right-up

Step 6: For the application like TCP or UDP to run, we need to set two agents and the application which should run in between.

When using TCP, we have FTP as the application and TCPsink as the end agent. Connection must be made between TCP and TCPsink , same in udp with cbr and null respectively.

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

This would make a tcp agent and connect it with the node.

Step 7: Connecting the FTP application with TCP

set ftp [new Application/FTP]
$ftp attach-agent $tcp

Step 8: Setting TCPSink to the node where the TCP packets are received:

set agent [new Agent/TCPSink]
$ns attach-agent $n3 $sink

Step 9: Connecting TCP and sink(agents) for making the network flow:

$ns connect $tcp $sink

Step 10: Similarly, for UDP, 

set udp [new Agent/UDP]
$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $n3 $null

$ns connect $udp $null

Step 11: Routing protocols can be used in the simulator using rtmodel (to break the link), rtproto (to use the protocol)

$ns rtmodel-at 1.0 down $n1 $n2

$ns rtmodel-at 2.0 up $n1 $n3

For distance vector we could use

$ns rtproto DV

For linkstate we could use

$ns rtproto LS

When all this is done, the TCP could be started at some point and could call the finish procedure to end. The out.tr file is used to trace the packets. A normal awk command could be used to analyse the packets.

 Step 12:   We could also stop the TCP or UDP in between using stop instead of start, hence nam out.nam need to be used if finish is not used.
$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"

$ns at 5.0 "finish"

Step 13: 'run' is used to run the whole simulation.

$ns run
Step 14: Save the file as example1.tcl; go to terminal and execute

ns example1.tcl 

 

Tuesday, October 28, 2014

Analog Communications lab MATLAB Programs

%% Amplitude Modulation And Demodulation
% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
fs=8000; % sampling frequency
fm=20; % message signal frequency
fc=500; % carrier signal frequency
t=[0:.1*fs]/fs; % time index
%% Message signal Generation
Am=1; % message signal amplitude
m=Am*cos(2*pi*fm*t); % message signal
subplot(4,3,1:3);
plot(t,m);
xlabel('time-->'), ylabel('amplitude')
title('Modulating or Message signal(fm=20Hz)');
%% Carrier signal Generation
Ac=1; % carrier signal amplitude
c=Ac*cos(2*pi*fc*t); % carrier signal
subplot(4,3,4:6);
plot(t,c);
xlabel('time-->'), ylabel('amplitude')
title('Carrier signal(fc=500Hz)');
%% Under Modulation
ka=0.5; % modulation sensitivity
u=ka*Am; % modulation Index
s1=Ac*(1+u*cos(2*pi*fm*t)).*cos(2*pi*fc*t);%AM
subplot(4,3,7);
plot(t,s1);
xlabel('time-->'), ylabel('amplitude')
title('Under Modulated signal(ka.Am=0.5)');
%% Critically Modulated signal
Am=2; % message signal amplitude
ka=0.5; % modulation sensitivity
u=ka*Am; % modulation index
s2=Ac*(1+u*cos(2*pi*fm*t)).*cos(2*pi*fc*t);
subplot(4,3,8);
plot(t,s2);
xlabel('time-->'), ylabel('amplitude')
title('Critically Modulated signal(ka.Am=1)');
%% Over Modulated signal
Am=5; % message signal amplitude
ka=0.5; % modulation sensitivity
u=ka*Am; % modulation index
s3=Ac*(1+u*cos(2*pi*fm*t)).*cos(2*pi*fc*t);
subplot(4,3,9);
plot(t,s3);
xlabel('time-->'), ylabel('amplitude')
title('Over Modulated signal(ka.Am=2.5)');
%% Demodulation
% for under modulated signal
r1= s1.*c; %detection
[b a] = butter(1,0.01);% LPF design
mr1= filter(b,a,r1);% demodulation
subplot(4,3,10);
plot(t,mr1);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal')

% for critically modulated signal
r2= s2.*c;%detection
[b a] = butter(1,0.01);% LPF design
mr2= filter(b,a,r2);% demodulation
subplot(4,3,11);
plot(t,mr2);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal')

% for overmodulated signal
r3= s3.*c;%detection
[b a] = butter(1,0.01);% LPF design
mr3= filter(b,a,r3);% demodulation
subplot(4,3,12);
plot(t,mr3);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal')


%% Pre emphasis and De emphasis
% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
f1=10;

for f=1:50
x(f)=(1/sqrt(1+(f1/f)^2));
f2(f)=f;
end
subplot(2,1,1);
plot(f2,x);
title('pre-emphasis filter response');

for f=1:50
y(f)=(1/sqrt(1+(f/f1)^2));
f3(f)=f;
end
subplot(2,1,2);
plot(f3,y);
title('de-emphasis filter response');


%% Time Division Multiplexing
% Name:
%
% R.No:
%% closing and clearing commands
close all;
clear all;
clc
%% Signal generation
x=0:.5:4*pi;     % signal taken upto 4pi
sig1=8*sin(x); % generate 1st sinusoidal signal
l=length(sig1);
sig2=8*triang(l);% Generate 2nd traingularSignal

%% Display of Both Signal
subplot(2,2,1);                         
plot(sig1);
title('Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

subplot(2,2,2);
plot(sig2);
title('Triangular Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

subplot(2,2,3);
stem(sig1);
title('Sampled Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

subplot(2,2,4);
stem(sig2);
title('Sampled Triangular Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

l1=length(sig1);
l2=length(sig2);
for i=1:l1
sig(1,i)=sig1(i);                        % Making Both row vector to a matrix
sig(2,i)=sig2(i);
end
 % TDM of both quantize signal
tdmsig=reshape(sig,1,2*l1);              
% Display of TDM Signal
figure
stem(tdmsig);
title('TDM Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
 % Demultiplexing of TDM Signal
demux=reshape(tdmsig,2,l1);
for i=1:l1
sig3(i)=demux(1,i);% Converting the matrix into row vectors
sig4(i)=demux(2,i);
end
% display of demultiplexed signal
figure
subplot(2,1,1)
xlabel('Time--->');
%Display of Both Sampled Signal
plot(sig3);
title('Recovered Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(2,1,2)
plot(sig4);
title('Recovered Triangular Signal');
ylabel('Amplitude--->');
xlabel('Time--->');


%% DSBSC Modulation And Demodulation% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
Vm= 1; % message signal amplitude
Vc= 1; % carrier signal amplitude
fm = 20;%message signal frequency
fc= 500; %carrier signal frequency
fs=2*fc; % sampling frequency
t =0:inv(fs):2*pi; %time index
m_t = Vm*sin(2*pi*(fm/fs)*t);% message signal
subplot(4,1,1);
plot(t,m_t);
xlabel('time-->'), ylabel('amplitude')
title('message signal')
c_t = Vc*sin(2*pi*(fc/fs)*t); % carrier signal
subplot(4,1,2);
plot(t,c_t);
xlabel('time-->'), ylabel('amplitude')
title('carrier signal')
s_t = m_t.*c_t; %DSB-SC signal
subplot(4,1,3);
hold on;
plot(t,s_t);
plot(t,m_t,'r:');
plot(t,-m_t,'r:');
hold off;
xlabel('time-->'), ylabel('amplitude')
title('DSB-SC modulated signal')
%% Demodulated signal
r = s_t.*c_t;% detection
[b a] = butter(1,0.01);%LPF design
mr= filter(b,a,r);%demodulation
subplot(4,1,4);
plot(t,mr);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal')

%% SSB-SC Modulation And Demodulation
% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
fs=8000; % sampling frequency
fm=20;%message signal frequency
fc=50;%carrier signal frequency
Am=1;% message signal amplitude
Ac=1;% carrier signal amplitude
t=[0:.1*fs]/fs;%time index
%% message signal generation
m1=Am*cos(2*pi*fm*t);% message signal 1
subplot(4,2,1);
plot(t,m1);
xlabel('time-->'), ylabel('amplitude')
title('Message Signal 1');

m2=Am*sin(2*pi*fm*t);% Message signal 2
subplot(4,2,2)
plot(t,m2);
xlabel('time-->'), ylabel('amplitude')
title('Message Signal 2');
%% carrier signal generation
c1=Ac*cos(2*pi*fc*t);% carrier signal 1
subplot(4,2,3)
plot(t,c1)
xlabel('time-->'), ylabel('amplitude')
title('Carrier Signal 1');

c2=Ac*sin(2*pi*fc*t);% carrier signal 2
subplot(4,2,4)
plot(t,c2)
xlabel('time-->'), ylabel('amplitude')
title('Carrier Signal 2');
%% SSB-SC generation
Susb=0.5*m1.*c1-0.5*m2.*c2;
subplot(4,2,5);
plot(t,Susb);
xlabel('time-->'), ylabel('amplitude')
title('SSB-SC Signal with USB');

Slsb=0.5*m1.*c1+0.5*m2.*c2;
subplot(426)
plot(t,Slsb);
xlabel('time-->'), ylabel('amplitude')
title('SSB-SC Signal with LSB');
%% Demodulation
r1 = Susb.*c1;% Detection
[b a] = butter(1,0.0001,'low');%LPF design
mr1= filter(b,a,r1);% demodulation
subplot(4,2,7);plot(t,mr1);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal 1')

r2 = Slsb.*c2;% Detection
[b a] = butter(1,0.0001,'low');%LPF design
mr2= filter(b,a,r2);% demodulation
subplot(4,2,8);plot(t,mr2);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal 2')


%% Frequency Modulation And Demodulation
% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
%fm=35HZ,fc=500HZ,Am=1V,Ac=1V,B=10
fs=10000; % sampling frequency
Ac=1;% carrier signal amplitude
Am=1;% message signal amplitude
fm=35;%message signal frequency
fc=500;%carrier signal frequency
B=10;
t=(0:.1*fs)/fs;%time index
%% MESSAGE SIGNAL GENERATION
m_t=Am*cos(2*pi*fm*t);
subplot(4,1,1);
plot(t,m_t);
title('Modulating or Message signal(fm=35Hz)');
%% carrier signal generation
c_t=Ac*cos(2*pi*fc*t);
subplot(4,1,2);
plot(t,c_t);
title('Carrier signal(fm=500Hz)');
%% Frequency modulated signal generation
s_t=Ac*cos(2*pi*fc*t+B*sin(2*pi*fm*t));
subplot(4,1,3);
plot(t,s_t);
title('Frequency Modulated signal');
%% Demodulated signal generation
d=demod(s_t,fc,fs,'fm');
subplot(4,1,4);
plot(t,d);
title('demodulated signal');

%% Pulse Amplitude Moulation And Demodulation
% Name:
%
% R.No:
%% ALGORITHM:
% STEP1:    Start
% STEP2:    define modulating and carrier frequency.
% STEP3:    define time axis
% STEP4:    define modulating signal and carrier signal
% STEP5:    modulate the signals using FFT Algorithm to get PAM signal
% STEP6:    demodulate the signal using IFFT Algorithm to get back the original signal.
% STEP7: Stop
%% PROGRAM:
%% clearing and closing commands
close all
close all
clc
%% Initialization
t=0:1/6000:((10/1000)-(1/6000));% time index
xa=sin(2*pi*100*abs(t));
subplot(3,1,1)
plot(xa);
grid

Ts=32;
x=sin(2*pi*600*(Ts*t));

X=fft(xa,abs(x));
subplot(3,1,2);
stem(t, abs(X));
grid

Y=ifft(xa,X);
subplot(3,1,3)
plot(t,abs(Y))


%% PPM Generation And Demodulation
% Name:
%
% R.No:
%    Choose the sampling frequency fs and modulating frequency f1 such that Nyquist criteria are satisfied.
%    Generate the message signal using f1 andfs .
%    Modulate the message signal using the carrier frequency.
%    FFT is applied to the modulated signal to get frequency spectrum.
%    Demodulate the modulated signal using the same carrier frequency.
%    Plot the graphs for the original message signal, modulated, frequency spectrum and demodulated signal.
%% clearing and closing commands
close all;   
clear all
clc
%% Initialization
fc=100; % carrier signal frequency
fs=1000; % sampling frequency
        f1=80;%f2=300
t=0:1/fs:((2/f1)-(1/fs));
x1=0.4*cos(2*pi*f1*t)+0.5;
        %x2=0.2*(cos(2*pi*f1*t)+cos(2*pi*f2*t))+0.5 ;
subplot(4,2,1)
plot(x1)
title('original msg signal')
       y1=modulate(x1,fc,fs,'ppm')
subplot(4,2,2)
plot(y1)
axis([0 50 -0.2 1.2])
title('ppm one of f1,fc=1000,f1=80 ')
       fx1=abs(fft(y1,1024))
       fx1=[fx1(512:1024) fx1(1:513)]
       f=[(511*fs/1024):(fs/1024):(512*fs/1024)]
subplot(4,2,3)
plot(fx1)
title('freq des ppm signal tone,fc=1000')
       x1_recov = demod(y1,fc,fs,'ppm')
subplot(4,2,4)
plot(x1_recov)
title('time domain recovered signal')


%% PWM MODULATION AND DEMODULATION
% Name:
%
% R.no:
%% clearing and closing commands
clear all
close all
clc
%% Initialization
fc=1000; % carrier signal frequency
fs=10000; % sampling frequency
f1=200;
t=0:1/fs:((2/f1)-(1/fs));

x1=0.4*cos(2*pi*f1*t)+0.5;
subplot(421);
plot(x1);
title('original signal tone mesage,f1=500,fs=10000')
%% modulation
y1=modulate(x1,fc,fs,'pwm');
subplot(422);
plot(y1);
axis([0 500 -0.2 1.2]);
title('PWM')
%% demodulation
x1_recov=demod(y1,fc,fs,'pwm');
subplot(423);
plot(x1_recov);
title('time domain recoverd signal tone,f1=200')

Friday, October 17, 2014

List of Websites to get free e-books

Here follows the list of websites to download e-books for free.

1. www.bookfi.org

2. www.bookzz.org 
These two websites offer 4 to 5 free downloads per day. Best advantage is that we need not go for any sign-in process or clicking on an advertisement.

3. http://manybooks.net     
In this website, apart from downloading in pdf format, one can download in many other e-book formats.

4. www.ebookee.org

5. www.bookboon.com

6. www.gutenberg.org

7. www.books123.me 

8. www.brupt.com
This website provides file in word (.doc), powerpoint (.ppt) and pdf file formats.

9. www.pdfoxy.com
This website provides books in pdf format, but we should wait for 12-15 seconds and enter a Captcha code. But no need to sign-in.

10. www.docstoc.com
This is another decent website of downloading ebooks in .pdf format, with no advertisement and no signing in.

11. www.freecomputerbooks.com
This is one of the best websites for downloading technical and professional certification related books. No sign-in, no captcha and no disturbing advertisements.

12. www.scridb.com
This is one of the popular website for downloading books, presentations and other  documents. Here, not all documents are free, few are free and others must be purchased. Need to sign-in and upload our documents or pay for downloading.

Note: Not all books come with .pdf (portable documentation format) format. Other popular ebook formats are .ePub and .djvu formats, which deserve us to install (free) ePub reader and djvu reader, respectively.

PS: I request the readers to add more such websites, in the comments.

Disclaimer: There may be copyright violated material in those websites, for which the author is not responsible.

Thursday, October 9, 2014

MATLAB tutorial for Begineers Part 3

click here for Part 2                     click here for Part 4

Working With MATLAB

Type the following following commands in the Command Window, and observe(verify) their corresponding results

 12

ans =

    12

% commenting operator in matlab
%12

a=23

a =

    23

a=23; %; is a output suppressor operator

who

Your variables are:

a    ans

whos
  Name      Size            Bytes  Class     Attributes

  a         1x1                 8  double            
  ans       1x1                 8  double            


who---- This command lists the variables in workspace.

whos----This command details the variables in workspace.


12+23

ans =

    35

12-23

ans =

   -11

mul=12*23

mul =

   276

div=12/23;
; is the output suppressing operator

who

Your variables are:

a    ans  div  mul

div

div =

    0.5217

1/2

ans =

    0.5000

2/1

ans =

     2

1/2

ans =

    0.5000

1\2

ans =

     2

%   \ is the inverted division operator a\b=b/a
2/3

ans =

    0.6667

2\3

ans =

    1.5000

3/2

ans =

    1.5000

% exponential representation

1e3

ans =

        1000
% e and E holds fine

1E3

ans =

        1000
% But, matlab is case-sensitive

a=exp(2)

a =

    7.3891

A=exp(1)

A =

    2.7183
who
a   A

% lookfor ----This command is used for command search
% Syntax: lookfor keywork_to_search

% we know the relation between exponential and logarithm

q=exp(12)

q =

   1.6275e+05

log(q)

ans =

    12

% = assignment operator
% == equivalence operator, tests equality between variables on both sides
% == results 1, if both are equal; 0, if they are not equal
1==2

ans =

     0

1==1

ans =

     1

a=exp(12),b=log(a)

a =

   1.6275e+05


b =

    12

log(exp(12))

ans =

    12
exp(log(12))

ans =

    12

% log ---command to compute natural logarithm
% log10---command to compute base 10 logarithm
exp(log10(12))

ans =

    2.9423

log10(exp(12))==log(exp(12))

ans =

     0

% both are not equal

log10(10^12)==10^(log10(12))
ans=
   
     1
%both are equal

%trigonometric computations
sin(90)

ans =

    0.8940

pi

ans =

    3.1416

22/7

ans =

    3.1429

sin(pi/2)

ans =

     1

sin(pi/2)==sin(90)

ans =

     0

sin(0)

ans =

     0

asin(sin(pi/2))

ans =

    1.5708

asin(sin(90))

ans =

    1.1062

sin(5)

ans =

   -0.9589

% Matlab’s trig functions are permanently set to radians mode

% The up-arrow key will display previous commands.
% And when you back up to a previous command that you like, hit Enter and it will execute.
% Or you can edit it when you get to it (use , , and Del), then execute it. Try doing this
% Now to re-execute the commands you have already typed.
diary off

clear all %to clear the workspace. Execute and observe the workspace window

close all % closes safely all the windows opened, except main window and the editor window

clc%clears the command window. Execute it and observe the command window

who
% As "clear all" command clears all the variables; so "who" can't display any variable.

% who lists active variables

% whos----lists active variables and their sizes

% what-------lists .m files available in the current directory


% Numerical Accuracy in MATLAB
225/331

ans =

    0.6798

format long e
225/331

ans =

     6.797583081570997e-01

format short% (the default format)
225/331

ans =

    0.6798

format long
225/331

ans =

   0.679758308157100

format short e
%e stands for exponential notation
225/331

ans =

   6.7976e-01

format bank
225/331

ans =

    0.67

format short% (the default format)

225/331

ans =

    0.6798


pi

ans =

    3.1416

who

Your variables are:

ans

pi=2%can be done

pi =

     2

who

Your variables are:

ans  pi 

pi

pi =

     2

% please don't do so. "pi" was assigned to 3.1416, by default
pi=3.1416

pi =

    3.1416

clear all
pi

ans =

    3.1416

power(2,5)
ans =

    32

power(10,2)

ans =

   100

pow2(2)

ans =

     4

%pow2(2) means 2^2
%X = pow2(Y) for each element of Y is 2 raised to the power Y
%  X = pow2(F,E) for each element of the real array F and a integer
%    array E computes X = F .* (2 .^ E).

X = pow2(Y) for each element of Y is 2 raised to the power Y

sqrt(25)

ans =

     5

% sqrt---to calculate the squarate


% working with complex numbers
a=2+2j

a =

   2.0000 + 2.0000i

b=3+5i

b =

   3.0000 + 5.0000i

% either "i" or "j" can be used to represent the imaginary number
diary off
2j

ans =

        0 + 2.0000i

2i

ans =

        0 + 2.0000i

%but, the notation "i2" or "j2" is forbidden
i2
{ Undefined function or variable 'i2'.
}
j2
{ Undefined function or variable 'j2'.
}
% abs----to calculate the absolute value of a complex number
who

Your variables are:

a    ans  b  

w=3;
abs(w)

ans =

     3
% abs(real_number)=real_number

% abs(imaginary_number)=real_number
abs(3j)

ans =

     3

abs(3.33j)

ans =

    3.3300

a

a =

   2.0000 + 2.0000i

abs(a)

ans =

    2.8284

%abs(m+nj)=sqrt(power(m,2)+power(n,2))
sqrt(power(2,2)+power(2,2))

ans =

    2.8284

z1=1+2i

z1 =

   1.0000 + 2.0000i

%% or you can multiply by i, like this
z1=1+2*i

z1 =

   1.0000 + 2.0000i

z2=2-3i

z2 =

   2.0000 - 3.0000i

% add and subtract
addition=z1+z2

addition =

   3.0000 - 1.0000i

subtraction=z1-z2

subtraction =

  -1.0000 + 5.0000i

% multiply and divide
multiply=z1*z2

multiply =

   8.0000 + 1.0000i

division=z1/z2

division =

  -0.3077 + 0.5385i

d1=z1\z2

d1 =

  -0.8000 - 1.4000i

z=3+4i

z =

   3.0000 + 4.0000i

real(z)

ans =

     3

imag(z)

ans =

     4

conj(z)

ans =

   3.0000 - 4.0000i

abs(z)

ans =

     5

angle(z)

ans =

    0.9273

diary off
%angle(m+nj)=atan(b/a)
z

z =

   3.0000 + 4.0000i

atan(4/3)==angle(3+4j)

ans =

     1

%Here, 1 means both are equal
%Euler’s famous formula e
%exp(xi)= cos x + i sin x
exp(i*pi/4)

ans =

   0.7071 + 0.7071i

%% Housekeeping Functions
% ceil(x)---the nearest integer to x looking toward +
% close 3--- closes figure window 3
% fix(x)---- the nearest integer to x looking toward zero
% fliplr(A)------ flip a matrix A, left for right
% flipud(A)-----flip a matrix A, up for down
% floor(x)------- the nearest integer to x looking toward -
% length(a)------the number of elements in a vector
% mod(x,y)-----the integer remainder of x/y; see online help if x or y are negative
% rem(x,y)------the integer remainder of x/y; see online help if x or y are negative
% rot90(A)------rotate a matrix A by 90
% round(x)------the nearest integer to x
% sign(x)--------the sign of x and returns 0 if x=0
% size(c)---------the dimensions of a matrix

MATLAB tutorial for Begineers Part 2

click here for Part 1                       click here for Part 3



1. How to open Matlab
    Method1: click on matlab shortcut in start menu or on desktop
    Method2: for windows os ::::Press windows+R--->type cmd--->type "matlab"
             for linux os:::::::go to terminal/Konsole--->type "matlab"

Note: It is recommended not to do any action, atleast-for-a-vial, while opening MATLAB because Matlab consumes more RAM while invoking.

MATLAB tutorial for Begineers Part 1

MATLAB is abbreviation for MATrix LABoratory.

MATLAB is an integrated development environment and computer language that enables users to perform computationally intensive tasks.  The software is used for numerical computation tasks, visualization, and mathematical programming.
For more details, see http://www.mathworks.com/products/matlab.

Latest release of MATLAB is R2014a.

As the name emphasis, MATLAB considers every variable/value in terms of matrices.
MatLab vs c: MatLab is not a programming language, but it is a scripting language. In simple English, you use English-like commands in the MatLab program; whereas corresponding c program will run in the background.

So, MatLab program is slower than c program; but is more flexible and lightweight than the former. 


The latest MATLAB constitutes FOUR platforms, within its environment.
1. M-file/Script file platform
2. Simulink
3. GUIDE- Graphical User Interface IDE(Integrated Development  Envinirment)
4. Matlab APPLICATIONS


Note: All these four are not independent, but dependent on each other. It is recommended to start with M-file/script-file programming for a beginner.


Cost of MatLab: MatLab is a proprietary software, but it is worthy-enough to pay for it. Even there is free student version of MatLab, which is available on demand from the mathworks website.
A humble request is to buy the MatLab products, and don't be a part of pirating the software(which is done by many torrent-hosting/file-sharing  websites).
Platform support: MatLab is available in Windows, Unix  and Macintosh platforms. The Unix flavor is used for linux too.
Alternatives to MatLab: In contract, there are free alternatives for MatLab, such as Octave, Scilab,etc. Scilab is available in windows and linux platforms; whereas Octave is available only for Linux platform.

Matlab programs are backward compatible.

MATLAB is a proprietary software, patented by MathWorks Inc.
Though it is commercial, the free student version can be downloaded from MathWorks website, on registration.

Windows in MATLAB:

By default, you will see the default window layout.
Else, to get the default layout of windows in MATLAB, go to File->Desktop->Desktop Layout->Default.

1. Command Window: It is heart of MATLAB, where the response of all inputted commands will the displayed, expect the graphical responses.

2. Command History: It stores all the commands executed in the command window, including the date and time of their execution.
It doesn't store commands executed through script-files.


3. Current Folder (Current working Directory): Indicates the current folder to which matlab is associated. All user-defined functions and other files(images, audio files, video files..) must be in the current folder. Also, the current folder association can also be changed.

4. Workspace: It stores all the variables, their data, data types, and some more information about the variables used in current computations. The variables can be cleared. Closing Matlab, automatically results in clearing of workspace.

5. Editor Window: All the M-files/script files must be written in this editor. Optionally, notepad can also be used as matlab editor, where the file should be saved with .m extension to make an M-file. But, Editor Window has some advanced options like direct execution, publishing, ...

6. Figure Window: All the graphical responses will be displayed in this window. By default, it saves with .fig extension; but there are options to save as conventional file formats like .jpg. .png,...

 File Types in MATLAB:

1) .m files -> These are the Matlab script files. user-defined functions are also saved as .m files.
2) .fig files -> The default file type to save the images/figures. The GUIDE files are also saved with .fig extension.
3) .mdl files -> The Simulink Model files are saved with .mdl extension. mdl files are text files.
4) .slx files -> New file format to save the simulink models, release from R2012a. SLX is a compressed file that contains model information in XML format.
 Note: It is optional to the user to choose between .mdl and .slx extensions.

5).mat files-> It is similar to the .dat file, used to store data.


Apart from these proprietary file formats, Matlab supports few file formats to import and export data.



          

Apache Airflow notes