restart;

H := subs(p=diff(f(q,P,t), q), 1/(2*m)*p^2 + 1/2*m*w^2*q^2) = -diff(f(q,P,t), t);

H:=subs( f(q,P,t) = f1(q) + f2(t), H);

sol:=dsolve({rhs(H)=E,lhs(H)=E});

S:=rhs(sol[1][1]+sol[1][2]);

subs(m=1, diff(S,q));

p := (E, w) -> -(1/2)*sqrt(-q^2*w^2+2*E)+(1/2)*q^2*w^2/sqrt(-q^2*w^2+2*E)-E*(sqrt(w^2)/sqrt(-q^2*w^2+2*E)+sqrt(w^2)*q^2*w^2/(-q^2*w^2+2*E)^(3/2))/(sqrt(w^2)*(1+w^2*q^2/(-q^2*w^2+2*E)));

lst := [seq(p((1/2)*E, w), E=0..5),seq(-p((1/2)*E, w), E=0 .. 5)]:

plot(lst, w= -3*Pi..3*Pi, color = black, numpoints=1000);

Q is the new coordinate which is diff(S,E)

t0 is the time when argument of solve(subs(m=1, diff(S,E))=0, t) vanish

initialq := solve(solve(subs(m=1, diff(S,E))=0, t)=0, q);

after find the initial value, how to plot with new coordinate?

dsolve({H, q=0}, f(q,P,t));

i try another way to do

restart;

H := subs(p=diff(f(q,P,t), q), 1/(2*m)*p^2 + 1/2*m*w^2*q^2) = -diff(f(q,P,t), t);

H:=subs( f(q,P,t) = f1(q) + f2(t), H);

sol:=dsolve({rhs(H)=E,lhs(H)=E});

S:=rhs(sol[1][1]+sol[1][2]);

p2 := subs(m=1, diff(S,q));

initialq := solve(solve(subs(m=1, diff(S,E))=0, t)=0, q);

H := subs(p=p2, 1/(2*m)*p^2 + 1/2*m*w^2*q^2) = -diff(f(t), t);

H := subs(w=theta(t), H);

sol := dsolve({H, q=0}, numeric, range = 0 .. 30);

odeplot(sol, [t, theta(t)], refine = 2);

Error, (in dsolve/numeric/process_input) invalid specification of initial conditions, got q = 0