# How to find the fit for a numerical solution

I solved numerically a differential equation and I found a solution (solution that can be plot). I also tried an Ansatz to find an analytical solution and I got an expression.

Using the numerical solution and the form of the analytical expression, I want to get the numerical values that make the analytical expression fit with the numerical solution. In other words, I want to find a fit for the numerical solution taking into account that I know the possible form of the expression.

Differential equation:

e[\[Phi]_] := (4 (3 + \[Alpha]) H[\[Phi]] + 1/(\[Eta] Derivative[1][H][\[Phi]]^2) ((-1 - 2 (9 + 2 \[Alpha]) \[Eta] H[\[Phi]]^2 +
Sqrt[(1 + 2 (9 + 2 \[Alpha]) \[Eta] H[\[Phi]]^2)^2 + 16 M^2 \[Eta] Derivative[1][H][\[Phi]]^2]) (H^\[Prime]\[Prime])[\[Phi]] + 4 \[Eta] Derivative[1][H][\[Phi]]^2 ((9 + 2 \[Alpha]) H[\[Phi]] - ((9 + 2 \[Alpha]) H[\[Phi]] +  2 (9 + 2 \[Alpha])^2 \[Eta] H[\[Phi]]^3 + 4 M^2 (H^\[Prime]\[Prime])[\[Phi]])/Sqrt[(1 + 2 (9 + 2 \[Alpha]) \[Eta] H[\[Phi]]^2)^2 + 16 M^2 \[Eta] Derivative[1][H][\[Phi]]^2])))


Numerical solution:

And the analytical expression has the form: a cos(b* phi)

Thanks a lot!!!

• Please provide complete Mathematica code (e[\[Phi]] isn't defined ) , not only a screen shot . Thanks. – Ulrich Neumann Jan 20 '19 at 21:28

## 1 Answer

Not knowing H[\[Phi]] a assume examplary

H[\[Phi]_] := E Cos[Pi \[Phi]]


Now you want to fit H[\[Phi]]~ a Cos[b \[Phi]]

J[a_?NumericQ, b_?NumericQ] :=NIntegrate[(H[\[Phi]] - a Cos[b \[Phi]])^2, {\[Phi], 0, 5}]
NMinimize[{J[a, b], a > 0, b > 0}, {a, b}]
(*{1.67347*10^-18, {a -> 2.71828, b -> 3.14159}}*)