How to solve this equation in mathematica? - solver

$$ \partial_{t}{v}+\frac{( v \cdot \nabla) \cdot v }{ A}=-\frac{\nabla{p_{1}}}{ \rho_{0} A}-\frac{e\rho(v \times B)}{\rho A}--e(v \cdot \Omega)(B\cdot \nabla)v+(\frac{\nabla{p_{1}}}{\rho_{0}} \times \Omega) \cdot \nabla{v}-\frac{eE}{A}+-e^{2}(E \cdot B)\Omega-(E \times \Omega) \cdot \nabla{v} $$
I have to find $p_{1}$ from this equation above where $v=-\nabla{\phi}$.How to do it in Mathematica? Here dot means diff w.r.t 't'
I tried to find out $p_{1}$ writing in Mathematica .It's showing errors

Related

Please break this equation into 4x4 $$f^{\mu}{\nu} $$ matrix form

$$ \partial_{t}((\frac{\rho}{c_{s0}^{2}}(\partial_{t}\psi_{1}+\vec{v_{0}}\cdot \nabla{\psi_{1}}))-(\vec{\nabla }\cdot (\rho_{0}\vec{\nabla}{\psi_{1}}))-(\vec{\nabla} \cdot (\frac{\rho}{c_{s0}^{2}}(\partial_{t}{\psi_{1}}-\vec{v_{0}} \cdot \vec{\nabla}{\psi_{1}}))) $$
I have to find matrix $$f^{\mu}{\nu}$$ which satisfies
$$ (\partial_{\mu}f^{\mu \nu}\partial_{\nu}=0$$
I tried to form $$f^{\mu}{\nu}$$ into 4x4 matrix from this equation: $$\partial_{t}((\frac{\rho}{c_{s0}^{2}}(\partial_{t}\psi_{1}+\vec{v_{0}}\cdot \nabla{\psi_{1}}))-(\vec{\nabla }\cdot (\rho_{0}\vec{\nabla}{\psi_{1}}))-(\vec{\nabla} \cdot (\frac{\rho}{c_{s0}^{2}}(\partial_{t}{\psi_{1}}-\vec{v_{0}} \cdot \vec{\nabla}{\psi_{1}}))) $$
But I'm not able to get the matrix form of $$f^{\mu}{\nu}$$

Why is Mathematica producing a seemingly wrong answer for a derivative?

I'm puzzled by what I think is a mistake in a partial derivative I'm having Mathematica do for me.
Specifically, this is what I have:
Derivative I'd like to take
I'm trying to take the partial derivative of the following w.r.t. the variable θ (apologies for the formatting):
f=(1/4)(-4e((1+θ)/2)ψ+eN((1+θ)/2)ψ+eN((1+θ)/2-θd)ψ)-s
But the solution Mathematica produces seems very different from the one I get when I take the derivative myself. While Mathematica says the partial derivative of f w.r.t. θ is:
(1/4)eψ(N-2)
By hand, I get and am quite confident the correct answer is instead:
(1/4)eψ(N(1-d)-2)
That is, Mathematica is producing something that drops the variable d when it is differentiating. I've explored different functions that take a derivative in Mathematica, and the possibility that maybe some of the variables I'm using (such as d) might be protected or otherwise special, but I can't say that I know why the answer's so off. This is the first time in the notebook that d appears, so it is not set to 0. For context, I'm trying to confirm that the derivative of the function is positive for values of the variables in certain ranges, and we have d>0 and d<(1/2). Doing this all by hand works but I'm trying to confirm with Mathematica as I will be dealing with more complicated functions and need to make sure I'm having Mathematica produce the right derivatives.
Your didn't add spaces in eN and θd, so it thinks they're some other 2-character variables.
Adding spaces between them gives your expected result:
f[θ,e,N,ψ,d,s] = (1/4) (-4 e ((1+θ)/2) ψ + e N ((1+θ)/2) ψ + e N ((1+θ)/2 - θ d) ψ) - s;
D[f[θ, e, N, ψ, d, s], θ] // FullSimplify
(* 1/4 e (-2 + N - d N) ψ *)

How Julia use Lufact to solve Ax=b

I want to recreate a solve function(solve Ax = b for x) for sparse matrix.
In the Julia documentation, it says that when we applied a sparse matrix to lufact(), it returns the following:
L, U, p, q, Rs = F[:(:)]
With the given formula in Julia doc: LU = Rs.*A[p,q], I did some algebra and obtained the following formula:
x = U \ ( L \ (Rs.*b[p]) )
ipermute!(x,q)
This formula matched with the default F\b solver in Julia when the matrix is dense but the result is off when the matrix is sparse. Does anyone know why?
using LinearAlgebra, then B = lu(A); B\b. Julia returns a type and its dispatch on \ handles the rest.

How to calculate expressions in better accuracy

I tried to calculate this:
limit(x^5−6*x^4+14*x^3−20*x^2+24*x−16, x, 1.999993580023622);
But I got 0.
I think it happens because a loss of significance. What can I do to get more precise result?
(1) use horner to rearrange the expression so that it can be evaluated more accurately.
(%i1) display2d : false;
(%o1) false
(%i2) horner (x^5-6*x^4+14*x^3-20*x^2+24*x-16, x);
(%o2) x*(x*(x*((x-6)*x+14)-20)+24)-16
(%i3) subst (x=1.999993580023622, %);
(%o3) -1.77635683940025E-15
(2) use bigfloat (variable precision) arithmetic.
(%i4) subst (x=1.999993580023622b0, x^5-6*x^4+14*x^3-20*x^2+24*x-16);
(%o4) -1.332267629550188b-15
(%i5) fpprec : 50 $
(%i6) fpprintprec : 8 $
(%i7) subst (x=1.999993580023622b0, x^5-6*x^4+14*x^3-20*x^2+24*x-16);
(%o7) -1.5876314b-15
(3) use rational arithmetic, which is exact.
(%i2) rat (1.999993580023622);
rat: replaced 1.999993580023622 by 38317775/19158949 = 1.999993580023622
(%o2) 38317775/19158949
(%i3) subst (x=38317775/19158949, x^5-6*x^4+14*x^3-20*x^2+24*x-16);
(%o3) -4098340979864306910009/2581418432543842245350194942774769749
(%i4) float (%);
(%o4) -1.587631407677531E-15

Blanks in the denominator of a replacement rule

Mathematica 7.0 seems to dislike having blanks in the denominator. Can anyone explain why this is?
Input:
ClearAll["Global`*"];
(*Without blanks:*)
a^2 / b^2 /. a^2 / b^2 -> d
(*with:*)
a^2 / b^2 /. a^c_ / b^c_ -> d
(*Without blanks:*)
a^2 / b^2 /. (a / b)^2 -> d
(*With:*)
a^2 / b^2 /. (a / b)^c_ -> d
(*Without blanks:*)
a^2 / b^2 /. a^2 * b^(-2) -> d
(*With:*)
a^2 / b^2 /. a^c_ * b^(-c_) -> d
Output:
d
a^2/b^2
d
a^2/b^2
d
a^2/b^2
I'm trying to work this for a more complicated problem. The substitution that I want to make is in an expression of the form:
(a ^ c_. * Coefficient1_. / b ^ c_. / Coefficient2_.) + (a ^ d_. * Coefficient3_. / b ^ d_. / Coefficient4_.)
Where the coefficients may involve sums, products, and quotients of variables that may or may not includea and b.
Possibly relevant:
The FullForm shows that the power in the denominator is stored as a product of -1 and c:
Input:
FullForm[a^2/b^2]
FullForm[a^c_/b^c_]
FullForm[ (a / b)^2 ]
FullForm[(a / b)^c_ ]
FullForm[a^2 * b^(-2) ]
FullForm[a^c_ * b^(-c_)]
Output:
Times[Power[a,2],Power[b,-2]]
Times[Power[a,Pattern[c,Blank[]]],Power[b,Times[-1,Pattern[c,Blank[]]]]]
Times[Power[a,2],Power[b,-2]]
Power[Times[a,Power[b,-1]],Pattern[c,Blank[]]]
Times[Power[a,2],Power[b,-2]]
Times[Power[a,Pattern[c,Blank[]]],Power[b,Times[-1,Pattern[c,Blank[]]]]]
Edit: Bolded change to my actual case.
Generally speaking you should try to avoid doing mathematical manipulation using ReplaceAll which is a structural tool.
As opposed to FullForm, I will use TreeForm to illustrate these expressions:
a^2/b^2 // TreeForm
a^c_/b^c_ // TreeForm
You can see that while these expressions are mathematically similar, they are structurally quite different. You may be able to hammer out a functioning replacement rule for a specific case, but you will usually be better off using the Formula Manipulation (or Polynomial Algebra) tools that Mathematica provides.
If you carefully describe the mathematical manipulation you wish to achieve, I will attempt to provide a better solution.
As belisarius humorously points out in a comment, trying to force Mathematica to "see" or display expressions the way you do is often largely futile. This is one of the reasons that the opening statement above is true.
I agree with everything Mr.Wizard wrote. Having said that, a replacement rule that would work in this specific case would be:
a^2/b^2 /. (Times[Power[a,c_],Power[b,e_]]/; e == -c )-> d
or
a^2/b^2 /. (a^c_ b^e_/; e == -c )-> d
Note that I added the constraint /; e == -c so that I effectively have a -c_ without actually creating the corresponding Times[-1,c_] expression
The primary reason a^2 / b^2 /. a^c_ / b^c_ -> d doesn't work is your using Rule (->) not RuleDelayed (:>). The second reason, as you found with FullForm, is that a/b is interpreted as Times[a, Power[b,-1]], so it is best to not use division. Making these changes,
a^2 / b^2 /. a^n_ b^m_ :> {n,m}
returns {2, -2}. Usually, you'll want to have a default value, so that
a / b^2 /. a^n_. b^m_. :> {n,m}
returns {1,-2}.
Edit: to ensure that the two exponents are equal, requires the addition of the Condition (/;)
a^2 / b^2 /. a^n_. b^m_. /; n == m :> n
Note: by using _. this will also catch a/b.

Resources