Message Boards Message Boards


How to make the code related to line and ellipse more concise?

Posted 13 days ago
6 Replies
4 Total Likes

My needs are shown in the picture is.

**1. Calculate the equation after a straight line and an ellipse are simultaneous (eliminate x)

  1. Calculating the discriminant of simultaneous equations

  2. Calculate Veda of simultaneous equations (x1+x2, x1 x2, y1+y2, y1 y2, x1 x2+y1 y2, x1 y2+x2 y1)

  3. Calculate the chord AB length where the ellipse intersects the line**

enter image description here

I wrote some tedious code myself, and I think it can be optimized.

It is better to use the symbols in the picture to represent the corresponding results.

Such as:

x1+x2=-((2 a^2 k m)/(b^2 + a^2 k^2))

and so on

How can we further optimize it? Thank you!

The code is as follows:

eqns = {x^2/a^2 + y^2/b^2 == 1, y == k x + m};

polyex = Apply[Subtract, eqns, {1}];

polys = Numerator[Together[Apply[Subtract, eqns, {1}]]];

xpoly = Collect[Resultant[polys[[1]], polys[[2]], y], x]

discx = Factor[Discriminant[xpoly, x]]   (*discriminant*)

frist = Solve[eqns, {x, y}] // FullSimplify;
{{x1, y1}, {x2, y2}} = {x, y} /. frist;

second = {x1 + x2, x1 x2, y1 + y2, y1 y2} // FullSimplify

thrid = {x1 x2 + y1 y2, x1 y2 + x2 y1} // FullSimplify

slope = -CoefficientList[polyex[[2]], x][[2]];    (*k*)

intercept = -CoefficientList[CoefficientList[polyex[[2]], y][[1]], 
    x][[1]] ;  (*m*)

Chordlength = 
  Sqrt[1 + slope^2] Sqrt[(x1 + x2)^2 - 4 x1 x2]]    (*AbsAB*)
POSTED BY: lee tao
6 Replies
Posted 13 days ago

The display result is correct

enter image description here

POSTED BY: lee tao
Posted 13 days ago

POSTED BY: lee tao
Posted 13 days ago

ClearAll by itself does not evaluate, it needs an argument. Use this to clear all definitions in the current context.

ClearAll[Evaluate[Context[] <> "*"]]
POSTED BY: Rohit Namjoshi
Posted 8 days ago


POSTED BY: lee tao

What about this?

sol = Solve[{(x/a)^2 + (y/b)^2 == 1, y - (k x + m) == 0}, {y, x}];
xx1 = {x, y} /. sol[[1]]
xx2 = {x, y} /. sol[[2]]
dd = xx1 - xx2 // FullSimplify
chordlength = Sqrt[dd.dd] // FullSimplify
POSTED BY: Hans Dolhaine
Posted 13 days ago


POSTED BY: lee tao
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract