海面波浪模拟 MATLAB
数学建模美赛集训的时候要用到一个海面模拟,分享一下海面模拟的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的更多相关文章
- PhoenixFD插件流体模拟——UI布局【Dynamics】详解
流体动力学 本文主要讲解Dynamics折叠栏中的内容.原文地址:https://docs.chaosgroup.com/display/PHX3MAX/Liquid+Dynamics 主要内容 Ov ...
- 合金装备V 幻痛 制作技术特辑
合金装备V:幻痛 制作特辑 资料原文出自日版CGWORLD2015年10月号 在[合金装备4(Metal Gear Solid IV)]7年后,序章作品[合金装备5 :原爆点 (Metal Gea ...
- 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。
[SIGGRAPH 2015][巫师3 狂猎 The Witcher 3: Wild Hunt ]顶级的开放世界游戏的实现技术 作者:西川善司 日文链接 http://www.4gamer.net/ ...
- matplotlib笔记2
颜色和样式 八种内建默认颜色缩写b:blue g:green r:red c:cyan m:magenta y:yellow k:black w:white其它颜色表示方法可以参照百度给的值https ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二)
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二) 代码工程地址: https:/ ...
- MATLAB模拟布丰投针实验
MATLAB模拟布丰投针实验 标签(空格分隔): 算法 Buffon's Needle 桌面上有距离为a的若干平行线,将长度为L的针随机丢在桌面上,则这根针与平行线相交的概率是多少?假定L < ...
- 多普勒失真信号采样Matlab模拟分析
多普勒失真信号采样Matlab模拟分析 方案 水声通信指的是使用声信号在水中数据传输. 相对而言.电磁信号在水中吸收严重衰减过快,光信号受水中悬浮颗粒的影响,也无法完毕远距离传输. 这两种信号的传播距 ...
- Matlab学以致用--模拟os任务装载情况
2012-06-08 21:26:42 用matlab来建模,仿真不同时刻os task在队列中的装载情况.输入参数如下 作为初学者,M文件写的有点长.能实现功能就算学以致用了. clear;clc ...
- 【matlab】模拟变焦拼接代码备份
1.初版,边缘未处理. % % In----near % If----far % In=imread('D:\文件及下载相关\桌面\模拟变焦拼接\Matlab_code\nearframe\frame ...
随机推荐
- Linux常用基本命令大全
1 一般情况下,自己安装linux系统都会选择简易版,这里面少很多命令,所以需要安装其他的包 yum install openssh-clients 安装scp的软件包 2 把当前一 ...
- Linux提权后获取敏感信息的方法与途径
在本文开始之前,我想指出我不是专家.据我所知,在这个庞大的区域,没有一个“神奇”的答案.分享,共享(我的出发点).下面是一个混合的命令做同样的事情,在不同的地方,或只是一个不同的眼光来看待事物.我知道 ...
- java多态实例
学校有两个打印机,一个彩印,一个黑白印,都打印输出 public class printerDemo { public static void main(String[] args) { colorP ...
- Mac Sublime Text 3
安装Package Control安装过程: 使用快捷键 control + ` 或者菜单栏选择View > Show Console安装Package Control参考官方页面.Sublim ...
- C语言支持的四种变量存储类型
http://blog.csdn.net/zhandoushi1982/article/details/5425835 一)auto:auto称为自动变量(局部变量).局部变量是指在函数内部说明的变量 ...
- 【[POI2010]ANT-Antisymmetry】
开始复习字符串了 第一步肯定得是\(hash\) 首先理性分析一波不可能出现长度为奇数的反回文串,对称轴位置取反之后肯定和原来不相等了 我们可以枚举所有回文串的对称中心,之后我们发现这个样子是具有单调 ...
- POJ3907 Build Your Home
嘟嘟嘟 题意:按逆时针或顺时针给出一个多边形,求面积. 解法:直接套用公式:\(S = \frac{1}{2}|\sum _ {i = 1} ^ {n} {v_i \times v_{i + 1}}| ...
- indexzero/http-server-2-使用
所以在ethereumjs-vm/examples/run-transactions-simple例子中要怎么使用http-server 1.首先在ethereumjs-vm/examples/run ...
- Factory(工厂)模式
设计模式一 工厂模式Factory 在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.但是在一些情况下, new操作符直接生成对象会带来一些问题. ...
- ArrayList两个对象之间的赋值
List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList&l ...