数学建模美赛集训的时候要用到一个海面模拟,分享一下海面模拟的MATLAB代码
先贴一下结果图:
下面是源代码~~~
1 function waterwave 2 3 4 n = 64; % grid size 5 g = 9.8; % gravitational constant 6 dt = 0.01; % hardwired timestep 7 dx = 1.0; 8 dy = 1.0; 9 nplotstep = 8; % plot interval 10 ndrops = 5; % maximum number of drops 11 dropstep = 500; % drop interval 12 D = droplet(1.5,21); % simulate a water drop 13 14 % Initialize graphics 15 16 [surfplot,top,start,stop] = initgraphics(n); 17 18 % Outer loop, restarts. 19 20 while get(stop,'value') == 0 21 set(start,'value',0) 22 23 H = ones(n+2,n+2); U = zeros(n+2,n+2); V = zeros(n+2,n+2); 24 Hx = zeros(n+1,n+1); Ux = zeros(n+1,n+1); Vx = zeros(n+1,n+1); 25 Hy = zeros(n+1,n+1); Uy = zeros(n+1,n+1); Vy = zeros(n+1,n+1); 26 ndrop = ceil(rand*ndrops); 27 nstep = 0; 28 29 % Inner loop, time steps. 30 31 while get(start,'value')==0 && get(stop,'value')==0 32 nstep = nstep + 1;