Domanda

I saw a code online to add two sine waves moving oppositely.

x = [0:0.1:2*pi];
w = 1;
for t=0:0.01:10
  f1 = sin(x - w*t);
  f2 = sin(x + w*t);
  f = f1+f2;
  plot(x, f);
  drawnow;
end

I am thinking to add two sines but moving at different direction (for example, one going along 30 degree and other going along 90 degree). I read the text and it said the sine wave moving at different direction depends on the wavevector

  f1 = sin(kx*x + ky*y - w*t);
  f2 = sin(kx*x + ky*y + w*t);

But here there are two variable, x and y, so how do I visualize the wave?

What I am trying to plot is not a sine plate in 3D, but a 3D line. For example, in the following plot, I want to show the 3D curve (in bold red) but not the surface

enter image description here

È stato utile?

Soluzione

You will need to visualize the wave with 3D plot in this case. For example, you can use surf command for this.

x = [0:0.1:2*pi];
y = [0:0.1:2*pi];
[X,Y]=meshgrid(x,y);
kx=1;
ky=0.5;
w = 1;
for t=0:0.01:10
  f1 = sin(kx*X +ky*Y - w*t);
  f2 = sin(kx*X + ky*Y - w*t);
  f = f1+f2;
  surf(X,Y,f)
  drawnow;
end

Update:

If you want two waves not in same direction you will need separate values for wave vector, e.g. for wave going along 30 degree kx1=sqrt(3)/2, ky1=1/2, for wave going in 90 degree kx2=0, ky2=1.

In addition, if you want wave line instead 3D plate than you can produce slice of sine plate in direction of some selected line.

For example, you want to receive slice in direction 45 degree (x = y).

First step is produce vectors of slice line points:

lx = 1/sqrt(2);    % (lx,ly) - direction vector for slice line
ly = 1/sqrt(2);
r = [0:0.1:4*pi];     % parameter to produce line in parametric form
x = lx * r;
y = ly * r;

Than we will use these vectors as inputs for sine equations:

kx1=sqrt(3)/2;
ky1=0.5;
kx2=0;
ky2=1;
w = 1;
for t=0:0.01:10
  f1 = sin(kx1*x + ky1*y + w*t);
  f2 = sin(kx2*x + ky2*y + w*t);
  f = f1+f2;
  plot(r,f);
  drawnow;
end

Of course, form of sine line will depend from direction of slice line and you can receive different waves for different slice directions.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top