0
$\begingroup$

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;

enter image description here

enter image description here

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

enter image description here

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} ]

enter image description here enter image description here

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

enter image description here

$\endgroup$
6
  • $\begingroup$ I'm a bit suspicious about the use of Quiet here. Is that masking any warning messages? $\endgroup$
    – Ymareth
    Sep 29, 2014 at 7:58
  • $\begingroup$ 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 $\endgroup$
    – user44840
    Sep 29, 2014 at 8:14
  • $\begingroup$ I don't suppose that is why its not working. It may though be hiding clues as to why its not working. $\endgroup$
    – Ymareth
    Sep 29, 2014 at 8:20
  • $\begingroup$ @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 $\endgroup$
    – user44840
    Sep 29, 2014 at 8:38
  • $\begingroup$ @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. $\endgroup$
    – kglr
    Sep 29, 2014 at 9:25

1 Answer 1

1
$\begingroup$

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

enter image description here

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]

enter image description here

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

enter image description here

ListDensityPlot[res]

enter image description here

$\endgroup$
4
  • $\begingroup$ 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}] $\endgroup$
    – user44840
    Sep 29, 2014 at 10:02
  • $\begingroup$ @user44840, if you get the desired result with that change, i will edit. $\endgroup$
    – kglr
    Sep 29, 2014 at 10:03
  • $\begingroup$ 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.. $\endgroup$
    – user44840
    Sep 29, 2014 at 10:08
  • 1
    $\begingroup$ @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. $\endgroup$
    – kglr
    Sep 29, 2014 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.