# differential equation hi

we are discovering Sagemaths and trying to solve and graph a diff equation x^2*y'=y

we tryed to agglomerate two sheets of code :

• the one which solves (and works alone)

• the other one which draws (and works alone)

but together they dont work (maybe because of _C ?)

any help welcome

Vinz+Erw

x = var('x'); y = function('y')
yprime=diff(y(x),x)
EqDf = (x**2)*yprime==y(x)
g(x)=desolve(EqDf, [y(x),x])
g(x)

#dessin = plot([g(x) for _C in srange(-8, 8, 0.4)], (x, -3, 3))
dessin = plot(g(x), (x, -3, 3))

y = var('y')
dessin += plot_vector_field((x^2, y), (x,-3,3), (y,-5,5))

dessin.show(aspect_ratio = 1, ymin = -3, ymax = 3)

edit retag close merge delete

Sort by » oldest newest most voted Indeed, if you want to plot $g(x)$, the expression $g(x)$ should evaluate to a number when $x$ is a number. You correctly determined that the presence of the symbolic variable _C (introduced by the ODE solver) spoils the fun. Your attempt [g(x) for _C in srange(-8, 8, 0.4)] however creates just a list of copies of $g(x)$, without doing any substitution (that you intended). To do the subtitution, you can write e.g. [g(x).subs({var('_C') : _C}) for _C in srange(-8, 8, 0.4)]. Then it works: more

As observed in the question, the solution contains a parameter _C.

That parameter does not get declared globally, so we need to do that before we can use it.

Here is one way using subs.

Define the variable, the function, the differential equation.

sage: x = SR.var('x')
sage: y = function('y')
sage: yprime = diff(y(x), x)
sage: de = (x^2)*yprime == y(x)
sage: de
x^2*diff(y(x), x) == y(x)


Solve.

sage: g(x) = desolve(de, [y(x), x])
sage: g
x |--> _C*e^(-1/x)


Declare the parameter as a symbolic variable.

sage: _C = SR.var('_C')


Plot solutions for a range of values of the parameter.

sage: dessin = plot([g(x).subs({_C: c}) for c in srange(-8, 8, 0.4)], (x, -3, 3))
sage: dessin.show(xmin=-1, xmax=1, ymin=-0.3, ymax=0.3)
Launched png viewer for Graphics object consisting of 80 graphics primitives
` more
{"ft_lang_label":"__label__en","ft_lang_prob":0.74976695,"math_prob":0.99946076,"size":646,"snap":"2023-40-2023-50","text_gpt3_token_len":242,"char_repetition_ratio":0.09190031,"word_repetition_ratio":0.0,"special_character_ratio":0.37925696,"punctuation_ratio":0.16447368,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.999938,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,null,null,null,null,6,null,7,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-24T16:59:45Z\",\"WARC-Record-ID\":\"<urn:uuid:fb6949b7-088e-4193-a4bf-13d8dd79f3d3>\",\"Content-Length\":\"59513\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ebcd13f0-c02d-4cb2-90a2-047dc51cb2f8>\",\"WARC-Concurrent-To\":\"<urn:uuid:74665cc8-2c95-4934-971b-60ee39644c5e>\",\"WARC-IP-Address\":\"194.254.163.53\",\"WARC-Target-URI\":\"https://ask.sagemath.org/question/57804/differential-equation/\",\"WARC-Payload-Digest\":\"sha1:M462FRVAACLXXQGS7TB5IAQKGL2JNSAL\",\"WARC-Block-Digest\":\"sha1:DUV537GMMYBM5PZ2Q2HE5TJ6IAMCJJ6Z\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506658.2_warc_CC-MAIN-20230924155422-20230924185422-00647.warc.gz\"}"}