数学建模美赛集训的时候要用到一个海面模拟,分享一下海面模拟的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. Centos7 下编译 Openjdk8

    本文主要介绍如何在 Centos7 下编译 Openjdk8 源码,<深入理解java虚拟机>第二版网上好多 openjdk7 的帖子,编译 jdk8 和 7 还是有些差别的,比如大家经常 ...

  2. CentOS7使用systemctl添加自定义服务

    一.简介 Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos 5 6已经是实质差别.systemd是靠管理unit的方式来控制开机服务,开机级别等功能. 在/u ...

  3. 乘风破浪:LeetCode真题_005_Longest Palindromic Substring

    乘风破浪:LeetCode真题_005_Longest Palindromic Substring 一.前言 前面我们已经提到过了一些解题方法,比如递推,逻辑推理,递归等等,其实这些都可以用到动态规划 ...

  4. December 12th 2016 Week 51st Monday

    Nothing is impossible for a willing heart. 心之所愿,无所不成. I wish I can be a strong, clever, powerful and ...

  5. update from select

    CREATE TABLE dualx( x_id ) NOT NULL , x_con ) ) CREATE TABLE dualy( y_id ) NOT NULL , y_con ) ) ','x ...

  6. vue项目出现的错误汇总

    报错一: expected "indent", got "!" 通过vue-cli创建的项目,不需要在webpack.base.conf.js中再手动配置关于c ...

  7. 将git关联到pycharm

    1. 安装pycharm 2. 安装git 3. 打开pycharm, 点钟File -> settings -> Version Control -> Git 该面板中,填写git ...

  8. collecitons.deque

    python队列,可以使用collections里面的deque,将列表当作队列使用. deque方法有: from collections import deque deque.append() d ...

  9. 【HNOI2009】梦幻布丁

    题目描述 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. 输入输出格式 输入格式 第一 ...

  10. [Python 网络编程] TCP编程/群聊服务端 (二)

    群聊服务端 需求分析: 1. 群聊服务端需支持启动和停止(清理资源); 2. 可以接收客户端的连接; 接收客户端发来的数据 3. 可以将每条信息分发到所有客户端 1) 先搭架子: #TCP Serve ...