polyfit error first two inputs must have the same number of elements.
6 次查看(过去 30 天)
显示 更早的评论
hello everyone, i have this error how can i fix it please instead of saying,write the code for it please because i am new in matlab and also my deadline is coming soon Thanks a lot for everything.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/809544/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/809539/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/809544/image.png)
1 个评论
Image Analyst
2021-11-22
编辑:Image Analyst
2021-11-22
We can't run an image. Post text. I'm not going to rewrite all your homework for you. Make it easy to help you, not hard.
In general if you have polyfit(x, f, 4) then x must have exactly as many elements as f has. Evidently your do not.
回答(1 个)
Walter Roberson
2021-11-23
You create a vector x . Then for the function values, you compute
temp = sqrt(1/(1+x.^2))
However, here x is the entire vector, so 1+x.^2 is a vector. You then compute 1 / the vector. However, in MATLAB, the / operator is not the division operator: the / operator is the matrix division operator. When B is a matrix, A/B is similar to A * pinv(B) where the * operator I show here is the algebraic matrix multiplication operator, also known as "inner product" . The operator for doing element-by-element division is the ./ operator -- notice the period.
If you look back to where you calculated x from k values, notice that you used k(i) in your calculation, so you were working with scalars there. You do have a /n in that calculation, but when n is a scalar, pinv(n) is 1 divided by the scalar, so in the expression A/B where B is a scalar, the expression becomes equivalent to A./B -- so your code for building x from k worked out, because you selected one k value to work with at a time. But your newer loop, you did not select one x value at a time.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!