Sunteți pe pagina 1din 3

A Matlab Primer 2

Article by c

Aims
To demonstrate how matlab can represent waves.

Learning Outcomes
Student will be able to represent waves with respect to distance and time. Student will see how Matlab can be used for plotting waves and hearing them.

Representing waves MATLAB


Matlab is ideal representing waves given its extensive support for complex arithmetic. For example consider a plane wave represented by

p = p 0 e j ( t kx )
If this wave was in a tube of say 10m in length and oscillating at 100Hz we could represent its pressure along the tube at a given instant of time by creating the following Matlab m-file. Try this for yourself. x=linspace(0,10,1000); c=340 f=100; w=2*pi*f; k=w/c; p0=1; t=0; p=p0*exp(j*(w*t-k*x)); plot(x,p); By default Matab will plot the real part of the complex number. If you want a pressure magnitude use abs(p). If you want propagation of spherical waves use something similar to p=p0*exp(j*(w*t-k*x))./x;

Note that the division uses ./ instead of / since we are using an array of x values. You could demonstrate wave propagation of the plane wave with respect to time using a for loop (to see how for loops work type in the command line >>help for). In the following Matlab code five successive plots are created using a for loop to generate five different times from t=0 to t~1/4 of the waves period. Note the use of hold on to plot multiple graphs to the same window. clear all close all x=linspace(0,10,1000); c=340 f=50; w=2*pi*f; k=w/c; p0=1; T=1/f; dT=T/20 for t=0:dT:(dT*5) p=p0*exp(j*(w*t-k*x)); plot(x,p) hold on end If there is a requirement to study the linear superimposition of multiple waves to hence determine say interference patterns then matlab will take care of much of the complex arithmetic. For example two similar plane waves travelling in opposite directions so that they interfere could be represented by p = p 0 e j ( t kx ) + p 0 e j ( wt + kx ) The corresponding matlab would be p=p0*exp(j*(w*t-k*x)) + p0*exp(j*(w*t+k*x)); So matlab could be used to say predict the pressure magnitude along an impedance tube where standing waves are created by the inference of a wave and its reflection.

A frequency sweep using MATLAB


Note for loops have many other useful applications. Lets say we try to generate a frequency sweep clear all close all sampleRate=5000 t=linspace(0,10,sampleRate*5); y=zeros(1,sampleRate*5);

for n=1:(sampleRate*5) f=n/1000; w=2*pi*f; y(n)=cos(w*t(n)); end plot(t,y); sound(y,sampleRate); Here we are using the for loop to index into the arrays y and t with the variable n. So for example y(n)=cos(w*t(n)); will take set the nth element of the y array and set it of the value of a function whose input argument is the nth element of t array. Note we had to initialise the y array to be of the same size as the t array created from linspace( ). Here the standard matlab function zeros( ) was used to create an array of zero values of appropriate size. You can hear the sweep with sound(y,sampleRate);

S-ar putea să vă placă și