所以我有一组有界丢番图方程,指定平面上的线。我想让mathematica绘制其中两个方程的交集,这样我就可以看到它们的样子。

到目前为止,我有类似的东西:

解决[0 < x-y < 3 && -1 < 2x-y < 2,{x,y},整数]

它返回一些结构,如:

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

但是我现在怎么能让mathematica绘制这个,所以我可以看到结果的形状。最好我希望情节考虑每个'点'是一个1x1正方形。

另外,我想知道是否有更好的方法来做这样的事情。谢谢.

有帮助吗?

解决方案

通过转换列表来定义要绘制的数据 Solve[] 回报。这可以做成

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

更一般地说,你可以使 Solve 使用以下技巧以列表格式(而不是作为一组规则)返回解决方案:

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

对于绘图,在许多替代方案中,您可以使用 ListPlot 作为

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

要获得以下输出

output image

您可以使用许多样式和其他选项进一步完善它 ListPlot.例如,您可以加入点

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

以获得

joined plot

编辑:要使用标记位置和大小,有几种选择。使用 ListPlot 你可以通过两种方式得到你需要的东西:

 (* 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}

在这两种情况下,您都需要使用 Epilog, ,否则连接点的线被标记物遮挡。这两种选择都产生以下输出:

listplot with markers

或者,您可以使用 Graphics, RegionPlot, ContourPlot, BubbleChart 与适当的转换 data 获得类似于 ListPlot 以上输出。

使用图形基元:

 (* 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}

使用BubbleChart:

 (* 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}

使用区域图:

 (* 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}

使用轮廓图:

 (* 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}

其他提示

可以是

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

还可以使用提取要绘制的点数

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top