教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 高等教育 >

上海交通大学python期末大作业题目(姚天昉)(2)

来源:网络收集 时间:2026-07-29
导读: Choose a random molecule (remember, the demon cannot be chosen here) Generate a random number between -Dv and Dv, where Dv is the velocity corresponding to 10% of the total system energy divided by N

Choose a random molecule (remember, the demon cannot be chosen here)

Generate a random number between -Dv and Dv, where Dv is the velocity corresponding to 10% of the total system energy divided by N. Thus, Dv = sqrt(2 * totalEnergy / N / 10).

Calculate the change in energy. Since we use unit mass (m = 1), the change in energy is deltaEnergy = vNew² / 2 - vOld² / 2.

If the change in energy is valid, then accept the change in velocity for that molecule. Remember that the change in energy is valid as long as the resulting demon energy is non-negative. Because not every change is legal, every iteration may not change N molecules, even though it executes N trials.

The demon and system energies must be updated to use in the next trial. However, since we must generate visuals for the next part of the project, we need to record these values in a list to keep track of the value at each iteration (not trial).

Calculate the average system energy and return it.

Experiments for the Ideal Gas Simulation

姚天昉是上海交大一位非常有名的计算机老师,期末大作业非常难,给弟弟妹妹们透露下。ps:这是大作业一,另有大作业二即将公布,敬请关注!

First, write and test the function ideal_gas. Note that in this project you will be handing in the function and the experimental work at the same time. Test the function ideal_gas with small values of N and small values of step. You won't know that you are computing the correct quantities until you generate the plots described next. Test the function with both initial system states.

Your experimental work should consider systems with N= 50 to 500, with an increment of 50. For each N, run the function ideal_gas with steps = 3000 and totalEnergy = 500. Each experiment should be done for each of the two initial system states.

The simulation can use MatPlotLib or VPython. Warning: If you use VPython, you should remove all traces of MatPlotLib from the skeleton code. The two have not been known to play well together. A starting point for the varying values of N is provided in the main of the skeleton code. This part of the project requires you to write code to draw two graphs and two histograms.

The main of the simulation uses the values returned by ideal_gas to generate the graph N_versus_Energy. The value returned by the function ideal_gas represents the average system energy of a simulation with particular value of N. You should graph the average system energy on the y-axis and N on the x-axis. In the same plot, show the total system energy in a different color (in the required experiment it is 500 for every value of N). You can generate the plot after the entire simulation or generate it incrementally after ideal_gas returns a value. Here is an example

plot. nvsenergy1.png

The ideal_gas function should generate two histograms and one graph. Whether and when these plots are generated is determined by the fifth parameter (visuals) of ideal_gas. For example, ideal_gas(N, 500, 3000, 2, N==500) will generate plots only when N=500 and start in initial state

2. Note that omitting the 5th argument will plot every time ideal_gas is called.

Histogram Final_Molecule_Velocity uses the values of the N molecule velocities after steps iterations of ideal_gas (i.e., the values just before ideal_gas returns). Plot the velocities on x-axis and their frequencies on y-axis. You should use the signed velocities and 0 is thus in the center of the x-axis. You should experiment with the most appropriate number of bins. Here is an example plot. velocitydistr1.png

姚天昉是上海交大一位非常有名的计算机老师,期末大作业非常难,给弟弟妹妹们透露下。ps:这是大作业一,另有大作业二即将公布,敬请关注!

Histogram Demon_Energy generates a histogram of the observed demon energy. The plot is generated after the steps of ideal_gas are completed. The demon energy is recorded after each simulation step (using a list or array of size step). Show the demon energies on the x-axis and their frequencies on the y-axis. Again, select an appropriate number of bins. Here is an example plot.

demonenergy1.png

Graph Demon_Energy_Time shows the steps demon energies over time. The graph has values from 1 to steps on the x-axis and the corresponding demon energies on the y-axis. Make sure to show these 3000 steps in an effective way.

If you are using VPython, you can show a histogram plot as soon you have data needed. If you are using Matplotlib, the interactive option allows showing results during the computation (See Interactive Matplotlib):

Interactive Matplotlib

Normally, matplotlib will not show plots until you call pylab.show(). However, if you call pylab.ion() before plotting, matplotlib will show the window immediately. For best results, decorate (set title, axis labels, etc) after calling pylab.plot().

Example code:

import pylab

姚天昉是上海交大一位非常有名的计算机老师,期末大作业非常难,给弟弟妹妹们透露下。ps:这是大作业一,另有大作业二即将公布,敬请关注!

xs = []

ys = []

pylab.ion()

for x in range(10):

xs.append(x)

ys.append(x ** 2)

pylab.plot(xs, ys, 'b-o')

pylab.pause(1)

pylab.title("curve of x squared")

pylab.xlabel("x")

pylab.ylabel("x ** 2")

pylab.show()

pylab.pause(10)

pylab.close()

Discussion

You need to hand in a discussion of your experimental results. The following questions should be addressed:

In graph N_versus_Energy, how close is the actual system energy to the total energy? How does it change as N changes? How do the plots differ for the two different initial system states?

Using histogram Final_Particle_Velocity, what kind of distribution do you judge the velocities to come from? How close is the molecule velocity to the initial velocity of sqr …… 此处隐藏:5452字,全部文档内容请下载后查看。喜欢就下载吧 ……

上海交通大学python期末大作业题目(姚天昉)(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/127270.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)