1
$\begingroup$

I want to fit 2nd order polynomial to multiple graphs and show them together; I imported file by using following command:

Mode1 = Import["1-CF.txt", "Table"];

Then, I plot each as following

PM20 = Mode1[[All, {1, 2}]];

PM21 = Mode1[[All, {1, 3}]];

Then, I make fit to each by using:

FPM20 = Fit[PM20, {1, x, x^2}, x]

FPM21 = Fit[PM21, {1, x, x^2}, x]

(I need these equations too, because further I need to have differential on each polynomial fit, which is I am doing by:)

DFPM20 = D[FPM20, {x, 1}][[1]];

DFPM21 = D[FPM21, {x, 1}][[1]];

I want to make all this process elegant. So far, I have been able to show all plots by single command:

plotlist = Table[
  ListLinePlot[Mode1[[All, {1, i}]]]
  , {i, 2, Dimensions[Mode1[[2]]}
 ]

Now, I want to fit and show polynomial fit together.

$\endgroup$
  • 3
    $\begingroup$ Have a look at Show. $\endgroup$ – LBogaardt Jul 17 at 17:02
  • $\begingroup$ Using formatted form and indentation makes the code on your question more readable. $\endgroup$ – rhermans Jul 17 at 17:12
  • $\begingroup$ Thank you @rhermans for pointing out. I will keep this in my mind for future. $\endgroup$ – Amanullah Malik Jul 17 at 17:19
1
$\begingroup$
Show[Plot[Evaluate[Fit[Mode1[[All, {1, #}]], {1, x, x^2}, x] & /@ {2, 3}], {x, 0, 5}], 
ListPlot[Mode1[[All, {1, #}]] & /@ {2, 3}, PlotStyle -> {Red, Black}]]

enter image description here

You may also add the derivatives:

Show[Plot[Evaluate[Fit[Mode1[[All, {1, #}]], {1, x, x^2}, x] & /@ {2, 3}], {x, 0, 5}], 
Plot[Evaluate[D[Fit[Mode1[[All, {1, #}]], {1, x, x^2}, x], x] & /@ {2, 3}], {x, 0, 5}, PlotStyle -> {Gray, Brown}],
ListPlot[Mode1[[All, {1, #}]] & /@ {2, 3}, PlotStyle -> {Red, Black}]]

enter image description here

$\endgroup$
  • $\begingroup$ Hi, Thanks, its works perfectly, can I show in table form separately for each column(FPM20, FPM21)? $\endgroup$ – Amanullah Malik Jul 18 at 9:20
  • $\begingroup$ Hi, I worked it out; Table[Show[ Plot[Evaluate[ Fit[Mode1[[All, {1, i}]], {1, x, x^2}, x] & /@ {i}], {x, -3, 3}], Plot[Evaluate[ D[Fit[Mode1[[All, {1, i}]], {1, x, x^2}, x], x] & /@ {i}], {x, -3, 3}, PlotStyle -> {Gray, Brown}], ListPlot[Mode1[[All, {1, i}]] & /@ {i}, PlotStyle -> {Red, Black}]], {i, 2, Dimensions[Mode1][[2]]}] $\endgroup$ – Amanullah Malik Jul 18 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.