# Finding maximum of a function of 2 variables

I have function defined in such way:

ω0=145199+1;
f=1;
b=ω0/4;
s[ω_] := NDSolve[{x''[t]+b x'[t]+ω0^2 x[t]==f Sin[ω t],x[0]==0,x'[0]==0},x[t],{t,0,2π/ω0}];
x[t_,ω_]:=s[ω][[1,1,2]];


I'm trying to find t and ω for which the max. of the function occurs.

ωmax = NMaximize[{x[t,omg], 0<=t<=2π/ω0},{t, omg}]


Overall I am trying to find resonant frequency for which the largest oscillation occurs. Unfortunately I get this error:

I have searched the reference, the stack and could not find the answer for the problem.

• Can you include the definition of f, b (make sure the example you show is complete) – Szabolcs Mar 23 '20 at 15:46
• That's right, sorry. – Pekureda Mar 23 '20 at 15:47

Clear["Global*"]

ω0 = 145199 + 1;
f = 1;
b = ω0/4;


The differential equation can be solved exactly

x[t_, ω_] = DSolveValue[
{x''[t] + b x'[t] + ω0^2 x[t] == f Sin[ω t],
x[0] == 0, x'[0] == 0}, x[t], t] //
FullSimplify[#, 0 <= t <= 2 π/ω0] &


EDIT: Use of arbitrary precision is necessary. This is done by specifying a WorkingPrecision for NMaximize

ωmax =
NMaximize[{x[t, ω], 0 <= t <= 2 π/ω0,
0 < ω < 2 ω0}, {t, ω},
WorkingPrecision -> 50] // N

(* {7.09473*10^-11, {t -> 0.0000274786, ω -> 84877.3}} *)

Show[
Plot3D[x[t, ω], {t, 0, 2 π/ω0}, {ω, 0, 2 ω0},
AxesLabel -> Automatic,
ClippingStyle -> None],
Graphics3D[{Red, AbsolutePointSize[5],
Point[{t, ω, x[t, ω]} /. ωmax[[2]]]}]]
`

• Awesome! This value should be calculated as a product of "numerical experiment", whatever it means. It looks like I have to watch some tutorials of using mathematica to know how does it actually works. Unfortunately I have all lectures cancelled because of ncov and we were made to do something here, without any preparation etc. so I am kind of lost. Anyway thanks and I wish you good health! – Pekureda Mar 23 '20 at 16:39
• @Pekureda - Note that I had to further increase the precision to get a correct value. – Bob Hanlon Mar 23 '20 at 16:42
• Yeah. Although it is not in the problem described here, I was able to get some values out of it, but only when ω was treated as constant, so I hoped I would get the answer. – Pekureda Mar 23 '20 at 16:46