# Solve PDE using ansatz

I'm trying to find u(x,y) such that

$$0=-axy+bx^2-cyu_y-du_x^2$$

using the ansatz

$$u(x,y)=1/2\begin{pmatrix}x \\ y\end{pmatrix}^T\begin{pmatrix} A_{11} & A_{12} \\ A_{12} & A_{22} \end{pmatrix}\begin{pmatrix}x \\ y\end{pmatrix}.$$

In fact, this is only a simple example of what I am trying to do, which I can solve by hand:

$$A_{11} = \sqrt{b/d}\\ A_{12} = \frac{-a}{c+2\sqrt{bd}}\\ A_{22} = -\frac{d}{c}\frac{a^2}{(c+2\sqrt{b/d})^2}$$

I could not reproduce this solution in Mathematica. I am very new to Mathematica and I feel like the little bit I could do is already awfully inelegant:

u[x_,y_]:=1/2*{x,y}.({{A11,A12},{A12,A22}}.{x,y})
dx=D[u[x,y],x]
dy=D[u[x,y],y]
equation=-Pi*x*y+x^2-y*dy-dx^2==0
SolveAlways[equation,{x,y}]


As you can see, I replaced a by \pi and set b=c=d=1. These commands returns the solution I am after in that special case (as well as a solution with A_{11}<0, which I'd like to exclude, but that's a different story). If I replace \pi by a and use SolveAlways[equation,{x,y,a}] Mathematica returns an empty set.

Things I'd like to be able to do:

1. Define the PDE for general functions, and then insert the specific ansatz, instead of using the specific ansatz directly.
2. Solve for the coefficients of the quadratic ansatz, in the case of general coefficients a,b,c,d.
3. Define general quadratic ansatz functions in higher dimensions, without typing out the symmetric matrix by hand
4. Learn about any other things to make my code more idiomatic.

PS: Could anyone tell me why the site keeps telling me that "Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button..."? The only way I was able to post this question was to replace all math blocks by code blocks. I apologize for that. Before this replacement the question was displayed properly in the preview window, except that line breaks in math mode were ignored.

• Re “contains code”: I can only assume the parser-detector is imperfect. I’ve had that problem too. Some years ago SE made it check more “rigorously” but it still gets things wrong in both directions – Michael E2 Jul 26 '19 at 14:02
• @MichaelE2 I posted this on mathematica.meta.stackexchange.com/questions/2508/… – Bananach Jul 26 '19 at 14:07
• Actually there’s probably an old Meta post about the change – Michael E2 Jul 26 '19 at 14:10

If you collect your equation

equation = -Pi*x*y + x^2 - y*dy - dx^2 == 0 // Collect[#, {x, y}, Simplify] &
(*(1 - A11^2) x^2 + (-(1 + 2 A11) A12 - \[Pi]) x y + (-A12^2 -A22) y^2 == 0*)


The equation must be fullfilled for arbitrary x,y

eqn = Map[Coefficient[equation /. Equal -> Subtract, #] &, {x^2, x y, y ^2}]
(*{1 - A11^2, -(1 + 2 A11) A12 - \[Pi], -A12^2 - A22}*)

sol=Solve[eqn == 0, {A11, A12, A22}]
(*{{A11 -> 1, A12 -> -(\[Pi]/3), A22 -> -(\[Pi]^2/9)}, {A11 -> -1,A12 -> \[Pi], A22 -> -\[Pi]^2}}*)


sol contains two sets of parameters!

general equation

The general case can be solved similarily

equation=a*x*y + b x^2 - c y*dy - d dx^2 == 0 //Collect[#, {x, y}, Simplify] &
eqn = Map[Coefficient[equation /. Equal -> Subtract, #] &, {x^2, x y, y ^2}]
Solve[eqn == 0, {A11, A12, A22}] // Simplify
(*{{A11 -> -(Sqrt[b]/Sqrt[d]), A12 -> a/(c - 2 Sqrt[b] Sqrt[d]),A22 -> -((a^2 d)/(c (c - 2 Sqrt[b] Sqrt[d])^2))},
{A11 -> Sqrt[b]/Sqrt[d], A12 -> a/(c + 2 Sqrt[b] Sqrt[d]),A22 -> -((a^2 d)/(c (c + 2 Sqrt[b] Sqrt[d])^2))}}*)

• How is this different from what I already have? – Bananach Jul 26 '19 at 9:14
• I don't know "what you already have" because I only understand the Mathematica part of your code! – Ulrich Neumann Jul 26 '19 at 9:36
• It returns the same answer you got. Does it not work for you? – Bananach Jul 26 '19 at 9:56
• Great! Could you explain what Collect and Equal->Subtract do? Also, how would you do this for a 4 dimensional PDE? It feels inelegant to type out all the matrix coefficients everywhere – Bananach Jul 26 '19 at 15:04
• Collect collects terms of x,y. With /.Equal->Subtract the equation lhs==rhs is changed to the expression lhs-rhs. – Ulrich Neumann Jul 26 '19 at 15:17