About NDsolve which may be asked several times, but I still failed to solve it - wolfram-mathematica

the error is At t == 0.030451749711041764`, step size is effectively zero;singularity or stiff system suspected
my calculation time is around 6000 seconds, however.
And the warning is like
Warning: scaled local spatial error estimate of 6884.220329195682 at t = 0.030451749711041764 in the direction of independent variable x is much greater than the prescribed error tolerance. Grid spacing with 25 points may be too large to achieve the desired accuracy or precision. A singularity may have formed or a smaller grid spacing can be specified using the MaxStepSize or MinPoints method option.
I tried to using
Method -> {"PDEDiscretization" -> {"MethodOfLines", "SpatialDiscretization" -> "TensorProductGrid", "MinPoints" -> 100}}}
and increasing the "minpoints", but the promblem was still existed.
CAN anyone help me with that?
my code be like solving an deviation equation
bound1 = {FEND[t] == 2*krec [t]*GEND[t]^2, F[t] == -Fb[t], FbEND[t] == 0, G[t] == Gb[t]/1};
bc1 = {y[0, x] == 0., yb[0, x] == 0., ytA1[0, x] == 0., ytA2[0, x] == 0.};
equ1 = {D[y[t, x], t] == Cdif[t] D[y[t, x], x, x] + SRC[t, x] - D[ytA1[t, x], t],
D[yb[t, x], t] == (Cdif[t]/fac^2) D[yb[t, x], x, x]-D[ytA2[t, x], t],
D[ytA1[t, x], t] == ktAT0[t] y[t, x] TRPA1[t, x] - ktATd0[t] ytA1[t, x],
D[ytA2[t, x], t] == ktAT0[t] yb[t, x] TRPA2[t, x] - ktATd0[t] ytA2[t, x]};
Sol1 = NDSolve[{equ1, bound1, bc1}, {y, yb, ytA1, ytA2}, {t, 0, tmax}, {x, 0, xmax}, MaxSteps -> Infinity, StartingStepSize -> 0.001, MaxStepSize -> 1, Method -> {"PDEDiscretization" -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid",
"MinPoints" -> 100}}}];

Related

2D Heat Equation Mathematica not solving analitically (DSolve) or numerically (NDSolve), what am I doing wrong?

my goal is to solve the following 2d heat conduction equation, along with initial and boundary conditions:
pde = D[u[x, y, t], t] == (c^2)*(D[u[x, y, t], {x, 2}] + D[u[x, y, t], {y, 2}]);
ic = {u[x,y,0] == 0};
bc = {Derivative[1, 0, 0][u][0, y, t] == k, u[x, 0, t] == 0, u[x, b, t] == 0,
u[a, y, t] == 0};
...but something always goes wrong when i input
sol = DSolve[{pde, bc, ic}, u[x, y, t], {x, y, t}]
I tried changing the conditions a bit to see if that is a problem on mathematica, and sometimes it works (example of this guy). I suspect there is something wrong with the derivative, but I tried using D[u[x,y,t],x]./x->0 == k but still it doesnt work. Is it even possible to solve this in Mathematica?

Mathematica about NDsolve PDE set

I am trying to use NDsolve function to solve a PDE set.
I am pretty new to mathematica and here is the code I put in.
NDSolve[{D[Cm[t, x], t] == Dm*D[Cm[t, x], x, x] + Kg*Cs[t, x] - Ka*Cm[t, x],
D[Cs[t, x], t] == Ds*D[Cs[t, x], x, x] + Ka*Cm[t, x] - Kg*Cs[t, x],
Cm[0, x] == Cm0,
Cs[0, x] == Cs0,
Dm*ND[Cm[t, 0]] == 0.5*FT,
Ds*ND[Cs[t, 0]] == 0.5*FT,
Cm[t, Infinity] == Cm0,
Cs[t, Infinity] == Cs0}
{Cm[t, x], Cs[t, x]}, {t, 0, 1000}, {x, 0, Infinity}];
plot3D[Cs, {t, 0, 1000}, {x, 0, 10000}]
Dm = 9 e - 8;
Ds = 5 e - 9;
Cm0 = 1.276 e + 15;
Cs0 = 1.276 e + 20;
Ka = 1;
Kg = 1 e - 5;
FT = 1 e + 11;
So, basically, we have two PDEs, 2 initial conditions and 4 boundary conditions(two constant B.C. two flux B.C.). We know all the values of parameters. I am not sure if its a formatting problem or boundary choosing problem. The system gives
"Thread::tdlen: Objects of unequal length in "
"NDSolve::argmu: NDSolve called with 1 argument; 3 or more arguments are expected."
Could somebody give some valuable suggestions?
Thanks
Update
Dm = 9*10^-8;
Ds = 5*10^-9;
Cm0 = 1.276*10^+15;
Cs0 = 1.276*10^+20;
Ka = 1;
Kg = 1*10^-5;
FT = 1*10^+11;
NDSolve[{D[Cm[t, x], t] ==
Dm*D[Cm[t, x], x, x] + Kg*Cs[t, x] - Ka*Cm[t, x],
D[Cs[t, x], t] == Ds*D[Cs[t, x], x, x] + Ka*Cm[t, x] - Kg*Cs[t, x],
Cm[0, x] == Cm0,
Cs[0, x] == Cs0,
Dm*(D[Cm[t, x], x] /. x -> 0) == 0.7*FT,
Ds*(D[Cs[t, x], x] /. x -> 0) == 0.3*FT,
Cs[t, 10000] == Cs0,
Cm[t, 10000] == Cm0},
{Cm[t, x], Cs[t, x]}, {t, 0, 1000}, {x, 0, 10001},
PrecisionGoal -> 2];
Animate[Plot[Cs[t, x], {x, 0, 10000},
PlotRange -> {{0, 1000}, {0, 5*10^20}}], {t, 0, 1000}]
The "unequal" error was because you are missing a comma between } and { on your 8th and 9th line.
But that isn't your only problem. This fixes some other, but not all problems.
Dm = 9*10^-8;
Ds = 5 *10^-9;
Cm0 = 1.276*10^+15;
Cs0 = 1.276*10^+20;
Ka = 1;
Kg = 1*10^-5;
FT = 1*10^+11;
NDSolve[{D[Cm[t, x], t] == Dm*D[Cm[t, x], x, x] + Kg*Cs[t, x] - Ka*Cm[t, x],
D[Cs[t, x], t] == Ds*D[Cs[t, x], x, x] + Ka*Cm[t, x] - Kg*Cs[t, x],
Cm[0, x] == Cm0, Cs[0, x] == Cs0, Dm*ND[Cm[t, 0]] == 0.5*FT,
Ds*ND[Cs[t, 0]] == 0.5*FT, Cm[t, Infinity] == Cm0,
Cs[t, Infinity] == Cs0}, {Cm[t, x], Cs[t, x]}, {t, 0, 1000}, {x, 0, Infinity}];
plot3D[Cs, {t, 0, 1000}, {x, 0, 10000}]
Everything (except for the functions you are solving for and the independent variables) inside an NDSolve must be initialized to numeric values before starting the NDSolve, so I moved your assignments up. Mathematica has its' own way of writing exponents.
Now for bigger issues.
You have an ND function that you haven't defined. That is going to have to be defined before the NDSolve starts.
It is possible, maybe even likely that NDSolve is going to be less than cooperative with limits of Infinity for your x variable. It may work, but I wouldn't bet on that. You might try a smaller finite value, maybe 10^4 because that is bigger than your 10^3, and see if that will work if Infinity doesn't.
I don't spot any other big problems at the moment, but without knowing what your ND function is I can't begin to test this and perhaps flush out the next layer or two or three of problems to look for.
But this is actually pretty good if this is your first try at Mathematica.

Mathematica: FindRoot for common tangent

I asked this question a little while back that did help in reaching a solution. I've arrived at a somewhat acceptable approach but still not fully where I want it. Suppose there are two functions f1[x] and g1[y] that I want to determine the value of x and y for the common tangent(s). I can at least determine x and y for one of the tangents for example with the following:
f1[x_]:=(5513.12-39931.8x+23307.5x^2+(-32426.6+75662.x-43235.4x^2)Log[(1.-1.33333x)/(1.-1.x)]+x(-10808.9+10808.9x)Log[x/(1.-1.x)])/(-1.+x)
g1[y_]:=(3632.71+3806.87y-51143.6y^2+y(-10808.9+10808.9y)Log[y/(1.-1.y)]+(-10808.9+32426.6y-21617.7y^2)Log[1.-(1.y)/(1.-1.y)])/(-1.+y)
Show[
Plot[f1[x],{x,0,.75},PlotRange->All],
Plot[g1[y],{y,0,.75},PlotRange->All]
]
Chop[FindRoot[
{
(f1[x]-g1[y])/(x-y)==D[f1[x],x]==D[g1[y],y]
},
{x,0.0000001},{y,.00000001}
]
[[All,2]]
]
However, you'll notice from the plot that there exists another common tangent at slightly larger values of x and y (say x ~ 4 and y ~ 5). Now, interestingly if I slightly change the above expressions for f1[x] and g1[y] to something like the following:
f2[x_]:=(7968.08-59377.8x+40298.7x^2+(-39909.6+93122.4x-53212.8x^2)Log[(1.-1.33333x)/(1.-1.x)]+x(-13303.2+13303.2x)Log[x/(1.-1.x)])/(-1.+x)
g2[y_]:=(5805.16-27866.2y-21643.y^2+y(-13303.2+13303.2y)Log[y/(1.-1.y)]+(-13303.2+39909.6y-26606.4y^2)Log[1.-(1.y)/(1.-1.y)])/(-1.+y)
Show[
Plot[f2[x],{x,0,.75},PlotRange->All],
Plot[g2[y],{y,0,.75},PlotRange->All]
]
Chop[FindRoot[
{
(f2[x]-g2[y])/(x-y)==D[f2[x],x]==D[g2[y],y]
},
{x,0.0000001},{y,.00000001}
]
[[All,2]]
]
And use the same method to determine the common tangent, Mathematica chooses to find the larger values of x and y for the positive sloping tangent.
Finally, my question: is it possible to have Mathematica find both the high and low x and y values for the common tangent and store these values in a similar way that allows me to make a list plot? The functions f and g above are all complex functions of another variable, z, and I am currently using something like the following to plot the tangent points (should be two x and two y) as a function of z.
ex[z_]:=Chop[FindRoot[
{
(f[x,z]-g[y,z])/(x-y)==D[f[x],x]==D[g[y],y]
},
{x,0.0000001},{y,.00000001}
]
[[All,2]]
]
ListLinePlot[
Table[{ex[z][[i]],z},{i,1,2},{z,1300,1800,10}]
]
To find estimates for {x, y} that would solve your equations, you could plot them in ContourPlot and look for intersection points. For example
f1[x_]:=(5513.12-39931.8 x+23307.5 x^2+(-32426.6+75662. x-
43235.4 x^2)Log[(1.-1.33333 x)/(1.-1.x)]+
x(-10808.9+10808.9 x) Log[x/(1.-1.x)])/(-1.+x)
g1[y_]:=(3632.71+3806.87 y-51143.6 y^2+y (-10808.9+10808.9y) Log[y/(1.-1.y)]+
(-10808.9+32426.6 y-21617.7 y^2) Log[1.-(1.y)/(1.-1.y)])/(-1.+y)
plot = ContourPlot[{f1'[x] == g1'[y], f1[x] + f1'[x] (y - x) == g1[y]},
{x, 0, 1}, {y, 0, 1}, PlotPoints -> 40]
As you can see there are 2 intersection points in the interval (0,1). You could then read off the points from the graph and use these as starting values for FindRoot:
seeds = {{.6,.4}, {.05, .1}};
sol = FindRoot[{f1'[x] == g1'[y], f1[x] + f1'[x] (y - x) == g1[y]},
{x, #1}, {y, #2}] & ### seeds
To get the pairs of points from sol you can use ReplaceAll:
points = {{x, f1[x]}, {y, g1[y]}} /. sol
(*
==> {{{0.572412, 19969.9}, {0.432651, 4206.74}},
{{0.00840489, -5747.15}, {0.105801, -7386.68}}}
*)
To show that these are the correct points:
Show[Plot[{f1[x], g1[x]}, {x, 0, 1}],
{ParametricPlot[#1 t + (1 - t) #2, {t, -5, 5}, PlotStyle -> {Gray, Dashed}],
Graphics[{PointSize[Medium], Point[{##}]}]} & ### points]
OK, so let's quickly rewrite what you've done so far:
Using your f1 and g1, we have the plot
plot = Plot[{f1[x], g1[x]}, {x, 0, .75}]
and the first shared tangent at
sol1 = Chop[FindRoot[{(f1[x] - g1[y])/(x - y) == D[f1[x], x] == D[g1[y], y]},
{x, 0.0000001}, {y, .00000001}]]
(* {x -> 0.00840489, y -> 0.105801} *)
Define the function
l1[t_] = (1 - t) {x, f1[x]} + t {y, g1[y]} /. sol1
then, you can plot the tangents using
Show[plot, Graphics[Point[{l1[0], l1[1]}]],
ParametricPlot[l1[t], {t, -1, 2}],
PlotRange -> {{-.2, .4}, {-10000, 10000}}]
I briefly note (for my own sake) that the equations you used
(e.g., to generate sol1 above)
come from requiring that the tangent line for f1 at x
tangentially hits g1 at some point y, i.e.,
LogicalExpand[{x, f[x]} + t {1, f'[x]} == {y, g[y]} && f'[x] == g'[y]]
To investigate where the shared tangents lie, you can use a Manipulate:
Manipulate[Show[plot, ParametricPlot[{x, f1[x]} + t {1, f1'[x]}, {t, -1, 1}]],
{x, 0, .75, Appearance -> "Labeled"}]
which produces something like
Using the eyeballed values for x and y, you can get the actual solutions using
sol = Chop[Table[
FindRoot[{(f1[x] - g1[y])/(x - y) == D[f1[x], x] == D[g1[y], y]},
{x, xy[[1]]}, {y, xy[[2]]}], {xy, {{0.001, 0.01}, {0.577, 0.4}}}]]
define the two tangent lines using
l[t_] = (1 - t) {x, f1[x]} + t {y, g1[y]} /. sol
then
Show[plot, Graphics[Point[Flatten[{l[0], l[1]}, 1]]],
ParametricPlot[l[t], {t, -1, 2}, PlotStyle -> Dotted]]
This process could be automated, but I'm not sure how to do it efficiently.

Why does this pde give Boundary and initial conditions are inconsistent error? (1D heat pde)

I have been staring at this simple pde for last 20 minutes and can't find why I am getting this error
Boundary and initial conditions are inconsistent
it is that standard 1D heat equation
eq = Derivative[2, 0][u][x, t] == Derivative[0, 1][u][x, t]
and boundary conditions are (notice they are both spatial derivatives)
u'(0,t)=0 (derivative here w.r.t. x )
u'(Pi,t)=0 (derivative here w.r.t. x )
and initial conditions are
u(x,0) = cos(2 x)
So at initial condition, u'(x,0) = -2 sin(2 x), which is zero at both x=0 and x=Pi.
So it seems to me to be consistent with boundary conditions, right? or Am I missing something?
Here is the actual Mathematica code:
ClearAll[u, x, t]
eq = Derivative[2, 0][u][x, t] == Derivative[0, 1][u][x, t]
sol = NDSolve[{eq,
Derivative[1, 0][u][0, t] == 0,
Derivative[1, 0][u][Pi, t] == 0,
u[x, 0] == Cos[2 x]},
u, {t, 0, 12}, {x, 0, Pi}
]
I have a feeling since it is a numerical solver, using Pi in the above, become Real Pi=3.1415... and so at exactly this value the initial and boundary conditions do not match? (floating point comparison somewhere?)
I know about the trick to resolve such an error from help ref/message/NDSolve/ibcinc but my question is really why I am getting this error in the first place, since it seems on the face of it, they are consistent. If it is due to the floating point issue with Pi, then how to resolve this? I tried to use the trick shown in help on this one (i.e. the use of exp(-1000 t) but did not help in removing this error.
question: Why I am getting this error message ?
version 8.04, on windows.
Actually I have been trying to get this solution shown here (also using Mathematica)
http://en.wikipedia.org/wiki/File:Heatequation_exampleB.gif
but the BC and IC shown in the above example gave me this error also, so I made the change in the BC in the hope they become consistent now.
thanks.
edit(1)
Here are the commands I used to plot the plot the solution, and it looks ok
eq = Derivative[2, 0][u][x, t] == Derivative[0, 1][u][x, t]
sol = u /.
First#NDSolve[{eq, Derivative[1, 0][u][0, t] == 0,
Derivative[1, 0][u][Pi, t] == 0, u[x, 0] == Cos[2 x]},
u, {t, 0, 1.5}, {x, 0, Pi}]
Animate[Plot[sol[x, t], {x, 0, Pi},
PlotRange -> {{0, Pi}, {-1, 1}}], {t, 0, 1.5}]
edit(3)
I am still little confused (also did not have coffee yet, which does not help).
I changed the IC so that it is not derivative any more so that the IC (non-derivative) now agrees with the BC's (but those are kept as derivatives). But I still get the same error:
eq = Derivative[2, 0][u][x, t] == Derivative[0, 1][u][x, t]
sol = u /.
First#NDSolve[{eq,
Derivative[1, 0][u][0, t] == 0,
Derivative[1, 0][u][Pi, t] == 0,
u[x, 0] == Sin[2 x]},
u, {t, 0, 1.5}, {x, 0, Pi}
]
NDSolve::ibcinc: Warning: Boundary and initial conditions are inconsistent. >>
Also the solution appears ok when plotted
Animate[Plot[sol[x, t], {x, 0, Pi},
PlotRange -> {{0, Pi}, {-1, 1}}], {t, 0, 1.5}]
What IC is needed for this problem to eliminate this erorr, or is it the case then that only essential BC must be used? and also non-derivative IC's be used, and only after that, I worry on the consistency part?
Szabolcs gave the right hint (See inconsistent boundary condition section in http://reference.wolfram.com/mathematica/tutorial/NDSolvePDE.html)
eq = Derivative[2, 0][u][x, t] == Derivative[0, 1][u][x, t]
sol = u /.
First#NDSolve[{eq, Derivative[1, 0][u][0, t] == 0,
Derivative[1, 0][u][Pi, t] == 0, u[x, 0] == Cos[2 x]},
u, {t, 0, 1.5}, {x, 0, Pi},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MinPoints" -> 100}}]
I have filed a suggestion, that the link to the tutorial be added to the NDSolve::ibcinc message page.

How to ask mathematica to compute higher order derivatives evaluated at 0

I have a function, let's say for example,
D[x^2*Exp[x^2], {x, 6}] /. x -> 0
And I want to replace 6 by a general integer n,
Or cases like the following:
Limit[Limit[D[D[x /((-1 + x) (1 - y) (-1 + x + x y)), {x, 3}], {y, 5}], {x -> 0}], {y -> 0}]
And I want to replace 3 and 5 by a general integer m and n respectively.
How to solve these two kinds of problems in general in mma?
Many thanks.
Can use SeriesCoefficient, sometimes.
InputForm[n! * SeriesCoefficient[x^2*Exp[x^2], {x,0,n}]]
Out[21]//InputForm=
n!*Piecewise[{{Gamma[n/2]^(-1), Mod[n, 2] == 0 && n >= 2}}, 0]
InputForm[mncoeff = m!*n! *
SeriesCoefficient[x/((-1+x)*(1-y)*(-1+x+x*y)), {x,0,m}, {y,0,n}]]
Out[22]//InputForm=
m!*n!*Piecewise[{{-1 + Binomial[m, 1 + n]*Hypergeometric2F1[1, -1 - n, m - n,
-1], m >= 1 && n > -1}}, 0]
Good luck extracting limits for m, n integer, in this second case.
Daniel Lichtblau
Wolfram Research
No sure if this is what you want, but you may try:
D[x^2*Exp[x^2], {x, n}] /. n -> 4 /. x -> 0
Another way:
f[x0_, n_] := n! SeriesCoefficient[x^2*Exp[x^2], {x, x0, n}]
f[0,4]
24
And of course, in the same line, for your other question:
f[m_, n_] :=
Limit[Limit[
D[D[x/((-1 + x) (1 - y) (-1 + x + x y)), {x, m}], {y, n}], {x ->
0}], {y -> 0}]
These answers don't give you an explicit form for the derivatives, though.

Resources