Gradient of a function at specific x,y,z values in Mathematica - wolfram-mathematica

I'm looking for a way to find the gradient of a function and then evaluate the output at specific x,y,z values. Thus far, I have
Needs["VectorAnalysis`"]
Clear[x, y, z]
v1 = Grad[x^2 + y^2 + z^2 - 9, Cartesian[x, y, z]]
which outputs
{2 x, 2 y, 2 z}. How could I evaluate this at x=2,y=2, and z=1, for instance? This seems trivial for the function above, but it would be immensely useful for more complicated functions. Thank you very much for any advice.

You can Replace the variables by values using Rule:
v1/.{x->2,y->2,z->1}

Related

Fixing arguments when using pmap in Julia

I have defined a function f(x, y, z) in Julia and I want to parallely compute f for many values of x, holding y and z fixed. What is the "best practices" way to do this using pmap?
It would be nice if it was something like pmap(f, x, y = 5, z = 8), which is how the apply family handles fixed arguments in R, but it doesn't appear to be as simple as that. I have devised solutions, but I find them inelegant and I doubt that they will generalize nicely for my purposes.
I can wrap f in a function g where g(x) = f(x, y = 5, z = 8). Then I simply call pmap(g, x). This is less parsimonious than I would like.
I can set 5 and 8 as default values for y and z when f is defined and then call pmap(f, x). This makes me uncomfortable in the case where I want to fix y at the value of some variable a, where a has (for good reason) not been defined at the time that f is defined, but will be by the time f is called. It works, but it kind of spooks me.
A good solution, which turns your apparently inflexible first option into a flexible one, is to use an anonymous function, e.g.
g(y, z) = x -> f(x, y, z)
pmap(g(5, 8), x)
or just
pmap(x -> f(x, 5, 8), x)
In Julia 0.4, anonymous functions have a performance penalty, but this will be gone in 0.5.

How to calculate the integral with the condition in wolfram

How to calculate the integral with the condition in Wolfram like
Integral(1/(x^4 + y^4)dxdy) where y >=x^2+1
I assume you mean the definite integral over all x, y>x^2+1. The syntax is this:
Integrate[1/(x^4 + y^4), {x, -Infinity, Infinity},{ y, x^2 + 1,Infinity}]
Note mathematica's ordering of the integration variables is reverse of standard convention, ie. left to right is outside to inside. This takes quite a while to report that it does not converge. However the numerical integration gives a result:
NIntegrate[1/(x^4 + y^4), {x, -Infinity, Infinity},{ y, x^2 + 1,Infinity}]
0.389712
My guess is the numeric result is correct and mathematica is simply wrong about the analytic convergence. You might try math.stackexchange.com or mathematica.stackexchange.com if you need to prove convergence. I am doubtful there is a nice analytic result.
How about
Integrate[1/(x^4 + y^4), y, x, Assumptions -> y >= x^2 + 1]
note also Multiple Integral

Right-angled triangle prolog construction

I'm to ask a question, which answers are solving this task:
Which right-angled triangles can be constructed by choosing three sides out of six segments of length being integers from 1 to 6
So, I'm thinking this is essential:
between(1,6,X),
between(1,6,Y),
between(1,6,Z),
Then we have to make sure it fits Pythagoras statement, so I'm trying this, adding to the above sentence:
(X^2 = Y^2 + Z^2 ;
Y^2 = X^2 + Z^2 ;
Z^2 = X^2 + Y^2)
Also I have been trying to replace X^2 with X*X, but it returns false every time. Why is that?
From my understanding, I need it to work like this:
Choose three sides from range 1-6, and make sure they fit Pythagoras statement. (Is triangle disparity also required here? I mean X>Y+Z,Y>X+Z,Z>X+Y ?
Check the prolog manual regarding the different comparators, etc. They mean and do various things. =:=/2 is specifically evaluates arithmetic expressions on either side and checks for equality of results. =/2 is not an equality operator; it performs prolog unification. It's important to know the difference. In your example, limiting all results to maximum of 6, then permutations of 3,4,5 are the only positive integer solutions to the right triangle.
?- between(1,6,X), between(1,6,Y), between(1,6,Z), Z^2 =:= X^2 + Y^2.
X = 3,
Y = 4,
Z = 5 ;
X = 4,
Y = 3,
Z = 5 ;
false.

Matematica. Residue integral

I just started to use Mathematica and wanted to try out if I got a result correct from a residue integral i made. I have two poles in the UHP, actually looking more complicated but I just wanted to see if Mathematica could to this. This is my code:
x1 = Iw;
x2 = 2 Iw;
Integrate [e^(Ixt)/((x - x1) (x - x2)), {x, -infinity, infinity}, Assumptions -> t > 0]
Is this integral possible to do in Mathematica?
Mathematica sees Iw as a two character variable name, but I w as the product of two things.
Mathematica is fanatic about correct capitalization and spelling.
x1=I w; x2=2 I w;
Integrate[E^(I x t)/((x-x1)(x-x2)), {x,-Infinity,Infinity}, Assumptions-> t>0]
returns a large complicated result depending on the signs and whether w might be zero. If you can supply additional domain information the result might be simplified.

Problem performing a substitution in a multiple derivative

I have a basic problem in Mathematica which has puzzled me for a while. I want to take the m'th derivative of x*Exp[t*x], then evaluate this at x=0. But the following does not work correct. Please share your thoughts.
D[x*Exp[t*x], {x, m}] /. x -> 0
Also what does the error mean
General::ivar: 0 is not a valid variable.
Edit: my previous example (D[Exp[t*x], {x, m}] /. x -> 0) was trivial. So I made it harder. :)
My question is: how to force it to do the derivative evaluation first, then do substitution.
As pointed out by others, (in general) Mathematica does not know how to take the derivative an arbitrary number of times, even if you specify that number is a positive integer.
This means that the D[expr,{x,m}] command remains unevaluated and then when you set x->0, it's now trying to take the derivative with respect to a constant, which yields the error message.
In general, what you want is the m'th derivative of the function evaluated at zero.
This can be written as
Derivative[m][Function[x,x Exp[t x]]][0]
or
Derivative[m][# Exp[t #]&][0]
You then get the table of coefficients
In[2]:= Table[%, {m, 1, 10}]
Out[2]= {1, 2 t, 3 t^2, 4 t^3, 5 t^4, 6 t^5, 7 t^6, 8 t^7, 9 t^8, 10 t^9}
But a little more thought shows that you really just want the m'th term in the series, so SeriesCoefficient does what you want:
In[3]:= SeriesCoefficient[x*Exp[t*x], {x, 0, m}]
Out[3]= Piecewise[{{t^(-1 + m)/(-1 + m)!, m >= 1}}, 0]
The final output is the general form of the m'th derivative. The PieceWise is not really necessary, since the expression actually holds for all non-negative integers.
Thanks to your update, it's clear what's happening here. Mathematica doesn't actually calculate the derivative; you then replace x with 0, and it ends up looking at this:
D[Exp[t*0],{0,m}]
which obviously is going to run into problems, since 0 isn't a variable.
I'll assume that you want the mth partial derivative of that function w.r.t. x. The t variable suggests that it might be a second independent variable.
It's easy enough to do without Mathematica: D[Exp[t*x], {x, m}] = t^m Exp[t*x]
And if you evaluate the limit as x approaches zero, you get t^m, since lim(Exp[t*x]) = 1. Right?
Update: Let's try it for x*exp(t*x)
the mth partial derivative w.r.t. x is easily had from Wolfram Alpha:
t^(m-1)*exp(t*x)(t*x + m)
So if x = 0 you get m*t^(m-1).
Q.E.D.
Let's see what is happening with a little more detail:
When you write:
D[Sin[x], {x, 1}]
you get an expression in with x in it
Cos[x]
That is because the x in the {x,1} part matches the x in the Sin[x] part, and so Mma understands that you want to make the derivative for that symbol.
But this x, does NOT act as a Block variable for that statement, isolating its meaning from any other x you have in your program, so it enables the chain rule. For example:
In[85]:= z=x^2;
D[Sin[z],{x,1}]
Out[86]= 2 x Cos[x^2]
See? That's perfect! But there is a price.
The price is that the symbols inside the derivative get evaluated as the derivative is taken, and that is spoiling your code.
Of course there are a lot of tricks to get around this. Some have already been mentioned. From my point of view, one clear way to undertand what is happening is:
f[x_] := x*Exp[t*x];
g[y_, m_] := D[f[x], {x, m}] /. x -> y;
{g[p, 2], g[0, 1]}
Out:
{2 E^(p t) t + E^(p t) p t^2, 1}
HTH!

Resources