# Plotting a solution and its derivative, of a first order ODE

I have this code to solve a simple first order ODE using odeint. I managed to plot the solution y(r), but I also want to plot the derivative y'= dy/dr. I know y' it is given by f(y,r), but I'm not sure how to call the function with the output of the integration. Thank you in advance.

``````    from math import sqrt
from numpy import zeros,linspace,array
from scipy.integrate import odeint
import matplotlib.pylab as plt

def f(y,r):
f = zeros(1)
f = -(y*(y-1.0)/r)-y*(2/r+\
((r/m)/(1-r**2/m))*(2*sqrt(1-r**2/m)-k)/(k-sqrt(1-r**2/m)))\
-(1/(1-r**2/m))*(-l*(l+1)/r+\
(3*r/m)*(k+2*sqrt(1-r**2/m))/(k-sqrt(1-r**2/m)))\
+((4*r**3)/((m**2)*(1-r**2/m)))*(1/(k-sqrt(1-r**2/m))**2)
return f

m = 1.15
k = 3*sqrt(1-1/m)
l = 2.0
r = 1.0e-10
rf = 1.0

rspan = linspace(r,rf,1000)
y0 = array([l])
sol = odeint(f,y0,rspan)
plt.plot(rspan,sol,'k:',lw=1.5)
``````

From `odeint` documentation:

For new code, use scipy.integrate.solve_ivp to solve a differential equation.

I have modified your code in the following manner and obtained the figure below.

``````import matplotlib.pyplot as plt
from numpy import gradient, squeeze, sqrt
from scipy.integrate import solve_ivp

def fun(t, y):
l = 2
m = 1.15
k = 3 * sqrt(1 - 1 / m)
return (-y * (y - 1) / t - y * (2 / t + t / m / (1 - t ** 2 / m)
* (2 * sqrt(1 - t ** 2 / m) - k)
/ (k - sqrt(1 - t ** 2 / m)))
- 1 / (1 - t ** 2 / m) * (-l * (l + 1) / t + 3 * t / m
* (k + 2 * sqrt(1 - t ** 2 / m))
/ (k - sqrt(1 - t ** 2 / m)))
+ 4 * t ** 3 / m ** 2 / (1 - t ** 2 / m)
/ (k - sqrt(1 - t ** 2 / m)) ** 2)

sol = solve_ivp(fun, t_span=[1e-10, 1], y0=, method='BDF',
dense_output=True)
if sol.success is True:
print(sol.t.shape, sol.y.shape)
label='Scipy Solution')
plt.plot(sol.t, fun(sol.t, squeeze(sol.y)), linestyle='dashed',
color='xkcd:purple', label='Derivative Using the Function')
color='xkcd:bright orange', label='Derivative Using Numpy')
plt.legend()
plt.tight_layout()
plt.savefig('so.png', bbox_inches='tight', dpi=300)
plt.show()
`````` To address the comment about `squeeze`, please see below (extracted from scipy.integrate.solve_ivp): where `n` is defined according to: • What is `squeeze`? Why is it necessary? Nov 14 '19 at 12:13
• Doc is here (docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html). After the if condition, if you execute `print(sol.t.shape, sol.y.shape)`, you should get `(32,) (1, 32)`, which shows that t is 1D while y is 2D. Therefore, squeeze is needed. Nov 14 '19 at 12:21
• I know... what I mean is that your answer is not complete. Nov 14 '19 at 13:44
• I think it's correct, I checked your code with my equation again and it works. I think you missed something when you rewrote the ODE. I did not know about the squeeze, thank you! Nov 14 '19 at 20:56
• @CamPos You are right, I had a `*` instead of a `/` in the before-last line. Would you mind accepting the answer (see here stackoverflow.com/help/someone-answers) if it helped you? Thank you. Nov 14 '19 at 23:10
{"ft_lang_label":"__label__en","ft_lang_prob":0.67243534,"math_prob":0.9862539,"size":841,"snap":"2021-43-2021-49","text_gpt3_token_len":343,"char_repetition_ratio":0.15292712,"word_repetition_ratio":0.0,"special_character_ratio":0.44946492,"punctuation_ratio":0.12017167,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99983066,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-19T03:51:04Z\",\"WARC-Record-ID\":\"<urn:uuid:4b5274ca-c500-4f2c-adb4-3308c0e8dc05>\",\"Content-Length\":\"171968\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a688628d-e23b-4dbf-b78d-5121baf9e5b8>\",\"WARC-Concurrent-To\":\"<urn:uuid:1472eeea-38ce-48c1-b7f9-e271fab99c0c>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/58853437/plotting-a-solution-and-its-derivative-of-a-first-order-ode/58854745#58854745\",\"WARC-Payload-Digest\":\"sha1:H3TFCMSGZGRFAXV3W3ITVC54XYJCFAOS\",\"WARC-Block-Digest\":\"sha1:5KJGBQDYJFRKQ3SP4Y2WWIBLACKLJ5ZX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585231.62_warc_CC-MAIN-20211019012407-20211019042407-00033.warc.gz\"}"}