Page 2 of 2 FirstFirst 12
Results 41 to 53 of 53

Thread: Polynomials [Resolved]

  1. #41

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    Originally posted by azteched
    If you want the inverse of a matrix, there's faster ways than Gauss-Jordan - read it up in a computational maths textbook
    Only for 2x2 matrices . . . (?)

  2. #42
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    Nah I think for N*N - but I can have a look tomorrow in the library.
    an ending

  3. #43

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Polynomials [Resolved]

    LU decomposition looks promising although it is only more efficient in very particular circumstances but still LU decomposition can be more efficient than row reduction.

    I'll look into it.

  4. #44
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Polynomials [Resolved]



    Mmmmm sweeet.
    I don't live here any more.

  5. #45

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Polynomials [Resolved]

    Methinks Wossy and Class-A drugs go hand in hand . . .

  6. #46
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Polynomials [Resolved]

    How do i convert this code to VB.Net?

  7. #47

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Polynomials [Resolved]

    Quote Originally Posted by yrwyddfa
    Pleased you liked it . . .

    The algorithm itself is quite optimised because I used Gauss-Jordan reduction to get the inverse matrix.

    If you use the classic multiplying method you would need n! calculations, but using Gauss-Jordan you need n^3.

    If you are using polynomials of degree 4 or higher then this algorithm is efficient (in terms of number of calculations) So it IS ineffecient for straight lines - particularly there is a shortcut to get the inverse of 2x2 matrix.

    If you are converting this to .Net you'll probably need to create a Matrix class with the following methods: Prop Get/Let Element, Inverse, GaussJordan, Multiply, Augment. This covers most of the major mathematics in the algorithm.

    The worst part of it all is that I couldn't find ANY source code examples (VB or otherwise) that do this ANYWHERE on the web.

    Maybe my googling is not up to it . . .

    (FYI - the technique is called least-squares curve fitting)
    You might be better talking to Wossy as he's already done the conversion
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  8. #48
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Polynomials [Resolved]

    For Stupid COders like me who find this conversion a mission....

    VB Code:
    1. Public Structure POINT
    2.         Friend x As Double
    3.         Friend y As Double
    4.     End Structure
    5.     Public Function Trend(ByVal Data() As POINT, ByVal Degree As Long) As POINT()
    6.  
    7.         'degree 1 = straight line y=a+bx
    8.         'degree n = polynomials!!
    9.  
    10.         Dim a(,) As Double
    11.         Dim Ai(,) As Double
    12.         Dim B(,) As Double
    13.         Dim P(,) As Double
    14.         Dim SigmaA() As Double
    15.         Dim SigmaP() As Double
    16.         Dim PointCount As Long
    17.         Dim MaxTerm As Long
    18.         Dim m As Long, n As Long
    19.         Dim i As Long, j As Long
    20.         Dim Ret() As POINT
    21.  
    22.         Degree = Degree + 1
    23.  
    24.         MaxTerm = (2 * (Degree - 1))
    25.         PointCount = UBound(Data) + 1
    26.  
    27.         ReDim SigmaA(MaxTerm - 1)
    28.         ReDim SigmaP(MaxTerm - 1)
    29.  
    30.         ' Get the coefficients lists for matrices A, and P
    31.         For m = 0 To (MaxTerm - 1)
    32.             For n = 0 To (PointCount - 1)
    33.                 SigmaA(m) = SigmaA(m) + (Data(n).x ^ (m + 1))
    34.                 SigmaP(m) = SigmaP(m) + ((Data(n).x ^ m) * Data(n).y)
    35.             Next
    36.         Next
    37.  
    38.         ' Create Matrix A, and fill in the coefficients
    39.         ReDim a(Degree - 1, Degree - 1)
    40.         For i = 0 To (Degree - 1)
    41.             For j = 0 To (Degree - 1)
    42.                 If i = 0 And j = 0 Then
    43.                     a(i, j) = PointCount
    44.                 Else
    45.                     a(i, j) = SigmaA((i + j) - 1)
    46.                 End If
    47.             Next
    48.         Next
    49.  
    50.         ' Create Matrix P, and fill in the coefficients
    51.         ReDim P(Degree - 1, 0)
    52.         For i = 0 To (Degree - 1)
    53.             P(i, 0) = SigmaP(i)
    54.         Next
    55.  
    56.         ' We have A, and P of AB=P, so we can solve B because B=AiP
    57.         Ai = MxInverse(a)
    58.         B = MxMultiplyCV(Ai, P)
    59.  
    60.         ' Now we solve the equations and generate the list of points
    61.         PointCount = PointCount - 1
    62.         ReDim Ret(PointCount)
    63.  
    64.         ' Work out non exponential first term
    65.         For i = 0 To PointCount
    66.             Ret(i).x = Data(i).x
    67.             Ret(i).y = B(0, 0)
    68.         Next
    69.  
    70.         ' Work out other exponential terms including exp 1
    71.         For i = 0 To PointCount
    72.             For j = 1 To Degree - 1
    73.                 Ret(i).y = Ret(i).y + (B(j, 0) * Ret(i).x ^ j)
    74.             Next
    75.         Next
    76.  
    77.         Trend = Ret
    78.         Dim equation As String
    79.         Equation = "y=" & Format$(B(0, 0), "0.00000") & " + "
    80.         For j = 1 To Degree - 1
    81.             Equation = Equation & Format$(B(j, 0), "0.00000") & "x^" & j & " + "
    82.         Next
    83.         Equation = Left$(Equation, Len(Equation) - 3)
    84.         MsgBox(equation)
    85.     End Function
    86.     Public Function MxMultiplyCV(ByVal Matrix1(,) As Double, ByVal ColumnVector(,) As Double) As Double(,)
    87.  
    88.         Dim i As Long
    89.         Dim j As Long
    90.         Dim Rows As Long
    91.         Dim Cols As Long
    92.         Dim Ret(,) As Double
    93.  
    94.         Rows = UBound(Matrix1, 1)
    95.         Cols = UBound(Matrix1, 2)
    96.  
    97.         ReDim Ret(UBound(ColumnVector, 1), 0) 'returns a column vector
    98.  
    99.         For i = 0 To Rows
    100.             For j = 0 To Cols
    101.                 Ret(i, 0) = Ret(i, 0) + (Matrix1(i, j) * ColumnVector(j, 0))
    102.             Next
    103.         Next
    104.  
    105.         MxMultiplyCV = Ret
    106.  
    107.     End Function
    108.     Public Function MxInverse(ByVal Matrix(,) As Double) As Double(,)
    109.  
    110.         Dim i As Long
    111.         Dim j As Long
    112.         Dim Rows As Long
    113.         Dim Cols As Long
    114.         Dim Tmp(,) As Double
    115.         Dim Ret(,) As Double
    116.         Dim Degree As Long
    117.  
    118.         Tmp = Matrix
    119.  
    120.         Rows = UBound(Tmp, 1)
    121.         Cols = UBound(Tmp, 2)
    122.         Degree = Cols + 1
    123.  
    124.         'Augment Identity matrix onto matrix M to get [M|I]
    125.         ReDim Preserve Tmp(Rows, (Degree * 2) - 1)
    126.         For i = Degree To (Degree * 2) - 1
    127.             Tmp(i Mod Degree, i) = 1
    128.         Next
    129.  
    130.         ' Now find the inverse using Gauss-Jordan Elimination which should get us [I|A-1]
    131.         MxGaussJordan(Tmp)
    132.  
    133.         ' Copy the inverse (A-1) part to array to return
    134.         ReDim Ret(Rows, Cols)
    135.         For i = 0 To Rows
    136.             For j = Degree To (Degree * 2) - 1
    137.                 Ret(i, j - Degree) = Tmp(i, j)
    138.             Next
    139.         Next
    140.  
    141.         MxInverse = Ret
    142.  
    143.     End Function
    144.     Public Sub MxGaussJordan(ByVal Matrix(,) As Double)
    145.  
    146.         Dim Rows As Long
    147.         Dim Cols As Long
    148.         Dim P As Long
    149.         Dim i As Long
    150.         Dim j As Long
    151.         Dim m As Double
    152.         Dim d As Double
    153.         Dim Pivot As Double
    154.  
    155.         Rows = UBound(Matrix, 1)
    156.         Cols = UBound(Matrix, 2)
    157.  
    158.         ' Reduce so we get the leading diagonal
    159.         For P = 0 To Rows
    160.             Pivot = Matrix(P, P)
    161.             For i = 0 To Rows
    162.                 If Not P = i Then
    163.                     m = Matrix(i, P) / Pivot
    164.                     For j = 0 To Cols
    165.                         Matrix(i, j) = Matrix(i, j) + (Matrix(P, j) * -m)
    166.                     Next
    167.                 End If
    168.             Next
    169.         Next
    170.  
    171.         'Divide through to get the identity matrix
    172.         'Note: the identity matrix may have very small values (close to zero)
    173.         'because of the way floating points are stored.
    174.         For i = 0 To Rows
    175.             d = Matrix(i, i)
    176.             For j = 0 To Cols
    177.                 Matrix(i, j) = Matrix(i, j) / d
    178.             Next
    179.         Next
    180.  
    181.     End Sub

  9. #49
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Polynomials [Resolved]

    Hi,

    Can I recommend for those who are interested in this sort of thing, amongst other mathematical topics,

    http://mathworld.wolfram.com/Matrix.html

    and links therein. They're the chaps who make Mathematica, so they know what they're on about when it comes to maths programming.

    zaza

  10. #50
    New Member
    Join Date
    Aug 2006
    Posts
    1

    Re: Polynomials [Resolved]

    Wossname or Yrwyddfa,

    I know I'm late for this party, but I still need guidance in the use of the code you posted [much] earlier in this thread. When I peruse the code, I get concerned that my input Data(n).x values might blow up the execution, as I have thousands of points to plot with nominal x values, Date and Time, and wish to experiment with high orders. One of you suggested using a For loop to generate the .x values. Will this work for large numbers of points?

    In an attempt to answer my own question, perhaps I should assume that a "Trend" line generator would, by definition, focus on a recent subset of points immediately prior to the Trend point being plotted. (I believe Excel limits me to ~3000 points for Poly trendlines.)

    Am I on the right track?

    Thanks,
    --Guineu

  11. #51

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Polynomials [Resolved]

    I have replied to your PM.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  12. #52
    New Member
    Join Date
    Oct 2015
    Posts
    11

    Re: Polynomials [Resolved]

    This is a very helpful topic and forum for me. I would like to know whether this code can be modified to fit any of the type of trend like linear, log, power, polynominal so on.. along with the value of coefficients. I mean, it will automatically identify the type of equation to best fit the data series and give the values of coefficients. I am also interested to get the R^2 value to evaluate how best the curve has fitted over the data. Regards. Tariq

  13. #53
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Polynomials [Resolved]

    You can figure out the R^2 easily enough, but you should keep in mind that it is a really bad idea to fit arbitrary polynomials to data in search of the best fit. The name for that is data dredging, and it has the particular problem that any result you get will almost certainly be wrong.

    When some type of regression analysis is performed, it is necessary for the person to start with a hypothesis, which will take the form of something like, "this pattern has a linear relationship to this variable" or "this pattern has a logarithmic relationship to this variable". You are then testing whether or not that relationship is true, and how much of the variation in the pattern is due to the variable. You can't say, "what relationship does this variable have to the pattern I am seeing?" because there MUST be such a relationship. In fact, you can generate two random sequences and look for the relationship between them. Given a sufficiently complex polynomial, you are guaranteed that there will be some equation that relates the two, even though they were independent random numbers. So, if you search through enough polynomials, you are guaranteed to find a relationship between a variable and a pattern, regardless of whether the two have any causal relationship whatsoever. That's the problem with data dredges: They ALWAYS find something, that something will only have meaning by pure chance.

    I wrote a program where you could put in some pattern, such as fish returning to spawn in a river. You could then put in whatever factors you thought might relate to the number of fish returning to spawn. The program would then use a genetic algorithm to figure out the relationship between the variables and the pattern, complete with R^2 (which is really just a measure of the minimum sum of the distances from the data points and the polynomial line). After a fair amount of testing, I could show that anybody who believed in the result of the program was a total fool. Fortunately, the program returned a population of excellent polynomial fits. While the best answer was a trap for fools, the population of good results could be examined to determine which variables were actually worth studying further.
    My usual boring signature: Nothing

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width