# How to make the code related to line and ellipse more concise？

Posted 13 days ago
297 Views
|
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) Calculating the discriminant of simultaneous equations Calculate Veda of simultaneous equations (x1+x2, x1 x2, y1+y2, y1 y2, x1 x2+y1 y2, x1 y2+x2 y1) Calculate the chord AB length where the ellipse intersects the line** 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 onHow 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*) 
6 Replies
Sort By:
Posted 13 days ago
 The display result is correct
Posted 13 days ago
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 8 days ago
 thx
Posted 13 days ago
 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 13 days ago
 thx