Message Boards Message Boards

0
|
2034 Views
|
6 Replies
|
4 Total Likes
View groups...
Share
Share this post:

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

Posted 1 year ago

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 = 
 FullSimplify[
  Sqrt[1 + slope^2] Sqrt[(x1 + x2)^2 - 4 x1 x2]]    (*AbsAB*)
POSTED BY: Lee Tao
6 Replies
Posted 1 year ago

thx

POSTED BY: Lee Tao

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 1 year ago

thx

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 1 year ago

POSTED BY: Lee Tao
Posted 1 year ago

The display result is correct

enter image description here

POSTED BY: Lee Tao
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract