Lesson 6 02: 蒙地卡羅方法求積分值
Lesson 6 01: 函數作圖
設定函數並作圖
Example Code
- import numpy as np
- import matplotlib
- import matplotlib.pyplot as plt
- def fun1(x):
- return np.abs(np.sin(x*10)/np.exp(x))
- upperL=3
- lowerL=0
- interval=0.01
- Xpoints = np.arange(lowerL,upperL,interval)
- #print(Xpoints)
- #print(fun1(Xpoints))
- plt.figure(1)
- plt.plot(Xpoints,fun1(Xpoints), 'r.')
- plt.show()
Note and Comments
- import numpy as np
- import math
- import matplotlib
- import matplotlib.pyplot as plt; 匯入圖形函式庫Matplotlib中的pyplot資料庫
- EVN=100000; 宣告要產生多少數據點
- posX, posY = (np.random.rand(EVN)-0.5, np.random.rand(EVN)-0.5) 產生兩組-0.5到0.5的亂數, 每組有EVN個亂數點.
- poxInCirx, poxInCiry =([],[]) 產生兩組X跟Y的集合,用來存放半徑小於0.5
- [(poxInCirx.append(posX[x]), poxInCiry.append(posY[x]) ) for x in range(EVN) if math.hypot(posX[x],posY[x]) <=0.5]
- 將位於圓內的亂數點用append加到poxInCirx, poxInCiry數組內. math.hypot(x,y)函數將回傳(x*x+y*y)的根號值
- plt.plot(posX,posY, 'r.') 以(posX,posY) 爲數據點,2維作圖 顏色爲紅色r, 數據點以.表示
- plt.plot(poxInCirx,poxInCiry, 'go') 以(poxInCirx,poxInCiry) 爲數據點,2維作圖. 顏色爲綠色g, 數據點以o表示
- plt.axes().set_aspect('equal') 將圖的X軸與Y軸設爲相同比例
- print("Monte Carlo Pi=", len(poxInCirx)/EVN*4)
- print("Error of Monte Carlo Pi=", (len(poxInCirx)/EVN*4-math.pi)/math.pi*100)
- plt.text(-0.15, -0.6, r'EVN=%d' %EVN, fontsize=20) 將文字EVN=10000設置在-0.15, -0.6的位置
- plt.show() 圖形設定完畢,開始作圖
result
Example Code
- import scipy as sp
- import scipy.integrate as spInt
- import matplotlib
- import matplotlib.pyplot as plt
-
- def fun1(x):
- return sp.fabs(sp.sin(x*10)/sp.exp(x))
- upperL=3.0
- lowerL=0.0
- interval=0.01
- Xpoints = sp.arange(lowerL,upperL,interval)
- Ypoints= fun1(Xpoints)
- #print(Xpoints)
- #print(Ypoints)
- #print(fun1(Xpoints))
- #print(Ypoints.max())
- #print(np.ceil(Ypoints.max()))
- #print(np.ceil(Xpoints.max()))
- randLx=sp.ceil(Xpoints.max())
- randLy=sp.ceil(Ypoints.max())
- EVN=10000;
- posAreaX, posAreaY =([],[])
- posX, posY = (sp.random.rand(EVN)*randLx, sp.random.rand(EVN)*randLy)
- for i in range(EVN):
- if(posY[i]<=fun1(posX[i])):
- posAreaX.append(posX[i])
- posAreaY.append(posY[i])
- plt.figure(1)
- plt.plot(Xpoints,fun1(Xpoints), 'r-')
- plt.plot(posAreaX,posAreaY, 'b.')
- plt.show()
- print("integrated area =", randLx *randLy *len(posAreaY)/EVN)
- result, err = spInt.quad(lambda x: fun1(x), lowerL, upperL)
- print("integrated area with sp.integrate.quad =", result)
Note and Comments
- import numpy as np
- import math
- import matplotlib
- import matplotlib.pyplot as plt; 匯入圖形函式庫Matplotlib中的pyplot資料庫
- EVN=100000; 宣告要產生多少數據點
- posX, posY = (np.random.rand(EVN)-0.5, np.random.rand(EVN)-0.5) 產生兩組-0.5到0.5的亂數, 每組有EVN個亂數點.
- poxInCirx, poxInCiry =([],[]) 產生兩組X跟Y的集合,用來存放半徑小於0.5
- [(poxInCirx.append(posX[x]), poxInCiry.append(posY[x]) ) for x in range(EVN) if math.hypot(posX[x],posY[x]) <=0.5]
- 將位於圓內的亂數點用append加到poxInCirx, poxInCiry數組內. math.hypot(x,y)函數將回傳(x*x+y*y)的根號值
- plt.plot(posX,posY, 'r.') 以(posX,posY) 爲數據點,2維作圖 顏色爲紅色r, 數據點以.表示
- plt.plot(poxInCirx,poxInCiry, 'go') 以(poxInCirx,poxInCiry) 爲數據點,2維作圖. 顏色爲綠色g, 數據點以o表示
- plt.axes().set_aspect('equal') 將圖的X軸與Y軸設爲相同比例
- print("Monte Carlo Pi=", len(poxInCirx)/EVN*4)
- print("Error of Monte Carlo Pi=", (len(poxInCirx)/EVN*4-math.pi)/math.pi*100)
- plt.text(-0.15, -0.6, r'EVN=%d' %EVN, fontsize=20) 將文字EVN=10000設置在-0.15, -0.6的位置
- plt.show() 圖形設定完畢,開始作圖
result
Example Code
- from sympy import integrate, erf, exp, sin, log, oo, pi, sinh, symbols, Abs
- upperL=3.0
- lowerL=0.0
- x, y = symbols('x,y', real=True)
- print(integrate(sin(x*10), x))
- print(integrate(sin(x*10)/exp(x), x))
- print(integrate(sin(x*10)/exp(x), x, (x,lowerL, upperL)))
- print(integrate(Abs(sin(x*10))/exp(x), x, (x,lowerL, upperL)))
Note and Comments
- import numpy as np
- import math
- import matplotlib
- import matplotlib.pyplot as plt; 匯入圖形函式庫Matplotlib中的pyplot資料庫
- EVN=100000; 宣告要產生多少數據點
- posX, posY = (np.random.rand(EVN)-0.5, np.random.rand(EVN)-0.5) 產生兩組-0.5到0.5的亂數, 每組有EVN個亂數點.
- poxInCirx, poxInCiry =([],[]) 產生兩組X跟Y的集合,用來存放半徑小於0.5
- [(poxInCirx.append(posX[x]), poxInCiry.append(posY[x]) ) for x in range(EVN) if math.hypot(posX[x],posY[x]) <=0.5]
- 將位於圓內的亂數點用append加到poxInCirx, poxInCiry數組內. math.hypot(x,y)函數將回傳(x*x+y*y)的根號值
- plt.plot(posX,posY, 'r.') 以(posX,posY) 爲數據點,2維作圖 顏色爲紅色r, 數據點以.表示
- plt.plot(poxInCirx,poxInCiry, 'go') 以(poxInCirx,poxInCiry) 爲數據點,2維作圖. 顏色爲綠色g, 數據點以o表示
- plt.axes().set_aspect('equal') 將圖的X軸與Y軸設爲相同比例
- print("Monte Carlo Pi=", len(poxInCirx)/EVN*4)
- print("Error of Monte Carlo Pi=", (len(poxInCirx)/EVN*4-math.pi)/math.pi*100)
- plt.text(-0.15, -0.6, r'EVN=%d' %EVN, fontsize=20) 將文字EVN=10000設置在-0.15, -0.6的位置
- plt.show() 圖形設定完畢,開始作圖
result
Example Code
- import scipy as sp
- v10= sp.floor(sp.random.rand()*10)
- v1= sp.floor(sp.random.rand()*10)
- print(v10,v1)
- A=0
- while(A!=2):
- A=0
- B=0
- a=int(input())
- b=int(input())
- print(a,v10)
- if(a==v10):
- A=A+1
- if(a==v1):
- B=B+1
- if(b==v10):
- B=B+1
- if(b==v1):
- A=A+1
- print("A=",A,"B=",B)
- print("yes the ture valuse is ",a,b)
Note and Comments
- import numpy as np
- import math
- import matplotlib
- import matplotlib.pyplot as plt; 匯入圖形函式庫Matplotlib中的pyplot資料庫
- EVN=100000; 宣告要產生多少數據點
- posX, posY = (np.random.rand(EVN)-0.5, np.random.rand(EVN)-0.5) 產生兩組-0.5到0.5的亂數, 每組有EVN個亂數點.
- poxInCirx, poxInCiry =([],[]) 產生兩組X跟Y的集合,用來存放半徑小於0.5
- [(poxInCirx.append(posX[x]), poxInCiry.append(posY[x]) ) for x in range(EVN) if math.hypot(posX[x],posY[x]) <=0.5]
- 將位於圓內的亂數點用append加到poxInCirx, poxInCiry數組內. math.hypot(x,y)函數將回傳(x*x+y*y)的根號值
- plt.plot(posX,posY, 'r.') 以(posX,posY) 爲數據點,2維作圖 顏色爲紅色r, 數據點以.表示
- plt.plot(poxInCirx,poxInCiry, 'go') 以(poxInCirx,poxInCiry) 爲數據點,2維作圖. 顏色爲綠色g, 數據點以o表示
- plt.axes().set_aspect('equal') 將圖的X軸與Y軸設爲相同比例
- print("Monte Carlo Pi=", len(poxInCirx)/EVN*4)
- print("Error of Monte Carlo Pi=", (len(poxInCirx)/EVN*4-math.pi)/math.pi*100)
- plt.text(-0.15, -0.6, r'EVN=%d' %EVN, fontsize=20) 將文字EVN=10000設置在-0.15, -0.6的位置
- plt.show() 圖形設定完畢,開始作圖
result