Ask Your Question
2

logistic differential equation

asked 2015-12-07 12:08:10 -0500

AndersM gravatar image

updated 2015-12-07 12:12:28 -0500

Using:

y = function('y', x)
desolve(diff(y,x) == y*(100-y), y)

results in

-1/100*log(y(x) - 100) + 1/100*log(y(x)) == _C + x

This is correct but not very helpful. I found out that "manually" feeding the answer above to solve() changing _C to a defined variable and the function y(x) for a variable and using the argument to_poly_solve=True gives the expected result: y == 100e^(100C + 100x)/(e^(100C + 100*x) - 1)

But I cannot get it to work in one calculation like this:

y = function('y', x)
de = diff(y,x) == y*(100-y)
sol=desolve(de,y)
print("Not so nice:")
sol
print("Better:")
solve(sol, y, to_poly_solve=True)

Would like to know:

a) Is there a way to get desolve or similar solver to return the y= answer directly?

b) If not a) how do I make the desolve -> solve calculation without manually rewriting the solution?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-12-15 01:54:24 -0500

updated 2015-12-15 10:09:19 -0500

The following works for now, but the substitution used below is deprecated, so it outputs a deprecation warning (once per session) and more importantly it might not work in a future version of Sage. Maybe someone can give a more future-proof solution.

sage: y = function('y', x)
sage: de = diff(y, x) == y*(100 - y)
sage: sol = desolve(de, y)
sage: z = SR.var('z')
sage: solz = sol.substitute({y(x):z})  # deprecated
sage: solz.solve(z, to_poly_solve=True)
[z == 100*e^(100*_C + 100*x)/(e^(100*_C + 100*x) - 1)]

To work around the deprecation warning, one could do the replacement in the string representation of the expression and then read the string again into an expression. Not very elegant, but here goes:

sage: y = function('y', x)
sage: de = diff(y, x) == y*(100 - y)
sage: sol = desolve(de, y)
sage: z = SR.var('z')
sage: solz = SR(str(sol.lhs()).replace('y(x)', 'z')) == sol.rhs()
sage: solz
-1/100*log(z - 100) + 1/100*log(z) == _C + x
sage: solz.solve(z, to_poly_solve=True)
[z == 100*e^(100*_C + 100*x)/(e^(100*_C + 100*x) - 1)]

I'd still be interested in a cleaner solution.

edit flag offensive delete link more

Comments

Thanks! I have now been searching for how to rewrite without getting the "DeprecationWarning: Substitution using function-call syntax and unnamed arguments ..." warning from sol.substitute({y(x):z}) But without finding the answer.

AndersM gravatar imageAndersM ( 2015-12-15 06:54:18 -0500 )edit

Note that you get the warning only once per Sage session. I edited my answer to say so and to add a workaround to the deprecated substitution.

slelievre gravatar imageslelievre ( 2015-12-15 10:11:33 -0500 )edit

Yes, Thanks again. I will use your answer. I think there should be a straight-forward way to go from a solution of a differential equation to using that solution in further calculations. But there seems not to be that way (yet?)

AndersM gravatar imageAndersM ( 2015-12-15 12:10:30 -0500 )edit

To avoid the deprecation problems:

sage: yx = function('y')(x)
sage: de = diff(yx, x) == yx*(100 - yx)
sage: sol = desolve(de, yx)
sage: z = SR.var('z')
sage: solz = sol.substitute({yx:z})
sage: solz.solve(z, to_poly_solve=True)

Note that yx is the expression y(x) and that yis a function. They are different objects (the documentation of desolve is itself confused about that). After the merger of http://trac.sagemath.org/ticket/17447 this will slightly improve.

nbruin gravatar imagenbruin ( 2015-12-16 02:48:29 -0500 )edit

I hope it will improve! Something like substitute(old variable or function, new variable or function) would be much easier to remember and makes a lot more sense than knowing that you have to rewrite y(x) as yx and use python dictionary notation...

AndersM gravatar imageAndersM ( 2015-12-17 02:26:43 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-12-07 12:08:10 -0500

Seen: 180 times

Last updated: Dec 15 '15