計算機仿真實驗_第1頁
計算機仿真實驗_第2頁
計算機仿真實驗_第3頁
計算機仿真實驗_第4頁
計算機仿真實驗_第5頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

計算機仿真實驗

1.射箭問題

某人每輪向靶子射10箭,已知其擊中靶心的概率為25%,問一輪中

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

設X表示10次射箭中擊中靶心的次數(shù),

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.產(chǎn)生均勻分布隨機數(shù)0.00-0.99(100個),某個數(shù)字出現(xiàn)的概率相等。若產(chǎn)生1000個這樣的數(shù),

(1)數(shù)值為0.00?0.24大約會有250個,比例大約為0.25

(2)數(shù)值為0.25~0.99大約會有750個,比例大約為0.75

2.以每產(chǎn)生?個隨機數(shù)代表射1箭,若產(chǎn)生的隨機教小于0.25,則代表擊中靶心,如果產(chǎn)生的隨機數(shù)

大于或等于。25,則表示沒有擊中靶心。

若實驗的次數(shù)很多(多大于1000),則?擊中靶心的頻率接近于25%。若實驗的次數(shù)無限多,m

靶心的概率等于25%。

3.確定一輪中擊中7箭的概率

(1)每輪由計算機產(chǎn)生10個均勻分布的隨機數(shù)Ni(i=L2,3,-10),代表射10箭

(2)其中額i小于0.25為擊中,記下該輪中擊中的次數(shù)

(3)重復(1)一(2),進行K輪實驗

(4)找出K輪中所有每輪擊中7次的總輪數(shù)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站經(jīng)過B站開往C站,某人每天趕往B站乘這趟火車。

已知火車從A站到B站運行時間為均值30分鐘、方差為2分鐘的正

態(tài)隨機變量?;疖嚧蠹s在下午1點離開A站。離開時刻的頻率分布為

出發(fā)時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是一個均勻分布的隨機數(shù),則取

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,

再產(chǎn)生正態(tài)分布的隨機數(shù)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.炮排射擊命中率問題

自我方前沿防守地域,敵人以一個炮排(含兩門火炮)為單位對

我方進行干擾和破壞。為躲避我放打擊,敵方對其陣地進行了偽裝并

經(jīng)常變換涉及地點。經(jīng)過長期的觀察發(fā)現(xiàn),我方指揮所對敵方目標的

指示有50%是準確的,而我方火力單位,在指示正確時,有1/3的射

擊效果能毀壞敵人一門火炮,有1/6的射擊效果能全部消滅敵人。

現(xiàn)在希望能用目中方式把我方將要對敵人實施的20次打擊效果顯示

出來,確定有效地射擊率及會上敵方火炮的平均值。

仿真模型:

將指示正確與否比作投硬幣,各有1/2的概率,則在計算

機上隨機產(chǎn)生一個隨機數(shù),如果小于1/2,則表示預測正確,

大于則不正確。將擊毀敵方火炮數(shù)比作擲篩子,在計算機上產(chǎn)

生一個隨機數(shù),將0~1之間的隨機數(shù)分成6份,前3份表示沒

有擊中敵人,中間兩份表示擊毀一門火炮,最后一份表示擊毀

兩門火炮,則仿真結束。

設E:有效射擊比率

E1:n次射擊平均每次毀傷敵人的火炮數(shù)

理論值: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);

}

回計算仿真作業(yè)回

射箭問題趕火車問題炮排射擊

..lx]

□炮排射擊間找....

實驗次數(shù)確定]

有效射擊率和擊傷平均數(shù)

實驗序號投硬市結J指示正確擲骰子結2_3

1向

2

3

4

5

6

7

9

10

仿真模擬結果有效射擊比率0.2553,平均每次毀傷敵人的火炮數(shù)

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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論