Message Boards Message Boards

Plotting change in first derivative of 3rd order polynomial over time?

Hi,

I am trying to set up a code that will first pull in the data from CVS files. Second, apply polynomial fit to each data set to obtain and equation in the form ax^3+bx^2+cx+d. Then I want to differentiate and find the y coordinate of both the stationary points. Then plot the change in the stationary points over time, note the each data set is separated by the same time interval. Here, I'm just trying to get it working on 6 data sets. However, the complete data will include around 200 data sets.

Please see my code so far. I can't seem to import the solutions to the ODE into the original polynomial to extrapolate y value.

Also, if anyone has a better solution to this, perhaps using matrices it would be appreciated.

Thanks in advance, Ryan

s1=Import["C:\\Users\\RyanG\\Desktop\\Function\\1.csv"]
s2=Import["C:\\Users\\RyanG\\Desktop\\Function\\2.csv"]
s3=Import["C:\\Users\\RyanG\\Desktop\\Function\\3.csv"]
s4=Import["C:\\Users\\RyanG\\Desktop\\Function\\4.csv"]
s5=Import["C:\\Users\\RyanG\\Desktop\\Function\\5.csv"]
s6=Import["C:\\Users\\RyanG\\Desktop\\Function\\6.csv"]
In[174]:= s11=Fit[s1, {1, x, x^2,x^3}, x]
s22=Fit[s2, {1, x, x^2,x^3}, x]
s33=Fit[s3, {1, x, x^2,x^3}, x]
s44=Fit[s4, {1, x, x^2,x^3}, x]
s55=Fit[s5, {1, x, x^2,x^3}, x]
s66=Fit[s6, {1, x, x^2,x^3}, x]
In[168]:= NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s11\)==0, x]
NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s22\)==0, x]
NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s33\)==0, x]
NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s44\)==0, x]
NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s55\)==0, x]
NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s66\)==0, x]
Out[168]= x==195.719||x==235.129
Out[169]= x==195.503||x==234.98
Out[170]= x==195.391||x==234.854
Out[171]= x==195.44||x==235.015
Out[172]= x==195.271||x==234.842
Out[173]= x==195.396||x==234.869
y1[x_]=s11
y2[x_]=s22
y3[x_]=s33
y4[x_]=s44
y5[x_]=s55
y6[x_]=s66
In[214]:= y1[NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s11\)==0, x]]
y2[NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s22\)==0, x]]
y3[NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s33\)==0, x]]
y4[NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s44\)==0, x]]
y5[NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s55\)==0, x]]
y6[NRoots[\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]s66\)==0, x]]


Out[214]= 9399.03 -133.272 (x==195.719||x==235.129)+0.623871 (x==195.719||x==235.129)^2-0.000965337 (x==195.719||x==235.129)^3
Out[215]= 10585.1 -150.227 (x==195.503||x==234.98)+0.703863 (x==195.503||x==234.98)^2-0.00109003 (x==195.503||x==234.98)^3
Out[216]= 10340.5 -146.831 (x==195.391||x==234.854)+0.68834 (x==195.391||x==234.854)^2-0.00106659 (x==195.391||x==234.854)^3
Out[217]= 11746.3 -166.75 (x==195.44||x==235.015)+0.781365 (x==195.44||x==235.015)^2-0.00121014 (x==195.44||x==235.015)^3
Out[218]= 11643.6 -165.405 (x==195.271||x==234.842)+0.77569 (x==195.271||x==234.842)^2-0.00120231 (x==195.271||x==234.842)^3
Out[219]= 11792.3 -167.447 (x==195.396||x==234.869)+0.784951 (x==195.396||x==234.869)^2-0.00121623 (x==195.396||x==234.869)^3
POSTED BY: Ryan Greenhalgh

A plot of the stationary points over time can be done this way:

ListPlot[Transpose@
  Table[x /. 
    NSolve[D[fitPolynomial, x] == 0, x, Reals], {fitPolynomial, {s11, 
     s22, s33, s44, s55, s66}}]]

If you want to do it with 200 data points you need a different naming strategy, for example

Do[s[i] = 
   Import[StringJoin["C:\\Users\\RyanG\\Desktop\\Function\\", 
     ToString[i], ".csv"]];
  ss[i] = Fit[s[i], {1, x, x^2, x^3}, x],
  {i, 200}];
ListPlot[Transpose@
  Table[x /. NSolve[D[ss[i], x] == 0, x, Reals], {i, 200}]]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract