资源简介

[计算方法作业]利用python中matplotlib实现绘制龙贝格公式和变步长梯形法图像,利用龙贝格公式及变步长积分两种算法计算定积分,用python中的matplotlib库绘制图像

资源截图

代码片段和文件信息

import sys
from PyQt5.QtWidgets import QApplication QMainWindow QSizePolicy QPushButton
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np

#显示中文和负数
plt.rcParams[‘font.sans-serif‘]=[‘SimHei‘]
plt.rcParams[‘axes.unicode_minus‘]=False

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowtitle(“数值积分“)
        self.setGeometry(200 50 800 600)
        m = Integration(self width=7 height=6)#实例化一个画布对象
        m.move(0 0)

        #设置龙贝格按钮
        button = QPushButton(‘龙贝格算法‘ self)
        button.move(700 75)
        button.resize(100 75)
        button.clicked.connect(m.Romberg1)

        #设置变步长积分按钮
        button = QPushButton(‘变步长积分‘ self)
        button.move(700 450)
        button.resize(100 75)
        button.clicked.connect(m.trap1)
        self.show()

#数值积分类
class Integration(FigureCanvas):
    def __init__(self parent=None width=5 height=4 dpi=100 ):
        fig = Figure(figsize=(width height) dpi=dpi frameon=False)
        self.axes=[]
        self.axes1 = fig.add_subplot(211)
        self.axes2 = fig.add_subplot(212)
        FigureCanvas.__init__(self fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(selfQSizePolicy.Expanding
                                   QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.a=0
        self.b=6
        self.eps=0.0001
        #挪动坐标位置
        self.axes1.set_xlim(06)
        self.axes2.set_xlim(06)
        #去掉边框
        self.axes1.spines[‘top‘].set_color(‘none‘)
        self.axes1.spines[‘right‘].set_color(‘none‘)
        #移位置 设为原点相交
        self.axes1.xaxis.set_ticks_position(‘bottom‘)
        self.axes1.spines[‘bottom‘].set_position((‘data‘0))
        self.axes1.yaxis.set_ticks_position(‘left‘)
        self.axes1.spines[‘left‘].set_position((‘data‘0))
        #去掉边框
        self.axes2.spines[‘top‘].set_color(‘none‘)
        self.axes2.spines[‘right‘].set_color(‘none‘)
        #移位置 设为原点相交
        self.axes2.xaxis.set_ticks_position(‘bottom‘)
        self.axes2.spines[‘bottom‘].set_position((‘data‘0))
        self.axes2.yaxis.set_ticks_position(‘left‘)
        self.axes2.spines[‘left‘].set_position((‘data‘0))
        #初始为画图
        self.x=np.linspace(06)
        self.y=[self.func(i) for i in self.x]
    

评论

共有 条评论