# Solving at zeroes of a matrix - code only works for simple equation

I have a matrix of zeroes (True) and non-zeroes (False). The matrix is generated from a table of values of two other variables.

I'm trying to observe how the solution for p3 changes with small 'additions' to the equation.

it only works for a simple equation (p1, p2, p3). For a more complex one below (Te, w, Pprobe), it doesn't!

Code working on simple equation

tabletry = (  Table[p1*p2, {p1, 0, 3}, {p2, 0, 5}]  ) // MatrixForm
tabletest = (  Map[ {# == 0} &, tabletry, {-1} ]  )  // MatrixForm
deltap = 10^-2;


eqn1[p1_?NumericQ, p2_?NumericQ, p3_] := 1/p3 - 5 p3 ==  p1 + p2

eqn2[p1_?NumericQ, p2_?NumericQ, p3_] := 1/p3 - 5 p3 + deltap == p1 + p2

diff[p1_?NumericQ,
p2_?NumericQ] := (p3 /. FindRoot[eqn2[p1, p2, p3], {p3, 1}]) - (p3 /. FindRoot[eqn1[p1, p2, p3], {p3, 1}])

(Array[# #2 /. {(0) -> Quiet@Style[Evaluate[diff[#, #2]], Red], _ :> 0} &, {4, 6}, 0]) // MatrixForm


Code NOT working on more complex equation

Variables

C1 = 10^(-1);
C2 = 0.1*C1;
R = 50;
Tb = 0.1;
Geb = 5.;
Z0 = 50;
L[Te_] := 1. + 1.*(Te - 0.1);
Zlcr[Te_, w_] := (1/R + 1/(I*L[Te]*w) + I*C1*w)^-1;
Zload[Te_, w_] := -I*w*C2 + Zlcr[Te, w];
\[CapitalGamma][Te_, w_] := (Zload[Te, w] - Z0)/(Zload[Te, w] + Z0);
y[Te_, w_] := (Abs[\[CapitalGamma][Te, w]])^2;
p[Te_, w_] := Abs[\[CapitalGamma][Te, w]]
DeltaPlocal = 10.^-5;


Table of values

tt2 = Table[ Te /. Chop@Solve[  (1 -   y[Te, w]) Pprobe  ==  (Te - Tb) Geb, Te , Method -> Reduce] ,
{w, 2.5, 3., 0.1}  , {Pprobe, 1., 2.5, 0.1} ];

tttt2 = ( Map[Chop@(Max[#] - Min[#] &), tt2, {2}] ) // MatrixForm
testmatrix = Map[ {# == 0} &, tttt2, {-1} ]


eqn8[w_?NumericQ, Pprobe_?NumericQ, Te_] :=
(1 -   y[Te, w]) Pprobe  ==  (Te - Tb) Geb

eqn9[w_?NumericQ, Pprobe_?NumericQ, Te_] :=
DeltaPlocal + (1 -   y[Te, w]) Pprobe  ==  (Te - Tb) Geb

diff2[w_?NumericQ, Pprobe_?NumericQ] :=
((Te /. FindRoot[eqn9[w, Pprobe, Te], {Te, 0.2}]) - (Te /. FindRoot[eqn8[w, Pprobe, Te], {Te, 0.2}]))

(Array[# #2 /. {(0) -> Quiet@Style[Evaluate[diff2[#, #2]], Red], _ :>  0} &, {6, 16}, 0]) // MatrixForm


• I'm a bit suspicious about the use of Quiet here. Is that masking any warning messages? Commented Sep 29, 2014 at 7:58
• I don't think that's why it's not working - The equations are not even being evaluated at the correct positions! (The red numbers should only appear at 'True' positions in the testmatrix Commented Sep 29, 2014 at 8:14
• I don't suppose that is why its not working. It may though be hiding clues as to why its not working. Commented Sep 29, 2014 at 8:20
• @Ymareth It basically says 'infinite expression 1/0 encountered' which doesn't make sense, since eq8 and eq9 are evaluated without trouble when I substitute in a specific value of (w,Pprobe) in the range Commented Sep 29, 2014 at 8:38
• @user44840, something got lost since the originial version of the linked question:) Array[# #2 /. ((0) -> anything...) , ...] will always produce an array with anything filling the first row and the first column. This Array[# #2 ...] thing was suggested for the special structure of the very first version of your linked question.
– kglr
Commented Sep 29, 2014 at 9:25

Given a matrix mat replace the 0s in mat with an expression that depends on the indices.

randommatrix = RandomInteger[1, {6, 6}];
randommatrix // MatrixForm
MapIndexed[# /. {(0) -> Quiet@Style[Evaluate[diff2 @@ #2], Red], _ :>
0} &, randommatrix, {2}] // MatrixForm


mat = 1 - Unitize[tttt2];
args = Table[{w, Pprobe}, {w, 2.5, 3., 0.1}, {Pprobe, 1., 2.5, 0.1}];
(*  or args =Array[{#,#2}&,{6,16},{{2.5,3.},{1,2.5}}] *)

res = MapIndexed[# /.
{(0) -> Quiet@Style[Evaluate[diff2 @@ (args[[##]] & @@ #2)], Red], _ :> 0} &, mat, {2}]

MatrixForm[res]


mat = Unitize[tttt2];
res =MapIndexed[# /. {(0) ->
Quiet@Style[Evaluate[diff2 @@ (args[[##]] & @@ #2)], Red], _ :> 0} &, mat, {2}]
MatrixForm[res]


ListDensityPlot[res]


• I think that's the opposite - I'm trying to evaluate the zeroes of tttt2 .I got the correct result with that, so it should be instead: mat = Unitize[tttt2] and MapIndexed[# /. {(0) -> Quiet@Style[Evaluate[diff2 @@ (args[[##]] & @@ #2)], Red], _ :> 0} &, mat, {-1}] Commented Sep 29, 2014 at 10:02
• @user44840, if you get the desired result with that change, i will edit.
– kglr
Commented Sep 29, 2014 at 10:03
• I try to do a density plot by using ListDensityPlot[] but it gave an error Must be a valid array. Any ideas why? It appears to be a normal matrix, so it should be able to simply plot it.. Commented Sep 29, 2014 at 10:08
• @user44840, please see the update. I think must be a valid array... message might have to do the way you use MatrixForm. If you use (mat2 =somematrix)/MatrixForm then you can use ListDensityPlot[mat2] or foo[mat2] in further calculations. But if you do mat2= somematrix//MatrixForm you are setting mat2 to MatrixForm[somematrix]`, and you get error when you try to pass it to another function.
– kglr
Commented Sep 29, 2014 at 10:24