Numerical Solution for a specified parameter in NDSolve (Mathematica) - math

I am working on a solution to solve a Partial Differential Equation, Fick's Second Law of Diffusion to be exact.
I was able to produce a 3D Plot using the NDSolve and Plot3D functions.
Code used:
NDSolve[{D[c[t, h], t] == 1*D[c[t, h], h, h],
c[0, h] == Erfc[h/(2*81.2)],
c[t, 0] == 1,
c[t, 4000] == 3.08*^-18}, c, {t, 0, 900}, {h, 0, 274}]
Instead of a graphical representation, I would like to find numerical points of the graph at t = 900.
I would like to know how to put in t = 900 into NDSolve (or other functions) so as to generate detailed numerical points of the solution.

Try saving the solution in a variable first:
e = NDSolve[{D[c[t, h], t] == 1*D[c[t, h], h, h], c[0, h] == Erfc[h/(2*81.2)], c[t, 0] == 1, c[t, 4000] == 3.08*^-18}, c, {t, 0, 900}, {h, 0, 274}]
Then we can Evaluate this expression for our desired variables:
Evaluate[c[900, 10] /. e]
(*{0.914014}*)
Or to make it more versatile, we can use Manipulate:
Manipulate[Evaluate[c[t, h] /. e], {t, 0, 900}, {h, 0, 274}]
Update:
Considering the information I received from the comments below; we can define a function like q[t,h] which will give us the solution as a function:
q[t_, h_] := Evaluate[c[t, h] /. e]
q[900, 10]
(*{0.914014}*)

Related

Why I generated wrong Taylor series?

I want to prove the expression Limit[Sin[x*x] *Exp[-x*x]*x, x -> Infinity] ==0
So I do this Normal[ Series[ Sin[x*x] *Exp[-x*x]*x, {x, 0, 40}]].And the result
imply the expression will be infinity.
That is odd, and I make some change. Let t=x^2, then the expression will be Limit[Sin[t] *Exp[-t]*Sqrt[t], t -> Infinity]. I try again Normal[ Series[Sin[x] *Exp[-x]*Sqrt[x], {x, 0, 40}]]. That's the answer I need.
The right series
I can't figure out what's wrong. Why I can't generate series directly?
I wnat to say thay f, f1 and f2 should be the same, but they looks like this:
f=f1,but f2 diffuse
Normal[Series[Sin[x*x]*Exp[-x*x]*x, {x, 0, 40}]]
with t = x^2 is equivalent to
Normal[Series[Sin[t]*Exp[-t]*x, {x, 0, 40}]]
because x != Sqrt[x^2] e.g. for `x = -2
Results for negative t are not plotted.
Plot[Sin[t]*Exp[-t]*Sqrt[t], {t, -8, 8}]

cant plot derivate from differential equation

Suppose this code in Mathematica:
w[w1_] := NDSolve[{y''[x] + y[x] == 2, y'[0] == 0, y[0] == w1},y[x], {x, 0, 30}];
Manipulate[Plot[Evaluate[{y[x], y'[x]} /. w[w1]], {x, 0, 30}], {w1, -2, 3}]
The code works this way: A slidebar (in Manipulate) controls one initial condition (value of y[0]) of differential equation saved in variable w and when it is changed, the solution is plotted. This works fine, but I have a problem with plotting the first derivative of the solution (y'[x]). Instead of a function graph there is just simply blank space and nothing is plotted, even I checked it in Mathematica help, where the example code
s = NDSolve[{y''[x] + Sin[y[x]] y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 30}]
Plot[Evaluate[{y[x], y'[x], y''[x]} /. s], {x, 0, 30},PlotStyle -> Automatic]
works. Does anybody know where's the problem?
I found the problem, it is just in NDSolve it is needed to write
w[w1_] := NDSolve[{y''[x] + y[x] == 2, y'[0] == 0, y[0] == w1},y, {x, 0, 30}]
y instead of y[x] and everything works.

Mathematica - Numerical and Analytic plots don't overlap

I want to plot simple functions like exponentials in Mathematica.
I have a problem with the following code:
A[r_, c_] := Exp[-c r]
IntNum[r_?NumericQ, c_] := Integrate[A[t, c], {t, 0, r}]
IntAnal[r_, c_] := -(1/c) Exp[-c r]
Plot[{IntNum[r, 1], IntAnal[r, 1]}, {r, 0, 3}]
I don't understand why the two functions don't give same plot.
Because:
A[r_, c_] := Exp[-c r]
Integrate[A[t, c], {t, 0, r}]
(1 - E^(-c r))/c
and
(1 - E^(-c r))/c != -(1/c) Exp[-c r]

Plot functions with different domains in Maxima

What is the best way of plotting several functions with different domains into the same plot? Is there a way to do this with plot2d, or do I have to use draw2d instead?
I especially like the possibility in plot2d to give several functions in a list, whereas I would have to add the different functions in draw2d as separate parameters, if I understand the documentation correctly.
An example of what I mean:
f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);
In this example I would like to plot f(x, a) for several a (e.g. using makelist(f(x, a), a, [0, 0.5, 1, 2, 5])) from -1 to 10 and fmax from 0 to 5 (to show where the maxima of the f(x, a) family of curves are located).
You can try draw2d
f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);
flist: makelist(f(x, a), a, [0, 0.5, 1, 2, 5]);
par: map(lambda([f], explicit(f, x, -1, 10)), flist);
par: append([explicit(fmax, x, 0, 5), color=red], par);
load(draw);
apply(draw2d, par);
One approach I am not particularly happy with is to declare the functions with smaller domains as parametric curves, with the x axis parameter being simply x:
f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);
plot2d(endcons([parametric, x, fmax(x), [x, 0, 5], [nticks, 80]],
makelist(f(x, a), a, [0, 1/2, 1, 2, 5])),
[x, -1, 10]);
This was frustrating me for hours but I found a way to have multiple differently domained functions on the same graph.
wxplot2d([if x < 0 then -x else sin(x), if x > -1 then x^2],[x,-%pi,%pi],[y,-2,2]);

Mathematica doesn't solve wave equation when given boundary conditions

Still new to Mathematica syntax. When I do:
DSolve[{
D[u[x, t], {x, 2}] == (1/(v*v))*D[u[x, t], {t, 2}],
u[0, t] == 0,
u[l, 0] == 0
}, u, {x, t}]
it just returns what I entered
DSolve[{(u^(2,0))[x,t]==(u^(0,2))[x,t]/v^2,u[0,t]==0,u[l,0]==0},u,{x,t}]
However, when I remove the boundary conditions, I get
{{u->Function[{x,t},C[1][t-(Sqrt[v^2] x)/v^2]+C[2][t+(Sqrt[v^2] x)/v^2]]}}
with C[1] and C[2] representing the functions for the boundary conditions.
Anyone know why this is happening?
2 things:
Don't you need more boundary and initial conditions than just 2? You have second order derivatives on left and right side, each requires 2 conditions. Hence total is 4. see http://mathworld.wolfram.com/WaveEquation1-Dimensional.html
I do not think DSolve or NDSolve can solve initial and boundary value problems? I seem to have read this somewhere sometime ago. No time to check now.
I think that Mathematica doesn't know how to deal with these boundary conditions for 2nd order PDEs. How would you want the answer returned? As a general Fourier series?
This is mentioned in the Mathematica Cookbook (and probably other places)...
Breaking down the problem for Mathematica (with the dimensional factor v->1), you find
In[1]:= genSoln = DSolve[D[u[x, t], {x, 2}] == D[u[x, t], {t, 2}], u, {x, t}] // First
Out[1]= {u -> Function[{x, t}, C[1][t - x] + C[2][t + x]]}
In[2]:= Solve[u[0, t] == 0 /. genSoln]
Out[2]= {{C[1][t] -> -C[2][t]}}
In[3]:= u[l, 0] == 0 /. genSoln /. C[1][x_] :> -C[2][x] // Simplify
Out[3]= C[2][-l] == C[2][l]
that the solution is written as f(t-x)-f(t+x) where f is periodic over [-l,l]...
You can't do any more with out making assumptions about the smoothness of the solution.
You can check that the standard Fourier series approach would work, e.g.
In[4]:= f[x_, t_] := Sin[n Pi (t + x)/l] - Sin[n Pi (t - x)/l]
In[5]:= And[D[u[x, t], {x, 2}] == D[u[x, t], {t, 2}],
u[0, t] == 0, u[l, 0] == 0] /. u -> f // Reduce[#, n] & // Simplify
Out[5]= C[1] \[Element] Integers && (n == 2 C[1] || n == 1 + 2 C[1])

Resources