首页 > 解决方案 > 将数组转换为多项式

问题描述

我正在做一个数值分析,我得到了一个矩阵作为我的结果。因此,我想使用matrixor 数组来快速创建多项式。

import matplotlib.pyplot as plt
import matplotlib.collections
import numpy as np

x1 = -2
x2 = -2 + (8 / 3)
x3 = -2 + (16 / 3)
x4 = -2 + (24 / 3)

A = np.array(
    [
        [1, x1, x1 ** 2, x1 ** 3],
        [1, x2, x2 ** 2, x2 ** 3],
        [1, x3, x3 ** 2, x3 ** 3],
        [1, x4, x4 ** 2, x4 ** 3],
    ]
)

R1 = np.array([[1], [0], [0], [0]])
N1 = np.linalg.solve(A, R1)


# The N1..N4 are my coefficients for my polynomium of third order.
# below I try to transform the N1 into a polynomium N1p`

N1p = np.array(N1.transpose()).tolist()
P1 = np.poly(N1p)

它给了我一个尺寸错误。有没有人可以帮助我?

标签: pythonarraysnumpy

解决方案


这是您的预期输出吗?

N1p = N1.transpose().reshape(-1)
P1 = np.poly(N1p)
P1
array([ 1.00000000e+00,  3.41796875e-02, -3.67412567e-02,  2.04887241e-03,
    2.08630809e-05])

P1.tolist()

[1.0,
 0.0341796875,
 -0.03674125671386719,
 0.002048872411251068,
 2.0863080862909555e-05]

推荐阅读