Mathematica - Numerical and Analytic plots don't overlap - plot

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]

Related

How to plot functions and combine into one graph in Mathematica using a Table[Plot[ approach?

Here is an attempt to plot 2-c Exp[-t] for c=-3 to 3 by 1 in Mathematica.
Table[Plot[2 - c Exp[-t], {t, 0, 1}], {c, -3, 3}]
I want these 7 plots to be combined into one image. The following gives an error message.
Show[{%, %%, %%%, %%%%, %%%%%, %%%%%%, %%%%%%%}]
How to do? Another approach?
There are many ways to do this. As the most basic case, this will work:
Show[
Table[Plot[2 - c Exp[-t], {t, 0, 1}], {c, -3, 3}],
PlotRange -> All
]
But here's a cleaner one:
Plot[Evaluate#Table[2 - c Exp[-t], {c, -3, 3}], {t, 0, 1}]

Mathematica: Unusual piecewise plot issue

Hello and thanks in advance,
I came upon an issue trying to plot a piecewise function in Mathematica. When entered directly into the Plot function, the piecewise did NOT plot. However, when I defined the piecewise as a variable, it did plot. Why is this issue occurring and can I still join my plot? (I would like to join it by setting exclusions to none)
The following is my code: (I define a Maxwell strain function and am trying to model the plastic deformation of a polymer over multiple stress cycles)
z = 2*10^10;
h = 10^12;
MaxwellStrain[s_, t_] := s/z + (s*t)/h;
stress = {0, 10^7, -10^7, 5*10^6, 10^7, -5*10^6};
time = {0, 100, 200, 300, 400, 500};
strainList = Join[{0}, Table[MaxwellStrain[stress[[i + 1]], t - time[[i]]], {i, 1, 5}]];
Plot[
Piecewise[
Table[{
Total[strainList[[1 ;; i + 1]]], time[[i]] < t < time[[i + 1]]},
{i, 1, 5}
],
Exclusions -> none]
,
{t, 0, 500}
]
x = Piecewise[
Table[{
Total[strainList[[1 ;; i + 1]]], time[[i]] < t < time[[i + 1]]},
{i, 1, 5}
],
Exclusions -> none]
Plot[x, {t, 0, 500}]
The following is my output: (first plot doesn't show, the second does)
output:
Thank you for the help,
SV

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.

Numerical Solution for a specified parameter in NDSolve (Mathematica)

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}*)

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