Why doesn't backsubstituting the result of Solve[] give the expected result? - matrix

I have this matrix
a = {{2, -2, -4}, {-2, 5, -2}, {-4, -2, 2}}
I then solved an equation with one missing entry. The equation is of the form
Inverse[p].a.p == q
where p is the 3x3 matrix with the missing entry (x5) and q is a given 3x3 matrix.
Solve[Inverse[( {
{1/Sqrt[5], 4/(3 Sqrt[5]), -2/3},
{-2/Sqrt[5], 2/(3 Sqrt[5]), -2/6},
{0, x5, -2/3}
} )].a.( {
{1/Sqrt[5], 4/(3 Sqrt[5]), -2/3},
{-2/Sqrt[5], 2/(3 Sqrt[5]), -2/6},
{0, x5, -2/3}
} ) == ( {
{6, 0, 0},
{0, 6, 0},
{0, 0, -3}
} )]
Mathematica can solve this easily and I get x5 -> -(Sqrt[5]/3) as the result.
However if I check it, the result ist very weird:
In[2]:= Inverse[( {
{1/Sqrt[5], 4/(3 Sqrt[5]), -2/3},
{-2/Sqrt[5], 2/(3 Sqrt[5]), -2/6},
{0, -Sqrt[5]/3, -2/3}
} )].a.( {
{1/Sqrt[5], 4/(3 Sqrt[5]), -2/3},
{-2/Sqrt[5], 2/(3 Sqrt[5]), -2/6},
{0, -Sqrt[5]/3, -2/3}
} )
Out[2]= {{6/5 - (2 (-(2/Sqrt[5]) - 2 Sqrt[5]))/Sqrt[5],
8/5 + (2 (-(2/Sqrt[5]) - 2 Sqrt[5]))/(3 Sqrt[5]), -(4/Sqrt[5]) +
1/3 (2/Sqrt[5] + 2 Sqrt[5])}, {-((
2 (-(8/(3 Sqrt[5])) + (4 Sqrt[5])/3))/Sqrt[5]) + (
4/(3 Sqrt[5]) + (4 Sqrt[5])/3)/Sqrt[5],
10/3 + (2 (-(8/(3 Sqrt[5])) + (4 Sqrt[5])/3))/(3 Sqrt[5]) + (
4 (4/(3 Sqrt[5]) + (4 Sqrt[5])/3))/(3 Sqrt[5]), (4 Sqrt[5])/3 +
1/3 (8/(3 Sqrt[5]) - (4 Sqrt[5])/3) -
2/3 (4/(3 Sqrt[5]) + (4 Sqrt[5])/3)}, {0, 0, -3}}
the expected result should be
( {
{6, 0, 0},
{0, 6, 0},
{0, 0, -3}
} )
like in the equation. If I calculate this by hand I get this result. What am I missing here?

Just Simplify or Expand the results.
Here is an example:
In[1]:= a = {{2, -2, -4}, {-2, 5, -2}, {-4, -2, 2}}
Out[1]= {{2, -2, -4}, {-2, 5, -2}, {-4, -2, 2}}
In[2]:= p = {{1/Sqrt[5], 4/(3 Sqrt[5]), -(2/3)}, {-(2/Sqrt[5]), 2/(
3 Sqrt[5]), -(2/6)}, {0, x5, -(2/3)}}
Out[2]= {{1/Sqrt[5], 4/(3 Sqrt[5]), -(2/3)}, {-(2/Sqrt[5]), 2/(
3 Sqrt[5]), -(1/3)}, {0, x5, -(2/3)}}
In[3]:= sol =
Solve[Inverse[p].a.p == {{6, 0, 0}, {0, 6, 0}, {0, 0, -3}}]
Out[3]= {{x5 -> -(Sqrt[5]/3)}}
In[4]:= Inverse[p].a.p /. sol[[1]]
Out[4]= <big output removed>
In[5]:= Simplify[%]
Out[5]= {{6, 0, 0}, {0, 6, 0}, {0, 0, -3}}
Expand would work too in place of Simplify. Expressions in terms of roots and fractions can often be written in several ways, and it's not immediately obvious if two expression are equivalent just by looking at them. You have to explicitly ask Mathematica to transform them, for example expr = 13/(2 Sqrt[3]) - 4/3 and Together[expr].
What is quite strange though, is that Solve does not work if you use the standard syntax and give variables explicitly:
In[6]:= Solve[Inverse[p].a.p == {{6, 0, 0}, {0, 6, 0}, {0, 0, -3}}, x5]
Out[6]= {}
In[7]:= Solve[
Inverse[p].a.p == {{6, 0, 0}, {0, 6, 0}, {0, 0, -3}}, x5,
VerifySolutions -> False]
Out[7]= {}
Can anyone explain why? NSolve works as expected.
In[8]:= NSolve[
Inverse[p].a.p == {{6, 0, 0}, {0, 6, 0}, {0, 0, -3}}, x5]
Out[8]= {{x5 -> -0.745356}}

Remove["Global`*"];
a = {{2, -2, -4}, {-2, 5, -2}, {-4, -2, 2}};
p = {{1/Sqrt[5], 4/(3 Sqrt[5]), -2/3}, {-2/Sqrt[5],
2/(3 Sqrt[5]), -2/6}, {0, x, -2/3}};
pInv = Inverse[p];
lhs = pInv.a.p;
q = {6, 6, -3};
eqs = N#Expand#
Map[Total[lhs[[#, All]]] - q[[#]] == 0 &, Range[Length[q]]]
Here are the 3 equations all in x. (3 equations, ONE unknown!)
-6. - 2.66667/(-0.444444 + 0.745356 x) + (4.47214 x)/(-0.444444 + 0.745356 x) ==
0.,
-6. - 2.66667/(-0.444444 + 0.745356 x) + (4.47214 x)/(-0.444444 + 0.745356 x) == 0.,
3. - 0.654283/(-0.444444 + 0.745356 x) -(1.5694 x)/(-0.444444 + 0.745356 x) + (
4.47214 x^2)/(-0.444444 + 0.745356 x) == 0.
first solve numerically
Map[NSolve[eqs[[#]],x]&,Range[3]]
Out[465]= {{{x->0.}},{{x->0.}},{{x->-0.745356}}}
To get Solve to accept x, First do not do Numerical, leave it symbolic:
eqs = Expand# Map[Total[lhs[[#, All]]] - q[[#]] == 0 &, Range[Length[q]]]
which gives
{-6 - 8/(3 (-(4/9) + (Sqrt[5] x)/3)) + (2 Sqrt[5] x)/(-(4/9) + (Sqrt[5] x)/3) ==
0,
-6 - 8/(3 (-(4/9) + (Sqrt[5] x)/3)) + (2 Sqrt[5] x)/(-(4/9) + (Sqrt[5] x)/3) == 0,
3 + 4/(3 (-(4/9) + (Sqrt[5] x)/3)) - (8 Sqrt[5])/(9 (-(4/9) + (Sqrt[5] x)/3))
+ (2 x)/(3 (-(4/9) + (Sqrt[5] x)/3)) - (
Sqrt[5] x)/(-(4/9) + (Sqrt[5] x)/3) + (
2 Sqrt[5] x^2)/(-(4/9) + (Sqrt[5] x)/3) == 0}
Now use Solve, with explicit x in there, now it is ok
Map[Solve[eqs[[#]], x] &, Range[3]]
{{{}}, {{}}, {{x -> -(Sqrt[5]/3)}}}
--Nasser

Related

NDSolve accuracy

We're asking NDSolve to solve x'' + x == 0 to 20 digits, but when we compare to the true solution, we only see 9 correct digits. Are we not using NDSolve correctly?
Clear["Global`*"]
sol = NDSolve[{x'[t] == v[t], v'[t] == -x[t], x[0] == 1, v[0] == 0}, {x, v},
{t, 0, 100},
PrecisionGoal -> 20, AccuracyGoal -> 20, WorkingPrecision -> 40, MaxSteps -> 10^6];
Plot[(x[t] - Cos[t]) /. sol[[1]], {t, 0, 100}]

How to shade a plot in Mathematica

I want to generate a plot like the following
I am not sure how to generate a shading even though I can get the frame done. I'd like to know the general approach to shade certain areas in a plot in Mathematica. Please help. Thank you.
Perhaps you are looking for RegionPlot?
RegionPlot[(-1 + x)^2 + (-1 + y)^2 < 1 &&
x^2 + (-1 + y)^2 < 1 && (-1 + x)^2 + y^2 < 1 && x^2 + y^2 < 1,
{x, 0, 1}, {y, 0, 1}]
Note the use of op_ in the following (only one set of equations for the curves and the intersection!):
t[op_] :=Reduce[op[(x - #[[1]])^2 + (y - #[[2]])^2, 1], y] & /# Tuples[{0, 1}, 2]
tx = Texture[Binarize#RandomImage[NormalDistribution[1, .005], 1000 {1, 1}]];
Show[{
Plot[y /. ToRules /# #, {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}}] &# t[Equal],
RegionPlot[And ## #, {x, 0, 1}, {y, 0, 1}, PlotStyle -> tx] &# t[Less]},
Frame->True,AspectRatio->1,FrameStyle->Directive[Blue, Thick],FrameTicks->None]
If, for any particular reason, you want the dotted effect in your picture, you can achieve this like so:
pts = RandomReal[{0, 1}, {10000, 2}];
pts = Select[pts,
And ## Table[Norm[# - p] < 1, {p,
{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}] &];
Graphics[{Thick,
Line[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}],
Circle[{0, 0}, 1, {0, Pi/2}],
Circle[{1, 0}, 1, {Pi/2, Pi}],
Circle[{1, 1}, 1, {Pi, 3 Pi/2}],
Circle[{0, 1}, 1, {3 Pi/2, 2 Pi}],
PointSize[Small], Point[pts]
}]

Using Boole with MaxValue and or PlotRegion

Why doesn't this work?
I have bypassed this before but i can't remember how i did it, and I never went on to figure out why this type of inputs didn't work. About time to get to know it!
For those who cant see the pic:
RegionPlot3D[
x^2 + 2 y^2 - 2 z^2 = 1 && -1 <= z <= 1, {x, -5, 5}, {y, -5,
5}, {z, -1, 1}]
Set::write: "Tag Plus in -2.+25.+50. is Protected"
And then there is just an empty cube without my surface.
If z is limited by other surfaces you could go like this:
RegionPlot3D[
x^2 + 2 y^2 - 2 z^2 < 1 && z < x + 2 y && z^2 < .5,
{x, -2, 2}, {y, -2, 2}, {z, -1, 1},
PlotPoints -> 50, MeshFunctions -> {Function[{x, y, z}, z]},
PlotStyle -> Directive[Red, Opacity[0.8]]]
Or with ContourPlot:
ContourPlot3D[
x^2 + 2 y^2 - 2 z^2 == 1,
{x, -2, 2}, {y, -2, 2}, {z, -1, 1},
RegionFunction -> Function[{x, y, z}, z < x + 2 y && z^2 < .5],
PlotPoints -> 50, MeshFunctions -> {Function[{x, y, z}, z]},
ContourStyle -> Directive[Red, Opacity[0.8]]]]
Try this
RegionPlot3D[x^2 + 2 y^2 - 2 z^2 < 1,
{x, -5, 5}, {y, -5, 5}, {z, -1, 1}]
Or, if you just want the surface
ContourPlot3D[x^2 + 2 y^2 - 2 z^2 == 1,
{x, -5, 5}, {y, -5, 5}, {z, -1, 1}]
Note the double equals sign, rather than the single equals sign.

Techniques for Minimization over Integers

I have to minimize a bunch of functions of n variables that can take values from an integer range.
The functions have the general form:
f[{s1_,... sn_}]:= Kxy KroneckerDelta[sx,sy] + Kwz KroneckerDelta[sw,sz] +/- ..
Where the Kmn are also integers.
As an example,
f[{s1_, s2_, s3_, s4_, s5_}:= KroneckerDelta[s1, s2] - KroneckerDelta[s1, s4] +
KroneckerDelta[s1, s5] + KroneckerDelta[s3, s4] +
KroneckerDelta[s3, s5] + KroneckerDelta[s4, s5];
Where the si_ must be in Range[3].
I can bruteforce easily, for example:
rulez = Table[s[i] -> #[[i]], {i, 5}] & /# Tuples[Range[3], 5];
k1 = f[Table[s[i], {i, 5}]] /. rulez;
{Min[k1], Tuples[Range[3], 5][[#]] & /# Position[k1, Min[k1]]}
(*
->
{-1,{{{1, 2, 2, 1, 3}}, {{1, 2, 3, 1, 2}}, {{1, 3, 2, 1, 3}}, {{1, 3, 3, 1, 2}},
{{2, 1, 1, 2, 3}}, {{2, 1, 3, 2, 1}}, {{2, 3, 1, 2, 3}}, {{2, 3, 3, 2, 1}},
{{3, 1, 1, 3, 2}}, {{3, 1, 2, 3, 1}}, {{3, 2, 1, 3, 2}}, {{3, 2, 2, 3, 1}}}}
*)
Obviously, that seems to take forever for large sets of variables and larger value ranges.
I tried Minimize[ ], but get results that don't satisfy the conditions (!):
Minimize[{f[Table[s[i], {i, 5}]], And ## Table[1 <= s[i] <= 3, {i, 5}]},
Table[s[i], {i, 5}], Integers]
(*
-> {2, {s[1] -> 0, s[2] -> 0, s[3] -> 0, s[4] -> 0, s[5] -> 0}}
*)
Or in other cases, it just fails:
g[{s1_, s2_, s3_, s4_, s5_}]:= KroneckerDelta[s1, s3] - KroneckerDelta[s1, s4] +
KroneckerDelta[s1, s5] + KroneckerDelta[s3, s4] +
KroneckerDelta[s3, s5] + KroneckerDelta[s4, s5];
Minimize[{g[Table[s[i], {i, 5}]], And ## Table[1 <= s[i] <= 3, {i, 5}]},
Table[s[i], {i, 5}], Integers]
(*
->
During evaluation of In[168]:= Minimize::infeas: There are no values of
{s[1],s[2],s[3],s[4],s[5]} for which the constraints 1<=s[1]<=3&&1<=s[2]<=3&&
1<=s[3]<=3&&1<=s[4]<=3&&1<=s[5]<=3 are satisfied and the objective function
KroneckerDelta[s[1],s[3]]-KroneckerDelta[s[1],s[4]]+KroneckerDelta[s[1],s[5]]+
KroneckerDelta[s[3],s[4]]+KroneckerDelta[s[3],s[5]]+KroneckerDelta[s[4],s[5]]
is real valued. >>
Out[169]= {\[Infinity], s[1]->Indeterminate, s[2]->Indeterminate,
s[3]->Indeterminate, s[4]->Indeterminate,
s[5]->Indeterminate}}
*)
So the question is twofold:
Why does Minimize[ ] fail?, and What is the better way to tackle this kind of problems with mathematica?
Edit
Just to emphasize, the first question is:
Why does Minimize[ ] fail?
Not that the other part is less important, but I am trying to learn when to invest my time in lurking with Minimize[ ], and when I shouldn't.
The problem seems to be related to the KroneckerDelta. If I define a function that is equivalent as long as integers are input it works (or at least it looks like it):
In[177]:= kd[x_, y_] := Round[10^-(x - y)^2]
In[179]:=
g[{s1_, s2_, s3_, s4_, s5_}] :=
kd[s1, s3] - kd[s1, s4] + kd[s1, s5] + kd[s3, s4] + kd[s3, s5] +
kd[s4, s5];
Minimize[{g[{s1, s2, s3, s4, s5}],
And ## Map[1 <= # <= 3 &, {s1, s2, s3, s4, s5}]}, {s1, s2, s3, s4,
s5}, Integers]
Out[180]= {-1, {s1 -> 1, s2 -> 1, s3 -> 2, s4 -> 1, s5 -> 3}}
You can set it up as an integer linear programming problem, and send it to Minimize in that form. I show one way to do this below. The Kronecker deltas are now just integer variables constrained between 0 and 1, with certain relations that force k[i,j] to be 1 when s[i]==s[j] and zero otherwise (this uses the coefficient signs and the max coefficient value).
I show the full set of constraints below, along with the expression we'll minimize.
highval = 3;
list = {{1, 2}, {1, 4}, {1, 5}, {3, 4}, {3, 5}, {4, 5}};
coeffs = {1, -1, 1, 1, 1, 1};
v1list = Apply[k, list, 1];
expr = coeffs.v1list
v2list = Map[s, Range[5]];
allvars = Flatten[{v1list, v2list}];
c1 = Map[0 <= # <= 1 &, v1list];
c2 = Map[1 <= # <= highval &, v2list];
c3 = Map[# <= 0 &,
Sign[coeffs]*
Map[{highval*(# - 1) - (s[#[[1]]] - s[#[[2]]]),
highval*(# - 1) - (s[#[[2]]] - s[#[[1]]])} &, v1list], {2}];
c4 = Element[allvars, Integers];
constraints = Flatten[{c1, c2, c3}]
k[1, 2] - k[1, 4] + k[1, 5] + k[3, 4] + k[3, 5] + k[4, 5]
{0 <= k[1, 2] <= 1, 0 <= k[1, 4] <= 1, 0 <= k[1, 5] <= 1, 0 <= k[3, 4] <= 1,
0 <= k[3, 5] <= 1, 0 <= k[4, 5] <= 1,
1 <= s[1] <= 3, 1 <= s[2] <= 3, 1 <= s[3] <= 3, 1 <= s[4] <= 3, 1 <= s[5] <= 3,
3*(-1 + k[1, 2]) - s[1] + s[2] <= 0, 3*(-1 + k[1, 2]) + s[1] - s[2] <= 0,
-3*(-1 + k[1, 4]) + s[1] - s[4] <= 0,-3*(-1 + k[1, 4]) - s[1] + s[4] <= 0,
3*(-1 + k[1, 5]) - s[1] + s[5] <= 0, 3*(-1 + k[1, 5]) + s[1] - s[5] <= 0,
3*(-1 + k[3, 4]) - s[3] + s[4] <= 0, 3*(-1 + k[3, 4]) + s[3] - s[4] <= 0,
3*(-1 + k[3, 5]) - s[3] + s[5] <= 0, 3*(-1 + k[3, 5]) + s[3] - s[5] <= 0,
3*(-1 + k[4, 5]) - s[4] + s[5] <= 0, 3*(-1 + k[4, 5]) + s[4] - s[5] <= 0}
Now just call Minimize, specifying Integers as domain.
Minimize[{expr, constraints}, allvars, Integers]
Out[235]= {-1, {k[1, 2] -> 0, k[1, 4] -> 1, k[1, 5] -> 0,
k[3, 4] -> 0, k[3, 5] -> 0, k[4, 5] -> 0,
s[1] -> 2, s[2] -> 2, s[3] -> 2, s[4] -> 2, s[5] -> 2}}
Daniel Lichtblau
Wolfram Research

in mathematica, how to make initial condition as a variable in ndsolve?

i'd like to have something like this
w[w1_] :=
NDSolve[{y''[x] + y[x] == 2, y[0] == w1, y'[0] == 0}, y, {x, 0, 30}]
this seems like it works better but i think i'm missing smtn
w := NDSolve[{y''[x] + y[x] == 2, y[0] == w1, y'[0] == 0},
y, {x, 0, 30}]
w2 = Table[y[x] /. w, {w1, 0.0, 1.0, 0.5}]
because when i try to make a table, it doesn't work:
Table[Evaluate[y[x] /. w2], {x, 10, 30, 10}]
i get an error:
ReplaceAll::reps: {<<1>>[x]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
ps: is there a better place to ask questions like that? mathematica doesn't have supported forums and only has mathGroup e-mail list. it would be nice if stackoverflow would have more specific mathematica tags like simplify, ndsolve, plot manipulation
There are a lot of ways to do that. One is:
w[w1_] := NDSolve[{y''[x] + y[x] == 2,
y'[0] == 0}, y[0] == w1,
y[x], {x, 0, 30}];
Table[Table[{w1,x,y[x] /. w[w1]}, {w1, 0., 1.0, 0.5}]/. x -> u, {u, 10, 30, 10}]
Output:
{{{0., 10, {3.67814}}, {0.5, 10, {3.25861}}, {1.,10, {2.83907}}},
{{0., 20, {1.18384}}, {0.5, 20, {1.38788}}, {1.,20, {1.59192}}},
{{0., 30, {1.6915}}, {0.5, 30, {1.76862}}, {1.,30, {1.84575}}}}
I see you already chose an answer, but I want to toss this solution for families of linear equations up. Specifically, this is to model a slight variation on Lotka-Volterra.
(*Put everything in a module to scope x and y correctly.*)
Module[{x, y},
(*Build a function to wrap NDSolve, and pass it
the initial conditions and range.*)
soln[iCond_, tRange_, scenario_] :=
NDSolve[{
x'[t] == -scenario[[1]] x[t] + scenario[[2]] x[t]*y[t],
y'[t] == (scenario[[3]] - scenario[[4]]*y[t]) -
scenario[[5]] x[t]*y[t],
x[0] == iCond[[1]],
y[0] == iCond[[2]]
},
{x[t], y[t]},
{t, tRange[[1]], tRange[[2]]}
];
(*Build a plot generator*)
GeneratePlot[{iCond_, tRange_, scen_,
window_}] :=
(*Find a way to catch errors and perturb iCond*)
ParametricPlot[
Evaluate[{x[t], y[t]} /. soln[iCond, tRange, scen]],
{t, tRange[[1]], tRange[[2]]},
PlotRange -> window,
PlotStyle -> Thin, LabelStyle -> Medium
];
(*Call the plot generator with different starting conditions*)
graph[scenario_, tRange_, window_, points_] :=
{plots = {};
istep = (window[[1, 2]] - window[[1, 1]])/(points[[1]]+1);
jstep = (window[[2, 2]] - window[[2, 1]])/(points[[2]]+1);
Do[Do[
AppendTo[plots, {{i, j}, tRange, scenario, window}]
, {j, window[[2, 1]] + jstep, window[[2, 2]] - jstep, jstep}
], {i, window[[1, 1]] + istep, window[[1, 2]] - istep, istep}];
Map[GeneratePlot, plots]
}
]
]
We can then use Animate (or table, but animate is awesome)
tRange = {0, 4};
window = {{0, 8}, {0, 6}};
points = {5, 5}
Animate[Show[graph[{3, 1, 8, 2, 0.5},
{0, t}, window, points]], {t, 0.01, 5},
AnimationRunning -> False]

Resources