Solve the differential equation xy'+(sin y-3x^2cos y)cos y=0
Solve the differential equation $xy'+(\sin y-3x^2\cos y)\cos y=0$.
Explanation of Method
First directly test the source's own guessed final answer against the original equation with \texttt{sympy} (the source's own step 9 verification trails off unfinished and never actually confirms it). Since it fails, solve the equation properly from scratch: divide by $\cos^2 y$ to expose $d(\tan y)/dx$, reducing the equation to a first-order linear ODE in $w=\tan y$, solve it with \texttt{sympy.dsolve}, and verify the result rigorously (symbolically and numerically) against the original equation.
Step 1: Test the source's guessed answer directly — it fails
The source's step 9 never finishes its own algebra ("After careful algebra, this verifies" — without showing it). Testing $x^2\cos^2y-\tfrac14\sin2y=C$ directly: implicit differentiation with \texttt{sympy}, solving for $y'$, and substituting into $xy'+(\sin y-3x^2\cos y)\cos y$ gives a residual that does not simplify to $0$ — it remains a nontrivial rational function of $x,y$. A numeric spot check (e.g. near $x_0=1.3,\ y_0=0.4$, using the guessed relation to define $y(x)$ implicitly and finite-differencing) gives a residual of about $-2.1$, far from zero. The source's guessed answer is wrong.
Step 2: Confirm the equation is not exact (source's own finding, checked independently)
With $M=\sin y\cos y-3x^2\cos^2y$, $N=x$: \texttt{sympy} confirms $\partial M/\partial y=\cos2y+3x^2\sin2y\ne 1=\partial N/\partial x$, so not exact — matching the source. The source's attempted $\mu(y)$ integrating factor correctly comes out depending on both $x$ and $y$, so it doesn't work either; also confirmed.
Step 3: Divide by $\cos^2 y$ to expose a hidden linear equation
Dividing the original equation through by $\cos^2y$: \[\frac{x\,y'}{\cos^2y}+\tan y-3x^2=0.\] Since $\dfrac{d}{dx}(\tan y)=\dfrac{y'}{\cos^2y}$, setting $w=\tan y$ turns this into \[x\frac{dw}{dx}+w=3x^2,\] a first-order linear equation in $w(x)$ — a substitution the source never tried, despite dividing by $\cos^2 y$-related quantities elsewhere in its (abandoned) attempts.
Step 4: Solve the linear equation with \texttt{sympy.dsolve}
Calling \texttt{sympy.dsolve} on $x w'+w=3x^2$ (equivalently $w'+w/x=3x$, integrating factor $x$) returns \[w(x)=\frac{C_1+x^3}{x}=x^2+\frac{C_1}{x},\] i.e. \[\tan y=x^2+\frac{C}{x}.\]
Step 5: Rigorous verification — symbolic and numeric
Symbolic: from $\tan y=x^2+C/x$, $y'=(2x-C/x^2)\cos^2y$. Substituting $\cos^2y=1/(1+\tan^2y)$ and $\sin y\cos y=\tan y\cdot\cos^2y$ with $\tan y=x^2+C/x$ throughout (so every trig quantity is expressed consistently in terms of $x$), \texttt{sympy.simplify} on the original left-hand side $xy'+(\sin y-3x^2\cos y)\cos y$ gives exactly $\mathbf{0}$.
Numeric (independent cross-check): at $x_0=1.3$, $C=0.7$ (so $y_0=\arctan(x_0^2+C/x_0)$), computing $y'$ by finite differences and substituting into the original equation gives a residual of about $-7.6\times10^{-11}$ — zero to floating-point precision.
Result
Final answer:
The source's guessed final answer ($x^2\cos^2y-\tfrac14\sin2y=C$) is incorrect — it fails a direct symbolic and numeric residual check against the original equation, consistent with the source's own step 9 never actually completing its claimed verification. Dividing the original equation by $\cos^2y$ exposes it as a first-order linear equation in $w=\tan y$, giving the correct solution: \[\boxed{\,\tan y=x^2+\frac{C}{x}\,}\] verified both symbolically (exact residual $0$) and numerically (residual $\sim10^{-11}$) against the original equation $xy'+(\sin y-3x^2\cos y)\cos y=0$.