简体   繁体   English

Matlab多项式拟合

[英]Matlab polynomial fitting

I want a vector of -2 to 2 in a step size of 0.5 which would be saved as x. 我想要步长为0.5的-2到2的向量,该向量将另存为x。 Next i want y as y=3*x^3+3*x+6. 接下来,我要y为y = 3 * x ^ 3 + 3 * x + 6。

When i do x=linspace(-2,3,9) i get 当我做x = linspace(-2,3,9)我得到

-2.0000   -1.5000   -1.0000   -0.5000         0    0.5000    1.0000    1.5000    2.0000

But still the compiler complains : Error using ^ 但是编译器仍然抱怨:使用^时出错

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 '.^'.

You need the elementwise .^ operation (see Error message). 您需要按元素进行。^操作(请参阅错误消息)。 There are two types of operations in matlab. matlab中有两种类型的操作。 The usual vectorwise operations (*, /, ^) and their elementwise counterparts (.*, ./, .^). 通常的矢量操作(*,/,^)及其元素对应的操作(。*,./,。^)。

When working with scalars this does not matter, but as soon as you run on vectors or matrices the operation changes. 当使用标量时,这无关紧要,但是一旦在向量或矩阵上运行,操作就会改变。

Look here for a more in depth explanation. 在这里寻找更深入的解释。

Just use a dot(.) before the power sign.It's because you are using the power sign to the whole matrix and you need to do the element-wise operation. 只需在幂符号之前使用点(。),这是因为您正在对整个矩阵使用幂符号,因此需要进行逐元素运算。

x=linspace(-2,3,9)
y=3*x.^3+3*x+6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM