Mathematica How do I plot a vector field with 1 variable? - vector

I cannot figure out how to plot a vector field with only 1 variable. Maybe Mathematica doesn't support this. For example:
r(t) = cost j + sint i
same as
<cost, sint>
This doesn't work:
VectorPlot[{cos t, sin t}, {t, 0, 2 Pi}]
As a bonus how to take the derivative of a vector?

An easy workaround would be to use a 2D-VectorPlot with a dummy variable like this:
VectorPlot[
{Cos[t], Sin[t]}, {t, 0, 2 \[Pi]}, {s, -1/2, 1/2},
AspectRatio -> Automatic,
VectorPoints -> {15, 3},
FrameLabel -> {"t", None}
]
Or what probably makes more sense is to discretize the curve that you get when you follow the vector while increasing t. This is e.g. useful for Feynman-style Action-integrals in quantum mechanics.
Module[
{t, dt = 0.1, vectors, startpoints, startpoint, vector, spv, spvs},
vectors = Table[dt {Cos[t], Sin[t]}, {t, 0, 2 \[Pi], dt}];
startpoints = Accumulate[vectors];
spvs = Transpose[{startpoints, vectors}];
Graphics[Table[Arrow[{spv[[1]], spv[[1]] + spv[[2]]}], {spv, spvs}]]
]

Related

Can anyone write a prog that change the sum(1 to n) to n*(n+1)/2 automatic?

with the rec sum:
let rec sum a=if a==0 then 0 else a+sum(a-1)
if the compiler use the tail recursive optimization,it may create a variable "sum" to iteration(when I use the "ocamlc -dlambda",the recursive still there.when I use "ocamlc -dinstr" got the assemably code,I can't read it now)
but on the book《Design Concepts of programming languages》,page 287,it can change the function to this(the key line):n*(n+1)/2
"You should convince yourself that the least fixed point of this
function is the computation csum that returns a summation procedure that,returns n*(n+1)/2 if its argument is a nonnegative integer in"
I can't understand it,the prog not Gauss!I think it can't chang the "rec sum" to n*(n+1)/2 automatic!only man can do it,right?
So how this book write here means?Is anyone know?Thanks!
I believe your book is merely making a small point about equivalence of pure functions. Nevertheless, optimising away a loop that only contains affine operations is relatively easy.
Equivalence of pure functions
I haven't read that book, but from the paragraph you quote, I think the book merely makes a point about pure functions. Since sum is a pure function, i.e. a function without side-effect, then in a sense,
let rec sum n =
if n = 0 then 0
else n + sum (n - 1)
is equivalent to
let sum n =
n * (n + 1) / 2
But of course "equivalent" here ignores the time and space complexity, and unless the compiler has some sort of hardcoding for common functions to optimise, I'd be extremely surprised if it optimised sum like that.
Also note that the two above functions are only equivalent so far as they are only called on a nonnegative argument. The recursive version will loop infinitely (and provoke a stack overflow) if n is negative; the direct formula version will always return a result, although that result will be nonsensical if n is negative.
Optimising loops that only contain affine operations
Nevertheless, writing a compiler that would perform such optimisations is not complete science-fiction. At the end of this answer you will find links to two blogposts which you might be interested in. In this answer I will summarise how the method described in those blog posts can be applied to your problem.
First let's rewrite function sum as a loop in pseudo-code:
function sum(n):
s := 0
i := 1
repeat n:
s += i
i += 1
return s
This kind of rewriting is similar to what happens when sum is transformed into a tail-recursive function.
Now if you consider the vector v = [s, i, 1], then the affine operations s += i and i += 1 can be described as multiplying v by a matrix:
s += i
[[ 1, 0, 0 ], # matrix Msi
[ 1, 1, 0 ],
[ 0, 0, 1 ]]
i += 1
[[ 1, 0, 0 ], # matrix Mi1
[ 0, 1, 0 ],
[ 0, 1, 1 ]]
s += i, i += 1
[[ 1, 0, 0 ], # M = Msi * Mi1
[ 1, 1, 0 ],
[ 0, 1, 1 ]]
This affine operation is wrapped in a "repeat n" loop. So we have to multiply v by this matrix M, n times. But matrix multiplication is associative; so instead of doing n multiplications by matrix M, we can raise matrix M to its nth power, and then multiply v by the resulting matrix M**n.
As it turns out:
[[1, 0, 0], [[ 1, 0, 0],
[1, 1, 0], to the nth = [ n, 1, 0],
[0, 1, 1]] [n*(n - 1)/2, n, 1]]
which represents the affine operation:
s = s + n * i + n * (n - 1) / 2
i = i + n
Starting from s, i = 0, 1, this gives us s = n * (n+1) / 2 as expected.
More reading:
Using the Quick Raise of Matrices to a Power to Write a Very Fast Interpreter of a Simple Programming Language;
Automatic Algorithms Optimization via Fast Matrix Exponentiation.

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}]

How to output periodogram result to a list in Mathematica?

I have a time domain data. After I did discrete fourier transform with Periodogram, how can I output the plot into a list for further manipulation?
Periodogram[data[[All, 2]], SampleRate -> 3000000/0.01, GridLinesStyle -> Directive[Red, Dashed], PlotRange -> {{100000000 - 10000, 100000000 + 10000}, All}]
Thanks!
A List can be extracted from the Periodogram output, based on its InputForm, as follow. (We begin with some made-up data.)
data = Table[2 Sin[0.2 \[Pi] n ] + Sin[0.5 \[Pi] n] + RandomReal[{-1, 1}], {n, 0, 127}];
plot = Periodogram[data, SampleRate -> 3000000/0.01,
GridLinesStyle -> Directive[Red, Dashed]];
Next, we use Position to locate the desired quantities within the plot and extract them.
plot[[First#Position[plot, Line] /. {0 -> 1} /. List -> Sequence]]
(* {{0., -8.99487}, {2.38095*10^6, 1.60543}, {4.7619*10^6, 1.82102}, ... *)
There is usually an associated function in instances like this. In this case PeriodogramArray outputs the data.
data = Table[
2 Sin[0.2 Pi n ] + Sin[0.5 Pi n] + RandomReal[{-1, 1}],
{n, 0, 127}];
Periodogram[data]
magdata = PeriodogramArray[data];
ListLinePlot[10 Log[10, magdata], PlotRange -> {{0, Length[magdata]/2}, All}]

Mathematica: Filling under an infinite function

Mathematica: Filling an infinitely deep potential well
Comment: The proper page for Mathematica questions is this one
I would like to visualize a potential well for a particle in a box in Mathematica similar to the second picture from Wikipedia here.
I have defined my function piecewise
(*Length of the box*)
L = 4;
(*Infinitly deep potential well between 0 and L*)
V[x_] := Piecewise[{
{\[Infinity], x <= 0},
{0, 0 < x < L},
{\[Infinity], L <= x}}]
and would like to obtain a plot function which gives a filled area where the potential goes to infinity.
Unfortunately my tries end up in shaded areas between the "zero region" of the potential, while I would like to have the shading in the infinity region.
Table[Plot[V[x], {x, -5, 10},
Filling -> f], {f, {Top, Bottom, Axis, 0.3}}]
The problem is that Infinity is too much for plot. So let's just give it some other big number. But to prevent it from rescaling the y axis we need to be specific with the upper plot range
Block[{\[Infinity] = 1*^1},
Plot[V[x], {x, -5, 10}, Filling -> Bottom,
PlotRange -> {Automatic, 1}]
]
Alternatively you could plot V[x]/.\[Infinity]->1*^1 instead of Block but I like Block's way better
Just give it values instead of infinity:
(*Length of the box*)L = 4;
(*Infinitly deep potential well between 0 and L*)
V[x_] := Piecewise[{{1, x <= 0}, {0, 0 < x < L}, {1, L <= x}}]
Plot[V[x], {x, -5, 10}, Filling -> Bottom]
Another way using graphic primitives:
wellLeft = 0;
leftBorder = wellLeft - 1;
rightBorder = L + 1;
wellRight = L;
top = 5;
Graphics[{
Hue[0.67, 0.6, 0.6],
Opacity[0.2],
Rectangle[{leftBorder, 0}, {wellLeft, top}],
Rectangle[{wellRight, 0}, {rightBorder, top}]
}, Axes -> True]

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

Resources