Come posso tracciare un elenco restituito dalla soluzione Mathematica a nelle equazioni integer delimitate

StackOverflow https://stackoverflow.com/questions/9011923

Domanda

Quindi ho un insieme di equazioni diofantine limitate che specificano linee sull'aereo.Voglio far tracciare la matematica l'intersezione di due di queste equazioni, quindi posso vedere cosa assomigliano.

Finora ho qualcosa come:

.

Risolvi [0

che restituisce una certa struttura come:

.

{{x -> -2, y -> -4}, {x -> -1, y -> -3}, {x -> -1, y -> -2}, {x ->0, y -> -1}}

Ma come posso ora fare la trama di matematica questo così posso vedere la forma risultante.Preferibilmente vorrei che la trama considerare ogni 'punto' essere un quadrato 1x1.

Inoltre, mi chiedo se c'è un modo migliore per fare cose del genere.Grazie.

È stato utile?

Soluzione

Definisci i dati che si desidera tracciare trasformando il rendimento della lista Solve[]. Questo può essere fatto come

 data = {x, y} /. Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers]
.

Più in generale, è possibile rendere Solve restituisce la soluzione in un formato di elenco (piuttosto che come una serie di regole) utilizzando il seguente trucco:

 data = Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers] /. Rule[a_,b_]->b
.

Per la tracciatura, tra molte alternative, è possibile utilizzare ListPlot come

ListPlot[data, PlotMarkers -> {Style["\[FilledSquare]", FontSize -> 16]}]
.

per ottenere la seguente output

immagine di uscita

È possibile perfezionarlo ulteriormente utilizzando molti styling e altre opzioni di ListPlot. Ad esempio, puoi unirti ai punti

ListPlot[data, PlotMarkers -> {Style["\[FilledSquare]", FontSize -> 16]}, 
 Joined -> True]
.

per ottenere

Plot unito

Modifica: per giocare con il posizionamento e la dimensione del marker ci sono diverse alternative. Usando ListPlot puoi ottenere ciò di cui hai bisogno in uno dei due modi:

 (* Alternative 1: use fontsize to change the marker size *)
 lp1 := ListPlot[{#} & /@ #1, 
 PlotMarkers -> {Style["\[FilledSquare]", FontSize -> Scaled[#2]]},
 AspectRatio -> 1, AxesOrigin -> {0, 0}, 
 PlotRange -> {{-5, 1}, {-5, 1}}, 
 PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], 
 Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, 
  Line@#1}, Frame -> True, FrameTicks -> All] &;
 (* usage example *)
 lp1 @@ {data, .30}

 (* Alternative 2: use the second parameter of PlotMarkers to control scaled size *)
 lp2 := ListPlot[{#} & /@ #1, 
 PlotMarkers -> {Graphics@{Rectangle[]}, #2}, AspectRatio -> 1, 
 AxesOrigin -> {0, 0}, PlotRange -> {{-5, 1}, {-5, 1}}, 
 PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], 
 Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, 
 Line@#1}, Frame -> True, FrameTicks -> All] &
 (* usage example *)
 lp2 @@ {data, 1/5.75}
.

In entrambi i casi, è necessario utilizzare Epilog, altrimenti i punti di giunzione delle linee sono occlusi dai marcatori. Entrambe le alternative producono la seguente uscita:

listplot con indicatori

In alternativa, è possibile utilizzare Graphics, RegionPlot, ContourPlot, BubbleChart con trasformazioni appropriate di data per ottenere risultati simili a quello nell'uscita ListPlot sopra.

Uso dei primitivi grafici:

 (* data transformation to define the regions *)
 trdataG[data_, size_] :=  data /. {a_, b_} :> 
         {{a - size/2, b - size/2}, {a + size/2, b + size/2}};
 (* plotting function *)
 gr := Graphics[
      {
      {Hue[RandomReal[]], Rectangle[##]} & @@@ trdataG @@ {#1, #2}, 
      GrayLevel[.3], PointSize[.02], Thick, Point@#1, Line@#1}, 
      PlotRange -> {{-5, 1}, {-5, 1}
      }, 
      PlotRangePadding -> 0, Axes -> True, AxesOrigin -> {0, 0}, 
      Frame -> True, FrameTicks -> All] &
 (* usage example *)
 gr @@ {data, .99}
.

Utilizzo di BubLechart:

 (* Transformation of data to a form that BubbleChart expects *)
 dataBC[data_] := data /. {a_, b_} :> {a, b, 1};
 (* custom markers *)
 myMarker[size_][{{xmin_, xmax_}, {ymin_, ymax_}}, ___] :=
      {EdgeForm[], Rectangle[{(1/2) (xmin + xmax) - size/2, (1/2) (ymin + ymax) - 
       size/2}, {(1/2) (xmin + xmax) + size/2, (1/2) (ymin + ymax) + size/2}]};
 (* charting function *)
 bc := BubbleChart[dataBC[#1], ChartElementFunction -> myMarker[#2], 
       ChartStyle -> Hue /@ RandomReal[1, {Length@#1}], Axes -> True, 
       AxesOrigin -> {0, 0}, PlotRange -> {{-5, 1}, {-5, 1}}, 
       PlotRangePadding -> 0, AspectRatio -> 1, FrameTicks -> All, 
       Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 bc @@ {data, .99}
.

usando regionplot:

 (* Transformation of data to a form that RegionPlot expects *)
  trdataRP[data_, size_] :=  data /. {a_, b_} :> 
            a - size/2 <= x <= a + size/2 && b - size/2 <= y <= b + size/2
 (* charting function *)
 rp := RegionPlot[Evaluate@trdataRP[#1, #2], {x, -5, 1}, {y, -5, 1}, 
          AspectRatio -> 1, Axes -> True, AxesOrigin -> {0, 0}, 
          PlotRange -> {{-5, 1}, {-5, 1}}, 
          PlotStyle -> Hue /@ RandomReal[1, {Length@#1}], FrameTicks -> All, 
          PlotPoints -> 100, BoundaryStyle -> None, 
          Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 rp @@ {data, .99}
.

Utilizzo di Contourplot:

 (* Transformation of data to a form that ContourPlot expects *)
 trdataRP[data_, size_] :=   data /. {a_, b_} :> 
            a - size/2 <= x <= a + size/2 && b - size/2 <= y <= b + size/2;
 trdataCP[data_, size_] := Which @@ Flatten@
           Thread[{trdataRP[data, size], Range@Length@data}];
 (* charting function *)
 cp := ContourPlot[trdataCP[#1, #2], {x, -5, 1}, {y, -5, 1}, 
             AspectRatio -> 1, Axes -> True, AxesOrigin -> {0, 0}, 
             PlotRange -> {{-5, 1}, {-5, 1}}, FrameTicks -> All, 
             ExclusionsStyle -> None, PlotPoints -> 100, 
             ColorFunction -> Hue, 
             Epilog -> {GrayLevel[.3], PointSize[.02], Point@#1, Thick, Line@#1}] &
 (* usage example *)
 cp @@ {data, .99}
.

Altri suggerimenti

può essere

sol = Solve[0 < x - y < 3 && -1 < 2 x - y < 2, {x, y}, Integers];
pts = Cases[sol, {_ -> n_, _ -> m_} :> {n, m}];
ListPlot[pts, Mesh -> All, Joined -> True, AxesOrigin -> {0, 0}, 
 PlotMarkers -> {Automatic, 10}]
.

Inserisci Descrizione dell'immagine qui

può anche estrarre i punti per tracciare usando

{#[[1, 2]], #[[2, 2]]} & /@ sol
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top