Search code examples
vb.netalgorithmcurve

How to fit straight line to variable curve and determining the x-intercept


I'm trying to figure out how to code a straight line to the straight part of a curve, the curve should look something like the exponential, click the link to open the image:

Straight line to Curve and determining the x-intercept

Here is the code, I'm only using the exponential as an example

`

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim s As String
        Dim xl As New Excel.Application
        Dim wb As Excel.Workbook
        Dim ws As Excel.Worksheet
        wb = xl.Workbooks.Add   'create new workbook
        ws = wb.Worksheets(1)   'select sheet 1
        ws.Activate()
        Dim Currents() As Double
        Dim PhotodiodeValues() As Double
        Dim AppliedCurrent As Double
        'AppliedCurrent = SerialPort1.ReadLine
        AppliedCurrent = 0
        If AppliedCurrent >= 0 And AppliedCurrent < 0.1 Then
            Dim j As Integer = 1
            For i As Double = 0 To 5 Step 0.5
                ReDim Preserve Currents(j)
                ReDim Preserve PhotodiodeValues(j)
                MsgBox(i)
                MsgBox("LDI " & CType(i, String))
                s = ("LDI " & CType(i, String))
                AppliedCurrent = i
                If AppliedCurrent >= i And AppliedCurrent < (i + 0.1) Then
                    Currents(j) = CType(i, Double)
                    Label1.Text = Currents(j)
                    PhotodiodeValues(j) = CType(Math.E ^ (i), Double)
                    ws.Cells(j, 1) = Currents(j)
                    ws.Cells(j, 2) = PhotodiodeValues(j)
                Else
                    System.Threading.Thread.Sleep(1000)
                End If
                j = j + 1
            Next
        Else
            System.Threading.Thread.Sleep(1000)
        End If
        sfd1.ShowDialog()       'get file name
        wb.SaveAs(sfd1.FileName) 'save data to file
        wb.Close()
        xl = Nothing  'dispose of excel
        ScatterGraph1.PlotXY(Currents, PhotodiodeValues)
        'SerialPort1.Close()
    End Sub
End Class`

Solution

  • First off, I'll explain my thought process. If I have misunderstood, please let me know and I will update my answer. The slope dy/dx of the curve y = e^x is dy/dx = e^x, a monotonically increasing function of x for all real x. There is never a point at which the function becomes linear and, while it has a horizontal asymptote (y = 0) it has no vertical asymptote.

    I take it that what you want is the equation of a tangent line taken at a point where the slope first becomes greater than some cutoff value m*. After that point, the graph of y = e^x "might as well" be a straight line for your intents and purposes.

    So, we must first solve the equation m* = dy/dx = e^x for the x at which m* occurs. The range of e^x is all positive real numbers and e^x is monotonically increasing, so any positive real number m* will have a unique solution x*. indeed, x* = ln(m*). Our tangent line will pass through the point (x*, e^x*) and have slope m*. Recall that m* = e^x*, so the point is (ln(m*), m*) and the slope is m*.

    With a point and the slope, we can figure out the equation of a line. We have that the slope from the given point to any other point must be m*; so, (y - y*)/(x - x*) = m*. Rearranging, (y - y*) = m*(x - x*), y = mx - mx* + y*, and finally y = mx + (y - mx) = mx + (m - mln(m)). The Y-intercept is therefore (m* - mln(m)). We can get the X-intercept by setting y = 0 and solving for x: 0 = mx + (m - mln(m)), mx = mln(m*) - m*, x = ln(m*) - 1.

    In summary:

    • the equation of the line tangent to y = e^x with slope m* is y = mx + (m - mln(m)).
    • the Y-intercept of this line is (m* - mln(m)).
    • the X-intercept of this line is ln(m*) - 1

    If the curve is known at compile time, I recommend hard-coding a closed form analytical solution for the derivative and any asymptotes. If the function is not known until runtime, the derivative at a given point can be approximated numerically using a variety of methods. Intuitively, the definition of the derivative as the limit of (f(x+d) - f(x)) / d as d approaches zero can be used to find approximations of the derivative where the derivative (likely) exists. For well-behaved analytic functions, you will typically be safe except in special cases.

    If the function's derivative is monotonically non-decreasing, as in this example, you can find the point (if any) at which the function's slope meets or exceeds a certain cutoff using approximation (as above) in conjunction with something akin to binary search. Start at a value such as x = 0, and increase or decrease x by some multiplicatively increasing factor until you have passed your target. Now, with bounds on the values of x between which your target can be found, check the middle of the range, and then either the left or right half recursively until a suitably good x* is found.