perturbations in mathematica - wolfram-mathematica

Write
y''[x] + ( [Epsilon] * Exp[x/3] * y[x] )==0.
For [Epsilon]=0
Solve for y[x] with initial conditions
y[0]=y'[0]=1

eq = y''[x] + Epsilon*Exp[x/3]*y[x] == 0
soln = DSolve[eq, y[x], x]
gives an answer, and then you can even do
Series[y[x] /. soln[[1]], {Epsilon, 0, 2}]
which is ugly.
To add initial conditions, you just add equations:
soln2 = DSolve[{eq, y[0]==1, y'[0]==1}, y[x], x]//Simplify
Series[y[x] /. soln2[[1]], {Epsilon, 0, 2}]//Simplify
(where I've added //Simplify to force Mathematica to put it into nice form.

Related

Solving a differential equation in Mathematica

I have a syntax problem solving a differential equation in Mathematica (10th version).
The input for the equation I need to solve is as follows:
solv = DSolve[{ a*u''[y] - b*u[y] == d, u'[0] == 0, u[1] == 0}, u, {y, -1, 1}]
Which after using ExpToTrig and FullSimplify I get the answer I am looking for:
(d (-1 + Cosh[(Sqrt[b] y)/Sqrt[a]] Sech[Sqrt[b]/Sqrt[a]]))/b
However, my problem comes when I want to place more coefficients in the equation. For example:
solv = DSolve[{ a* u''[y] - b* c* u[y] == d, u'[0] == 0, u[1] == 0}, u, {y, -1, 1}]
This time, I get for:
FullSimplify[ExpToTrig[Evaluate[u[y] /. solv]]]
The next answer:
(d (1 + E^((2 Sqrt[b] Sqrt[c])/Sqrt[a]) - E^(-((Sqrt[b] Sqrt[c] (-1 + y))/Sqrt[a])) - E^((Sqrt[b] Sqrt[c] (1 + y))/Sqrt[a])) (-1 + Tanh[(Sqrt[b] Sqrt[c])/Sqrt[a]]))/(2 b c)
Instead, when I merge b and c (substitute: bc=b*c):
solv = DSolve[{ a*u''[y] - bc*u[y] == d, u'[0] == 0, u[1] == 0}, u, {y, -1, 1}]
I get:
(d (-1 + Cosh[(Sqrt[bc] y)/Sqrt[a]] Sech[Sqrt[bc]/Sqrt[a]]))/bc
In my case I can't just substitute because there are too many equations and some of the parameters (coefficients) cancel.
Thanks!
Your issue is with FullSimplify. It deems the exp form more "simple" than the trig form so it is undoing what ExpToTrig is doing. Using just Simplify in its place will maintain the ExpToTrig conversion. My quick try below shows a comparison.

NDSolve inside NDSolve in Mathematica

I need to use NDSolve which in turn uses the solution from another ODE as function in terms of output from another NDSolve.
If I use the exact solution from the first differential equation inside the NDSolve, it's OK. But when I use the same solution in the form of function (which uses InterpolatingFunction) it does not work.
I believe, it's got to do with the structure of NDSolve output. Could anyone please enlighten me on this. Will be of great help!
The code is:
feq = 2 V alpha fip F''[fi] - (V^2 - (V^2 + sigma - 2 fi) (F'[fi])^2 + (F'[fi])^4
Frange[lo_, hi_] :=
Module[{fii, sol},
sol = NDSolve[{(feq == 0 /.fi -> fii), F[0] == 0}, F, {fii, lo, hi}]]
eqpois = fi''[x] == ne[x] - F[fi[x]]/.sol
NDSolve[{eqpois, fi'[0] == 0, fi[0] == 0}, fi, {x,0,1}]
Here in order to find F[phi], I need to solve the 1st diff eq that is feq, which is solved by NDSolve inside the function Frange[lo,hi]. The solution is then used inside the second equation eqpois, which has to be solved using NDSolve again. The problem comes up in the second NDSolve, which does not produce the result. If I use the analytical solution of F[phi] in eqopis, then there is no problem.
Example Problem
I have done a little experiment with this. Let's take an example of coupled ODEs
1st eqn : dg/dx = 2f(g) with initial condition g(0) = 1
The function f(y) is a solution from another ODE, say,
2nd eqn : df/dy = 2y with IC f(0) = 0
The solution of the 2nd ODE is f(y) = y^2 which when put into the the 1st ODE becomes
dg/dx = 2 g^2 and the final solution is g(x) = 1/(1-2x)
The issue:
When I use DSolve, it finds the answer correctly
In[39]:= s = DSolve[{f'[y] == 2 y, f[0] == 0}, f, y]
Out[39]= {{f -> Function[{y}, y^2]}}
In[40]:= ss = DSolve[{g'[x] == 2 (f[g[x]]/.First#s), g[0] == 1}, g, x]
Out[40]= {{g -> Function[{y}, 1/(1 - 2 x)]}}
The problem comes when I use NDSolve
In[41]:= s = NDSolve[{f'[y] == 2 y, f[0] == 0}, f, {y, 1, 5}]
Out[41]= {{f -> InterpolatingFunction[{{1., 5.}}, <>]}}
In[42]:= ss1 = NDSolve[{g'[x] == 2 (Evaluate[f[g[x]]/.First#s1]), g[0] == 1}, g, {x, 1, 2}]
Out[42]= {}
The erros are:
During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.01726} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.01726} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
During evaluation of In[41]:= InterpolatingFunction::dmval: Input value {2.04914} lies outside the range of data in the interpolating function. Extrapolation will be used. >>
During evaluation of In[41]:= General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation. >>
During evaluation of In[41]:= NDSolve::ndsz: At y == 0.16666654771477857, step size is effectively zero; singularity or stiff system suspected. >>
During evaluation of In[41]:= NDSolve::ndsz: At y == 0.16666654771477857, step size is effectively zero; singularity or stiff system suspected. >>
Any help in this regard will be highly appreciated!
--- Madhurjya
I got your simple example to work with a little mod ..
f0 = First#First#DSolve[{f'[y] == 2 y, f[0] == 0}, f, y]
g0 = g /.
First#First#DSolve[{g'[x] == 2 (f[g[x]] /. f0), g[0] == 1}, g, x]
fn = f /. First#First#NDSolve[{f'[y] == 2 y, f[0] == 0}, f, {y, 0, 10}]
gn = g /.
First#First#
NDSolve[{g'[x] == 2 (fn[g[x]]), g[0] == 1}, g, {x, 0, 9/20}]
GraphicsRow[{
Plot[{g0#x, gn#x}, {x, 0, 9/20},
PlotStyle -> {{Thick, Black}, {Thin, Red, Dashed}}],
Plot[{f#x /. f0, fn#x}, {x, 0, 2},
PlotStyle -> {{Thick, Black}, {Thin, Red, Dashed}}]}]
note we need to ensure the y range in the first NDSolve is sufficient to cover the expected range of g from the second. That is where all those interpolation range errors come from.

Syntax to figure out the y value for a particular x value in Mathematica

Here's the problem statement:
Two non-linear inter-dependent, initial value first order differential equations were solved using NDSolve to yield an analytical solution. The solution was used to calculate another parameter, as a function of the same x value.
Let's say we have the ODEs as:
X'[t]=a*S[t]*X[t]/(b+S[t]
S'[t]=-a*S[t]*X[t]/(c(b+S[t])) where a,b,c are also known constants
X[0]=constant
S[0]=constant
soln = NDSolve[{X'[t]=a*S[t]*X[t]/(b+S[t],S'[t]=-a*S[t]*X[t]/(c(b+S[t])),X[0]=constant,S[0]=constant},{X,S},{t,0,50}]
The solution is of the form
X-> InterpolatingFunction[{{0.0,50}},<>],S->InterpolationFunction[{{0.0,50}},<>}}
Now the new parameter is: Yvalue=(S[t]/.soln)+(X[t]/.soln)
I'm trying to figure out the correct syntax to calculate Yvalue for an entered t value.
Ex- One needs to calculate Yvalue at t=0.1,0.56, 2.3 etc
Thank you for your time.
Regards,
Ankur
NDSolve demands that all parameters be given specific numeric values. If you assign values to a,b,c,X[0],S[0] and carefully match up all your parens and carefully use == versus = correctly, then this can work
In[1]:= a = 2; b = 3; c = 5;
soln = NDSolve[{X'[t] == a*S[t]*X[t]/(b + S[t]),
S'[t] == -a*S[t]*X[t]/(c(b+S[t])), X[0]==7, S[0]==11}, {X,S}, {t,0,50}][[1]]
Out[2]= {X -> InterpolatingFunction[{{0.,50.}}, <>],
S -> InterpolatingFunction[{{0.,50.}}, <>]}
In[3]:= Yvalue = S[t] + X[t] /. soln /. t -> 0.1
Out[3]= 18.9506
In[4]:= Yvalue = S[t] + X[t] /. soln /. t -> 0.56
Out[4]= 25.6919
In[5]:= Yvalue = S[t] + X[t] /. soln /. t -> 2.3
Out[5]= 61.9823
and even
In[6]:= Plot[S[t] + X[t] /. soln, {t, 0, 50}, PlotRange -> {0, 70}]
Out[6]= ...PlotSnipped...

How to get mathematica to carry out a Sum when only part of it is defined?

I'm having a sum like this:
Sum[1 + x[i], {i, 1, n}]
Mathematica doesn't simplify it any more. What would I need to do so it translates it into:
n + Sum[x[i],{i,1,n}]
Maybe this?
Distribute[Sum[1 + x[i], {i, 1, n}]]
which returns:
n + Sum[x[i], {i, 1, n}]
AFAIK Sum simply won't give partial answers. But you can always split off the additive part manually, or semi-automatically. Taking your example,
In[1]:= sigma + (x[i] - X)^2 // Expand
Out[1]= sigma + X^2 - 2 X x[i] + x[i]^2
There's nothing we can do with the parts that contain x[i] without knowing anything about x[i], so we just split off the rest:
In[2]:= Plus ## Cases[%, e_ /; FreeQ[e, x[i]]]
Out[2]= sigma + X^2
In[3]:= Sum[%, {i, 1, n}]
Out[3]= n (sigma + X^2)
Unrelated: It is a good idea never to use symbols starting with capital letters to avoid conflicts with builtins. N has a meaning already, and you shouldn't use it as a variable.
A quick and dirty way would be to use Thread, so for example
Thread[Sum[Expand[sigma + (x[i] - X)^2], {i, 1, n}], Plus, 1]
A simpler way would be
Total[Sum[#, {i, 1, n}] & /# {sigma, x[i]}]
If your expression is longer, this should give you the answer without having to manually split the terms
expr = sigma + (x[i] + i)^2 + Cos[Sin[i - x[i]]];
Total[Sum[#, {i, 1, n}] & /# Level[expr, {1}]]
This can also be done in an easy to understand manner with rules:
sumofsumsrule = Sum[a_+b_,{i_,c_,d_}] :> Sum[a,{i,c,d}]+Sum[b,{i,c,d}];
expandsummandrule = Sum[a_,{i_,c_,d_}] :> Sum[Expand[a],{i,c,d}];
MyRules = {sumofsumsrule, expandsummandrule};
Now, if you are messing around, you can use this (here are some examples):
error = Sum[sigma+(x[i]-X)^2,{i,1,n}]
error /. sumofsumsrule
% /. expandsummandrule
error //. MyRules

Creating points while using Mathematica's Table Function

I'm trying to plot points that I've created in a table in mathematica but for some reason one component of my points seems to have double braces around it while the other only has one as below:
{{x},y},{{x1},y1}....{{xn},yn}
and list plot will not recognize these as points and will not plot them.
Here is my mathematica code:
Remove["Global`*"]
b = .1;
w = 1;
Period = 1;
tstep = 2 Pi/Period;
s = NDSolve[{x''[t] + b x'[t] - x[t] + x[t]^3 - .5 Cos[w t] == 0,
x'[0] == 0, x[0] == 0}, x[t], {t, 0, 1000}, MaxSteps -> Infinity];
x[t_] = x[t] /. s
data = Table[Evaluate[{x'[t], .5}], {t, 0, 1000, tstep}]
ListPlot[data]
I've also tried using the command
ListPlot[Flatten[Table[Evaluate[{x'[t], .5}], {t, 0, 1000, tstep}]]]
to no avail as well as
ListPlot[Table[Evaluate[{Flatten[x'[t]], .5}], {t, 0, 1000, tstep}]]]
How can I remove the {}?
You may try something along these lines:
Clear["Global`*"]
b = .1;
w = 1;
s = NDSolve[{x''[t] + b x'[t] - x[t] + x[t]^3 - .5 Cos[w t] == 0,
x'[0] == 0, x[0] == 0}, x[t], {t, 0, 1000}, MaxSteps -> Infinity];
xr[u_] := ((x[t] /. s[[1]]) /. t -> u)
Plot[(xr'[u]), {u, 0, 30}]
But I am not sure what are you trying to get from the {x'[t], .5} part
My colleagues are correct, but I think there is more that can be said. First, to your actual question. The output of NDSolve is a list of the form
{{x[t]->InterpolatingFunction[...]}, {x[t]->InterpolatingFunction[...]}, ...}
where the second and subsequent replacement rules are only there if more than one solution is present. I have never encountered a case using NDSolve where that is true, but it makes the answer consistent with Solve, where multiple solutions is not uncommon. Therefor, with only one solution, you have a double list, i.e.
{{x[t]->InterpolatingFunction[...]}}
As per Mr. Wizard, you can use First, or you can use Part, i.e.
NDSolve[ ... ][[ 1 ]]
which is my preferred method, although it is slightly more difficult to read and may obscure your intent. You should be aware that the InterpolatingFunction that NDSolve returns is a function, and it will accept variables directly. So, the variables on the left hand side of the declarations
x[t_] = x[t] /. s
and from Belisarius
xr[u_] := ((x[t] /. s[[1]]) /. t -> u)
are superfluous at best, and the second one requires the replacement to occur every time xr is used. Instead, you can declare
x = x[t] /. s
and then writing x[t] afterwards will return IntepolatingFunction[t], exactly like you want. Then, as Belisarius points out, you can use it, or its derivative, in Plot directly, instead of first building a table of values and feeding them into ListPlot.
Edit: when I first posted this, I didn't notice a quirk with NDSolve. If you explicitly solve for x[t] not x, then NDSolve returns InterpolatingFunction[...][t], but if you just solve for x you get what I posted. This quirk allows both the OP's and Belisarius's solutions to function, otherwise the replacement shouldn't occur.
It is most likely that x'[t] is returning something of the form {x_i}. Try replacing the data=Table... line with this
data = Table[Evaluate[{First[x'[t]], .5}], {t, 0, 1000, tstep}]
An alternative would be to do
data=data /. {{x_}, y_} :> {x, y};
which uses ReplaceAll (/.) to replace every occurrence of {{x_i},y_i} with {x_i,y_i}
Example:
There are arguably better ways to accomplish what you are doing, but that is not what you asked.
To remove the extra {} recognize this comes from the result of NDSolve, and therefore use:
s = First # NDSolve[{x''[t] + b x'[t] - x[t] + x[t]^3 - .5 Cos[w t] == 0,
x'[0] == 0, x[0] == 0}, x[t], {t, 0, 1000}, MaxSteps -> Infinity];

Resources