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.
在这里插入图片描述

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))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值