0
$\begingroup$

I was using scipy.integrate.odeint function , the ode is

$$\frac{y\ dx - x\ dy}{(x+y)^2} + dy = dx$$

with solution

$$y^2 - x^2 - y = c (x + y)\ .$$

Solving it via odeint

from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
def dy_dx(y, x):
  return ((x+y)**2 - y)/((x+y)**2 - x)

xs = np.linspace(1,100,100)
y0 = 1
ys = odeint(dy_dx, y0, xs ,atol = 2)

ys = np.array(ys).flatten()

plt.plot(xs, ys)
plt.xlabel("x")
plt.ylabel("y")

enter image description here

which certainly doesn't match the answer. What could be going wrong?

$\endgroup$
4
$\begingroup$

Nothing is going wrong.

  • Your differential equation is: $$\frac{\mathrm{d}y}{\mathrm{d}x} = \frac{(x+y)^2 - y}{(x+y)^2 - x}$$ Obviously if $x=y$, you have $\frac{\mathrm{d}y}{\mathrm{d}x} = 1$, i.e., $x$ grows exactly as “fast” as $y$. Thus, if $x=y$ for your initial conditions, it will stay that way.

  • Your initial conditions are $x=y=1$ and your numerical result looks like $x=y$.

  • For $c=-\tfrac{1}{2}$ and $c=\tfrac{1}{2}$, respectively, $x=y$ solves your analytical solutions (“solved answers”).

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.