How to plot harmonics
Show older comments
%PLOTTING TIME DEPENDENT MOTION OF A STANDING WAVE
%Define Parameters
a_n = 1;
k = 5;
w_n = 5;
x = 0:0.05:4;
t = 1:0.05:20;
for j=1:length(t)
for i=1:length(x)
u(i) = a_n*sin(k.* x(i)).* cos(w_n.*t(j)); %Formula for displacement
end
pause (0.1)
plot(u)
axis([1,65,-1.5,1.5])
grid on
end
Above is the code for a standing wave along a string. I need to create a few more of these, to end up with a series of standing waves which I can then calculate the sum of, which gives me the net displacement of the wave. I then need to plot this net displacement, which will result in a travelling wave.
Equation 1 on the formula sheet attached is the equation that governs the source that generates the series of standing waves. Tau is a 'tuning parameter'. I can just assign it a value. It determines the angular frequency. Imagine plucking a guitar. That is the source. We pluck the string of a guitar at a certain distance along the string. That distance is denoted as X_s (X subscript s). That can just be a value.
Equation 2 is the displacement for the nth standing wave (harmonic) where L is the length of the string, x is displacement and w_n (omega subscript n) is the angular frequency n is the harmonic number. I need to incorporate this somehow into the loop I have used to generate the wave in my code, in order to plot, lets say, the first 5 harmonics as subplots.
Equation 3 is then used to sum all of these harmonics together, to generate a final plot.
Any ideas on how to do this using the code and formula I have written

Answers (1)
Learn to use vectorisation. You can calculate all the u(x,t) at once:
x = 0:0.05:4;
t = 1:0.05:20;
[tt, xx] = ndgrid(t, x);
u = a_n*sin(k*xx).*cos(w_n*tt);
%now you can plot u
for j = 1:numel(t)
pause(0,1);
plot(u(j, :));
%...
x = 0:0.05:4;
t = 1:0.05:20;
u = bsxfun(@(x, t) a_n*sin(k*x).*cos(w_n*t), x, t');
%now you can plot u
for j = 1:numel(t)
pause(0,1);
plot(u(j, :));
%...
If you want to add a third variable L, just add an extra dimension to u:
x = 0:0.05:4;
t = 1:0.05:20;
L = 1:5
[tt, xx, LL] = ndgrid(t, x, L);
u = a_n*sin(k*xx./LL).*cos(w_n*tt);
%sum all the L together:
u = sum(u, 3);
6 Comments
JDilla
on 18 May 2015
Guillaume
on 18 May 2015
You use vectorisation to generate u, for the plotting, you still have to use a loop:
[xx, tt, LL] = ndgrid(x, t, L);
u = a_n*sin(k*xx./LL).*cos(w_n*tt);
for j = 1:numel(t)
pause(0.1)
plot(squeeze(u(:, j, :))) %time is 2nd dimension
axis([1,65,-1.5,1.5])
grid on
end
JDilla
on 18 May 2015
Guillaume
on 19 May 2015
I'm afraid I don't understand what you're asking anymore. The u that is generated has three dimensions, the first one is x, the second one is the time and the third one is the harmonic. Thus if you want to see the 5 harmonics at time th, you just plot
plot(squeeze(u(:, th, :)); %plot all harmonics at time |th|.
If you want to sum these harmonics:
sumharmonic = sum(u, 3);
%to plot that sum at time |th|:
plot(sumharmonic(:, th));
Guillaume
on 19 May 2015
Right, I misread your image, I thought the harmonic was L. It does not change much anyway. You have, u as a function of three variables x, t and n:
u = f(x, t, n) %the specific of f don't matter.
You generate u all at once, for whatever combination of values:
x = 0:0.05:4;
t = 1:0.05:20;
n = 1:50;
[xx, tt, nn] = ndgrid(x, t, n);
u = f(xx, tt, nn);
And if you want to fix a variable, you just fix that dimension. For example to plot the first 5 harmonics at time t0
figure;
for nh = 1:5
subplot(nh, 1, 5);
plot(squeeze(u(:, t0, nh));
end
The sum of the 50 harmonics (3rd dimension) is still:
sum(u, 3)
Categories
Find more on Title in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!