MATLAB Answers

## Can someone please help me correct the error in my code? I'm trying to construct a superposition of two waves of equal amplitude and very similar frequency to create an envelope wave. Thanks

Asked by trinikotch

### trinikotch (view profile)

on 12 Apr 2014
Latest activity Edited by trinikotch

### trinikotch (view profile)

on 1 May 2015
Accepted Answer by Alberto

### Alberto (view profile)

close all
clear all
clc
x=1:0.1:300;
%
a=1;%m
h=10;%m
k1=2*pi/(pi/2);%1/m where k=2pi/wavelength
k2=2*pi/(pi/1.95);
g=9.81;%m/s^2
w1=sqrt(g*k1*tanh(k1*h)); %rad/s
w2=sqrt(g*k2*tanh(k2*h));
e= -pi + (2*pi).*rand(1,1);% phase is a random number between -pi and pi
T=100;%s
n=zeros(length(x),T);
t=1:0.1:T;
for m=1:length(t)
n(:,m) = 2*a*cos(((w2-w1)*t(m)*0.5) - ((k2-k1)*x*0.5))) * cos(((w2-w1)*t(m)*0.5) - ((k2-k1)*x*0.5)));
plot(n(:,m),'b-','LineWidth',2);
axis([pi 100*pi -10 10])
title(['Propagating Plane Wave at time, t=' num2str(t(m)) ' s.'])
xlabel('Horizontal Excursion, m')
ylabel('Surface Elevation, m')
drawnow
pause(0.5)
end

#### 0 Comments

Sign in to comment.

## 1 Answer

### Alberto (view profile)

Answer by Alberto

### Alberto (view profile)

on 12 Apr 2014
Accepted Answer

Check the parenthesis in the line:
n(:,m) = 2*a*cos( ( (w2-w1)*t(m)*0.5) - ((k2-k1)*x*0.5))) *... cos( ( (w2-w1)*t(m)*0.5) - ((k2-k1)*x*0.5)));
your problem is there, it depends in
close all
clear all
clc
x=1:0.1:300;
%
a=1;%m
h=10;%m
k1=2*pi/(pi/2);%1/m where k=2pi/wavelength
k2=2*pi/(pi/1.95);
g=9.81;%m/s^2
w1=sqrt(g*k1*tanh(k1*h)); %rad/s
w2=sqrt(g*k2*tanh(k2*h));
e= -pi + (2*pi).*rand(1,1);% phase is a random number between -pi and pi
T=100;%s
n=zeros(length(x),T);
t=1:0.1:T;
for m=1:length(t)
n(:,m) = 2*a*cos( ( (w2-w1)*t(m)*0.5) - ((k2-k1)*x*0.5)).*...
cos( ( (w2-w1)*t(m)*0.5) - ((k2-k1)*x*0.5));
plot(n(:,m),'b-','LineWidth',2);
axis([pi 100*pi -10 10])
title(['Propagating Plane Wave at time, t=' num2str(t(m)) ' s.'])
xlabel('Horizontal Excursion, m')
ylabel('Surface Elevation, m')
drawnow
pause(0.5) % this is causin error too, you can delete it
end

trinikotch

### trinikotch (view profile)

on 12 Apr 2014
Thanks its working now but I realize Its not the graph I was trying to plot
Alberto

### Alberto (view profile)

on 13 Apr 2014
The problem was that parenthesis didn't matched: too much parenthesis. Try to rewrite carefully.

Sign in to comment.