数学建模美赛集训的时候要用到一个海面模拟,分享一下海面模拟的MATLAB代码

先贴一下结果图:

下面是源代码~~~

 function waterwave

 n = 64;                  % grid size
g = 9.8; % gravitational constant
dt = 0.01; % hardwired timestep
dx = 1.0;
dy = 1.0;
nplotstep = 8; % plot interval
ndrops = 5; % maximum number of drops
dropstep = 500; % drop interval
D = droplet(1.5,21); % simulate a water drop % Initialize graphics [surfplot,top,start,stop] = initgraphics(n); % Outer loop, restarts. while get(stop,'value') == 0
set(start,'value',0) H = ones(n+2,n+2); U = zeros(n+2,n+2); V = zeros(n+2,n+2);
Hx = zeros(n+1,n+1); Ux = zeros(n+1,n+1); Vx = zeros(n+1,n+1);
Hy = zeros(n+1,n+1); Uy = zeros(n+1,n+1); Vy = zeros(n+1,n+1);
ndrop = ceil(rand*ndrops);
nstep = 0; % Inner loop, time steps. while get(start,'value')==0 && get(stop,'value')==0
nstep = nstep + 1; % Random water drops
if mod(nstep,dropstep) == 0 && nstep <= ndrop*dropstep
w = size(D,1);
i = ceil(rand*(n-w))+(1:w);
j = ceil(rand*(n-w))+(1:w);
H(i,j) = H(i,j) + rand*D;
end % Reflective boundary conditions
H(:,1) = H(:,2); U(:,1) = U(:,2); V(:,1) = -V(:,2);
H(:,n+2) = H(:,n+1); U(:,n+2) = U(:,n+1); V(:,n+2) = -V(:,n+1);
H(1,:) = H(2,:); U(1,:) = -U(2,:); V(1,:) = V(2,:);
H(n+2,:) = H(n+1,:); U(n+2,:) = -U(n+1,:); V(n+2,:) = V(n+1,:); % First half step % x direction
i = 1:n+1;
j = 1:n; % height
Hx(i,j) = (H(i+1,j+1)+H(i,j+1))/2 - dt/(2*dx)*(U(i+1,j+1)-U(i,j+1)); % x momentum
Ux(i,j) = (U(i+1,j+1)+U(i,j+1))/2 - ...
dt/(2*dx)*((U(i+1,j+1).^2./H(i+1,j+1) + g/2*H(i+1,j+1).^2) - ...
(U(i,j+1).^2./H(i,j+1) + g/2*H(i,j+1).^2)); % y momentum
Vx(i,j) = (V(i+1,j+1)+V(i,j+1))/2 - ...
dt/(2*dx)*((U(i+1,j+1).*V(i+1,j+1)./H(i+1,j+1)) - ...
(U(i,j+1).*V(i,j+1)./H(i,j+1))); % y direction
i = 1:n;
j = 1:n+1; % height
Hy(i,j) = (H(i+1,j+1)+H(i+1,j))/2 - dt/(2*dy)*(V(i+1,j+1)-V(i+1,j)); % x momentum
Uy(i,j) = (U(i+1,j+1)+U(i+1,j))/2 - ...
dt/(2*dy)*((V(i+1,j+1).*U(i+1,j+1)./H(i+1,j+1)) - ...
(V(i+1,j).*U(i+1,j)./H(i+1,j)));
% y momentum
Vy(i,j) = (V(i+1,j+1)+V(i+1,j))/2 - ...
dt/(2*dy)*((V(i+1,j+1).^2./H(i+1,j+1) + g/2*H(i+1,j+1).^2) - ...
(V(i+1,j).^2./H(i+1,j) + g/2*H(i+1,j).^2)); % Second half step
i = 2:n+1;
j = 2:n+1; % height
H(i,j) = H(i,j) - (dt/dx)*(Ux(i,j-1)-Ux(i-1,j-1)) - ...
(dt/dy)*(Vy(i-1,j)-Vy(i-1,j-1));
% x momentum
U(i,j) = U(i,j) - (dt/dx)*((Ux(i,j-1).^2./Hx(i,j-1) + g/2*Hx(i,j-1).^2) - ...
(Ux(i-1,j-1).^2./Hx(i-1,j-1) + g/2*Hx(i-1,j-1).^2)) ...
- (dt/dy)*((Vy(i-1,j).*Uy(i-1,j)./Hy(i-1,j)) - ...
(Vy(i-1,j-1).*Uy(i-1,j-1)./Hy(i-1,j-1)));
% y momentum
V(i,j) = V(i,j) - (dt/dx)*((Ux(i,j-1).*Vx(i,j-1)./Hx(i,j-1)) - ...
(Ux(i-1,j-1).*Vx(i-1,j-1)./Hx(i-1,j-1))) ...
- (dt/dy)*((Vy(i-1,j).^2./Hy(i-1,j) + g/2*Hy(i-1,j).^2) - ...
(Vy(i-1,j-1).^2./Hy(i-1,j-1) + g/2*Hy(i-1,j-1).^2)); % Update plot
if mod(nstep,nplotstep) == 0
C = abs(U(i,j)) + abs(V(i,j)); % Color shows momemtum
t = nstep*dt;
tv = norm(C,'fro');
set(surfplot,'zdata',H(i,j),'cdata',C);
set(top,'string',sprintf('t = %6.2f, tv = %6.2f',t,tv))
drawnow
end if all(all(isnan(H))), break, end % Unstable, restart
end
end
close(gcf) % ------------------------------------ function D = droplet(height,width)
% DROPLET 2D Gaussian
% D = droplet(height,width)
[x,y] = ndgrid(-1:(2/(width-1)):1);
D = height*exp(-5*(x.^2+y.^2)); % ------------------------------------ function [surfplot,top,start,stop] = initgraphics(n);
% INITGRAPHICS Initialize graphics for waterwave.
% [surfplot,top,start,stop] = initgraphics(n)
% returns handles to a surface plot, its title, and two uicontrol toggles. clf
shg
set(gcf,'numbertitle','off','name','Shallow_water')
x = (0:n-1)/(n-1);
surfplot = surf(x,x,ones(n,n),zeros(n,n));
grid off
axis([0 1 0 1 -1 3])
caxis([-1 1])
shading faceted
c = (1:64)'/64;
cyan = [0*c c c];
colormap(cyan)
top = title('Click start');
start = uicontrol('position',[20 20 80 20],'style','toggle','string','start');
stop = uicontrol('position',[120 20 80 20],'style','toggle','string','stop');

海面波浪模拟 MATLAB的更多相关文章

  1. PhoenixFD插件流体模拟——UI布局【Dynamics】详解

    流体动力学 本文主要讲解Dynamics折叠栏中的内容.原文地址:https://docs.chaosgroup.com/display/PHX3MAX/Liquid+Dynamics 主要内容 Ov ...

  2. 合金装备V 幻痛 制作技术特辑

    合金装备V:幻痛 制作特辑 资料原文出自日版CGWORLD2015年10月号   在[合金装备4(Metal Gear Solid IV)]7年后,序章作品[合金装备5 :原爆点 (Metal Gea ...

  3. 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。

    [SIGGRAPH 2015][巫师3 狂猎 The Witcher 3: Wild Hunt ]顶级的开放世界游戏的实现技术 作者:西川善司 日文链接  http://www.4gamer.net/ ...

  4. matplotlib笔记2

    颜色和样式 八种内建默认颜色缩写b:blue g:green r:red c:cyan m:magenta y:yellow k:black w:white其它颜色表示方法可以参照百度给的值https ...

  5. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二) 代码工程地址: https:/ ...

  6. MATLAB模拟布丰投针实验

    MATLAB模拟布丰投针实验 标签(空格分隔): 算法 Buffon's Needle 桌面上有距离为a的若干平行线,将长度为L的针随机丢在桌面上,则这根针与平行线相交的概率是多少?假定L < ...

  7. 多普勒失真信号采样Matlab模拟分析

    多普勒失真信号采样Matlab模拟分析 方案 水声通信指的是使用声信号在水中数据传输. 相对而言.电磁信号在水中吸收严重衰减过快,光信号受水中悬浮颗粒的影响,也无法完毕远距离传输. 这两种信号的传播距 ...

  8. Matlab学以致用--模拟os任务装载情况

    2012-06-08 21:26:42 用matlab来建模,仿真不同时刻os task在队列中的装载情况.输入参数如下 作为初学者,M文件写的有点长.能实现功能就算学以致用了. clear;clc ...

  9. 【matlab】模拟变焦拼接代码备份

    1.初版,边缘未处理. % % In----near % If----far % In=imread('D:\文件及下载相关\桌面\模拟变焦拼接\Matlab_code\nearframe\frame ...

随机推荐

  1. recycle bin tip

    if you have a question about recycle bin that can look the follow link; http://www.dba-oracle.com/t_ ...

  2. August 09th 2017 Week 32nd Wednesday

    Find hope from despair, life will become brilliant. 从绝望中寻找希望,人生终将辉煌. Have you ever seen the movie Ba ...

  3. December 10th 2016 Week 50th Saturday

    Storms make trees take deeper roots. 风暴使树木深深扎根. Sometimes, you may feel frustrated for failing to wi ...

  4. [EffectiveC++]item12:copy all parts of an object

    在小书C++中,4.2.2 派生类的构造函数和析构函数的构造规则(103页) 在定义派生类对象时,构造函数执行顺序如下: 基类的构造函数 对象成员的构造函数 派生类的构造函数.

  5. 内存池-转载自IBM

    [转载自IBM]讲的很好~推荐看看 6.1 自定义内存池性能优化的原理 如前所述,读者已经了解到"堆"和"栈"的区别.而在编程实践中,不可避免地要大量用到堆上的 ...

  6. C对64位整数类型的支持

    在使用C语言过程中可能需要接触长整数类型,其中包括固定长度数据类型的声明.输入输出函数的标志符等细节,在此记录. int64_t 与 uint64_t C的标准只规定特定数据类型需要实现的最小长度,特 ...

  7. SpringMVC 多文件上传

    springMVC.xml 配置 <bean id="multipartResolver" class="org.springframework.web.multi ...

  8. Monkeyrunner测试小实践

    环境搭建完成后,我们通过命令打开模拟器,前提是在Eclipse中创建了一个模拟器 (1)cmd命令:emulator -avd 模拟器名称 启动了模拟器,此时你就会看到一个安卓模拟器的弹出 (2)cm ...

  9. CodeForces - 999D Equalize the Remainders (模拟+set)

    You are given an array consisting of nn integers a1,a2,…,ana1,a2,…,an , and a positive integer mm . ...

  10. Template Method(模板方法)模式

    1.概述 在面向对象开发过程中,通常我们会遇到这样的一个问题:我们知道一个算法所需的关键步骤,并确定了这些步骤的执行顺序.但是某些步骤的具体实现是未知的,或者说某些步骤的实现与具体的环境相关.例子1: ...