# Series of Standing Waves in Matlab

1. May 17, 2015

### henrybrent

%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

2. May 19, 2015

### kreil

Which parameter do you think will vary to produce all of the different waves?

3. May 19, 2015

### henrybrent

Angular frequency/harmonic number.

4. May 19, 2015

### kreil

Yes. So you should be able to adapt your code to have a for loop that loops through different values for n, and calculates the displacement for the nth standing wave. Inside that loop is the loop over j that simulates the passage of time. I'm not sure how many values of n you'll need to use to get suitable final results (where everything is summed), so you'll have to play with that. I'd try 100 to start perhaps.

You'll also want to change the way this is visualized, since you probably do not want to visualize the standing waves one at a time.

HINT: This loop is entirely unnecessary:

for i=1:length(x)

5. May 19, 2015

### henrybrent

using the i=1:length(x) was only way I could figure out how to animate the wave.

I'm aware of what I need to do regarding n, but I wasn't too sure how to implement it. I'm not very strong with Matlab, but unfortunately it is all I have at the moment. later for my research work I'll be working in a unix environment, but for now it's matlab!

6. May 19, 2015

### kreil

Here is a good starting point to continue from. This code runs but still needs a lot of work. Let me know if you have any questions!

Code (Text):

%PLOTTING TIME DEPENDENT MOTION OF A STANDING WAVE
%Define Parameters
a_n = 1;
k = 5;
L = 1;
w_n = @(n,x,t) n*pi.*x./L.*t;
x = 0:0.01:4;
t = 1:0.001:20;

for j=1:length(t)
u = a_n*sin(k.* x).* cos(w_n(j,x,t(j)).*t(j)); %Formula for displacement
pause (0.1)
plot(u)
axis([1,65,-1.5,1.5])
grid on
end

Last edited: May 19, 2015
7. May 19, 2015

### henrybrent

So this is where I apply the loop over the initial loop for n = 1:4, to seperate the harmonics. The code looks like a load of harmonics looping over and over again.

8. May 19, 2015

### henrybrent

Would Indexing instead of using nested for loops be a better/more efficient way of doing this?

9. May 19, 2015

### kreil

I'd advise against optimizing a program that isn't fully written. For your application, it's more important that the program runs and produces correct results.

While it's true that vectorized code will generally run faster in MATLAB than equivalent code that uses loops, for small applications like this the difference will be pretty insignificant. So ultimately your choice to use one or the other at this point should be entirely based on what you're more comfortable working with.

10. May 19, 2015

### henrybrent

Ok, that makes sense.

What is K in your formula?

In mine, it was to represent n*pi/L, just a constant in my mind, but obviously that's wrong. In your code its still present as k=5?

11. May 19, 2015

### kreil

I just left k alone, but you can and should update it to be equal to n*pi/L, where "n" is an input variable that can change. Then you can update the definition of w_n to use k.

12. May 19, 2015

### henrybrent

k = n*pi/L
w_n = @(n,x,t) (k*x)*t;

Getting a dimensional error

13. May 19, 2015

### kreil

You'll want to use function handles, so that when you call k(n) it evaluates k for that value of n. In these expressions the @(...) expression defines what the variables (inputs) are.

Code (Text):

k = @(n) n*pi/L;
w_n = @(n,x,t) k(n).*x./t;

Now, since k and w_n are function handles, you always need to call them with the proper inputs. So anywhere there is a k should be replaced by k(n), and anywhere there is w_n should be replaced by w_n(n,x,t).

Also, notice that n is not a variable in your workspace based on these lines of code. It was used passively to define a variable in the function handles, but that's it. Really these could have used anything, like z or P, but the n is suggestive. Later on when you have a loop over values of n, you can call k(n) without conflict.

14. May 19, 2015

### henrybrent

Code (Text):

a_n = 1;

L = 1;
k(n) = @(n) n*pi/L;
w_n = @(n,x,t) k(n).*x./t;

x = 0:0.05:4;
t = 1:0.01:20;

for n = 1:4

for j=1:length(t)
u = a_n*sin(k(n)* x).* cos(w_n(j,x,t(j)).*t(j)); %Formula for displacement
pause (0.1)
plot(u)
axis([1,65,-1.5,1.5])
grid on
end

end

Atm, this is what I have down; trying to loop over the first four values of n.

This just subtitutes 1 - 4 into each iteration (I hope) and represents the harmonic number.

But I still don't get how I'd use the subplot command to somehow split each harmonic up? Meshgrid?

Last edited: May 19, 2015
15. May 19, 2015

### kreil

I did some MATLAB visualizations in a PF insights post I wrote, and the code for the subplot animations is here:

In this case, the standing waves are really time independent (the denominator of w_n cancels the time in the expression for u), so you don't even need an animation. For example:

Code (Text):

a_n = 1;

L = 1;
k = @(n) n*pi/L;
w_n = @(n,x,t) k(n).*x./t;

x = 0:0.05:4;
t = 1:0.01:20;

u = @(n,x,t) a_n*sin(k(n)* x).* cos(w_n(n,x,t).*t);

figure
subplot(2,2,1)
plot(u(1,x,t(1)))
subplot(2,2,2)
plot(u(2,x,t(1)))
subplot(2,2,3)
plot(u(3,x,t(1)))
subplot(2,2,4)
plot(u(4,x,t(1)))

16. May 19, 2015

### henrybrent

I ran your code, it looks/runs great.

My standing waves should be depending on at least the tuning parameter used in equation 1 on the sheet I attached. The standing waves result due to a source being applied (equation 1). The angular frequency is a variable that changes, yes, but I think this change is because of the relationship between the tuning parameter (tau) and angular frequency (w_n). I don't think I can/should define w_n = @(n,x,t) k(n).*x./t;

I'm still quite unsure how to approach this problem,

I have a source applied to a string, for example, that is given by equation 1. This generates a series of standing waves. I want to then subplot the first 5 harmonics of the standing waves.

Then sum them upto...100? And plot this result, this should end up with a travelling wave. (I think).

At the moment, I have a standing wave generating 4 harmonics. But not a standing wave as a result of a source applied.

17. May 19, 2015

### kreil

Your equation for w_n did not depend on tau, so what is their relationship to each other? You're correct that my definition of w_n is probably not right, as the velocity v depends on the wavelength and frequency (I did this quickly and just used x and t).

Equation 1 describes an exponentially decreasing force, yes? Since w_n is inversely proportional to time, then as t goes to infinity w_n goes to zero and F settles on a value of 1. I haven't studied standing waves extensively, so I assumed the second equation resulted from the first and was the one we wanted to use. Otherwise, what do you hope to do with these values of F?

I believe your problems right now are with the physics and not with the code. Except for the source, the rest of the problem is mostly solved, and the last part of summing the harmonics and plotting the time simulation should be straightforward as well.

18. May 19, 2015

### henrybrent

The equation used to describe the standing wave is

The code for the standing wave uses equation

$u(x,t) = A_nsin(\frac{n \pi x}{L})cos(\omega_n t)$

where $\omega_n = \frac{n\pi v x}{L}$

the source is described as $F( \omega_n) = e^\frac{ {(- \omega_n \tau)^2}}{4}$

When the source is applied at distance $x_s$, then the net displacement (sum of the standing waves) is given by the expression

$u(x,t) = \sum\limits_{n=0}^{\infty} \sin(\frac{n\pi x_s}{L})F(\omega_n)\sin(\frac{n\pi x}{L})\cos(\omega_nt)$

19. May 20, 2015

### kreil

Alright, thanks for the clarification. Let's just get the goals solidified quickly to make sure we're on track (correct me where I'm wrong):

1. Use the equation for the standing wave harmonics and plot time simulations of the first 5 harmonics in subplots:
$$u(x,t) = A_n \sin \left( \frac{n \pi x}{L} \right) \cos(\omega_n t)$$

2. Then, use the net displacement equation to plot the net displacement below the harmonic subplots.
$$u(x,t) = \sum\limits_{n=0}^{\infty} \sin \left( \frac{n\pi x_s}{L} \right) F(\omega_n)\sin(\frac{n\pi x}{L})\cos(\omega_nt)$$

Is that all? If so, we've essentially finished the first task and just need to do the second. It doesn't appear that the second step uses any of the results from the first step, though. Is there an expression for A_n that is missing here?

20. May 20, 2015

### henrybrent

Almost, rather than think of it as two problems, I'm thinking of it as just 1.
Write a script that sums the first 100? harmonics of the string when the source is applied, and plot the first 5 harmonics as well as the combination of 100.

I should be able to use just the equation for the source $F(\omega_n)$ and the expression for net displacement to solve this problem.

EDIT: just to confirm, this is time dependent motion of waves i'm looking at.

Last edited: May 20, 2015
21. May 21, 2015

### kreil

I believe you use that equation for the net displacement (sum of 100 or so harmonics), but for the individual harmonics (plotting the first 5 harmonics) you need to use the other equation for u(x,t), right? This is why I said the results aren't really related to each other before.

I have some code I've been working on but can't post it until later today. In the meantime, post any new code you've been working on for this problem.

22. May 21, 2015

### henrybrent

$u(x,t) = \sum\limits_{n=0}^{\infty} \sin \left( \frac{n\pi x_s}{L} \right) F(\omega_n)\sin(\frac{n\pi x}{L})\cos(\omega_nt)$

I think this is the equation we use.

However, to plot the first few harmonics, we just use $\sin \left( \frac{n\pi x_s}{L} \right) F(\omega_n)\sin(\frac{n\pi x}{L})\cos(\omega_nt)$ and set n = 1, 2, 3, 4, 5 etc and plot those .

THEN we can use the summation, and then plot that? I think that's easier (and correct, as this is the equation that involves the source, which is vital)

23. May 21, 2015

### kreil

Ok, I'll incorporate that into the code I'm working on.

24. May 21, 2015

### henrybrent

Ok, thank you

25. May 24, 2015

### henrybrent

Still haven't managed to solve this. Will post what I've got soon when I'm home