File size: 3,691 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Imports DWSIM.DrawingTools.Point
Imports System.Linq

Namespace MathEx

    Public Class Intersection

        Public Shared Function FindIntersection(ByVal curve1 As List(Of Point), curve2 As List(Of Point), curve1fit As LMFit.FitType, curve2fit As LMFit.FitType, tolerance As Double, xmin As Double, xmax As Double, npoints As Integer) As List(Of Point)

            Dim polyfit As New DWSIM.MathOps.MathEx.LMFit()

            Dim ie1, ie2 As Double()

            Select Case curve1fit
                Case LMFit.FitType.Linear
                    ie1 = New Double() {1.0, 1.0}
                Case LMFit.FitType.SecondDegreePoly
                    ie1 = New Double() {1.0, 1.0, 1.0}
                Case LMFit.FitType.ThirdDegreePoly
                    ie1 = New Double() {1.0, 1.0, 1.0, 1.0}
                Case LMFit.FitType.FourthDegreePoly
                    ie1 = New Double() {1.0, 1.0, 1.0, 1.0, 1.0}
                Case Else
                    ie1 = New Double() {}
            End Select

            Select Case curve2fit
                Case LMFit.FitType.Linear
                    ie2 = New Double() {1.0, 1.0}
                Case LMFit.FitType.SecondDegreePoly
                    ie2 = New Double() {1.0, 1.0, 1.0}
                Case LMFit.FitType.ThirdDegreePoly
                    ie2 = New Double() {1.0, 1.0, 1.0, 1.0}
                Case LMFit.FitType.FourthDegreePoly
                    ie2 = New Double() {1.0, 1.0, 1.0, 1.0, 1.0}
                Case Else
                    ie2 = New Double() {}
            End Select

            Dim c1 = polyfit.GetCoeffs(curve1.Select(Function(p) p.X).ToArray, curve1.Select(Function(p) p.Y).ToArray, ie1, curve1fit, 0.000001, 0.000001, 0.000001, 1000)

            Dim c2 = polyfit.GetCoeffs(curve2.Select(Function(p) p.X).ToArray, curve2.Select(Function(p) p.Y).ToArray, ie2, curve2fit, 0.000001, 0.000001, 0.000001, 1000)

            Dim c1coeffs = c1.Item1

            Dim c2coeffs = c2.Item1

            Dim intersections As New List(Of Point)

            Dim c1v, c2v As Double

            For x As Double = xmin To xmax Step (xmax - xmin) / npoints

                Select Case curve1fit
                    Case LMFit.FitType.Linear
                        c1v = c1coeffs(0) + c1coeffs(1) * x
                    Case LMFit.FitType.SecondDegreePoly
                        c1v = c1coeffs(0) + c1coeffs(1) * x + c1coeffs(2) * x ^ 2
                    Case LMFit.FitType.ThirdDegreePoly
                        c1v = c1coeffs(0) + c1coeffs(1) * x + c1coeffs(2) * x ^ 2 + c1coeffs(3) * x ^ 3
                    Case LMFit.FitType.FourthDegreePoly
                        c1v = c1coeffs(0) + c1coeffs(1) * x + c1coeffs(2) * x ^ 2 + c1coeffs(3) * x ^ 3 + c1coeffs(4) * x ^ 4
                End Select

                Select Case curve2fit
                    Case LMFit.FitType.Linear
                        c2v = c2coeffs(0) + c2coeffs(1) * x
                    Case LMFit.FitType.SecondDegreePoly
                        c2v = c2coeffs(0) + c2coeffs(1) * x + c2coeffs(2) * x ^ 2
                    Case LMFit.FitType.ThirdDegreePoly
                        c2v = c2coeffs(0) + c2coeffs(1) * x + c2coeffs(2) * x ^ 2 + c2coeffs(3) * x ^ 3
                    Case LMFit.FitType.FourthDegreePoly
                        c2v = c2coeffs(0) + c2coeffs(1) * x + c2coeffs(2) * x ^ 2 + c2coeffs(3) * x ^ 3 + c2coeffs(4) * x ^ 4
                End Select

                If Math.Abs(c1v - c2v) < tolerance Then

                    intersections.Add(New Point(x, c1v))

                End If

            Next

            Return intersections

        End Function

    End Class

End Namespace