Skip to content

Instantly share code, notes, and snippets.

@thorade
Last active December 16, 2015 05:59
Show Gist options
  • Save thorade/5388205 to your computer and use it in GitHub Desktop.
Save thorade/5388205 to your computer and use it in GitHub Desktop.
Modelica function that calculates intersection of two polynomials using Modelica.Math.Vectors.Utilities.roots
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment