计算机仿真实验_第1页
计算机仿真实验_第2页
计算机仿真实验_第3页
计算机仿真实验_第4页
计算机仿真实验_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

计算机仿真实验

1.射箭问题

某人每轮向靶子射10箭,已知其击中靶心的概率为25%,问一轮中

集中3,5,7箭的概率。

设X表示10次射箭中击中靶心的次数,

X~b(10,0.25)

理论值为:

37

P(X=3)=CIO(O.25)(0.75)=0.25028228759765625

P(X=5)=C?O(0.25)5(0.75)5=0.058399200439453125

P(X=7)=Clo(O-25)7(O.75)3=0.00308990478515625

计算机仿真:

1.产生均匀分布随机数0.00-0.99(100个),某个数字出现的概率相等。若产生1000个这样的数,

(1)数值为0.00〜0.24大约会有250个,比例大约为0.25

(2)数值为0.25~0.99大约会有750个,比例大约为0.75

2.以每产生•个随机数代表射1箭,若产生的随机教小于0.25,则代表击中靶心,如果产生的随机数

大于或等于。25,则表示没有击中靶心。

若实验的次数很多(多大于1000),则•击中靶心的频率接近于25%。若实验的次数无限多,m

靶心的概率等于25%。

3.确定一轮中击中7箭的概率

(1)每轮由计算机产生10个均匀分布的随机数Ni(i=L2,3,-10),代表射10箭

(2)其中额i小于0.25为击中,记下该轮中击中的次数

(3)重复(1)一(2),进行K轮实验

(4)找出K轮中所有每轮击中7次的总轮数M,则K轮中每轮击中7箭的频率为M/K,若K趋向无

穷大时,M/K为每轮击中7箭的概率。

publicdoubleshoot(intn,intnumber){

doubleresult=0;

for(inti=l;i<=number;i++){

intnum=0;

for(intj=l;j<=10;j++){

if(newRandom().nextDouble()<=0.24)

num++;

}

if(num=:=n)result";

)

return(result/number);

}

仿真结果:

X=3时p=0.2459

X=5时p=0.0564

X=7时p=0.0033

2.赶火车过程仿真

一列火车从A站经过B站开往C站,某人每天赶往B站乘这趟火车。

已知火车从A站到B站运行时间为均值30分钟、方差为2分钟的正

态随机变量。火车大约在下午1点离开A站。离开时刻的频率分布为

出发时1:001:051:10

刻(T)

频率0.70.20.1

这个人到达B站时刻的频率分布为

到达时刻(T)1:281:301:321:34

频率0.30.40.20.1

问他能赶上火车(比火车先到达B站)的概率有多大?

引入以下变量:

X火车从A站开出的时刻;

Y火车从A站运行到B站所需要的时间;

Z此人到达B站的时刻;

此人能赶上火车的条件是Z<X+Y

Y〜N(30,2)

令丫

Ti=X+FTI(t)=P(h<t)=P(X+Y<t)

FT1(t)=p(x=o)p(y<t)+p(x=5)p(y<t-5)

+p(x=IO)P(Y<t-10)

=0.7&(t)+0.2Fx(t-5)+0.1f>(t-10)

令=T1-ZF?2(0=P(T2<0=P(T1-z<t)

FRQ)=P(z=28)P(T1<t+28)+P(Z=30)P(Y<t+30)

+P(Z=32)P(Y<t4-32)

+P(Z=34)P“<t+34)

=0.3FT1(t+28)+0.4F?i(t+30)+0.2FTi(t+32)+OAF^t+34)

理论值为1-FT2(0)=1-0.3FTI(28)-0.4FTI(30)-0.2Fn(32)-

0.1FTI(34)

=1-0.3[0.7f>(28)+0.2&(23)4-

0.1&(18)]-

0.4[0.7&(30)+0.2及(25)+

0.1及(20)卜

0.2[0.7f>(32)+0.2以(27)4-

0.1&(22)]-

0.1[0.7&(34)+0.2&(29)+

0.1&(24)]

=1-0.310.7中偿)+0.2中层)+

0.1中(尚卜

0.4[0.7①(0)+0.2中信)+

0.1①倒-

0.2[0.7①隐)+0.2①偿)+

0」中融

0.1@7中偿)+0.2①偿)+

0.1中(割

=1-0.3*(0.7*(1-0.927)+0.2*0+0.1*0)

-0.4*(0.7*0.5+0.2*0+0.1*0)-0.2(0.7*0.9207+0.2*0.0170+0.1*0)

-0.1(0.7*0.9976+0.2*0.2206+0.1*0)=0.640848

理论值为0.640848

计算机仿真:

若t是一个均匀分布的随机数,则取

X=0,if0<t<=0.7,

X=5,if0.7<t<=0.9,

X=10,if0.9<t<=1,

同理,取

Z=28,if0<t<=0.3,

Z=30,if0.3<t<=0.7,

Z=32,if0.7<t<=0.9,

Z=34,if0.9<t<=1,

再产生正态分布的随机数T2,模拟该人n次赶火车

计算P(T3<T1+T2)得到是否能赶上火车的概率大小。

核心代码:

publicdoublerinding(intnumber,List<Node>s){

doublex=0.0,y=0.0,z=0.0,n=0.0;

Listlist=newArrayList();

for(inti=l;i<=number;i++){

doubleb=newRandom().nextDouble();

if(b<0.7)x=0.0;

if(b<0.9&8b>=0.7)x=5.0;

if(b>=0.9)x=10.0;

doubleyl=0.0;

for(intj=l;j<=6;j++)(

yl+=(newRandom().nextDouble()-newRandom().nextDouble());

}

y=yl*Math.pow(2,0.5)+30;

b=newRandom().nextDouble();

if(b<0.3)z=28.0;

if(b<0.7&8b>=0.3)z=30.0;

if(b<0.9&8b>=0.7)z=32.0;

if(b>=0.9)z=34.0;

list.add(x+y-z);

if(x+y-z>=0){

//System.out.println(x+y-z);

n++;

}

)

Collections.sort(list);

doublemin=(double)list.get(0);

doublemax=(double)list.get(list.size()-1);

doubled=(max-min)/10;

System.out.println(list.size());

intj=0;

for(inti=l;i<=10;i++){

intm=0;

for(;j<list.size()-1;j++)

(

doubleq=(double)list.get(j);

if(q>=min&&q<=min+d)m++;

elsebreak;

)

s.add(newNode(String./or/??at("%.2f",min)+"-

"+String.format("%.,min+d),m+""));

min=min+d;

}

return(n/number);

}

}

仿真模拟后赶上火车的概率为0.6394

X+Y-Z的大致分布情况

3.炮排射击命中率问题

自我方前沿防守地域,敌人以一个炮排(含两门火炮)为单位对

我方进行干扰和破坏。为躲避我放打击,敌方对其阵地进行了伪装并

经常变换涉及地点。经过长期的观察发现,我方指挥所对敌方目标的

指示有50%是准确的,而我方火力单位,在指示正确时,有1/3的射

击效果能毁坏敌人一门火炮,有1/6的射击效果能全部消灭敌人。

现在希望能用目中方式把我方将要对敌人实施的20次打击效果显示

出来,确定有效地射击率及会上敌方火炮的平均值。

仿真模型:

将指示正确与否比作投硬币,各有1/2的概率,则在计算

机上随机产生一个随机数,如果小于1/2,则表示预测正确,

大于则不正确。将击毁敌方火炮数比作掷筛子,在计算机上产

生一个随机数,将0~1之间的随机数分成6份,前3份表示没

有击中敌人,中间两份表示击毁一门火炮,最后一份表示击毁

两门火炮,则仿真结束。

设E:有效射击比率

E1:n次射击平均每次毁伤敌人的火炮数

理论值:E=1*(i+i)=i

23263

核心代码:

doubledl,d2;

intx,y;

publicStringattack(Stringstr[][],intnumber){

for(inti=0;i<number;i++){

Strings[]=newString[8];

s[0]=i+l+"";

x=(int)(newRandom().nextDouble()*2);

if(x>=l)

{s[T]="正

y=(int)(newRandom().nextDouble()*6);

s[4]=y+"";

if(y==3||y==4){

dl4-+;s[6]="V";

}

if(y==5){

d2++;s[7]="V";

}

s[5]=W";

)

else{

S[1]="M";S[3]="V";

}

str[i]=s;

}

return((dl+d2)/number)+"^n"+(dl/number+2*d2/number);

}

回计算仿真作业回

射箭问题赶火车问题炮排射击

..lx]

□炮排射击间找....

实验次数确定]

有效射击率和击伤平均数

实验序号投硬市结J指示正确掷骰子结2_3

1向

2

3

4

5

6

7

9

10

仿真模拟结果有效射击比率0.2553,平均每次毁伤敌人的火炮数

0.3407

附录:

Simulation,java

importjava.awt.BorderLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.util.ArrayList;

importjava.util.List;

importjavax.swing.ComboBoxModel;

importjavax.swing.DefaultcomboBoxModel;

importjavax.swing.JButton;

importjavax.swing.JComboBox;

importjavax.swing.JDesktopPane;

importjavax.swing.3InternalFrame;

importjavax.swing.JLabel;

importjavax.swing.JLayeredPane;

importjavax.swing.DScrollPane;

importjavax.swing.JTable;

importjavax.swing.JTextField;

importjavax.swing.JToolBar;

importjavax.swing.Windowconstants;

importjavax.swing.table.DefaultTableModel;

importjavax.swing.table.TableModel;

importjavax.swing.Swingutilities;

importorg.jfree.chart.ChartPanel;

importorg.jfree.chart.JFreeChart;

importorg.jfree.ui.ApplicationFrame;

publicclassSimulationextendsjavax.swing.JFrame{

{

try(

javax.swing.UIManager.setLoo/?4ndFeeL("com.jgoodies.looks.plastic.Plastic3DLDokA

ndFeel");

}catch(Exceptione){

e.printStackTrace();

)

)

privateDToolBarjToolBarl;

privateJButtonQI;

privateDInternalFrameQA1;

privateJLabeljLabelS;

privateDTextFieldanswer2;

private3LabeljLabel7;

privateJButtonstart2;

privateDTextFieldnumber2;

privateDLabeljLabel6;

privateDInternalFrameQA2;

privateJTabletable;

privateJScrollPanejScrollPanel;

privateJTextFieldanwer31;

privateJTextFieldnumbers;

privateJLabeljLabel4;

private^Buttonstart3;

privateDInternalFrameQA3;

privateDTextFieldanswerl;

privateJLabeljLabel3;

privateJLabeljLabel2;

privateJTextFieldnumberl;

privateJComboBoxselecttion;

privateJLabeljLabell;

privateJButtonstartl;

privateJButtonQ3;

privateJButtonQ2;

privateJDesktopPanejDesktopPanel;

privateJFreeChartchart;

publicstaticvoidmain(String[]args){

Swingutilities.invo/?e£ater(newRunnable(){

publicvoidrun(){

Simulationinst=newSimulation。;

inst.setLocationRelativeTo(null);

inst.setVisible(true);

)

});

)

publicSimulation(){

super。;

initGUI();

)

publicvoidQA1()

{

QA1=new3InternalFrame();

jDesktopPanel.add(QAl,3LayeredPane.DEFAULT_LAYER);

QAl.setBounds(19,16,554,141);

QA1.setFont(newjava.awt.Font("SimplifiedArabicFixed",0,9));

QAl.setTitle("\u5c04\u7bad\u95ee\u9898");

QA1.setVisible(true);

{

startl=new3Button();

QAl.getContentPane().setLayout(null);

QAl.setClosable(true);

QA1.getContentPane().add(startl);

startl.setText("\u786e\u5b9a");

startl.setBounds(442,12,64,26);

startl.addictionListener(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

doublenumber=new

She3i().shoot((Integer.parselnt(selecttion.getSelectedItem().toString()))

,Integer.parselnt(numberl.getText()));

answer1.setText(number+"");

)

});

)

{

jLabell=newJLabel();

QAl.getContentPane().add(jLabell);

jLabell.setTextC,\u4e00\u8f6e\u4e2d\u5c04\u4e2d\u51e0\u7bad");

jLabell.setBounds(35,18,105,22);

}

ComboBoxModelselecttionModel=newDefau1tComboBoxModel(newString口

f"3","5",”⑴;

selecttion=new3ComboBox();

QA1.getContentPane().add(selecttion);

selecttion.setModel(selecttionModel);

selecttion.setBounds(158,17,39,24);

selecttion.setName("\u8bf7\u9009\u62e9");

selecttion.setRequestFocusEnabled(false);

selecttion.setSelectedIndex(2);

)

{

numberl=newJTextField();

QA1.getContentPane().add(numberl);

numberl.setBounds(158,60,66,27);

)

{

jLabel2=new3Label();

QA1.getContentPane().add(jLabel2);

jLabe12.setText("\u5b9e\u9a8c\u6b21\u6570");

jLabel2.setBounds(47,63,60,20);

}

{

jLabel3=new3Label();

QA1.getContentPane().add(jLabel3);

jLabels.setText("\u5b9e\u9a8c\u7ed3\u679c");

jLabel3.setBounds(269,63,60,22);

}

{

answerl=newJTextField();

QA1.getContentPane().add(answerl);

answerl.setBounds(365,60,64,25);

)

}

publicvoidQA2()

{

QA2=new3InternaIFrame();

QA2.getContentPane().setLayout(null);

jDesktopPanel.add(QA2,JLayeredPane.DEFAULT_LAYER);

QA2.setBounds(78,40,538,226);

QA2.setVisible(true);

QA2.setClosable(true);

QA2.setTitle("\u8d76\u706b\u8f66\u95ee\u9898");

jLabel6=newJLabel();

QA2.getContentPane().add(jLabel6,BorderLayout.CENTER);

jLabel6.setText(',\u6a21\u62df\u8d76\u7e6b\u8f66\u6b21\u6570");

jLabel6.setBounds(37,12,115,27);

)

{

number?=new3TextField();

QA2.getContentPane().add(number?);

number2.setBounds(159,14,71,25);

}

{

start2=newJButton();

QA2.getContentPane().add(start2);

start2.setText("\u786e\u5b9a");

start2.setBounds(399,13,44,27);

start2.addActionListener(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

List<Node>s-newArrayList();

doublerate=new

GC().rinding(Integer.parselnt(number2.getText()),s):

answer2.setText(rate+"");

chart=newChart(),get3FreeChart(s);

ApplicationFrameaf=newApplicationFrame("赶火车");

af.setContentPane(newChartPanel(chart));

af.pack();

af.setVisible(true);

}

});

)

{

jLabel7=new3Label();

QA2.getContentPane().add(jLabel7);

jLabel7.setText("\u8d76\u4e0a\u706b\u8f66\u7684\u6982\u7387");

jLabel7.setBounds(37,64,105,20);

}

{

answer2=newJTextField();

QA2.getContentPane().add(answer2);

answer2.setBounds(160,61,70,28);

}

}

publicvoidQA3()

QA3=new3InternaIFrame();

jDesktoppanel.add(QA3,3LayeredPane.DFF4L/LT_LAYER);

QA3.setBounds(51,26,656,385);

QA3.setVisible(true);

QA3.setTitle("\u70ae\u6392\u5c04\u51fb\u95ee\u9898");

QA3.getContentPane().setLayout(null);

QA3.setClosable(true);

{

start?=new[Button。;

QA3.getContentPane().add(start3);

start3.setText("\u786e\u5b9a");

start3.setBounds(438,29,44,27);

start3.addActionListener(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

Stringstr[][]=newString[Integer.parseInt(number3.getText())][8];

Stringa三new「IRC().attack(str,Integer.parselnt(number?.getText()));

anwer31.setText(a);

{

TableModeltableModel=

newDefaultTableModel(

str,

newString[]{“实验序号","投硬币结果","

指示正确”,

”指示不正确;“掷骰子结果

table=newJTable();

jScrollPanel.setViewportView(table);

table.setModel(tableModel);

)

}

});

}

{

jLabel4=newJLabel();

QA3.getContentPane().add(jLabel4);

jLabel4.setText("\u5b9e\u9a8c\u6b21\u6570");

jLabel4.setBounds(61,33,60,20);

)

{

jScrollPanel=newJScrollPane();

QA3.getContentPane().add(jScrollPanel);

jScrollPanel.setBounds(19,137,610,188);

}

number3=new3TextField();

QA3.getContentPane().add(number3);

number3.setBounds(203,30,92,28);

}

{

jLabel5=new3Label();

QA3.getContentPane().add(jLabelS);

jLabel5.setText("\u6709\u6548\u5c04\u51fb\u7387\u548c\u51fb\u4f24\u5e73\u5747\u

6570");

jLabel5.setBounds(7,78,168,20);

}

{

anwer31=new3TextField();

QA3.getContentPane().add(anwer31);

anwer31.setDounds(203,74,219,29);

}

}

privatevoidinitGUI(){

this.getContentPane().setLayout(null);

try{

setDefaultCloseOperation(Windowconstants.DISPOSE_OA/_CLOSE);

this.setTitle("\u8bal\u7b97\u4eff\u771f\u4f5c\u4ela");

(

jToolBarl=new3ToolBar();

getContentPane().add(jToolBarl,BorderLayout.CFA/7FR);

jToolBarl.setBounds(0,0,728,40);

{

QI=newJButton();

jToolBarl.add(Ql);

QI.setText("\u5c04\u7bad\u95ee\u9898");

QI.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente){

QA1();

}

});

}

{

Q2=newJButton();

jToolBarl.add(Q2);

Q2.setText("\u8d76\u706b\u8f66\u95ee\u9898");

Q2.addActionListener(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

QA2();

}

});

}

{

Q3=new3Button();

jToolBarl.add(Q3);

Q3.setText("\u70ae\u6392\u5c04\u51fb");

Q3.setPreferredSize(newjava.awt.Dimension(72,32));

Q3.addActionListener(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

QA3();

}

});

)

}

{

jDesktopPanel=newJDesktopPane();

getContentPane().add(jDesktopPanel);

jDesktopPanel.setBounds(0,32,728;453);

jDesktopPanel.setDoubleBuffered(true);

pack();

this.setSize(746,525);

}catch(Exceptione){

e.printStackTrace();

)

}

)

FZ,java

importjava.util.*;

importorg.jfree.chart.ChartFactory;

importorg.jfree.chart.JFreeChart;

importorg.jfree.chart.plot.PlotOrientation;

importorg.jfree.data.DefaultKeyedValues;

importorg.jfree.data.category.CategoryDataset;

importorg.jfree.data.general.Datasetutilities;

importorH.jfree.ui.ApplicationFrame;

importsun.text.resources.FormatData;

classShe3i{

publicdoubleshoot(intn,intnumber){

doubleresult=0;

for(inti=l;i<=rumber;i++){

intnum=0;

for(intj=l;j<=10;j++){

if(newRandom().nextDouble()<=0.24)

num++;

)

if(num==n)result++;

}

return(result/rumber);

)

)

classGC(

publicdoublerinding(intnumber,List<Node>s){

doublex=0.0,y=3.0,z=0.0,n=0.0;

Listlist=newArrayListO;

for(inti=l;i<=number;i++){

doubleb=newRandom().nextDouble();

if(b<0.7)x=0.0;

if(b<0.9&&b>=0.7)x=5.0;

if(b>=0.9)x=10.0;

doubleyl=0.0;

for(intj=l;j<=6;j++){

yl+=(newRandom().nextDouble()-newRandom().nextDouble());

}

y=yl*Math.pow(2,0.5)+30;

b=newRandom().nextDouble();

if(b<0.3)z=28.0;

if(b<0.78&b>=0.3)z=30.0;

if(b<0.9&&b>=0.7)z=32.0;

if(b>=0.9)z=34.0;

list.add(x+y-z);

if(x+y-z>=0){

//System.out.printin(x+y-z);

n++;

}

)

Collections.sort(list);

doublemin=(double)list.get(0);

doublemax=(double)list.get(list.size()-1);

doubled=(max-min)/10;

System.out.println(list.size());

intj=0;

for(inti=l;i<=10;i++){

intm=0;

for(;j<list.size()-l;j++)

{

doubleq=(double)list.get(j);

if(q>=niin&&q<=min+d)m++;

elsebreak;

}

s.add(newNode(String.fonnat("%.2f”,min)+”-

"+S

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论