I can not figure out how to express this equation in Matlab

1 view (last 30 days)
I have tried in two methods as follows:
if true
clear;
clc;
a=8.1*10^(-3);
b=0.74;
U=10;
g=9.8;
M=500;
N=500;
w_max=1.5;
w_min=0.5;
del_w=(w_max-w_min)/(M-1);
del_Q=pi/(N-1);
phase=2*pi*rand(M,N);% generate random initial phase
i=1:1:M;
j=1:1:N;
w_i=w_min+(i-1)*del_w;
Q_j=-pi/2+(j-1)*del_Q;
[w_i,Q_j]=meshgrid(w_i,Q_j);
W=a.*g^2.*exp(-b*g^4./(U^4.*w_i.^4))./w_i.^5.*(cos(Q_j./2)).^4;%wave spectrum
%amp=sqrt(2*W*del_w*del_Q);
amp=sqrt(2.*W.*del_w.*del_Q);
Ki=w_i.^2./g;
x=1:1:M;
y=1:1:N;
[X,Y]=meshgrid(x,y);
S=0;
for t=1
figure(t)
for a=1:M
for b=1:N
S=amp.*cos(w_i(a,b).*t-Ki(a,b).*(X.*cos(Q_j(a,b))+Y.*sin(Q_j(a,b)))+phase(a,b))+S;
end
end
mesh(X,Y,S);
view(60,60);
xlabel('x(m)');
ylabel('y(m)');
shading interp;
end
end
and also in this way
if true
clear;
clc;
%%constant numbers
a=8.1*10^(-3);
b=0.74;
U=10;
g=9.8;
M=100;%length
N=100;% length
w_max=1.5;
w_min=0.5;
del_w=(w_max-w_min)/(M-1);
del_Q=pi/(N-1);
phase=2*pi*rand(M,N);%generate random initial phase
x=1:1:M;
y=1:1:N;
[X,Y]=meshgrid(x,y);
S=0; %initialize the wave height
wave=zeros(M,N);% construct wave height matrix
for t=1%time
figure(t)
for x=1:1:M;
for y=1:1:N;
for i=1:M
for j=1:N
w(i)=w_min+(i-1)*del_w;%frequency
Q(j)=-pi/2+(j-1)*del_Q;% angle
W=a.*g^2.*exp(-b*g^4./(U^4.*w(i).^4))./w(i).^5.*(cos(Q(j)./2)).^4;%wave spectrum
amp=sqrt(2.*W.*del_w.*del_Q);%amplitude
K(i)=w(i).^2./g;%dispersion relation
S=amp.*cos(w(i).*t-K(i).*(X(x,y).*cos(Q(j))+Y(x,y).*sin(Q(j)))+phase(i,j))+S;
end
end
wave(x,y)=S;%record the calculated wave height in the matrix
end
x
end
mesh(X,Y,wave);%draw
%view(60,60);
xlabel('x(m)');
ylabel('y(m)');
shading interp;
end
end
but I can not figure it out if this is right, the first code gave out a good image but do not make sense for the code. The second code do not give good results but the code seem to be right. I do not what is wrong. maybe someone can help me. thanks
  1 Comment
arun
arun on 16 Jun 2015
Edited: arun on 16 Jun 2015
Ur second code looks correct, but in ur first code u r treating "amp" as a scalar throughout the code but that supposed to be a tensor, in ur second code u have treated that as a temporary variable inside the 2 loop so that is fine. please note that i didn't check any index mismatch error or syntax error according to the formula u provided

Sign in to comment.

Answers (1)

Zhang Xudong
Zhang Xudong on 17 Jun 2015
thanks, but the second code will take a long time to get the result. this is an easy question, it should not be that complicated. I just can not figure it out..thanks any way
  1 Comment
arun
arun on 17 Jun 2015
Loops are relatively very slow in matlab. U can consider parallelizing the code. I have no idea about parallel computing . I'm sorry to say that in this blog that if u use high level programming languages like C, C++, FORTRAN, python etc that can build those nested loop within one second!

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!