ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 15 May 2019 06:06:47 -0500Solving an ODE and simplifying the resulthttp://ask.sagemath.org/question/46517/solving-an-ode-and-simplifying-the-result/ I'm interested in solving the differential equation $$3 h' + 3 h^2 = c_1,$$ where $c_1$ is a positive real number.
var('t')
var('c1', latex_name=r'c_1')
h = function('h')(t)
eq = -3*h^2 + c1 - 3*diff(h, t)
eq_sol = desolve(eq, h, ivar=t, contrib_ode=True)
The above code works, but it's not solved explicitly for $h$, so
h_sol = solve(eq_sol, h)
h_sol = h_sol[0]
h_sol
This gives something like $$h\left(t\right) = \frac{\sqrt{3} \sqrt{c_{1}} {\left(e^{\left(\frac{2}{3} \, \sqrt{3} C \sqrt{c_{1}} + \frac{2}{3} \, \sqrt{3} \sqrt{c_{1}} t\right)} + 1\right)}}{3 \, {\left(e^{\left(\frac{2}{3} \, \sqrt{3} C \sqrt{c_{1}} + \frac{2}{3} \, \sqrt{3} \sqrt{c_{1}} t\right)} - 1\right)}},$$
in sage notation (non-LaTeX) it starts like
h(t) == 1/3*sqrt(3)*sqrt(c1)* ...
**Question 1:** Is there a way to allocate to the solution (i.e. `h_sol`) the RHS of the above? without the `h(t) == ` part.
I had to set by hand (it is ease, but it would be nice to automatize the allocation)
var('C') # the integration constant introduced above
h_sol = 1/3*sqrt(3)*sqrt(c1)* ...
Then, by simply looking at the solution it is clear that it can be simplified. I tried things like
h_sol = h_sol.canonicalize_radical()
h_sol = h_sol.collect_common_factors()
h_sol = h_sol.simplify_rectform(complexity_measure = None)
but none of them returns the expected result, which could be obtained from Mathematica's kernel
mathematica("DSolve[3*h'[t] + 3*h[t]^2 == C[1], h[t], t]//FullSimplify")
$$ \sqrt{\frac{c_1}{3}} \tanh\left( \sqrt{\frac{c_1}{3}} (t - 3 c_2) \right) $$
**Question 2:** How could the expression `h_sol` be manipulated to obtain the hyperbolic tangent?Tue, 14 May 2019 02:41:24 -0500http://ask.sagemath.org/question/46517/solving-an-ode-and-simplifying-the-result/Answer by dan_fulea for <p>I'm interested in solving the differential equation $$3 h' + 3 h^2 = c_1,$$ where $c_1$ is a positive real number.</p>
<pre><code>var('t')
var('c1', latex_name=r'c_1')
h = function('h')(t)
eq = -3*h^2 + c1 - 3*diff(h, t)
eq_sol = desolve(eq, h, ivar=t, contrib_ode=True)
</code></pre>
<p>The above code works, but it's not solved explicitly for $h$, so</p>
<pre><code>h_sol = solve(eq_sol, h)
h_sol = h_sol[0]
h_sol
</code></pre>
<p>This gives something like $$h\left(t\right) = \frac{\sqrt{3} \sqrt{c_{1}} {\left(e^{\left(\frac{2}{3} \, \sqrt{3} C \sqrt{c_{1}} + \frac{2}{3} \, \sqrt{3} \sqrt{c_{1}} t\right)} + 1\right)}}{3 \, {\left(e^{\left(\frac{2}{3} \, \sqrt{3} C \sqrt{c_{1}} + \frac{2}{3} \, \sqrt{3} \sqrt{c_{1}} t\right)} - 1\right)}},$$</p>
<p>in sage notation (non-LaTeX) it starts like</p>
<pre><code>h(t) == 1/3*sqrt(3)*sqrt(c1)* ...
</code></pre>
<p><strong>Question 1:</strong> Is there a way to allocate to the solution (i.e. <code>h_sol</code>) the RHS of the above? without the <code>h(t) ==</code> part.</p>
<p>I had to set by hand (it is ease, but it would be nice to automatize the allocation)</p>
<pre><code>var('C') # the integration constant introduced above
h_sol = 1/3*sqrt(3)*sqrt(c1)* ...
</code></pre>
<p>Then, by simply looking at the solution it is clear that it can be simplified. I tried things like</p>
<pre><code>h_sol = h_sol.canonicalize_radical()
h_sol = h_sol.collect_common_factors()
h_sol = h_sol.simplify_rectform(complexity_measure = None)
</code></pre>
<p>but none of them returns the expected result, which could be obtained from Mathematica's kernel</p>
<pre><code>mathematica("DSolve[3*h'[t] + 3*h[t]^2 == C[1], h[t], t]//FullSimplify")
</code></pre>
<p>$$ \sqrt{\frac{c_1}{3}} \tanh\left( \sqrt{\frac{c_1}{3}} (t - 3 c_2) \right) $$</p>
<p><strong>Question 2:</strong> How could the expression <code>h_sol</code> be manipulated to obtain the hyperbolic tangent?</p>
http://ask.sagemath.org/question/46517/solving-an-ode-and-simplifying-the-result/?answer=46529#post-id-46529(1) One can use either `solution_dict=True`... or the `rhs` method...
sage: h_sol = solve(eq_sol, h, solution_dict=True)[0][ h(t) ]
sage: h_sol
1/3*sqrt(3)*sqrt(c1)*(e^(2/3*sqrt(3)*_C*sqrt(c1) + 2/3*sqrt(3)*sqrt(c1)*t) + 1)/(e^(2/3*sqrt(3)*_C*sqrt(c1) + 2/3*sqrt(3)*sqrt(c1)*t) - 1)
sage: h_sol = solve(eq_sol, h)[0].rhs()
sage: h_sol
1/3*sqrt(3)*sqrt(c1)*(e^(2/3*sqrt(3)*_C*sqrt(c1) + 2/3*sqrt(3)*sqrt(c1)*t) + 1)/(e^(2/3*sqrt(3)*_C*sqrt(c1) + 2/3*sqrt(3)*sqrt(c1)*t) - 1)
(2) The human eye can do this better, just divide by the exponential of the half exponent. For me, the delivered form is acceptable, rational function composed with exponential function.
(No `simplify....` method gave me an other expression.)
Wed, 15 May 2019 06:06:47 -0500http://ask.sagemath.org/question/46517/solving-an-ode-and-simplifying-the-result/?answer=46529#post-id-46529