# Optimization of the solution to an ODE

Apologies if this is obvious -- I'm very new to Mathematica.

I'm trying to minimize the solution to an ODE with respect to a variable. The following code generates the solution to the ODE,

sol=DSolve[
{(1/2) * σ^2 * k''[q] + μ*k'[q] - λ*k[q] == 0,
k'[0] == -mc, k'[b] == me}, k, q]


but when I try minimizing using the Minimize (with respect to only q) command,

Minimize[k[q] /.First@sol, q]


I'm coming up empty -- it should return the minimum value of k(q)* in terms of $b, \lambda, \mu, \sigma$, $me$ and $mc$ as well as q* in terms of $b, \lambda, \mu, \sigma$, $me$ and $mc$.

* Update *

The following code works (thank you @bbgodfrey):

s = Simplify[k[q] /. DSolve[{(1/2)*σ^2*k''[q] + μ*k'[q] - λ*k[q] == 0,k'[0] == -mc, k'[b] == me}, k[q], q][[1, 1]]]
sq=q /. Solve[D[s, q] == 0, q][[2, 1]]
kq = Simplify[s /. q -> sq]


But a second minimization with respect to b of the function

Gq = kq + b*\[Gamma]


I start to run into trouble again. I try:

Minimize[Gq,b]


returns unevaluated. This also doesn't work:

sb=b/. Solve[D[Gq, b] == 0, b]


Any help would be very much appreciated!

• Specifically what code have you written to use Minimize? I am also not sure that I understand your double-minimization requirements. Could you expand on that? Also, consider sol = DSolve[...]; k[q] /. First@sol: that should give you the function you want to minimize. – MarcoB May 23 '16 at 23:50
• @MarcoB Thank you for the response! I clarified the question to eliminate the confusion (I think the second optimization step will be easy once I get the first one to work) and also added code for minimization (but I think I'm doing this wrong!). Thanks again. – Karthik May 24 '16 at 0:17
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey May 24 '16 at 2:54

A slight variant of the code in the question yields

s = Simplify[k[q] /. DSolve[{(1/2)*σ^2*k''[q] + μ*k'[q] - λ*k[q] == 0,
k'[0] == -mc, k'[b] == me}, k[q], q][[1, 1]]]

(* (E^(-((q (μ + Sqrt[μ^2 + 2 λ σ^2]))/σ^2)) (E^((2 q Sqrt[μ^2 + 2 λ σ^2])/σ^2)
mc λ σ^2 + E^((2 q Sqrt[μ^2 + 2 λ σ^2] + b (μ + Sqrt[μ^2 + 2 λ σ^2]))/σ^2)
me λ σ^2 + E^((2 b Sqrt[μ^2 + 2 λ σ^2])/σ^2) mc (μ^2 + λ σ^2 - μ Sqrt[μ^2 +
2 λ σ^2]) + E^((b (μ + Sqrt[μ^2 + 2 λ σ^2]))/σ^2) me (μ^2 + λ σ^2 -
μ Sqrt[μ^2 + 2 λ σ^2])))/((-1 + E^((2 b Sqrt[μ^2 +
2 λ σ^2])/σ^2)) λ (-μ + Sqrt[μ^2 + 2 λ σ^2])) *)


Indeed, Minimize[s, q] returns unevaluated. However, the solution can be obtained by hand, so to speak, by solving D[s, q] == 0 for q.

q /. Solve[D[s, q] == 0, q][[1, 1]]

(* (σ^2 Log[-((E^((b Sqrt[μ^2 + 2 λ σ^2])/(2 σ^2))
Sqrt[E^((b Sqrt[μ^2 + 2 λ σ^2])/σ^2) mc + E^((b μ)/σ^2) me])/
Sqrt[mc + E^((b μ)/σ^2 + (b Sqrt[μ^2 + 2 λ σ^2])/σ^2)
me])])/Sqrt[μ^2 + 2 λ σ^2] *)


Presumably, minimization with respect to other parameters can be computed in a similar, although algebraically tedious, manner.

• This is great for finding q* -- I'm getting stuck trying to get k(q*) though also. Is there an easy way to put q* back into the function to get k(q*)? – Karthik May 24 '16 at 1:28
• (Also, what does [[1,1]] do?) Thank you so much for your help! – Karthik May 24 '16 at 1:39
• @Karthik If you name the second result sq (not q*, which is not valid syntax), then s/.q->sq gives k[sq]. Look up Rule in the documentation. [[1,1]] eliminates the extra curly brackets in the first answer, which sometimes get in the way. Look up Part in the documentation. – bbgodfrey May 24 '16 at 2:51
• One could also choose to use DSolveValue[] to avoid Part[] gymnastics in this case. – J. M.'s ennui May 24 '16 at 4:22
• @Karthik γ does not appear in your ODE, so I do not understand why you are introducing it here. σ cannot be set to 0, unless one of the boundary conditions is eliminated. With σ != 0, it can be eliminated by rescaling other constants, which I recommend you do. Other analytical simplifications also may be possible. Investigating such possibilities before trying to minimize k over so many constants will make your life easier. – bbgodfrey May 25 '16 at 0:18