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

7 views (last 30 days)
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 Comment
Image Analyst
Image Analyst on 22 Nov 2021
Edited: Image Analyst on 22 Nov 2021
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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 23 Nov 2021
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.

Community Treasure Hunt

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

Start Hunting!