# How to fit 2nd order polynomial to multiple graphs and show them together?

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.

• Have a look at Show. Jul 17, 2019 at 17:02
• Using formatted form and indentation makes the code on your question more readable. Jul 17, 2019 at 17:12
• Thank you @rhermans for pointing out. I will keep this in my mind for future. Jul 17, 2019 at 17:19

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}]]


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}]]


• Hi, Thanks, its works perfectly, can I show in table form separately for each column(FPM20, FPM21)? Jul 18, 2019 at 9:20
• 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]]}] Jul 18, 2019 at 9:24