# Finding the global maximum of a function

I've the following code:

R = 1;
L = 100;
c = 5;
\[Omega] = 1;
Uin = 1;
Plot[InverseLaplaceTransform[((Uin*\[Omega])/(s^2 + \
\[Omega]^2))*(R/(c*L*R*s^2 + L*s + R)), s, t], {t, 0,
10*((2 Pi)/\[Omega])}]


The output gives: How can I find the global maximum of that function? So I need to find the time $$t$$ where the function on the y-axis is the biggest. I can see it is roundabout $$t=17$$ but how can I use Mathematica to solve for that point?

• R = 1; L = 100; c = 5; \[Omega] = 1; Uin = 1; NMaximize[{ InverseLaplaceTransform[((Uin*\[Omega])/(s^2 + \[Omega]^2))*(R/(c*L* R*s^2 + L*s + R)), s, t], 10 < t < 20}, t] – wuyudi Dec 19 '19 at 12:39
• @wuyudi I want to find a general solution, so I can not use the known values for $t$. In general do I not know the boundaries of $t$ – Jan Dec 19 '19 at 12:39
• How about NMaximize[ InverseLaplaceTransform[((Uin*[Omega])/(s^2 + [Omega]^2))*(R/(cL Rs^2 + Ls + R)), s, t], t, Method -> "RandomSearch"] which produces $\{0.0108751,\{t\to 17.0718\}\}$? – user64494 Dec 19 '19 at 13:08
• @user64494 That does not always work. This gives a wrong answer: R = 10000; L = 340*10^(-3); c = 6*10^(-6); [Omega] = 2*Pif; f = 50; Uin = 33/10; NMaximize[{InverseLaplaceTransform[((Uin*[Omega])/(s^2 + \ [Omega]^2))*(R/(cLRs^2 + L*s + R)), s, t], t >= 0}, t, Method -> "RandomSearch"] – Jan Dec 19 '19 at 14:36
• @user64494 Because it should find: $t\approx0.0248259$ – Jan Dec 19 '19 at 14:44

Clear["Global*"]

R = 1;
L = 100;
c = 5;
ω = 1;
Uin = 1;

f[t_] = InverseLaplaceTransform[((Uin*ω)/(s^2 + ω^2))*(R/(c*L*R*
s^2 + L*s + R)), s, t] // FullSimplify

(* (1/259001)(-100 Cos[t] - 499 Sin[t] +
5 E^(-t/10) (20 Cosh[t/(5 Sqrt)] + 509 Sqrt Sinh[t/(5 Sqrt)])) *)


Plot the function to adaptively sample it

plt = Plot[f[t], {t, 0, 10*((2 Pi)/ω)}] The points in the function are

data = Cases[plt, Line[pts_] :> pts, Infinity][];


The peak data point is

peak = data[[SortBy[FindPeaks[data[[All, 2]]], Last][[-1, 1]]]]

(* {17.0672, 0.0108751} *)


Using this as an initial estimate, the function's maximum is then

FindMaximum[f[t], {t, peak[]}, WorkingPrecision -> 20]

(* {0.010875072207704462201, {t -> 17.071813673825464498}} *)


or

Maximize[{f[t], 19/20 peak[] < t < 21/20 peak[]}, t,
WorkingPrecision -> 20]

(* {0.0108751, {t -> 17.0718}} *)


With additional information, that second derivative is negative (which NMaximize should know anyway), you get the right result.

Edit: I think, this works, because second derivative reproduces the Sin and Cos term with same but negative prefactor, so that NMaximize can eliminate these terms.

NMaximize[{f[t], D[f[t], {t, 2}] < 0}, t]

(*  {0.0108751, {t -> 17.0718}}    *)


Edit2

Even Maximizedoes the job easily if you restrict t a little bit away from zero.

Maximize[{f[t], 10^-6 < t < 100}, t, Reals]


You get a root expression for t which you can use for further analytical calculations.

{t -> Root[{-998 E^(1/50 (-5 + 2    Sqrt) #1) +
501 Sqrt E^(1/50 (-5 + 2 Sqrt) #1) -
998 E^(-(1/50) (5 + 2 Sqrt) #1) -
501 Sqrt E^(-(1/50) (5 + 2 Sqrt) #1) + 1996 Cos[#1] -
400 Sin[#1] &, 17.0718136738254647391}]}
`