polyfit error first two inputs must have the same number of elements.

6 ビュー (過去 30 日間)
alp onur karacay
alp onur karacay 2021 年 11 月 22 日
回答済み: Walter Roberson 2021 年 11 月 23 日
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.
  1 件のコメント
Image Analyst
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
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.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by