필터 지우기
필터 지우기

polyfit not giving working correctly?

조회 수: 23 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by