equation solving – Perform Eliminate, Solve and Format at once in Mathematica

I have a series of equations where I use eliminate to get a new equation with desired variables. This equation is then solved for a specific variable and then the result is presented in a specific InputForm. Here is how I am doing it currently.

Eliminate({f == x^2 + y^2, a == x + y, b == x y}, {x, y})
2 b + f == a^2

Solve(2 b + f == a^2, b)
{{b -> 1/2 (a^2 - f)}}

Format(b, InputForm) /. {b -> 1/2 (a^2 - f)}
(a^2 - f)/2

So, I copy the result from Eliminate and then solve it for my variable and then copy the output and format it later.

Is there an easier way to do all this? For example, can I somehow specify in a single step the variables I want to eliminate and the variable for which I want to solve the equation and then get the output in InputForm?

I will probably have to do this for a lot of equations. So, copying each output, again and again, gets very tiresome.

Thanks.

Matrix with ConjugateTranspose – Mathematica Stack Exchange

I am trying to calculate the matrix multiplication and then take its conjugate transpose. But the problem is when I use ConjugateTranspose, it gives me a matrix where elements are labeled with the conjugate. Here are the matrices:

And here is what I am trying to calculate:

  Subscript[U, 23 ] = {{1, 0, 0, 0}, {0, Subscript[c, 23], Subscript[s, 
   23], 0}, {0, -Subscript[s, 23], Subscript[c, 23], 0}, {0, 0, 0, 
   1}}

Subscript[U, 13] = {{Subscript[c, 13], 0, 
   Exp[-I*Subscript[z, 13]]*Subscript[s, 13], 0}, {0, 1, 0, 
   0}, {-Exp[I*Subscript[z, 13]]*Subscript[s, 13], 0, Subscript[c, 
   13], 0}, {0, 0, 0, 1}}


Subscript[U, 12 ] = {{Subscript[c, 12], Subscript[s, 12], 0, 
   0}, {-Subscript[s, 12], Subscript[c, 12], 0, 0}, {0, 0, 1, 0}, {0, 
   0, 0, 1}}


Subscript[U, 34 ] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, Subscript[c, 
   34], Exp[-I*Subscript[z, 34]]*Subscript[s, 34]}, {0, 
   0, -Exp[I*Subscript[z, 34]]*Subscript[s, 34], Subscript[c, 34]}}


Subscript[U, 24 ] = {{1, 0, 0, 0}, {0, Subscript[c, 24], 0, Subscript[
   s, 24]}, {0, 0, 1, 0}, {0, -Subscript[s, 24], 0, Subscript[c, 
   24]}}


Subscript[U, 14 ] = {{Subscript[c, 14], 0, 0, 
   Exp[-I*Subscript[z, 14]]*Subscript[s, 14]}, {0, 1, 0, 0}, {0, 0, 1,
    0}, {-Exp[I*Subscript[z, 14]]*Subscript[s, 14], 0, 0, Subscript[c,
    14]}}

MatrixForm[Subscript[U, 34]]
MatrixForm[Subscript[U, 24]]
MatrixForm[Subscript[U, 14]]
MatrixForm[Subscript[U, 23]]
MatrixForm[Subscript[U, 13]]
MatrixForm[Subscript[U, 12]]


mp = Assuming[{Subscript[z, 14], Subscript[z, 34], Subscript[c, 24], 
    Subscript[c, 34], Subscript[c, 14], Subscript[s, 24], Subscript[s,
     34], Subscript[s, 14], Subscript[c, 23], Subscript[c, 12], 
    Subscript[c, 13], Subscript[s, 23], Subscript[s, 13], Subscript[s,
     12], Subscript[z, 13]} [Element] Reals, 
  Simplify@ConjugateTranspose[
    Subscript[U, 34].Subscript[U, 24].Subscript[U, 14].Subscript[U, 
     23].Subscript[U, 13].Subscript[U, 12]]]

How can I get rid of this Conjugate term?

Why mathematica is returning error when computing correlation under Compile?

Can someone tell me what am I doing wrong here ..

corr = Compile[{{array1, _Real, 1}, {array2, _Real, 1}}, 
  Block[{corr}, corr = Correlation[array1, array2]; corr],CompilationTarget :> "C"]
lm = Compile[{{array1, _Real, 1}, {array2, _Real, 1}}, 
  Block[{mdlfit, x}, 
mdlfit = LinearModelFit[Join[Transpose[{array1}], Transpose[{array2}], 2], x, x]; 
{mdlfit, Sqrt[mdlfit["RSquared"]]}],CompilationTarget :> "C"]
array1 = N[{2, 4, 5, 6, 3, 7, 5, 8, 9, 6, 4, 7, 6, 5}];
array2 = N[{5, 7, 8, 6, 3, 1, 2, 4, 5, 8, 7, 2, 3, 4}];
corr[array1, array2] // AbsoluteTiming
lm[array1, array2] // AbsoluteTiming

Output

CompiledFunction::cfte: Compiled expression -0.218004439363 should be a rank 1 tensor of machine-size real numbers. >>

CompiledFunction::cfexe: Could not complete external evaluation; proceeding with uncompiled evaluation. >>

{0.049968, -0.218004439363}

{0.001135, {FittedModel[6.09022556391 -0.263157894737 x], 0.218004439363}}

why computation of Correlation is giving error while LinearModelFit is working?
If I get rid of ‘CompilationTarget’ from LinearModelFit then it’s also giving error

CompiledFunction::cfex: Could not complete external evaluation at instruction 6; proceeding with uncompiled evaluation. >>

```

When to trust the result of numerical integration in mathematica?

How does one know when to trust the result of numerical integration, using NIntegrate, for higher (5 or 6) dimensional integrals in mathematica? For example, I get the following result

In(3):= Integrate( Exp(-a^2 - b^2 - c^2 - x^2 - y^2 - z^2), {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, {a, -2, 2}, {b, -2, 2}, {c, -2, 2}) // N

Out(3)= 30.1462

whereas if I do the same integral numerically, I get

In(2):= NIntegrate( Exp(-a^2 - b^2 - c^2 - x^2 - y^2 - z^2), {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, {a, -2, 2}, {b, -2, 2}, {c, -2, 2}) // N


During evaluation of In(2):= NIntegrate::eincr: The global error of the strategy
 GlobalAdaptive has increased more than 2000 times. The global error is expected
 to decrease monotonically after a number of integrand evaluations. Suspect one
 of the following: the working precision is insufficient for the specified 
precision goal; the integrand is highly oscillatory or it is not a (piecewise) 
smooth function; or the true value of the integral is 0. Increasing the value of
 the GlobalAdaptive option MaxErrorIncreases might lead to a convergent 
numerical integration. NIntegrate obtained 30.14615590437465 and 
0.0001823733624988688 for the integral and error estimates.

Out(2)= 30.1462

This seems to indicate that one can trust the result of NIntegrate if the error estimate is a small percentage (~1%) of the estimated value of the integral. If that’s true then how would one understand the discrepancy in the values obtained in the following two cases when Mathematica does not report any errors.

In(102):= NIntegrate( Exp(-a^2 - b^2 - c^2 - x^2 + y^2), {x, -2, 2}, {y, -2, 2}, {a, -2, 2}, {b, -2, 2}, {c, -2, 2}, Method -> "AdaptiveMonteCarlo") // N

Out(102)= 298.918

In(104):= NIntegrate( Exp(-a^2 - b^2 - c^2 - x^2 + y^2), {x, -2, 2}, {y, -2, 2}, {a, -2, 2}, {b, -2, 2}, {c, -2, 2}, Method -> "MonteCarloRule") // N

Out(104)= 313.592

In the following example, I also get a result with an error bar that is a small (~2%) percentage of the value of the integral

In(7):= NIntegrate( Exp(-a^2 - b^2 - c^2 - x^2 - y^2 - z^2), {x, -5, 5}, {y, -5, 5}, {z, -5, 5}, {a, -5, 5}, {b, -5, 5}, {c, -5, 5}, Method -> "AdaptiveQuasiMonteCarlo") // N

During evaluation of In(7):= NIntegrate::maxp: The integral failed to converge 
after 1000100 integrand evaluations. NIntegrate obtained 0.29686054547957375 and 
0.005304460608762476 for the integral and error estimates.

Out(7)= 0.296861

but comparing it with the following analytic evaluation of the integral


Out(8)= 31.0063

we see that the answer is way off. Any guidance in this regard will be greatly appreciated.

export – How to run a whole mathematica notebook within a for loop?

I have some data sets labelled data1.dat, data2.dat, and so on, and I have written a notebook which will import a single .dat file, generate a plot, and export that into a .pdf.

For example, to generate the plot graph5.pdf from data5.dat, my code is like this.

index = ToString(5);
importfilename = StringJoin("/path/to/data/data", index, ".dat");
exportfilename = StringJoin("/path/to/graphs/graph", index, ".pdf");
(** Lines to generate the plot from importfilename **)
Export(exportfilename, %)

Is there any way to write index = ToString(k) and perform a for loop over the variable k so that the whole Notebook will be evaluated for every k?

I know that something similar is possible in MATLAB/Octave, where I can write my script to a file, and then call that script file inside a for loop for different values of k.

Why does Mathematica try to take the first element of the empty list when plotting?

I’m using Mathematica 12.1.1.0.

When I enter

ListPlot[{{{1500, 4}, {1510, 4}, {1520, 4}, {1850, 6}}, {{1500, 
    1}, {18100, 1}}}, PlotLabels -> {"a", "b"}, Joined -> True, 
 PlotRange -> Full]

I get

Part::partw: Part 1 of {} does not exist.
Callout::copos: {19382.2,0.733053} is not a valid position for the placement of callouts.

enter image description here

What’s going on here? How do I avoid getting these error messages?

Edit: I’ve also emailed Wolfram Support about this, with ID [CASE:4760915]. I’ll post any updates they give. I’m still interested in a workaround.

calculus and analysis – How to force mathematica to take derivatives in a specific way?

I am attempting to take some derivatives of some Lagrange planetary equations. In this I have two types of anomaly which have derivatives that are found geometrically. I’m trying to force mathematica to use the results of these derivatives. I realize that to do this I have defined the derivatives. To get Mathematica to be happy I unprotect D before doing so. Heres my code for that:

Unprotect(D);

D(f, e) := (a/r + ((Mu)*a)/(((Mu)*a)^(1/2)*(1 - e^2)^(1/2))^2)*Sin(f)

Unprotect(D);

D(f, M) := (1 + e*Cos(f))^2/(1 - e^2)^(3/2)

Okay so this is all well. When I evaluate D(f,M) or D(f,e) it seems to work correctly; however when I take the derivatives of other functions derivatives don’t follow those rules I set above. For example, I made up a simple function to check this:

In(58):= abc (a, e, i, f, c) := e*f*Sin(f)

In(59):= D(abc(a, e, i, f, c), e)

Out(59)= f Sin(f)

Uh oh. So my question is how do I get mathematica to match the derivatives I want?

Thanks for all your help

differential equations – ParametricNDSolveValue::ndsz – Mathematica Stack Exchange

How to overcome this error?

ode = D(theta(x), {x, 2}) == SH*theta(x)^2
sol = ParametricNDSolveValue({ode, theta(0) == 1, theta'(1) == 0}, theta(x), {x, 0, 1}, {SH})
Ns(SH_, Tr_) := SH*sol(SH)*(Log(1 + Tr*sol(SH)) - sol(SH)/(sol(SH) + (Tr - 1)^(-1)))
Plot(Ns(10, 1.2), {x, 0, 1})

ParametricNDSolveValue::ndsz: At x$2319 == 0.940612307913943`, step size is effectively zero; singularity or stiff system suspected.