1
$\begingroup$

In this way, the x==y+n straight line and the x ^ 2/a ^ 2+y ^ 2/b ^ 2==1 elliptic image can be drawn to the same coordinate system.

For example:

x^2/16 + y^2/9 == 1, x == 2 y + 1

Specific to the above ellipse and straight line

ClearAll["`*"]
eqns = {x^2/16 + y^2/9 == 1, x == 2 y + 1};
line = eqns[[2]]
ell = eqns[[1]]

pts = SolveValues[{line, ell}, {x, y}];
normalized = First[ell] - Last[ell];
(*ass=ResourceFunction["EllipseProperties"][ell,{x,y}];
params={a->ass["SemimajorAxisLength"],b->ass["SemiminorAxisLength"]}*)
params = {a -> Sqrt[Denominator[Coefficient[normalized, x^2]]],
  b -> Sqrt[Denominator[Coefficient[normalized, y^2]]]}
glin = line[[2]] /. params
gell = b {-1, 1} Sqrt[1 - x^2/a^2] /. params;
gpts = pts /. params;
(*Plot[{{glin,gell}},{x,-a,a}/. \
params,Epilog->{Red,PointSize[0.02],Point[gpts]}]*)

ParametricPlot[{glin, y}, {y, -b - 0.5, b + 0.5} /. params,
 AxesLabel -> {x, y}]

The above code does not draw an ellipse and a straight line in the same coordinate system

1. This is how to draw a single line, but the system will give an error prompt when replacing the value of b:

ParametricPlot::plln: Limiting value -0.5-b in {y,-0.5-b,0.5 +b} is not a machine-sized real number.

Reference resources: An answer by Daniel Huber

ParametricPlot[{glin, y}, {y, -b - 0.5, b + 0.5},
  AxesLabel -> {x, y}] /. params

The function of line range expressed by b is that the range of line y value is consistent with the range of ellipse minor axis

2. The code for drawing the intersection of a single ellipse image and a straight line is:
Plot[{gell}, {x, -a, a} /. params,
 Epilog -> {Red, PointSize[0.02], Point[gpts]}]

There isn't any error in replacing a value here. Why?

Reference resources: Comments by Alexei Boulbitch

3. Now, how can I integrate the line and ellipse images into a coordinate system?

The range of coordinates is automatically adjusted according to the value a and b of the major and minor axes of the ellipse. That's why the above replacement is used.

Code Update 1

ClearAll["`*"]
eqs = {x^2/16 + y^2/9 == 1, x == 2 y + 1};
line = eqs[[2]]
ell = eqs[[1]]
pts = SolveValues[{line, ell}, {x, y}];
normalized = First[ell] - Last[ell];
ax = Sqrt[Denominator[Coefficient[normalized, x^2]]]
bx = Sqrt[Denominator[Coefficient[normalized, y^2]]]
(*ass=ResourceFunction["EllipseProperties"][ell,{x,y}];
params={a->ass["SemimajorAxisLength"],b->ass["SemiminorAxisLength"]}*)\
params = {a -> Sqrt[Denominator[Coefficient[normalized, x^2]]],
  b -> Sqrt[Denominator[Coefficient[normalized, y^2]]]}
glin = line[[2]] /. params
gell = b {-1, 1} Sqrt[1 - x^2/a^2] /. params;
gpts = pts /. params;
(*Hold@ContourPlot[Evaluate@{eqs},{x,-a-1,a+1},{y,-b-0.5,b+0.5},\
PlotLegends->Placed[eqs,{0.8,0.15}],AspectRatio->Automatic,Frame->\
False,Axes->True,AxesStyle->Arrowheads[{0.0,0.04}],AxesLabel->{x,y}]/. \
params//ReleaseHold*)
ContourPlot[
 Evaluate@{eqs}, {x, -ax - 1, ax + 1}, {y, -bx - 0.5, bx + 0.5},
 Epilog -> {Red, PointSize[0.02], Point[gpts]},
 PlotLegends -> Placed[eqs, {0.8, 0.15}], AspectRatio -> Automatic,
 Frame -> False, Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}],
 AxesLabel -> {x, y}]

Code Update 2

Clear["Global`*"]
eqs = {x^2/4 + y^2/3 == 1, y == 2 x + 1};
line = eqs[[2]]
ell = eqs[[1]]
pts = SolveValues[{line, ell}, {x, y}];
normalized = First[ell] - Last[ell];
ax = Sqrt[Denominator[Coefficient[normalized, x^2]]]
bx = Sqrt[Denominator[Coefficient[normalized, y^2]]]
p = Plot[y /. Solve[line, y], {x, -ax - 0.5, ax + 0.5}];
pts = SolveValues[{line, ell}, {x, y}]
Graphics[{{First@p}, {Red, Circle[{0, 0}, {ax, bx}],
   Point[{0, 0}]}, {Blue, PointSize[.03], Point[pts]}}, Axes -> True,
 AxesLabel -> {x, y}, AxesStyle -> Arrowheads[{0.0, 0.04}],
 AspectRatio -> 1]
plx = Apply[Subtract, eqs, {1}];
pls = Numerator[Together[Apply[Subtract, eqs, {1}]]];
xpl = Collect[Resultant[pls[[1]], pls[[2]], y], x];
Collect[Coefficient[xpl, x^2] x^2 +
   Factor@FactorTerms[Coefficient[xpl, x], x] x +
   Select[xpl, FreeQ[x]], x, # &, Defer[+##]~Reverse~2 &] == 0
Collect[xpl, x, Simplify];
pl = {% == 0}
discx = Factor[Discriminant[xpl, x]]   (*discriminant*)
frist = Solve[eqs, {x, y}] // FullSimplify;
{{x1, y1}, {x2, y2}} = {x, y} /. frist;
second = {x1 + x2, x1 x2, y1 + y2, y1 y2,
   y1 y2/(x1 x2), (x1 + x2)/2, (y1 + y2)/2} // FullSimplify
thrid = {x1 x2 + y1 y2, x1 y2 + x2 y1} // FullSimplify
slope = CoefficientList[line[[2]], x][[2]];    (*k*)
intercept = CoefficientList[line[[2]], x][[1]];  (*m*)
Chordlength =
 FullSimplify[
  Sqrt[1 + slope^2] Sqrt[(x1 + x2)^2 - 4 x1 x2]]    (*AbsAB*)
area = 1/2 Chordlength Sqrt[intercept^2]/Sqrt[slope^2 + 1] //
  FullSimplify
$\endgroup$
3
  • $\begingroup$ ContourPlot[Evaluate@{x^2/16 + y^2/9 == 1, x == 2 y + 1}, {x, -5, 5}, {y, -5, 5}, PlotLegends -> Placed[eqns, {0.8, 0.15}]] $\endgroup$
    – Bob Hanlon
    Commented Jan 28, 2023 at 1:41
  • $\begingroup$ thank you! I want to input the elliptic equation and the linear equation at the beginning, and then I will not input the elliptic equation and the linear equation for drawing and calculation, but directly use variables to extract. Includes the coordinate system x-axis y-axis range. It is better to draw the coordinate axis of the coordinate system. I have updated the problem. $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 1:49
  • $\begingroup$ ContourPlot[ Evaluate@{x^2/16 + y^2/9 == 1, x == 2 y + 1}, {x, -5, 5}, {y, -5, 5}, PlotLegends -> Placed[eqns, {0.8, 0.15}], Axes -> True, AxesLabel -> {x, y}] $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 2:17

4 Answers 4

2
$\begingroup$

can be drawn to the same coordinate system.

Why not just use Graphics? Note that the Circle command can also draw an ellipse!

Mathematica graphics

enter image description here

ClearAll["Global`*"];
a = 4;
b = 3
centerAt = {0, 0}
lineEq = x == 2 y + 1
p = Plot[y /. Solve[lineEq, y], {x, -a, a}];
Graphics[{{Dashed,First@p},{Red,Circle[centerAt,{a, b}], Point[centerAt]}},Axes->True]

How to add an arrow to the coordinate axis?

You can use AxesStyle

Graphics[{{Dashed, First@p},
  {Red, Circle[centerAt, {a, b}], Point[centerAt]}
  },
 Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}]]

Mathematica graphics

How to show the intersection of line and ellipse

You can do similar to what Bob did which is use SolveValues and then use the Point command in the Graphics. Like this

ClearAll["Global`*"];
a = 4;
b = 3
centerAt = {0, 0}
lineEq = x == 2 y + 1
elipseEq = x^2/a^2 + y^2/b^2 == 1;
p = Plot[y /. Solve[lineEq, y], {x, -a, a}];
pts = SolveValues[{lineEq, elipseEq}, {x, y}]
Graphics[{
  {Dashed, First@p},
  {Red, Circle[centerAt, {a, b}], Point[centerAt]},
  {Blue, PointSize[.03], Point[pts]}
  },
 Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}]]

Mathematica graphics

You can change the color, point size, etc.. as you want.

How to label the corresponding curve with the corresponding equation in the image

One way could be to use Text command and place the equations where you want them

Mathematica graphics

ClearAll["Global`*"];
a = 4;
b = 3
centerAt = {0, 0}
lineEq = x == 2 y + 1
elipseEq = x^2/a^2 + y^2/b^2 == 1;
p = Plot[y /. Solve[lineEq, y], {x, -a, a}];
pts = SolveValues[{lineEq, elipseEq}, {x, y}]
Graphics[{
  {Dashed, First@p},
  {Red, Circle[centerAt, {a, b}], Point[centerAt]},
  {Blue, PointSize[.03], Point[pts]},
  {Rotate[Text[lineEq, {2, .8}], 30 Degree], 
   Text[elipseEq, {1.4, 3.4}]}
  },
 Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}]
 ]
$\endgroup$
10
  • $\begingroup$ Circle command can also draw an ellipse.I remember this usage. thank you! How to add an arrow to the coordinate axis? $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 2:47
  • $\begingroup$ @csn899 added arrows $\endgroup$
    – Nasser
    Commented Jan 28, 2023 at 2:55
  • $\begingroup$ Thank you very much! This method can draw an ellipse! It's amazing!Graphics[Circle[{0, 0}, {4, 3}], Axes -> True, AxesLabel -> {x, y}] $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 2:58
  • $\begingroup$ How to show the intersection of line and ellipse in the image. For example, the red dot represents the intersection of the two. $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 3:41
  • $\begingroup$ @csn899 updated for intersection $\endgroup$
    – Nasser
    Commented Jan 28, 2023 at 3:50
2
$\begingroup$
Clear["Global`*"]

eqns = {x^2/16 + y^2/9 == 1, x == 2 y + 1};

EDIT: Intersections are at

pts = SolveValues[eqns, {x, y}]

(* {{2/13 (2 - 3 Sqrt[51]), 3/26 (-3 - 2 Sqrt[51])}, 
    {2/13 (2 + 3 Sqrt[51]), 3/26 (-3 + 2 Sqrt[51])}} *)

rng[var_] := 
  Insert[(#[{var, eqns[[1]]}, {x, y}] & /@ 
   {MinValue, MaxValue}), var, 1];

Module[{xrng = rng[x], yrng = rng[y]},
 Show[
  ContourPlot @@ {eqns, xrng, yrng,
    AspectRatio -> 
      (Subtract @@ Rest@yrng)/(Subtract @@ Rest@xrng),
    Frame -> False,
    Axes -> True,
    AxesLabel -> Automatic},
  Graphics[{Thin,
    Arrow[{1.1 #, 0} & /@ (Rest@xrng)],
    Arrow[{0, 1.13 #} & /@ (Rest@yrng)],
    Red, AbsolutePointSize[4],
    Tooltip[Point[#], N@#] & /@ pts}]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you very much! A small question, how to add arrows to two coordinate axes? $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 2:56
  • $\begingroup$ Thank you very much! $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 3:10
2
$\begingroup$

Using Region functionality:

Clear[x, y, r1, r2]
r1 = ImplicitRegion[x^2/16 + y^2/9 == 1, {x, y}];
r2 = ImplicitRegion[x == 2 y + 1, {{x, -4, 4}, y}];
    
Show[{Region[r1], Region[r2]
  , Region[Style[
    BooleanRegion[And, {r1, r2}]
    , AbsolutePointSize[6], Red]
   ]
  }
 , PlotRange -> {{-4.5, 4.5}, {-4, 4}}
 , AspectRatio -> Automatic
 , Frame -> False
 , Axes -> True
 , AxesLabel -> {x, y}
 , AxesStyle -> Arrowheads[{-0.04, 0.04}, {-0.04, 0.04}]
 ]

enter image description here

$\endgroup$
0
$\begingroup$
ContourPlot[
  Evaluate@{eqns}, {x, -a - 1, a + 1}, {y, -b - 0.5, b + 0.5}, 
  PlotLegends -> Placed[eqns, {0.8, 0.15}], AspectRatio -> Automatic, 
  Axes -> True, AxesLabel -> {x, y}] /. params

This code can show that lines and ellipses are in the same coordinate system, but why does this error prompt still appear

ContourPlot::plln: Limiting value -1-a in {x,-1-a,1+a} is not a machine-sized real number.

ContourPlot[
  Evaluate@{eqns}, {x, -a - 1, a + 1}, {y, -b - 0.5, b + 0.5}, 
  PlotLegends -> Placed[eqns, {0.8, 0.15}], AspectRatio -> Automatic, 
  Axes -> True, AxesLabel -> {x, y}, Frame -> False] /. params
$\endgroup$
6
  • 1
    $\begingroup$ It's a matter of evaluation order. If you don't want to see it: Hold@ContourPlot[ Evaluate@{eqns}, {x, -a - 1, a + 1}, {y, -b - 0.5, b + 0.5}, PlotLegends -> Placed[eqns, {0.8, 0.15}], AspectRatio -> Automatic, Axes -> True, AxesLabel -> {x, y}] /. params//ReleaseHold $\endgroup$
    – xzczd
    Commented Jan 28, 2023 at 3:51
  • $\begingroup$ How to modify this code can be shown to the intersection of line and ellipse in the image. For example, the red dot represents the intersection of the two. $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 5:58
  • $\begingroup$ Please read Nasser's answer carefully。 $\endgroup$
    – xzczd
    Commented Jan 28, 2023 at 7:13
  • $\begingroup$ {Blue, PointSize[.03], Point[pts] $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 9:46
  • $\begingroup$ ContourPlot[ Evaluate@{eqs, {Red, PointSize[0.01], Point[pts]}}, {x, -ax - 1, ax + 1}, {y, -bx - 0.5, bx + 0.5}, PlotLegends -> Placed[eqs, {0.8, 0.15}], AspectRatio -> Automatic, Frame -> False, Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}], AxesLabel -> {x, y}] $\endgroup$
    – csn899
    Commented Jan 28, 2023 at 9:55

Your Answer

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

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