python 使用蒙特卡罗技术计算椭圆的近似面积

Note: lf you use functions which have not been taught in class, you will getzero for this question no matter whether the code is correct or not.
Write a complete Python program that
(a)uses the Monte Carlo technique to compute the approximate area of an ellipse. The program must input a,the length of the semi-major axis, and b, the length of the semi-minor axis of the ellipse, lf either of these valuesis less than or equal to zero display an appropriate error message that includes the length as shown in the firstsample run of the program, Otherwise use the Monte Carlo technique to create random points whose Xcoordinates range from -a to a and whose Y coordinates range from -b to b. Calculate the probability of a pointbeing inside the ellipse and the approximate area of the ellipse, The ellipse is fully enclosed in a rectangle whoseK coordinates range from -a to a and whose Y coordinates range from -b to b, Calculate the actual area of theellipse as rab. Calculate the error in the approximate area by taking the absolute value of the diference of thetwo areas. Display the probability to 4 decimals places. Display the two areas to 14 decimal places in exponentiaformat. Display the error in the approximation to 6 decimal places in exponential format, There are no loops inthis program, use vector arithmetic where appropriate.

A point is inside the ellipse if (x/a) ** 2 + (y/b) ** 2 <= 1

A sample run of the program where the value entered as the ength of the semi-major axis is not valid is shown below.
Enter the length of the semi-major axis in cm (> 0): 0
The length of the semi-major axis, 0, must be greater than zero.
A sample run of the program where the values entered are valid is shown below.
python 使用蒙特卡罗技术计算椭圆的近似面积_第1张图片

import math
import numpy as np


def getInputVal(prompt):
    val = eval(input('Enter the length of the semi-major axis in cm (> 0): '))
    if val <= 0:
        print('The length of the semi-major axis, 0, must be greater than zero.')
        return getInputVal(prompt)
    return val


def computeRealYCoordinates(a, b, xCoordinates):
    return b * ((1 - (xCoordinates / a) ** 2) ** 0.5)


def ellipse(a, b, intervals):
    points = int((2 * a) / intervals)
    random_x = np.random.uniform(a * -1, a, points)
    random_y = np.random.uniform(b * -1, b, points)
    res = sum(np.where(np.absolute(random_y) < np.absolute(computeRealYCoordinates(a, b, random_x)), 1, 0))
    _area = 4 * a * b * (res / points)
    return _area, res / points


if __name__ == '__main__':
    _a = getInputVal('Enter the length of the semi-major axis in cm (> 0): ')
    _b = getInputVal('Enter the length of the semi-minor axis in cm(> 0): ')

    actual_area = math.pi * _a * _b

    area, probability = ellipse(_a, _b, 0.0001)

    print('The probability of a point being in the ellipse is %s' % round(probability, 4))

    print('The approximate area of the ellipse is %.14e cm^2' % area)
    print('Actual area of the ellipse is %.14e cm^2' % actual_area)
    print('The error in the approximation is %.6e cm^2' % abs(actual_area - area))

你可能感兴趣的:(python,开发语言,numpy)