polyfit not giving working correctly?

52 ビュー (過去 30 日間)
Neil
Neil 2014 年 6 月 10 日
コメント済み: Star Strider 2014 年 6 月 10 日
Hello everyone,
so I've been working on a code which first asks a user what part of the data is linear by asking for the start point x value (or strain in this case) of the linear region of a graph(aka C1_Strain_Start), and the x value of the end of the linear section(aka C1_Strain_End).
After it has done that, I want it to make it fit a first order polynomial to it, in order to extract the slope (which is stiffness in my case). Here is the code I have:
m=1;
while Cycle1UpStrain(m,1)<C1_Strain_Start
m=m+1;
end
n=1;
while Cycle1UpStrain(n,1)<C1_Strain_End
n=n+1;
end
C1_Strain_Stiffness_pts(m:n,1)=Cycle1UpStrain(m:n,1);
C1_Stress_Stiffness_pts(m:n,1)=Cycle1UpStrain(m:n,1);
Stiffness_Eqn1 = [5 5];
Stiffness_Eqn1 = polyfit(C1_Strain_Stiffness_pts,C1_Stress_Stiffness_pts,1);
Stiffness1=Stiffness_Eqn1(1,1)
In here C1_Strain_Start and C1_Strain_End are the start and end x values I spoke of earlier, Stiffness_Eqn1 is the polyfit coefficients, and Stiffness1 would be the slope.
Cycle1UpStrain and Cycle1UpStress are all the data points including non-linear region. C1_Strain_Stiffness_pts and C1_Stress_Stiffness_pts extracts only the linear region as designated by the user's earlier input.
So am I using polyfit wrong? it keeps giving me a polyfit of [1, 1.6x10^-17] (the second value changes a bit, but is usually x10^-17 or x10^-18) for ANY values I give it when I runt he program. Which shows something is wrong.
Thanks for your time!
Neil
  3 件のコメント
John D'Errico
John D'Errico 2014 年 6 月 10 日
BTW, I deleted the spam answer you got. We have been deluged with spam lately, so until they can fix the spam filters, when you see something that is clearly spam, add a flag to it to identify it as spam so that one of us can delete it.
Neil
Neil 2014 年 6 月 10 日
Thank you John D'Errico, I appreciate it!

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2014 年 6 月 10 日
The polyfit function is returning a unity slope and zero intercept because in your call to it, you’re giving it the same vectors ‘Cycle1UpStrain(m:n,1)’ for both its x and y data.
  2 件のコメント
Neil
Neil 2014 年 6 月 10 日
Thanks I hadn't caught that... Works perfectly now!
Star Strider
Star Strider 2014 年 6 月 10 日
My pleasure!

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2014 年 6 月 10 日
I thought you asked the same question just recently. In fact, you did!
LOOK AT YOUR DATA. PLOT IT. When you see a problem like this, LOOK AT WHAT YOU ARE PASSING INTO POLYFIT! With 100% certainty, unless you modified polyfit yourself, it is working correctly. So the question is, what did you pass it?
You have not provided us here anything we can test, nor anything we can even see. So showing us a block of useless code will not help us to help you.
The trick in all of these cases is to always move slowly. Don't just throw together a mess of code, then bemoan to us the fact that polyfit is not working correctly. Instead, VERIFY that the numbers you are passing into polyfit are correct! Test each line of code to verify that the numbers mean what you expect them to mean.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by