【问题标题】:Graphing polynomials绘图多项式
【发布时间】:2018-10-02 15:50:33
【问题描述】:

在一些帮助下,我生成了以下代码。以下是给定输入的一些所需输出。但是,我在完成此代码的最后一项任务时遇到了一些麻烦。寻求一些帮助,非常感谢任何指导或帮助,谢谢!

flops = 0

def add(x1, x2):
    global flops
    flops += 1
    return x1 + x2

def multiply(x1, x2):
    global flops
    flops += 1
    return x1 * x2

def poly_horner(A, x):
    global flops
    flops = 0
    p = A[-1]
    i = len(A) - 2
    while i >= 0:
        p = add(multiply(p, x), A[i])
        i -= 1
    return p

def poly_naive(A, x):
    global flops
    p = 0
    flops = 0
    for i, a in enumerate(A):
        xp = 1
        for _ in range(i):
            xp = multiply(xp, x)
            p = add(p, multiply(xp, a))
    return p

给定以下输入,我得到以下输出:

poly_horner([1,2,3,4,5], 2)
129
print(flops)
8
poly_naive([1,2,3,4,5, 2])
129
print(flops)[![enter image description here][1]][1]
20
np.polyval([5,4,3,2,1], 2)
129

【问题讨论】:

  • 您在完成最后一项任务时遇到了什么具体问题?
  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 On topichow to ask... the perfect question 在此处申请。 StackOverflow 不是设计、编码、研究或教程资源。我们需要比“我遇到麻烦”更具体的内容,特别是因为在线教程和示例中很好地涵盖了“绘图”。
  • @martineau 我不太确定如何完成这部分问题
  • @Prune 谢谢,请记住这一点
  • user123:这仍然过于模糊。也许下载/安装类似matplotlib 的东西会有所帮助(假设您还没有它,这就是您不确定如何完成任务的意思)。

标签: python numpy graph


【解决方案1】:

我假设你想创建一个数字,虽然你的问题很模糊......但是当我的代码运行时,我有几分钟的时间可以杀死。无论如何,您似乎在绘图时遇到了困难。

import numpy as np
import pylab as pl

x = np.arange(10)
y = x * np.pi

# you can calculate a line of best fit (lobf) using numpy's polyfit function
lobf1 = np.polyfit(x, y, 1)  # first degree polynomial
lobf2 = np.polyfit(x, y, 2)  # second degree polynomial
lobf3 = np.polyfit(x, y, 3)  # third degree polynomial

# you can now use the lines of best fit to calculate the 
# value anywhere within the domain using numpy's polyval function
# FIRST, create a figure and a plotting axis within the fig
fig = pl.figure(figsize=(3.25, 2.5))
ax0 = fig.add_subplot(111)

# now use polyval to calculate your y-values at every x
x = np.arange(0, 20, 0.1)
ax0.plot(x, np.polyval(lobf1, x), 'k')
ax0.plot(x, np.polyval(lobf2, x), 'b')
ax0.plot(x, np.polyval(lobf3, x), 'r')

# add a legend for niceness
ax0.legend(('Degree 1', 'Degree 2', 'Degree 3'), fontsize=8, loc=2)

# you can label the axes whatever you like
ax0.set_ylabel('My y-label', fontsize=8)
ax0.set_xlabel('My x-label', fontsize=8)

# you can show the figure on your screen
fig.show()

# and you can save the figure to your computer in different formats
# specifying bbox_inches='tight' helps eliminate unnecessary whitespace around
# the axis when saving...it just looks better this way.
pl.savefig('figName.png', dpi=500, bbox_inches='tight')
pl.savefig('figName.pdf', bbox_inches='tight')

# don't forget to close the figure
pl.close('all')

【讨论】:

  • 所以我只需要将它添加到我的代码中以使其运行?
  • 我还需要使用 plt.show() 而不是 fig.show() 来运行我的代码!
  • 或者这只是一种通用的绘图方式?没有真正回答我原来的帖子
  • 您的原始帖子含糊不清,无法回答。我抓住了一个机会,你可能需要帮助来创建你的人物......看起来我浪费了我的时间。
  • 没有帮助!我将查看它以了解如何创建图形
猜你喜欢
  • 2021-10-24
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 2023-01-17
  • 2020-05-08
相关资源
最近更新 更多