I have an equation like:
y= Sum[ i x[i] , {i,10}]
and I want to calculate the derivative :
D[y,x[i]] -> = i
How can I do that in mathematica ?
I can do D[y, x[3]] and it gives me 3
but if I enter D[y, x[i]] it returns 0 but I expect i.
Is there a way to define the parametric derivative for series like the above in Mathematica ?
Probably not the best way to think about your problem, anyway :
Build the list of your variables :
vars = Table[Symbol["x" <> ToString[i]], {i, 1, 10}]
(* {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} *)
Build your function :
expr = Dot[Range[10], vars]
(* x1 + 10 x10 + 2 x2 + 3 x3 + 4 x4 + 5 x5 + 6 x6 + 7 x7 + 8 x8 + 9 x9 *)
Take the derivatives :
D[expr, #] & /# vars
(* {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)
Here is a few things to consider.
The notation x_3 does not mean x with index 3. It means three times x_.
You should use Subscript[x,3] instead.
Your y is: Sum[n * Subscript[x, n], {n, 1, 5}]
You can now find the partial deriviative:
D[Sum[n * Subscript[x, n], {n, 1, 5}], Subscript[x, 2]]
gives 2.
D[Sum[Subscript[x, n], {n, 1, 5}], Subscript[x, j]] gives 0.
The reason is that Subscript[x, j] is considered a variable.
Related
I have a simple Mathematica code below where I first introduce a scalar function ϕ = ϕ[x,y,z] and then calculate the gradient of ϕ. Now, I would like to evaluate the Gradient at point P by substituting in proper values for x, y, z. Please assist me with last step with plugging values into x and y into gradient. See code Below:
ϕ = y^2 + z^2 - 4;
varlist = {x, y, z}
Delϕ = Table[D[ϕ, varlist[[i]]], {j, 1, 1}, {i, 1, 3}]
Delϕ // MatrixForm
P = {2, 1, Sqrt (3)}
Thanks
Assuming you meant y^2 + z^2 - 4 x
φ = y^2 + z^2 - 4 x;
varlist = {x, y, z};
g = D[φ, #] & /# varlist
{-4, 2 y, 2 z}
p = {2, 1, Sqrt[3]};
grad = g /. Thread[varlist -> p]
{-4, 2, 2 Sqrt[3]}
another approach is to make your derivative a function:
\[Phi] = y^2 + z^2 - 4 x;
varlist = {x, y, z};
Del\[Phi][{x_, y_, z_}] = Table[D[\[Phi], varlist[[i]]], {i, 1, 3}];
then you can simply do this:
P = {2, 1, Sqrt[3]};
Del\[Phi][P]
{-4, 2, 2 Sqrt[3]}
I have a non-analytical equation. I could solve for different values of parameters but my program is not working at all. At the end i want to plot y vs x
f[x_] := y + Sqrt[3 + x*y - x^20 - y^4]
Table[f[x], {x, 0.1, 0.5, 0.1}]
NSolve[f[x] == 0, y]
f[x_] := y + Sqrt[3 + x*y - x^20 - y^4]
sol = Solve[f[x] == 0, y];
x0 = Table[i, {i, 0.1, 0.5, 0.1}];
subs = N[sol /. x -> #] & /# x0
This creates results from which we can see that the first and second solutions produce complex numbers. Plotting the two real solutions first.
y3 = subs[[All, 3, 1, 2]];
y4 = subs[[All, 4, 1, 2]];
ListLinePlot[{Transpose[{x0, y3}], Transpose[{x0, y4}]}]
Alternatively the plot can be produced from the solutions with
Plot[{sol[[3, 1, 2]], sol[[4, 1, 2]]}, {x, 0.1, 0.5}]
The complex solutions can be plotted like so:
ParametricPlot[{{Re[sol[[1, 1, 2]]], Im[sol[[1, 1, 2]]]},
{Re[sol[[2, 1, 2]]], Im[sol[[2, 1, 2]]]}}, {x, 0, Pi/2}]
I want to evaluate f[x,y]=-4 x + x^2 - 4 y - y^2 at points (1,-2); (2,-3); (3,-2); (2,-1).
I tried using Outer but for some reason it does not give me actual values. Help.
Remember that Mathematica has a specific way of defining functions. In your case it would be f[x_,y_]:=-4 x + x^2 - 4 y - y^2. Then you could simply use f[1,-2] etc.
Perhaps consider using a 'pure' function. For example:
-4 #1 + #1^2 - 4*#2 - #2^2 & ### {{1, -2}, {2, -3}, {3, -2}, {2, -1}}
gives
{1, -1, 1, -1}
Here are some variations on the theme:
Clear[f]
f[{x_, y_}] := -4 x + x^2 - 4 y - y^2
points = {{1, -2}, {2, -3}, {3, -2}, {2, -1}};
Map[f, points]
{1, -1, 1, -1}
f[x_, y_] := -4 x + x^2 - 4 y - y^2
f[1, -2]
1
f = Function[{x, y}, -4 x + x^2 - 4 y - y^2];
f[1, -2]
1
You can use functions like Apply and Map to evaluate a function in a list of points, for example
f[x_, y_] := -4 x + x^2 - 4 y - y^2
pts = {{1, -2}, {2, -3}, {3, -2}, {2, -1}};
Apply[f, pts, {1}]
(* out: {1, -1, 1, -1} *)
or using ### as a short hand for Apply[ ...., {1}]
f ### pts
I am trying to quickly solve the following problem:
f[r_] := Sum[(((-1)^n (2 r - 2 n - 7)!!)/(2^n n! (r - 2 n - 1)!))
* x^(r - 2*n - 1),
{n, 0, r/2}];
Nw := Transpose[Table[f[j], {i, 1}, {j, 5, 200, 1}]];
X1 = Integrate[Nw . Transpose[Nw], {x, -1, 1}]
I can get the answer quickly with this code:
$starttime = AbsoluteTime[]; Quiet[LaunchKernels[]];
DIM = 50;
Print["$Version = ", $Version, " ||| ",
"Number of Kernels : ", Length[Kernels[]]];
Nw = Transpose[Table[f[j], {i, 1}, {j, 5, DIM, 1}]];
nw2 = Nw.Transpose[Nw];
Round[First[AbsoluteTiming[nw3 = ParallelMap[Expand, nw2]; ]]]
intrule = (pol_Plus)?(PolynomialQ[#1, x]&) :>
(Select[pol, !FreeQ[#1, x] & ] /.
x^(n_.) /; n > -1 :> ((-1)^n + 1)/(n + 1)) + 2*(pol /. x -> 0)]);
Round[First[AbsoluteTiming[X1 = ParallelTable[row /. intrule, {row, nw3}]; ]]]
X1
Print["overall time needed in seconds: ", Round[AbsoluteTime[] - $starttime]];
But how can I manage this code if I need to solve the following problem, where a and b are known constants?
X1 = a Integrate[Nw.Transpose[Nw], {x, -1, 0.235}]
+ b Integrate[Nw.Transpose[Nw], {x, 0.235,1}];
Here's a simple function to do definite integrals of polynomials
polyIntegrate[expr_List, {x_, x0_, x1_}] := polyIntegrate[#, {x, x0, x1}]&/#expr
polyIntegrate[expr_, {x_, x0_, x1_}] := Check[Total[#
Table[(x1^(1 + n) - x0^(1 + n))/(1 + n), {n, 0, Length[#] - 1}]
]&[CoefficientList[expr, x]], $Failed, {General::poly}]
On its range of applicability, this is about 100 times faster than using Integrate. This should be fast enough for your problem. If not, then it could be parallelized.
f[r_] := Sum[(((-1)^n*(2*r - 2*n - 7)!!)/(2^n*n!*(r - 2*n - 1)!))*
x^(r - 2*n - 1), {n, 0, r/2}];
Nw = Transpose[Table[f[j], {i, 1}, {j, 5, 50, 1}]];
a*polyIntegrate[Nw.Transpose[Nw], {x, -1, 0.235}] +
b*polyIntegrate[Nw.Transpose[Nw], {x, 0.235, 1}] // Timing // Short
(* Returns: {7.9405,{{0.0097638 a+0.00293462 b,<<44>>,
-0.0000123978 a+0.0000123978 b},<<44>>,{<<1>>}}} *)
Somebody have idea how to use all cores for calculating integration? I need to use parallelize or parallel table but how?
f[r_] := Sum[(((-1)^n*(2*r - 2*n - 7)!!)/(2^n*n!*(r - 2*n - 1)!))*
x^(r - 2*n - 1), {n, 0, r/2}];
Nw := Transpose[Table[f[j], {i, 1}, {j, 5, 200, 1}]];
X1 = Integrate[Nw . Transpose[Nw], {x, -1, 1}];
Y1 = Integrate[D[Nw, {x, 2}] . Transpose[D[Nw, {x, 2}]], {x, -1, 1}];
X1//MatrixForm
Y1//MatrixForm
I changed the integration of a list into a list of integrations so that I can use ParallelTable:
X1par=ParallelTable[Integrate[i, {x, -1, 1}], {i, Nw.Transpose[Nw]}];
X1par==X1
(* ===> True *)
Y1par = ParallelTable[Integrate[i,{x,-1,1}],{i,D[Nw,{x,2}].Transpose[D[Nw,{x,2}]]}]
Y1 == Y1par
(* ===> True *)
In my timings, with {j, 5, 30, 1} instead of {j, 5, 200, 1} to restrict the time used somewhat, this is about 3.4 times faster on my quod-core. But it can be done even faster with:
X2par = Parallelize[Integrate[#, {x, -1, 1}] & /# (Nw.Transpose[Nw])]
X2par == X1par == X1
(* ===> True *)
This is about 6.8 times faster, a factor of 2.3 of which is due to Parallelize.
Timing and AbsoluteTiming are not very trustworthy when parallel execution is concerned. I used AbsoluteTime before and after each line and took the difference.
EDIT
We shouldn't forget ParallelMap:
At the coarsest list level (1):
ParallelMap[Integrate[#, {x, -1, 1}] &, Nw.Transpose[Nw], {1}]
At the deepest list level (most fine-grained parallelization):
ParallelMap[Integrate[#, {x, -1, 1}] &, Nw.Transpose[Nw], {2}]
If one helps Integrate a bit by expanding the matrix elements first,
things are doable with a little bit of effort.
On a quad-core laptop with Windows and Mathematica 8.0.4 the following code below runs
for the asked DIM=200 in about 13 minutes,
for DIM=50 the code runs in 6 second.
$starttime = AbsoluteTime[]; Quiet[LaunchKernels[]];
DIM = 200;
Print["$Version = ", $Version, " ||| ", "Number of Kernels : ", Length[Kernels[]]];
f[r_] := f[r] = Sum[(((-1)^n*(-(2*n) + 2*r - 7)!!)*x^(-(2*n) + r - 1))/(2^n*n!*(-(2*n) + r - 1)!), {n, 0, r/2}];
Nw = Transpose[Table[f[j], {i, 1}, {j, 5, DIM, 1}]];
nw2 = Nw . Transpose[Nw];
Print["Seconds for expanding Nw.Transpose[Nm] ", Round[First[AbsoluteTiming[nw3 = ParallelMap[Expand, nw2]; ]]]];
Print["do the integral once: ", Integrate[x^n, {x, -1, 1}, Assumptions -> n > -1]];
Print["the integration can be written as a simple rule: ", intrule = (pol_Plus)?(PolynomialQ[#1, x] & ) :>
(Select[pol, !FreeQ[#1, x] & ] /. x^(n_.) /; n > -1 :> ((-1)^n + 1)/(n + 1)) + 2*(pol /. x -> 0)];
Print["Seconds for integrating Nw.Transpose[Nw] : ", Round[First[AbsoluteTiming[X1 = ParallelTable[row /. intrule, {row, nw3}]; ]]]];
Print["expanding: ", Round[First[AbsoluteTiming[preY1 = ParallelMap[Expand, D[Nw, {x, 2}] . Transpose[D[Nw, {x, 2}]]]; ]]]];
Print["Seconds for integrating : ", Round[First[AbsoluteTiming[Y1 = ParallelTable[py /. intrule, {py, preY1}]; ]]]];
Print["X1 = ", (Shallow[#1, {4, 4}] & )[X1]];
Print["Y1 = ", (Shallow[#1, {4, 4}] & )[Y1]];
Print["seq Y1 : ", Simplify[FindSequenceFunction[Diagonal[Y1], n]]];
Print["seq X1 0 : ",Simplify[FindSequenceFunction[Diagonal[X1, 0], n]]];
Print["seq X1 2: ",Simplify[FindSequenceFunction[Diagonal[X1, 2], n]]];
Print["seq X1 4: ",Simplify[FindSequenceFunction[Diagonal[X1, 4], n]]];
Print["overall time needed in seconds: ", Round[AbsoluteTime[] - $starttime]];