我想要一个 -2 到 2 的向量,步长为 0.5,它将保存为 x。接下来我想要 y 作为 y=3*x^3+3*x+6。
当我执行 x=linspace(-2,3,9) 时,我得到 p>
-2.0000 -1.5000 -1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000
但编译器仍然提示: 使用 ^ 时出错
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.
最佳答案
您需要按元素 .^ 操作(请参阅错误消息)。 matlab中有两种运算。常见的向量运算(*、/、^)及其对应的元素运算(.*、./、.^)。
当使用标量时,这并不重要,但是一旦你在向量或矩阵上运行,操作就会改变。
看here以获得更深入的解释。
关于arrays - Matlab多项式拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51259556/