5 计算机模拟实验.docx
《5 计算机模拟实验.docx》由会员分享,可在线阅读,更多相关《5 计算机模拟实验.docx(9页珍藏版)》请在冰豆网上搜索。
![5 计算机模拟实验.docx](https://file1.bdocx.com/fileroot1/2023-2/3/59d7d836-98c0-4c41-8391-0e3bea60ac1b/59d7d836-98c0-4c41-8391-0e3bea60ac1b1.gif)
5计算机模拟实验
计算机模拟实验
实验目的
实验指导
模拟的概念
模拟就是利用物理的、数学的模型来类比、模仿现实系统及其演变过程,以寻求过程规律的一种方法。
模拟的基本思想是建立一个试验模型,这个模型包含所研究系统的主要特点.通过对这个实验模型的运行,获得所要研究系统的必要信息。
模拟的方法
1、物理模拟:
对实际系统及其过程用功能相似的实物系统去模仿。
例如,军事演习、船艇实验、沙盘作业等。
物理模拟通常花费较大、周期较长,且在物理模型上改变系统结构和系数都较困难。
而且,许多系统无法进行物理模拟,如社会经济系统、生态系统等。
2、数学模拟
在一定的假设条件下,运用数学运算模拟系统的运行,称为数学模拟。
现代的数学模拟都是在计算机上进行的,称为计算机模拟。
计算机模拟可以反复进行,改变系统的结构和系数都比较容易。
在实际问题中,面对一些带随机因素的复杂系统,用分析方法建模常常需要作许多简化假设,与面临的实际问题可能相差甚远,以致解答根本无法应用。
这时,计算机模拟几乎成为唯一的选择。
time=0;tt=0;
valume=2000;
total=2;
rate=N[total/valume,7];
Print["time","","valume","","total","","rate"];
While[rate<0.2,
time=time+1;valume=valume+2;
total=total+3-4rate;
rate=total/valume;
If[time>=10,
Print[tt+time,"",valume,"",N[total,3],"",N[rate,3]];
tt=tt+time;
time=0]
];
Print[tt+time,"",valume,"",N[total,3],"",N[rate,3]];
note:
设t时刻的盐水浓度为r(t),水池中的含盐量为y(t),则
t时刻的盐水量是2000+(6-4)t=2000+2t,故
y(t)=(2000+2t)r(t),
在[t,t+△t]时间内,容器内盐量的改变量是:
y(t+△t)-y(t)=(2000+2t+2△t)r(t+△t)-(2000+2t)r(t)
=(6*0.5-4r(t))△t
(2000+2t)r'(t)+6r(t)=3
DSolve[{r'[t]==(3-6r[t])/(2000+2t),r[0]==0.001},r[t],t]
r=r[t]/.%[[1]]
Plot[r,{t,0,200}]
t=12.;dt=0.02;v=1;n=t/dt;
robit={{{0,10}},{{10,10}},{{10,0}},{{0,0}}};
For[j=1,j<=n,j++,
For[i=1,i<=4,i++,
xx1=robit[[i,j,1]];
yy1=robit[[i,j,2]];
If[i!
=4,xx2=robit[[i+1,j,1]];
yy2=robit[[i+1,j,2]],
xx2=robit[[1,j,1]];
yy2=robit[[1,j,2]]];
dd=Sqrt[(xx2-xx1)^2+(yy2-yy1)^2]//N;
xx1=xx1+v*dt*(xx2-xx1)/dd;
yy1=yy1+v*dt*(yy2-yy1)/dd;
robit[[i]]=
Append[robit[[i]],{xx1,yy1}]
]
];
g=Graphics[
{Line[robit[[1]]],Line[robit[[2]]],
Line[robit[[3]]],Line[robit[[4]]],
Line[{{0,0},{0,10},{10,10},{10,0},{0,0}}]}];
Show[g,AspectRatio->Automatic]
note:
Animation
For[i=1,i<=600,i=i+10,g=Graphics[
{Line[Take[robit[[1]],i]],Line[Take[robit[[2]],i]],
Line[Take[robit[[3]],i]],Line[Take[robit[[4]],i]],
Line[{{0,0},{0,10},{10,10},{10,0},{0,0}}]}];
Show[g,AspectRatio->Automatic]]
gotonext[x_]:
=
x+{Random[Real,{-1,1}],Random[Real,{-1,1}]};
rdmmove[n_]:
=Module[{location},
location=NestList[gotonext,{0,0},n];
Show[Graphics[{Line[location]}],
AspectRatio->Automatic,
PlotRange->All]]
rdmmove[50]
rdmmove[1000]
note:
Animation
location=NestList[gotonext,{0,0},100];
mm=Min[location];nn=Max[location];
For[i=1,i<=100,i++,
Show[Graphics[{Line[Take[location,i]]}],
AspectRatio->Automatic,AxesOrigin->{0,0},
PlotRange->{{mm,nn},{mm,nn}}]]
pts[n_]:
=Table[{Random[],Random[]},{k,n}]
ListPlot[pts[1000],
PlotStyle->PointSize[0.003],
AspectRatio->Automatic]
rdmIntegrate[n_]:
=Module[{k,p=0.0},
Do[If[Random[]<=Sqrt[1-Random[]^2],k=1,k=0];
p=p+k,{x,n}];
Return[p/n]]
4*rdmIntegrate[1000]
note:
?
[a,b]