## How to minimize that expression in four variables?

I mean $$\sqrt{w^2+(21-x)^2}+\sqrt{(20-w)^2+z^2}+\sqrt{x^2+(20-y)^2}+\sqrt{y^2+(21-z)^2}.$$

The command

Minimize[Sqrt[x^2 + (20 - y)^2] + Sqrt[y^2 + (21 - z)^2] +  Sqrt[z^2 + (20 - w)^2] + Sqrt[w^2 + (21 - x)^2], {x, y, z, w}] 

is running without any response on my comp for hours. The numerical optimizations

NMinimize[ Sqrt[x^2 + (20 - y)^2] + Sqrt[y^2 + (21 - z)^2] +  Sqrt[z^2 + (20 - w)^2] + Sqrt[w^2 + (21 - x)^2], {x, y, z, w},  Method -> "DifferentialEvolution"] 

{58., {x -> 11.579, y -> 8.97237, z -> 11.579, w -> 8.97237}

and the same with Method->"RandomSearch"

{58., {x -> 10.5551, y -> 9.94753, z -> 10.5551, w -> 9.94753}}

and the same with Method->"NelderMead"

{58., {x -> 18.3218, y -> 2.55062, z -> 18.3218, w -> 2.55062}}

suggest the optimal value under consideration is taken in many points.

## ND gives expression instead of number

I have a function sol defined as a solution to an equation (computed numerically). Specifically

sol[p_, a_] := NSolve[g[p, x, a] == 0, x, Reals][[1, 1, 2]] 

Trying it out

sol[.2, .51] sol[.2, .52] sol[.2, .53] 

gives .51, .52, .53 — on this range it behaves like identity. Now I try

ND[sol[.2, aa], aa, .51 ] 

and instead of giving me 1 as I would expect, it gives me very long expression involving variable x. I am not using the variable aa anywhere else in the code. And yes, I have done Needs["NumericalCalculus"].

How is this possible? It seems obvious to me that it has to return a number! How can it return anything else?

Edit: here is the whole code (I have simplified it a little bit)

Needs["NumericalCalculus"] g[  x_, a_] = D[RealAbs[1 - x]^1.2 + RealAbs[a - x]^1.2, x]; sol[a_] := NSolve[g[ x, a] == 0, x, Reals][[1, 1, 2]]; ND[sol[aa], aa, .55] 

## Infinity: Indeterminate expression 0.0389874+Complexinfinity+Complexinfinity encountered

I’d like to solve 2nd order differential equation:

Which I’d like to draw a graph of x_M and h. m, g, and M is a real constant. Cd is well defined function that has a parameter as x_M’ theta is piecewise defined function, that appears as below:

for this equation, I used code :

\[Theta]0 = 0.5236;  \[Omega]1 = 5.2359877;  \[Omega]2 = 1.396; t1 = \[Theta]0 / \[Omega]1; t2 = \[Theta]0 / \[Omega]2; T=2*(t1+t2); f[t_]=Piecewise[{{ -Mod[t,T]*\[Omega]1 ,0<=Mod[t,T]<t1},{ -\[Theta]0+((Mod[t,T]-t1)*\[Omega]2), t1<=Mod[t,T]<(t1+2t2)},{\[Theta]0-((Mod[t,T]-t1-2t2)*\[Omega]1),(t1+2t2)<=Mod[t,T]<T}}] 

It is clear that derivative of the function is

thetaprime[t_]:=Piecewise[{{-5.2359877 ,0<=Mod[t,T]<t1},{ 1.396, t1<=Mod[t,T]<(t1+2t2)},{-5.2359877,(t1+2t2)<=Mod[t,T]<=T}}];  

for differential equation, I used a code

initconds = {x[0] == 0, x'[0] == 0.00001, h[0] == (M + m)/(\[Rho]*A), h'[0] == 0} eqns = {m Cos[\[Theta][t]] Sin[\[Theta][t]] h''[t] + (M + m (Sin[\[Theta][t]]^2)) x''[t] == - Cd[Derivative[1][x][t]] *h[t]* Derivative[1][x][t] + m Sin[\[Theta][t]] (g Cos[\[Theta][t]] + r thetaprime[t]^2),( -M-(m*Cos[\[Theta][t]]^2))*h''[t] - m Cos[\[Theta][t]] Sin[\[Theta][t]] x''[t] == -g M + A g \[Rho] h[t] - m Cos[\[Theta][t]] (g Cos[\[Theta][t]] + r thetaprime[t]^2)} 

But when I try to solve the Equation,

sol = NDSolve[Append[eqns, initconds], {x, h}, {t, 0, tf}, Method -> {"DiscontinuityProcessing" -> False}] 

It gives following three errors. I can’t understand why these errors occur.

Infinity::indet: Indeterminate expression 0.0389874 +ComplexInfinity+ComplexInfinity encountered.

Infinity::indet: Indeterminate expression -0.160636+ComplexInfinity+ComplexInfinity+ComplexInfinity+ComplexInfinity+ComplexInfinity encountered.

NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 0..

Please help me to solve the problem.

## Writing mathematical expression in a file

I am trying to write a mathematical expression as variable = expression in the output file.

The current method that I am doing is through WriteString as

expr = -((a*b*y^2*(-1 + z)*z*(1 + a*b + z))/(1 + a*b)^2) +         (z*(1 + a*b - a*b*z + (z + a*b*z)^2))/(1 + a*b)^2 -         (y*z*((-1 + z)^2 + a*b*(2 - 3*z + z^2) + a^2*b^2*(1 - z + z^2)))/(1 + a*b)^2 expression = CForm[expr] /. {Power->pow}; (*expression = CForm[HornerForm[expr]] /. {Power->pow};*) str=OpenWrite["output.hh"]; WriteString[str," double variable = ", expression,";"]; (*WriteString[str," double variable = ", ExportString[expression,"Text"]];*) Close[str]; 

The output is

 cat output.hh  double variable = -(a*b*(-1 + z)*z*(1 + a*b + z)*pow(1 + a*b,-2)*pow(y,2)) - y*z*pow(1 + a*b,-2)*(pow(-1 + z,2) + a*b*(2 - 3*z + pow(z,2)) + pow(a,2)*pow(b,2)*(1 - z + pow(z,2))) + z*pow(1 + a*b,-2)*(1 + a*b - a*b*z + pow(z + a*b*z,2)); 

I want to break the line as the original expr (or after some user-specified column say 80) i.e.

 double variable = -(a*b*(-1 + z)*z*(1 + a*b + z)*pow(1 + a*b,-2)*pow(y,2)) -                    y*z*pow(1 + a*b,-2)*(pow(-1 + z,2) + a*b*(2 - 3*z + pow(z,2)) + pow(a,2)*pow(b,2)*(1 - z + pow(z,2))) +                     z*pow(1 + a*b,-2)*(1 + a*b - a*b*z + pow(z + a*b*z,2)); 

Although this might not lead to optimized expression for numerical evaluation, however it is wanted as beautification. Also how should one optimize expression for further processing to other languages. Any suggestion? ExperimentalOptimizeExpression and HornerForm are some options. Any other recommendation? MathCode is much costly 😀

## Passing an expression to the Plot Function does not Work

Solving a differential equation I found that its solution was analytic, and I stored it as an expression from the default rule output of the DSolve[] function:

soleq2 = y[x] /. soleq2[[1, 1]]

Which has output:

Sqrt[x] BesselJ[Sqrt[13]/2, x] C[1] + Sqrt[x] BesselY[Sqrt[13]/2, x] C[2]

I then plotted this:

Manipulate[Plot[soleq2, {x, 0, 10}], {C[1], 0, 10}, {C[2], 0, 10}]

but the plot does not show anything, only the sliders for the modulation of C[1] and C[2]. I tried substituting these constants with other letters thinking their format might interfere with the kernel, but to no avail.

## FullSimplify a expression with a even function“Df[x] == Df[-x]“?

I want to simplify a expression with a even functionDf[x] == Df[-x] like:

FullSimplify[-(1/2) Df[x - x4] J[x] - 1/2 Df[-x + x4] J[x], Assumptions -> {Df[x] == Df[-x]}]

This code can not give the desired result like -Df[-x + x4] J[x].

And the strange thing here is that if I replace x4 with x2, it works well.

FullSimplify[-(1/2) Df[x - x2] J[x] - 1/2 Df[-x + x2] J[x], Assumptions -> {Df[x] == Df[-x]}] will give -Df[-x + x2] J[x].

So any solutions here for x4 or arbitrary symbol?

## How to add a goal in Google analytics with regular expression such as

I want to add Goal in Google Analytics with the same URL twice with a different regular expression such as

www.example.com/checkout#shipping www.example.com/checkout#payment 

these are on the same page with different tabs. So how I track user go which tab and other go where because google analytics just track my checkout page, not #shiiping and #payment

## Manipulation of a symbolic matrix expression

I have the following code:

enter code here 

which provides me the answer:

## Mathematica not computing Matrix problem, just returning multiplication expression

I’m doing some very simple matrix operations in Mathematica, but for some reason, the last operation I’m trying to evaluate is not returning the actual product, just shows the symbolic multiplication.

P = { {1, 2}, {3, 4}} /10;  im = {{1},{1}} in = {{1},{1}}  A = ArrayFlatten[ { {KroneckerProduct[in\[Transpose], IdentityMatrix[2]]}, {KroneckerProduct[IdentityMatrix[2], im\[Transpose]]} } ]  p = Flatten[P] // MatrixForm  A.p 

This last operation, $$A\cdot p$$ is returning the following:

Why is that so?

## Access to Interpolation expression of Mathematica’s Interpolate command

To reproduce fits which have been done in the framework of a standard, I was reviewing the Interpolation engine of Mathematica (for InterpolationOrder -> 3) to get more information about the mechanics Mathematica is using for Interpolation.

Currently there is limited control especially for the "Hermite" Interpolation method to control the way the Splines are fitted. My interpretation is that with the "Hermite" setting the default parameter boundaries are resulting in natural Splines, whereas the PeriodicInterpolation->True setting enforces periodic boundary conditions. However there are many more methods to Spline Interpolation of 3rd order like Not-a-Knot spline or Quadratic Spline boundary conditions (see Link) Taking a simple interpolation example in Mathematica as:

data = {{0, 21}, {1, 24}, {2, 24}, {3, 18}, {4, 16}}; intf1=Interpolation[data, Method -> "Spline"]; intf2=Interpolation[data, Method -> "Hermite"]; 

I was wondering how the underlying interpolation expressions generated by the Interpolation routine could be accessed.

InputForm[intf1[[4, 1]]]  Out[1]/InputForm=  BSplineFunction[1, {{0., 4.}}, {3}, {False}, {{21., 22.61111111111111, 30.166666666666664,  13.055555555555554, 16.}, {}}, {{0., 0., 0., 0., 2., 4., 4., 4., 4.}}, {0}, MachinePrecision, "Unevaluated"] 

yields at least a BSpline function description, but intf2 hides it’s Hermite Spline implementation from the user

InputForm[intf2]  Out[2]/InputForm=  InterpolatingFunction[{{0, 4}}, {5, 3, 0, {5}, {4}, 0, 0, 0, 0, Automatic, {}, {}, False}, {{0, 1, 2, 3, 4}}, {{21}, {24}, {24}, {18}, {16}}, {Automatic}] `

Does anyone know how to get more information about the parameters resulting from Hermite Spline interpolation of order 3 in Mathematica?