polyfit not giving working correctly?

47 views (last 30 days)
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 Comments
John D'Errico
John D'Errico on 10 Jun 2014
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 on 10 Jun 2014
Thank you John D'Errico, I appreciate it!

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 10 Jun 2014
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 Comments
Neil
Neil on 10 Jun 2014
Thanks I hadn't caught that... Works perfectly now!

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 10 Jun 2014
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.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!