Memory exhaustion while running NDSolve - windows

I run into the "No more memory available" error message in Mathematica. I understand that "Parallelize[]" isn't (obviously) going to help me. Neither has "ClearSystemCache[]".
What gives? Do I just need more RAM?
My Code
Needs["VectorAnalysis`"]
Needs["DifferentialEquations`InterpolatingFunctionAnatomy`"];
Clear[Eq4, EvapThickFilm, h, S, G, E1, K1, D1, VR, M, R]
Eq4[h_, {S_, G_, E1_, K1_, D1_, VR_, M_, R_}] := \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]h\) +
Div[-h^3 G Grad[h] +
h^3 S Grad[Laplacian[h]] + (VR E1^2 h^3)/(D1 (h + K1)^3)
Grad[h] + M (h/(1 + h))^2 Grad[h]] + E1/(
h + K1) + (R/6) D[D[(h^2/(1 + h)), x] h^3, x] == 0;
SetCoordinates[Cartesian[x, y, z]];
EvapThickFilm[S_, G_, E1_, K1_, D1_, VR_, M_, R_] :=
Eq4[h[x, y, t], {S, G, E1, K1, D1, VR, M, R}];
TraditionalForm[EvapThickFilm[S, G, E1, K1, D1, VR, M, R]];
L = 318; TMax = 10;
Off[NDSolve::mxsst];
Clear[Kvar];
Kvar[t_] := Piecewise[{{1, t <= 1}, {2, t > 1}}]
(*Ktemp = Array[0.001+0.001#^2&,13]*)
hSol = h /. NDSolve[{
(*S,G,E,K,D,VR,M*)
EvapThickFilm[1, 3, 0.1, 7, 0.01, 0.1, 0, 160],
h[0, y, t] == h[L, y, t],
h[x, 0, t] == h[x, L, t],
(*h[x,y,0] == 1.1+Cos[x] Sin[2y] *)
h[x, y, 0] ==
1 + (-0.25 Cos[2 \[Pi] x/L] - 0.25 Sin[2 \[Pi] x/L]) Cos[
2 \[Pi] y/L]
},
h,
{x, 0, L},
{y, 0, L},
{t, 0, TMax},
MaxStepSize -> 0.1
][[1]]
hGrid = InterpolatingFunctionGrid[hSol];
Error message
No more memory available.
Mathematica kernel has shut down.
Try quitting other applications and then retry.
My OS specs
Intel Core 2 Duo with 4.00 GB ram, 64 bit OS (Windows 7)

Here you may get a taste of what is happening:
Replace
MaxStepSize -> 0.1
by
MaxStepFraction -> 1/30
And run your code.
Then:
p = Join[#,Reverse##]&#
Table[Plot3D[hSol[x, y, i], {x, 0, L}, {y, 0, L},
PlotRange -> {All, All, {0, 4}}],
{i, 7, 8, .1}]
Export["c:\\plot.gif", p]
So, Mma is trying to refine the solution at those peaks, to no avail.

Related

Nonlinear PDE solving in Mathematica

I am trying to solve the following non linear coupled PDE equation related with ginzburg landau using NDsolve.I am new to Mathematica. I am getting the following error.What is the mistake I am doing?
pde = {D[u[t, x, y], t] ==
D[u[t, x, y], {x, x}] +
D[u[t, x, y], {y,
y}] - (1/u[t, x, y])^3*(D[v[t, x, y], y]^2 +
D[v[t, x, y], x]^2) - u[t, x, y] + u[t, x, y]^3,
D[v[t, x, y], t] ==
D[v[t, x, y], {x, x}] + D[v[t, x, y], {y, y}] -
v[t, x, y]*u[t, x, y] +
(2/u[t, x, y])*(D[u[t, x, y], x]*D[v[t, x, y], x] -
D[u[t, x, y], y]*D[v[t, x, y], y])};bc = {u[0, x, y] == 0, v[0, x, y]== 0, u[t, 5, y] == 1, u[t, x, 5] == 1, D[v[t, 0, y], x] == 0, D[v[t, x, 0], y] == 0};
NDSolve[{pde, bc}, {u, v}, {x, 0, 5}, {y, 0, 5}, {t, 0, 2}]
'Error: NDSolve::deqn: Equation or list of equations expected instead of True in the first argument {{(u^(1,0,0))[t,x,y]==-u[t,x,y]+u[t,x,y]^3+(u^(0,0,y))[t,x,y]-((<<1>>^(<<3>>))[<<3>>]^2+(<<1>>^(<<3>>))[<<3>>]^2)/u[t,x,y]^3+(u^(0,x,0))[t,x,y],(v^(1,0,0))[t,x,y]==-u[t,x,y] v[t,x,y]+(v^(0,0,y))[t,x,y]+(2 (-(<<1>>^(<<3>>))[<<3>>] (<<1>>^(<<3>>))[<<3>>]+(<<1>>^(<<3>>))[<<3>>] (<<1>>^(<<3>>))[<<3>>]))/u[t,x,y]+(v^(0,x,0))[t,x,y]},{u[0,x,y]==0,v[0,x,y]==0,u[t,5,y]==1,u[t,x,5]==1,True,True}}.
NDSolve[{{Derivative[1, 0, 0][u][t, x, y] == -u[t, x, y] +
u[t, x, y]^3 + Derivative[0, 0, y][u][t, x, y] -
(Derivative[0, 0, 1][v][t, x, y]^2 +
Derivative[0, 1, 0][v][t, x, y]^2)/u[t, x, y]^3 +
Derivative[0, x, 0][u][t, x, y],
Derivative[1, 0, 0][v][t, x, y] == (-u[t, x, y])*v[t, x, y] +
Derivative[0, 0, y][v][t, x, y] +
(2*((-Derivative[0, 0, 1][u][t, x, y])*
Derivative[0, 0, 1][v][t, x, y] +
Derivative[0, 1, 0][u][t, x, y]*
Derivative[0, 1, 0][v][t, x, y]))/u[t, x, y] +
Derivative[0, x, 0][v][t, x, y]}, {u[0, x, y] == 0,
v[0, x, y] == 0, u[t, 5, y] == 1, u[t, x, 5] == 1, True,
True}}, {u, v}, {x, 0, 5}, {y, 0, 5}, {t, 0, 2}]
If you look at the value of bc you will see
bc = {u[0, x, y] == 0, v[0, x, y] == 0, u[t, 5, y] == 1,
u[t, x, 5] == 1, D[v[t, 0, y], x] == 0, D[v[t, x, 0], y] == 0}
gives you
{u[0, x, y] == 0, v[0, x, y] == 0, u[t, 5, y] == 1, u[t, x, 5] == 1, True, True}
That is where your error message about True is coming from.
What you were doing was differentiating an expression with respect to x, but the expression had no x in it, thus the result was zero. And 0==0 is always True. Likewise with y. So let's change the way you are trying to tell it the boundary conditions.
bc = {u[0, x, y] == 0, v[0, x, y] == 0, u[t, 5, y] == 1, u[t, x, 5] == 1,
Derivative[0, 1, 0][v][t, 0, y] == 0, Derivative[0, 0, 1][v][t, x, 0] == 0}
or
bc = {u[0, x, y] == 0, v[0, x, y] == 0, u[t, 5, y] == 1, u[t, x, 5] == 1,
D[v[t, x, y], x] == 0/.x->0, D[v[t, x, y], y] == 0/.y->0}
either of which I think should give you what you are looking for.
Once you fix those then you get a different error about Derivative order and non-negative integer.
I believe you fix that by changing your pde from {x,x} and {y,y} to {x,2} and {y,2} like this
pde = {D[u[t, x, y], t] == D[u[t, x, y], {x, 2}] + D[u[t, x, y], {y, 2}] -
(1/u[t, x, y])^3*(D[v[t, x, y], y]^2 + D[v[t, x, y], x]^2) - u[t, x, y] +
u[t, x, y]^3,
D[v[t, x, y], t] == D[v[t, x, y], {x, 2}] + D[v[t, x, y], {y, 2}] - v[t, x, y]*
u[t, x, y] + (2/u[t, x, y])*(D[u[t, x, y], x]*D[v[t, x, y], x] - D[u[t, x, y], y]*
D[v[t, x, y], y])};
Which makes that error go away.
Once you have fixed that and try your NDSolve then zeros in your denominators start to bite you.
Fixing those looks like more than just understanding MMA syntax. That may require understanding your problem and seeing if you can eliminate those zero denominators.

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.

Free boundary conditions in MATHEMATICA - is this right? Second opinion

I'm trying to prescribe free boundary conditions for a non-linear evolution equation in mathematica and I wanted as second opinion on whether or not what I am doing is right.
The boundary conditions have been marked with a comment, viz., (FREE BOUNDARY CONDITIONS)
I'd also like to run this for pinned boundary conditions.
Needs["VectorAnalysis`"]
Needs["DifferentialEquations`InterpolatingFunctionAnatomy`"];
Clear[Eq5, Complete, h, S, G, E1, K1, D1, VR, M]
Eq5[h_, {S_, G_, E1_, K1_, D1_, VR_, M_}] := \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]h\) +
Div[-h^3 G Grad[h] +
h^3 S Grad[Laplacian[h]] + (VR E1^2 h^3)/(D1 (h + K1)^3)
Grad[h] + M (h/(1 + h))^2 Grad[h]] + E1/(h + K1) == 0;
SetCoordinates[Cartesian[x, y, z]];
Complete[S_, G_, E1_, K1_, D1_, VR_, M_] :=
Eq5[h[x, y, t], {S, G, E1, K1, D1, VR, M}];
TraditionalForm[Complete[S, G, E1, K1, D1, VR, M]]
L = 185.62; TMax = 100; km = 0.0381;
Off[NDSolve::mxsst];
Off[NDSolve::ibcinc];
hSol = h /. NDSolve[{Complete[100, 0, 0, 0, 0.001, 0, 5],
(*FREE BOUNDARY CONDITIONS*)
Derivative[2, 0, 0][h][0, y, t] == 0,
Derivative[2, 0, 0][h][L, y, t] == 0,
Derivative[0, 2, 0][h][x, 0, t] == 0,
Derivative[0, 2, 0][h][x, L, t] == 0,
Derivative[3, 0, 0][h][0, y, t] == 0,
Derivative[3, 0, 0][h][L, y, t] == 0,
Derivative[0, 3, 0][h][x, 0, t] == 0,
Derivative[0, 3, 0][h][x, L, t] == 0,
(*FREE BOUNDARY CONDITIONS*)
h[x, y, 0] == 1 + (-0.05*Cos[2*Pi*(x/L)] - 0.05*Sin[2*Pi*(x/L)])*
Cos[2*Pi*(y/L)]},
h, {x, 0, L}, {y, 0, L}, {t, 0, TMax}][[1]]
hGrid = InterpolatingFunction[hSol];
{TMin, TRup} = InterpolatingFunctionDomain[hSol][[3]]
The consensus achieved from reading the comments is that the implementation of free boundary conditions in the code above is correct.
More detail should be available in books dealing with mechanics of materials or strength of materials in chapters referring to Bending moments and shear stress diagrams where very often free-free or free-fixed or fixed-fixed boundary conditions are used.

How to "embed" Piecewise in NDSolve in Mathematica

I am using NDSolve to solve a non-linear partial differential
equation.
I'd like one of the variables (Kvar) to be a function
of the time step currently being solved and hence and using
Piecewise.
Mathematica generates an error message saying:
SetDelayed::write: Tag Real in 0.05[t_] is Protected. >>
NDSolve::deqn: Equation or list of equations expected instead of
$Failed in the first argument ....
ReplaceAll::reps: ....
I haven't included the entire error message for ease of reading.
My code is as follows:
Needs["VectorAnalysis`"]
Needs["DifferentialEquations`InterpolatingFunctionAnatomy`"];
Clear[Eq4, EvapThickFilm, h, S, G, E1, K1, D1, VR, M, R]
Eq4[h_, {S_, G_, E1_, K1_, D1_, VR_, M_, R_}] := \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]h\) +
Div[-h^3 G Grad[h] +
h^3 S Grad[Laplacian[h]] + (VR E1^2 h^3)/(D1 (h + K1)^3)
Grad[h] + M (h/(1 + h))^2 Grad[h]] + E1/(
h + K1) + (R/6) D[D[(h^2/(1 + h)), x] h^3, x] == 0;
SetCoordinates[Cartesian[x, y, z]];
EvapThickFilm[S_, G_, E1_, K1_, D1_, VR_, M_, R_] :=
Eq4[h[x, y, t], {S, G, E1, K1, D1, VR, M, R}];
TraditionalForm[EvapThickFilm[S, G, E1, K1, D1, VR, M, R]];
And the second cell where I am trying to implement Piecewise in NDSolve:
L = 318; TMax = 7.0;
Off[NDSolve::mxsst];
(*Ktemp = Array[0.001+0.001#^2&,13]*)
hSol = h /. NDSolve[{
(*S,G,E,K,D,VR,M*)
Kvar[t_] := Piecewise[{{0.01, t <= 4}, {0.05, t > 4}}],
EvapThickFilm[1, 3, 0.1, Kvar[t], 0.01, 0.1, 0, 160],
h[0, y, t] == h[L, y, t],
h[x, 0, t] == h[x, L, t],
(*h[x,y,0] == 1.1+Cos[x] Sin[2y] *)
h[x, y, 0] ==
1 + (-0.25 Cos[2 \[Pi] x/L] - 0.25 Sin[2 \[Pi] x/L]) Cos[
2 \[Pi] y/L]
},
h,
{x, 0, L},
{y, 0, L},
{t, 0, TMax}
][[1]]
hGrid = InterpolatingFunctionGrid[hSol];
PS: I am sorry but the first cell block doesn't display so well here. And thanks to not having enough "reputation", I can't post images.
The error message occurs when using the NDSolve cell block.
Define the function Kvar outside of a set of equations in NDSolve, like
Off[NDSolve::mxsst];
(*Ktemp=Array[0.001+0.001#^2&,13]*)
Kvar[t_] := Piecewise[{{0.01, t <= 4}, {0.05, t > 4}}];
hSol = ...
and remove it from the list in NDSolve, so that it starts as NDSolve[{(*S,G,E,K,D,VR,M*)EvapThickFilm[..., and it will work. It gives warnings, but those are related to possible singularities in your equation.
Also, your original error indicates that your Kvar was assigned a value of 0.05. So, add Clear[Kvar] before anything else in the second cell.

ReplaceAll not working as expected

Still early days with Mathematica so please forgive what is probably a very obvious question. I am trying to generate some parametric plots. I have:
ParametricPlot[{
(a + b) Cos[t] - h Cos[(a + b)/b t],
(a + b) Sin[t] - h Sin[(a + b)/b t]},
{t, 0, 2 \[Pi]}, PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}
No joy: the replacement rules are not applied and a, b and h remain undefined.
If I instead do:
Hold#ParametricPlot[{
(a + b) Cos[t] - h Cos[(a + b)/b t],
(a + b) Sin[t] - h Sin[(a + b)/b t]},
{t, 0, 2 \[Pi]}, PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}
it looks like the rules ARE working, as confirmed by the output:
Hold[ParametricPlot[{(2 + 1) Cos[t] -
1 Cos[(2 + 1) t], (2 + 1) Sin[t] - 1 Sin[(2 + 1) t]}, {t, 0,
2 \[Pi]}, PlotRange -> All]]
Which is what I'd expect. Take the Hold off, though, and the ParametricPlot doesn't work. There's nothing wrong with the equations or the ParametricPlot itself, though, because I tried setting values for a, b and h in a separate expression (a=2; b=1; h=1) and I get my pretty double cardoid out as expected.
So, what am I doing wrong with ReplaceAll and why are the transformation rules not working? This is another fundamentally important aspect of MMA that my OOP-ruined brain isn't understanding.
I tried reading up on ReplaceAll and ParametricPlot and the closest clue I found was that "ParametricPlot has attribute HoldAll and evaluates f only after assigning specific numerical values to variables" which didn't help much or I wouldn't be here.
Thanks.
Mathematica evaluates each head without holding attributes by first evaluating head of each subexpression. Since ReplaceAll doesn't have holding attributes, ParametricPlot becomes Graphics before replacement
To see the expression tree, do
ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] -
h Sin[(a + b)/b t]}, {t, 0, 2 \[Pi]},
PlotRange -> All] /. {a -> 2, b -> 1, h -> 1} // Hold // TreeForm
From that tree you can see that your command is the same as doing
temp1=ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] -
h Sin[(a + b)/b t]}, {t, 0, 2 \[Pi]},
PlotRange -> All]
temp2={a -> 2, b -> 1, h -> 1}
temp1/.temp2
Look at FullForm[temp1] to confirm that there's no a or b in that expression.
If you set ReplaceAll to HoldFirst, that prevents ParametricPlot from being evaluated before ReplaceAll, and result is what you expected. In this case, ReplaceAll evaluates to expression with head ParametricPlot, and only at that point ParametricPlot is evaluated. Make sure to reset the attributes back because changing behavior of built-in commands can have unexpected side-effects.
SetAttributes[ReplaceAll, HoldFirst];
ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] -
h Sin[(a + b)/b t]}, {t, 0, 2 \[Pi]},
PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}
ClearAttributes[ReplaceAll, HoldFirst]
A useful trick when needing to evaluate arguments passed to function with HoldAll is to do operations on an expression with List head, and substitute ParametricPlot in the end, for instance
ParametricPlot ## ({{(a + b) Cos[t] -
h Cos[(a + b)/b t], (a + b) Sin[t] - h Sin[(a + b)/b t]}, {t, 0,
2 \[Pi]}, PlotRange -> All} /. {a -> 2, b -> 1, h -> 1})
The best way for using local variables in Mathematica is Module[]:
Module[{a = 2, b = 1, h = 1},
ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] - h Sin[(a + b)/b t]},
{t, 0, 2 \[Pi]},
PlotRange -> All]]
This way a, b, and h do not get assigned values in the Global context but only inside the Module. If you still want to use replacement rules you just have to ReleaseHold after you have done the replacement:
ReleaseHold[
Hold#ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] - h Sin[(a + b)/b t]},
{t, 0, 2 \[Pi]},
PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}]
EDIT: As to why this happens. The way I understand it, HoldAll prevents the arguments of the function from being modified by any rules (internal or explicit). What your Hold does, is place the entire function on hold (not just the arguments), and the replacement rule gets applied after the function has gone through evaluation (which it didn't so there is still something there to replace) and HoldAll is no longer valid.
In[1] := Hold[a /. a -> 5]
Out[1] := Hold[a /. a -> 5]
In[2] := Hold[a] /. a -> 5
Out[2] := Hold[5]
Of course, Hold also has HoldAll as an attribute, so this doen't explain why ParametricPlot's HoldAll is different. :-(
EDIT2: I used Trace to look at what happens, and it seems like ReplaceAll gets applied only at the very end, when ParametricPlot has already turned into a graphical object (and does not contain a, b, or h anymore). In the case of Hold[a] /. a -> 5 the hold evaluates to Hold[a] and the replacement rule can then be successfully applied.
That is the way ReplaceAll always work.
See for example:
In[10]:= (a/a) /. a -> 0
Out[10]= 1
Clearly the replacement is done AFTER the evaluation, because if you do:
In[11]:= a = 0; a/a
During evaluation of In[11]:= Power::infy: Infinite expression 1/0 encountered. >>
During evaluation of In[11]:= Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered. >>
Out[12]= Indeterminate
Now, is a matter of inserting the replacement at the level you want it to operate. As the result of a Plot is basically an Image with the numeric coordinates already "solved", you want to put those coordinates in before the plot is calculated. In your case:
ParametricPlot[
{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] - h Sin[(a + b)/b t]}
/. {a -> 2, b -> 1, h -> 1},
{t, 0, 2 \[Pi]},
PlotRange -> All
]
This is not an answer as such, just a comment on using Module with Plot.
If I proceed as follows
f[t_] := {(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] -
h Sin[(a + b)/b t]}
The following will NOT work
Method 1:
Module[{a = 2, b = 1, h = 1},
ParametricPlot[f[t], {t, 0, 2 \[Pi]}, PlotRange -> All]]
Method 2:
Module[{a = 2, b = 1, h = 1},
ParametricPlot[Evaluate[f[t]], {t, 0, 2 \[Pi]}, PlotRange -> All]]
The following does work (Method 3)
ParametricPlot[
Module[{a = 2, b = 1, h = 1}, Evaluate[f[t]]], {t, 0, 2 \[Pi]},
PlotRange -> All]
as does the method described above (method 4)
Module[{a = 2, b = 1, h = 1},
ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] - h Sin[(a + b)/b t]},
{t, 0, 2 \[Pi]},
PlotRange -> All]]
Can anyone explain why method 4 works but method 2 doesn't? (The same applies to With, which I find more intuitive to Module).
For what its worth, I would generate the original parametric plot using replacement rules as follows:
ParametricPlot[
Evaluate[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] -
h Sin[(a + b)/b t]}] /. {a -> 2, b -> 1, h -> 1}, {t, 0,
2 \[Pi]}, PlotRange -> All]
EDIT
f[x_] := (a x)/(b + x);
With[{a = 10, b = 100}, Plot[Evaluate[f[x]], {x, 0, 100}]]
With[{a = 10, b = 100}, Plot[(a x)/(b + x), {x, 0, 100}]]
Plot[With[{a = 10, b = 100}, Evaluate[f[x]]], {x, 0, 100}]
Plot[Evaluate[f[x]] /. {a -> 10, b -> 100}, {x, 0, 100}]
Method 1 (of Edit) does not work (because 'Plot' treats the variable x as local, effectively using Block'?)
It seems to me that it is absolutely clear to anyone, even those with a rudimentary knowledge of Mathematica, what is going on with Method 2, showing the power and ease-of-use of Mathematica. When the equations become more complex, is it advantageous to define them separately. It is now not so clear that Method 3 must be used instead of Method 1. (Method 4, of course, is probably the best of all.)

Resources