mathematica exponential Nth derivative treated as an unknown function - wolfram-mathematica

I'd like to create a list of Hankel functions, defined in terms of an Nth derivative, but the Nth order derivatives get treated in the way that is described in the docs under "Derivatives of unknown functions", and left unevaluated. Here's an example:
Clear[x, gaussianExponential]
gaussianExponential[x_] := Exp[- x^2]
FullSimplify[Derivative[2][gaussianExponential[x]]]
I get:
(E^-x^2)^[Prime][Prime]
(instead of seeing the derivatives evaluated (and the final expressions are left unsimplified)).
Any idea what's going on here?

The correct syntax is:
Clear[x, gaussianExponential]
gaussianExponential[x_] := Exp[-x^2]
FullSimplify[Derivative[2][gaussianExponential][x]]

The Derivative applies to the function symbol f, not to the function evaluated at a point f[x]. So what you want is
Clear[x, gaussianExponential]
gaussianExponential[x_] := Exp[-x^2]
Derivative[2][gaussianExponential][x]//FullSimplify

Related

How can I "expand"/change the form of the derivatives of Normal PDF and CDF?

I am computing the partial derivatives of a likelihood function involving the PDF and CDF of the normal distribution.
I obtain an expression with PDF^(0,1)[NormalDistribution,...] and CDF^(0,1)[NormalDistribution,...], the derivatives of the functions with respect to its second argument.
How can I ask Mathematica to further "transform" these expressions to obtain a final expression that only contains PDF[NormalDistribution,...]?
Thanks for your help!
flnl[x1_, x2_] :=
Log[CDF[NormalDistribution, (x1)]*PDF[NormalDistribution, x1] +
CDF[NormalDistribution, x2]*PDF[NormalDistribution, x2 ]]
In[76]:= D[flnl[x1,x2],x1]
Out[76]= (PDF[NormalDistribution,x1] (CDF^(0,1))[NormalDistribution,x1]+CDF[NormalDistribution,x1] (PDF^(0,1))[NormalDistribution,x1])/(CDF[NormalDistribution,x1] PDF[NormalDistribution,x1]+CDF[NormalDistribution,x2] PDF[NormalDistribution,x2])
Check your syntax. See examples in CDF - Generalizations & Extensions.
You need function brackets, i.e. NormalDistribution[].
flnl[x1_, x2_] :=
Log[CDF[NormalDistribution[], (x1)]*PDF[NormalDistribution[], x1] +
CDF[NormalDistribution[], x2]*PDF[NormalDistribution[], x2]]

Javascript Function syntax in plain English

I'm a part-time English teacher and I want to teach students basic programming so I try to rewrite syntax into plain English for them to understand more easily.
So I rewrite Javascript:
function functionName(a, b, c) {
// actual function
return a + b + c
};
functionName(1,2,3);
as plain English:
formula formulaName(input1, input2, input3) {
// actual formula
run input1 + input2 + input3
};
// without the formula keyword it means execute
// with the formula keyword it means define:
formulaName(1,2,3);
// formula that assigns 1,2,3 as input1, input2, input3, then adds them to get the output when executed
Is my english translation correct?
I'd reckon to call it function instead of formula. A function can do more things than a formula itself. A formula is something applied on one or more expressions whereas a function can contain more than one formula, even an output of a formula can be used as an input for another formula within the same function. Also, the keyword execute make more sense than run. You can also use the keyword statement to refer each expression. There is nothing wrong as long as syntactical pseudo code is correct.

Compound Expressions in a Function in Mathematica

I wanted to calculate the power sum S_p(x) = 1^p + 2^p + 3^p + ... + x^p using the code
powersum[x_,p_]:=sum=0;For[i=1,i<x,i++,sum=sum+i^p];sum
but it seems to output 0 every time. Why does it do that?
As written, Mathematica is parsing your expression like this:
powersum[x_,p_]:=sum=0; (*Definition ended here*)
For[i=1,i<x,i++,sum=sum+i^p];
sum
You need to use to wrap your expression in parenthesis to make them all part of the function definition.
powersum[x_,p_]:=(sum=0;For[i=1,i<x,i++,sum=sum+i^p];sum)
Often it is preferable to use Module[]:
powersum[x_,p_]:=Module[{sum},sum=0;For[i=1,i<x,i++,sum=sum+i^p];sum]
or
powersum[x_,p_]:=Module[{sum=0},For[i=1,i<x,i++,sum=sum+i^p];sum]
this is essentially the same as wrapping in () except sum is protected in a local context.
of course for this example you could as well use :
powersum[x_,p_]:=Sum[i^p,{i,1,x-1}]
or
powersum[x_, p_] := Range[x - 1]^p // Total

How do I make a function use the altered version of a list in Mathematica?

I want to make a list with its elements representing the logic map given by
x_{n+1} = a*x_n(1-x_n)
I tried the following code (which adds stuff manually instead of a For loop):
x0 = Input["Enter x0"]
a = Input["a"]
M = {x0}
L[n_] := If[n < 1, x0, a*M[[n]]*(1 - M[[n]])]
Print[L[1]]
Append[M, L[1]]
Print[M]
Append[M, L[2]]
Print[M]
The output is as follows:
0.3
2
{0.3}
0.42
{0.3,0.42}
{0.3}
Part::partw: Part 2 of {0.3`} does not exist. >>
Part::partw: Part 2 of {0.3`} does not exist. >>
{0.3, 2 (1 - {0.3}[[2]]) {0.3}[[2]]}
{0.3}
It seems that, when the function definition is being called in Append[M,L[2]], L[2] is calling M[[2]] in the older definition of M, which clearly does not exist.
How can I make L use the newer, bigger version of M?
After doing this I could use a For loop to generate the entire list up to a certain index.
P.S. I apologise for the poor formatting but I could find out how to make Latex code work here.
Other minor question: What are the allowed names for functions and lists? Are underscores allowed in names?
It looks to me as if you are trying to compute the result of
FixedPointList[a*#*(1-#)&, x0]
Note:
Building lists element-by-element, whether you use a loop or some other construct, is almost always a bad idea in Mathematica. To use the system productively you need to learn some of the basic functional constructs, of which FixedPointList is one.
I'm not providing any explanation of the function I've used, nor of the interpretation of symbols such as # and &. This is all covered in the documentation which explains matters better than I can and with which you ought to become familiar.
Mathematica allows alphanumeric (only) names and they must start with a letter. Of course, Mathematic recognises many Unicode characters other than the 26 letters in the English alphabet as alphabetic. By convention (only) intrinsic names start with an upper-case letter and your own with a lower-case.
The underscore is most definitely not allowed in Mathematica names, it has a specific and widely-used interpretation as a short form of the Blank symbol.
Oh, LaTeX formatting doesn't work hereabouts, but Mathematica code is plenty readable enough.
It seems that, when the function definition is being called in
Append[M,L2], L2 is calling M[2] in the older definition of M,
which clearly does not exist.
How can I make L use the newer, bigger version of M?
M is never getting updated here. Append does not modify the parameters you pass to it; it returns the concatenated value of the arrays.
So, the following code:
A={1,2,3}
B=Append[A,5]
Will end up with B={1,2,3,5} and A={1,2,3}. A is not modfied.
To analyse your output,
0.3 // Output of x0 = Input["Enter x0"]. Note that the assignment operator returns the the assignment value.
2 // Output of a= Input["a"]
{0.3} // Output of M = {x0}
0.42 // Output of Print[L[1]]
{0.3,0.42} // Output of Append[M, L[1]]. This is the *return value*, not the new value of M
{0.3} // Output of Print[M]
Part::partw: Part 2 of {0.3`} does not exist. >> // M has only one element, so M[[2]] doesn't make sense
Part::partw: Part 2 of {0.3`} does not exist. >> // ditto
{0.3, 2 (1 - {0.3}[[2]]) {0.3}[[2]]} (* Output of Append[M, L[2]]. Again, *not* the new value of M *)
{0.3} // Output of Print[M]
The simple fix here is to use M=Append[M, L[1]].
To do it in a single for loop:
xn=x0;
For[i = 0, i < n, i++,
M = Append[M, xn];
xn = A*xn (1 - xn)
];
A faster method would be to use NestList[a*#*(1-#)&, x0,n] as a variation of the method mentioned by Mark above.
Here, the expression a*#*(1-#)& is basically an anonymous function (# is its parameter, the & is a shorthand for enclosing it in Function[]). The NestList method takes a function as one argument and recursively applies it starting with x0, for n iterations.
Other minor question: What are the allowed names for functions and lists? Are underscores allowed in names?
No underscores, they're used for pattern matching. Otherwise a variable can contain alphabets and special characters (like theta and all), but no characters that have a meaning in mathematica (parentheses/braces/brackets, the at symbol, the hash symbol, an ampersand, a period, arithmetic symbols, underscores, etc). They may contain a dollar sign but preferably not start with one (these are usually reserved for system variables and all, though you can define a variable starting with a dollar sign without breaking anything).

how to use units along function parameter values in Mathematica

I would like to pass the parameter values in meters or kilometers (both possible) and get the result in meters/second.
I've tried to do this in the following example:
u = 3.986*10^14 Meter^3/Second^2;
v[r_, a_] := Sqrt[u (2/r - 1/a)];
Convert[r, Meter];
Convert[a, Meter];
If I try to use the defined function and conversion:
a = 24503 Kilo Meter;
s = 10198.5 Meter/Second;
r = 6620 Kilo Meter;
Solve[v[r, x] == s, x]
The function returns the following:
{x -> (3310. Kilo Meter^3)/(Meter^2 - 0.000863701 Kilo Meter^2)}
which is not the user-friendly format.
Anyway I would like to define a and r in meters or kilometers and get the result s in meters/second (Meter/Second).
I would be very thankful if anyone of you could correct the given function definition and other statements in order to get the wanted result.
Here's one way of doing it, where you use the fact that Solve returns a list of rules to substitute a value for x into v[r, x], and then use Convert, which will do the necessary simplification of the resulting algebraic expression as well:
With[{rule = First#Solve[v[r,x]==s,x]
(* Solve always returns a list of rules, because algebraic
equations may have multiple solutions. *)},
Convert[v[r,x] /. rule, Meter/Second]]
This will return (10198.5 Meter)/Second as your answer.
You just need to tell Mathematica to simplify the expression assuming that the units are "possitive", which is the reason why it doesn't do the simplifications itself. So, something like
SimplifyWithUnits[blabla_, unit_List]:= Simplify[blalba, (#>0)&/#unit];
So if you get that ugly thing, you then just type %~SimplifyWithUnits~{Meter} or whatever.

Resources