ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 02 Jan 2015 07:41:16 +0100Defining constants after solving ODE/PDEhttps://ask.sagemath.org/question/25373/defining-constants-after-solving-odepde/Hello
Solving the differential equation $f'(x) = f(x)$ the answer leads to $f(x) = ce^x$ where $c$ is a constant. How can I generically solve this ODE in SAGE and define $c$ afterwards (I don't want to use `ics`)?
Consider this script
x = var('x')
f = function('f',x)
f = desolve(diff(f,x) == f(x), f, ival=x)
print str(f(x))
>> ce^x
#I want to define c afterwards
A method I found:
#continue the previous prompt
f(x,c) = f(x)
h(x) = f(x,10)
Is there something else that I can do?
Thu, 01 Jan 2015 21:26:59 +0100https://ask.sagemath.org/question/25373/defining-constants-after-solving-odepde/Answer by rws for <p>Hello</p>
<p>Solving the differential equation $f'(x) = f(x)$ the answer leads to $f(x) = ce^x$ where $c$ is a constant. How can I generically solve this ODE in SAGE and define $c$ afterwards (I don't want to use <code>ics</code>)?</p>
<p>Consider this script</p>
<pre><code>x = var('x')
f = function('f',x)
f = desolve(diff(f,x) == f(x), f, ival=x)
print str(f(x))
>> ce^x
#I want to define c afterwards
</code></pre>
<p>A method I found:</p>
<pre><code>#continue the previous prompt
f(x,c) = f(x)
h(x) = f(x,10)
</code></pre>
<p>Is there something else that I can do? </p>
https://ask.sagemath.org/question/25373/defining-constants-after-solving-odepde/?answer=25375#post-id-25375First, although you set `f` to be a function, you later overwrite it with the result of `desolve` which is a symbolic expression. You can, instead of using it as function, simply substitute any other expression for `c`:
f = desolve(diff(f,x) == f(x), f, ival=x)
sage: f
c*e^x
sage: f.subs(c=10)
10*e^x
sage: f.subs(c=sin(x))
e^x*sin(x)
If you want that in function notation, assign the expression to a function call:
sage: f(x,c)=f
sage: f
(x, c) |--> c*e^x
sage: f(x,10)
10*e^x
Fri, 02 Jan 2015 07:41:16 +0100https://ask.sagemath.org/question/25373/defining-constants-after-solving-odepde/?answer=25375#post-id-25375