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

1 回表示 (過去 30 日間)
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

採用された回答

Alberto
Alberto 2014 年 4 月 12 日
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
  2 件のコメント
trinikotch
trinikotch 2014 年 4 月 12 日
編集済み: trinikotch 2015 年 5 月 1 日
Thanks its working now but I realize Its not the graph I was trying to plot
Alberto
Alberto 2014 年 4 月 13 日
The problem was that parenthesis didn't matched: too much parenthesis. Try to rewrite carefully.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by