As I understood your question you have two polynomials and want to find all points where they are equal.
Here is a function that does that using Modelica.Math.Vectors.Utilities.roots
:
First, you give the two polynomials poly1
and poly2
. Finding poly1=poly2
is identical to finding poly1-poly2=0
, so I define a third polynomial polyDiff = polyLong-polyShort
and then hand over that polynomial to Modelica.Math.Vectors.Utilities.roots
. It will return all roots, even complex ones.
function polyIntersect
input Real[:] poly1={3,2,1,0};
input Real[:] poly2={8,7};
output Real[:,2] intersect;
protected
Integer nPoly1 = size(poly1,1);
Integer nPoly2 = size(poly2,1);
Integer nPolyShort = min(nPoly1, nPoly2);
Integer nPolyLong = max(nPoly1, nPoly2);
Real[nPolyShort] polyShort;
Real[nPolyLong] polyLong;
Real[nPolyLong] polyDiff;
algorithm
if (nPoly1<nPoly2) then
polyShort := poly1;
polyLong := poly2;
else
polyShort := poly2;
polyLong := poly1;
end if;
polyDiff := polyLong;
for i in 0:nPolyShort-1 loop
polyDiff[nPolyLong-i] := polyLong[nPolyLong-i] - polyShort[nPolyShort-i];
end for;
intersect := Modelica.Math.Vectors.Utilities.roots(polyDiff);
end polyIntersect;
The above code is also available here: https://gist.github.com/thorade/5388205