Message Boards Message Boards

4 Replies
3 Total Likes
View groups...
Share this post:

Substituting values into a DSolve solution of ODE

Posted 11 years ago
I'm trying to subsitute values to a rule, which I obtain as a solution to a ode.
sol = DSolve[V''[x] + V[x]/(L C) ==  Es/(L C), V[x], x]
Now the goal is to convert the answer into following format

I tried to use /. command as follow , but not success.
% /. Sqrt[L]  Sqrt[C]  -> w
Pls give me a certain direction on this. 
POSTED BY: Rashmil D
4 Replies
Posted 11 years ago
Thanks. Most importantly for tipping for mathematically deriving it rather than subsituting variables.

I used system of ode's with initial conditions. Hence the solution already cater for the initial states.(of capacitor and inductor)

Revised code 
 In[29]:= eqn = {iL'[t] + 1/L Vc[t] == Es/L, Vc'[t] == 1/c  iL[t],  Vc[0] == Vc0 , iL[0] == iL0}
 Out[29]= {Vc[t]/L + Derivative[1][iL][t] == Es/L, Derivative[1][Vc][t] == iL[t]/c, Vc[0] == Vc0, iL[0] == iL0}
 In[51]:= sol2 = Vc[t] /. DSolve[eqn, {Vc[t], iL[t]}, t][[1, 2]] //  FullSimplify  (* Capacitor Voltage *)
 Out[51]= Es + (-Es + Vc0) Cos[t/(Sqrt[c] Sqrt[L])] + (iL0 Sqrt[L] Sin[t/(Sqrt[c] Sqrt[L])])/Sqrt[c]
 In[55]:= sol2 =  sol2 /. Power[c_, Rational[-1, 2]]*Power[L_, Rational[-1, 2]]*t ->  w *t   (* 1/Sqrt[LC] \[Rule] w *)

Out[55]= Es + (-Es + Vc0) Cos[t w] + (iL0 Sqrt[L] Sin[t w])/Sqrt[c]

In[63]:= sol1 = iL[t] /. DSolve[eqn, {Vc[t], iL[t]}, t][[1, 1]] // FullSimplify  (*  inductor current *)

Out[63]= iL0 Cos[t/(Sqrt[c] Sqrt[L])] + ( Sqrt[c] (Es - Vc0) Sin[t/(Sqrt[c] Sqrt[L])])/Sqrt[L]

In[64]:= sol1 =  sol1 /. Power[c_, Rational[-1, 2]]*Power[L_, Rational[-1, 2]]*t ->   w *t   (* 1/Sqrt[LC] \[Rule] w *)

Out[64]= iL0 Cos[t w] + (Sqrt[c] (Es - Vc0) Sin[t w])/Sqrt[L]

In[67]:= sol2a =  sol2 /. a_*Sin[q_] + b_*Cos[q_] ->    Sqrt[a^2 + b^2]*Sin[q + ArcTan[b/a]] (*  Trignomertry Linear Combination *)

Out[67]= Es +  Sqrt[(iL0^2 L)/c + (-Es + Vc0)^2] Sin[t w + ArcTan[(Sqrt[c] (-Es + Vc0))/(iL0 Sqrt[L])]]

Agree. Its much simpler to fix minor adjustment for display using the word processor
POSTED BY: Rashmil D
Posted 11 years ago
I am glad you were able to see how to use the example to accomplish your goal. That is what I hoped you would be able to do.

You can always try to force Mathematica to do things and display things the way you want and not the way Mathematica wants. But you should probably be warned that this may be more and more difficult and time consuming the harder you try to fight against what Mathematica wants to do. Again and again you can find examples where people want the output to be "desktop published" or to look like what is in their textbook. This often leads to difficulties and errors and people begging to be told why what they have tried to do is not working. But some people just must do that.

Try this.
In[1]:= Sin[t w] /. t w -> HoldForm[w t]

Out[1]= Sin[w t]

In[2]:= FullForm[%]

Out[2]//FullForm= Sin[HoldForm[w t]]]]
 This may display they way you want, but you will then find you cannot do anything else with it until you undo what you have just asked for.
You may find it easier to read the result, in the wrong order, and then go type it into your desktop publishing system in the order that you desire and publish that.

If you need to do too much pattern matching for replacement then this might be a hint to use your desktop publishing system or word processor instead. Pattern matching and replacement have no understanding of mathematics at all and will just as easily let you make an incorrect replacement as a correct replacement. If you give DSolve the correct initial conditions for your differential equation then it does have some understanding of mathematics and should not do incorrect substitutions or give incorrect results. Then at the last step you can desktop publish the result into the style that you wish.
POSTED BY: Bill Simpson
Posted 11 years ago
Thanks for the informative response.

I further tweaked the code to obtain the desired answer
 In[13]:= sol =  V[t] /. DSolve[{V''[t] + V[t]/(L c) == Es/(L c)}, V[t], t][[1]]
 Out[13]= Es + C[1] Cos[t/(Sqrt[c] Sqrt[L])] +  C[2] Sin[t/(Sqrt[c] Sqrt[L])]
 In[14]:=  sol = sol /.  Power[c_, Rational[-1, 2]]*Power[L_, Rational[-1, 2]]*t -> w *t   (* 1/Sqrt[LC] \[Rule] w *)
 Out[14]= Es + C[1] Cos[t w] + C[2] Sin[t w]
 In[15]:= sol2 = sol /. a_*Sin[q_] + b_*Cos[q_] ->  Sqrt[a^2 + b^2]*Sin[q + ArcTan[b/a]] (*  Trignomertry Linear Combination *)

Out[15]= Es + Sqrt[C[1]^2 + C[2]^2] Sin[t w + ArcTan[C[1]/C[2]]]

In[21]:= sol3 = sol2 /. {Sqrt[C[1]^2 + C[2]^2] -> Xs, (C[1]/C[2]) -> \[Phi]} (* where C[1]=(Vcio -Es) and C[2]=(Ilio/(wc)) *)

Out[16]= Es + Xs Sin[t w + ArcTan[\[Phi]]] (* is it possible to list Sin (w t) instead of Sin (t w) *)

One minor question is it possible to change the order of variable display format
ie i'd like to have the output as Sin(w t) instead of Sin (t w)  . Its seems like mathematica automatically rearrange them alphabetically.
POSTED BY: Rashmil D
Posted 11 years ago
Substitutions, particularly for powers, radicals and denominators, have always been "somewhat challenging."

See if this will help.
 will not substitute
 while this
 x/(Sqrt[c]Sqrt[L])/. 1/Sqrt[L]*1/Sqrt[c]->1/w
 will correctly substitute.
 Or try this
 In[1]:= sol = V[x] /. DSolve[V''[x] + V[x]/(L c) == Es/(L c), V[x], x][[1]]

Out[1]= Es + C[1] Cos[x/(Sqrt[c] Sqrt[L])] + C[2] Sin[x/(Sqrt[c] Sqrt[L])]

In[2]:= sol = sol/.x*Power[c_,Rational[-1,2]]*Power[L_,Rational[-1,2]]->x/w

Out[2]= Es + C[1] Cos[x/w] + C[2] Sin[x/w]

Now use _trigonometric _identities#Linear_combinations

In[3]:= sol = sol /. a_*Sin[q_] + b_*Cos[q_] -> Sqrt[a^2 + b^2] Sin[q + ArcSin[b/Sqrt[a^2 + b^2]]]

Out[3]= Es + Sqrt[C[1]^2 + C[2]^2] Sin[x/w + ArcSin[C[1]/Sqrt[C[1]^2 + C[2]^2]]]

Note that I have used c instead of C because C has a defined meaning to Mathematica and that isn't the size of a capacitor.

When you can't get a replacement pattern to work the advice is always to use FullForm to see what the internal Mathematica form is for the expression and then use that literally in your replacement pattern.  Sometimes you can use something less detailed than that and still have it work, but that is always a good first step to try. Also be very careful to include a space between /. and 1 if you do anything like expr/. 1/w so that it does not interpret .1 as decimal one-tenth.

Also watch out for the domain information given on that wiki page for the trig substitution to make sure that I haven't chosen the wrong replacement. If there were a single Mathematica command that would do that subsitution and automatically take into account the signs that would be great, but I am not aware of something that will do that.

If you can give some initial conditions to DSolve then it may automatically replace C[1] and C[2] with the appropriate values.

Note to staff. It would be really really really nice if <ctrl>v would correctly paste inside a code box someday. <ctrl>v woks fine for an initial paste into a code box, but any subsequent attempt to highlight part of that box and paste a replacement doesn't paste. Being able to make simple edits like that would be very helpful and far less error prone than just "typing it all back in again." Thank you.
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract