I'd like to see how close I can fit an exponential function to a cosine function in the domain of $\;-\frac{\pi}{2}$ to $\frac{\pi}{2}$ in the least squares sense.
FindFit[Cos[x], {A*Exp[l*(Cos[x] - 1)], -π/2 <= x <= π/2}, {A, l}, x]
FindFit::fitd: First argument Cos[x] in FindFit is not a list or a rectangular array.
I could create a table for Cos[x]
to pass as the first parameter but I feel like I'm approaching this wrong and should be able to give Mathematica my problem in symbolic form. Perhaps, I should try Minimize but I didn't get any results out of it.
Minimize[{(Cos[x] - A*Exp[l*(Cos[x] - 1)])^2, -π/2 <= x <= π/2}, {A, l, x}]
Minimize[{(-A E^(l (-1 + Cos[x])) + Cos[x])^2, -π/2 <= x <= π/2}, {A, l, x}]
Answer
You can do the integral symbolically, then minimize numerically:
distance =
Integrate[(A*Exp[l*(Cos[x] - 1)] - Cos[x])^2, {x, -Pi/2, Pi/2},
Assumptions -> (A | l) \[Element] Reals]
{min, sol} = NMinimize[distance, {A, l}]
Plot[{A*Exp[l*(Cos[x] - 1)], Cos[x]} /. sol // Evaluate, {x, -Pi/2, Pi/2},
Filling -> {1 -> {2}}, PlotStyle -> Black]
Comments
Post a Comment