lunedì 5 luglio 2010

The Wave: The Movie

How to propagate waves in matlab!!!
This extremely simple matlab code, explain how to create a really simple wave propagation simulator in space. It's natural possible to convert this simple code to propagate wave also in space, by simple change of the variable.
The code use the definition of velocity, frequency (converted in pulsation), and amplitude. Here we propagate just 3 waves trains, for which the variable parameter is the wave number. 
This is a really really simple example of how to code the wave propagation, and i didn't spend so muche time in try to write it in elegant way. 
In the last part of the code, there is the most important think (for my point of view). This is an example of how to create a simple movie of the propagative waves.



Here the code:

% Wave propagation movie

%movie(M,20)

clear all
close all
x=linspace(0,10,1000);
c=340;  %velocity
f=50;   % frequency
w=2*pi*f;  %pulsation
k=w/c;     % wavenumber
k1=100*2*pi/c;
k2=80*2*pi/c;

p0=1;      %amplitude
T=1/f;
dT=T/20
Time=[0:dT:dT*500];
n=length(Time);
for t=1:n

   
    p=p0*exp(j*(w*Time(t)-k*x));
    p1=p0*exp(j*(w*Time(t)-k1*x));
    p2=p0*exp(j*(w*Time(t)-k2*x));
  
    figure(1)
    plot(x,p,x,p1,'r',x,p2,'k')
    grid on
ylim([-p0-10 p0+10])
xlim([-0 10])
xlabel('space')

M(:,t)=getframe;
end

movie(M,1)
 


Nessun commento:

Posta un commento