﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Bluebit Software Support Forum / Technical Support and Help / Matrix ActiveX Component  / Least Squares Fit of a 3rd order polynomial / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>Bluebit Software Support Forum</description><link>http://www.bluebit.gr/forum/</link><webMaster>support@bluebit.gr</webMaster><lastBuildDate>Wed, 08 Feb 2012 06:40:15 GMT</lastBuildDate><ttl>20</ttl><item><title /><link>http://www.bluebit.gr/forum/Topic12-1-1.aspx</link><description>Yes, Matrix ActiveX Component can do what you want. I have prepared some sample code for you, that demonstrates just this, how to perform a least squares fit of a 3rd order polynomial for a given set of a (x,y) pairs using Singular Value Decomposition.&lt;BR&gt;&lt;BR&gt;This is some sample code in NET Basic:&lt;BR&gt;[code]&lt;BR&gt;Imports BluebitMatrix31&lt;BR&gt;&lt;BR&gt;Module Module1&lt;BR&gt;' The following code will demonstrate how to find&lt;BR&gt;' the coeficients of a 3rd order polynomial &lt;BR&gt;' in order to have a least squares fit solution&lt;BR&gt;' for a set of (x, y) data pairs&lt;BR&gt;'&lt;BR&gt;' 3rd order polynomial &lt;BR&gt;' a*1 + b*x + c*x^2 + d*x^3 = y&lt;BR&gt;'&lt;BR&gt;' Data set&lt;BR&gt;' - x - - y -&lt;BR&gt;' x0 = 2 y0 = 2&lt;BR&gt;' x1 = 4 y1 = 3&lt;BR&gt;' x2 = 5 y2 = 5&lt;BR&gt;' x3 = 6 y3 = 7&lt;BR&gt;' x4 = 8 y4 = 5&lt;BR&gt;' x5 = 7 y5 = 6&lt;BR&gt;'&lt;BR&gt;' We need a least squares fit solution for the &lt;BR&gt;' system of equations :&lt;BR&gt;'&lt;BR&gt;' X * C = Y&lt;BR&gt;'&lt;BR&gt;' Matrix X contains the powers of xn of the dataset&lt;BR&gt;' x0^0, x0^1, x0^2, x0^3&lt;BR&gt;' x1^0, x1^1, x1^2, x1^3&lt;BR&gt;' x2^0, x2^1, x2^2, x2^3&lt;BR&gt;' x3^0, x3^1, x3^2, x3^3&lt;BR&gt;' x4^0, x4^1, x4^2, x4^3&lt;BR&gt;' x5^0, x5^1, x5^2, x5^3&lt;BR&gt;' &lt;BR&gt;' Matrix Y contains the right hand tems y0 to yn&lt;BR&gt;' &lt;BR&gt;' Matrix C contains the coeficients&lt;BR&gt;'&lt;BR&gt;' Using Singular Value decomposition we calculate the&lt;BR&gt;' pseudoinverse of X, Xinv and then C becomes:&lt;BR&gt;' &lt;BR&gt;' C = Xinv * Y&lt;BR&gt;&lt;BR&gt;Sub Main()&lt;BR&gt;Dim X As Matrix, Y As Matrix, C As Matrix&lt;BR&gt;&lt;BR&gt;X = New MatrixClass&lt;BR&gt;X.Size(6, 4)&lt;BR&gt;&lt;BR&gt;'Entering data in the X matrix&lt;BR&gt;X(0, 0) = 1 : X(0, 1) = 2 : X(0, 2) = 2 * 2 : X(0, 3) = 2 * 2 * 2&lt;BR&gt;X(1, 0) = 1 : X(1, 1) = 4 : X(1, 2) = 4 * 4 : X(1, 3) = 4 * 4 * 4&lt;BR&gt;X(2, 0) = 1 : X(2, 1) = 5 : X(2, 2) = 5 * 5 : X(2, 3) = 5 * 5 * 5&lt;BR&gt;X(3, 0) = 1 : X(3, 1) = 6 : X(3, 2) = 6 * 6 : X(3, 3) = 6 * 6 * 6&lt;BR&gt;X(4, 0) = 1 : X(4, 1) = 8 : X(4, 2) = 8 * 8 : X(4, 3) = 8 * 8 * 8&lt;BR&gt;X(5, 0) = 1 : X(5, 1) = 7 : X(5, 2) = 7 * 7 : X(5, 3) = 7 * 7 * 7&lt;BR&gt;&lt;BR&gt;Y = New MatrixClass&lt;BR&gt;Y.Size(6, 1)&lt;BR&gt;&lt;BR&gt;'Entering data for the Y matrix&lt;BR&gt;Y(0, 0) = 2&lt;BR&gt;Y(1, 0) = 3&lt;BR&gt;Y(2, 0) = 5&lt;BR&gt;Y(3, 0) = 7&lt;BR&gt;Y(4, 0) = 5&lt;BR&gt;Y(5, 0) = 6&lt;BR&gt;&lt;BR&gt;'Calculating the pseudoinverse of X using SVD&lt;BR&gt;Dim U As Matrix, S As Matrix, V As Matrix, Xinv As Matrix, i As Integer&lt;BR&gt;&lt;BR&gt;U = New MatrixClass&lt;BR&gt;S = New MatrixClass&lt;BR&gt;V = New MatrixClass&lt;BR&gt;&lt;BR&gt;X.SVD(U, S, V)&lt;BR&gt;&lt;BR&gt;' Xinv = V * SInv * UInv&lt;BR&gt;&lt;BR&gt;' Matrix S is a diagonal matrix. In order to find its&lt;BR&gt;' inverse is faster to replace its diagonal elements by&lt;BR&gt;' their reciprocals&lt;BR&gt;For i = 0 To 3&lt;BR&gt;S(i, i) = 1 / S(i, i)&lt;BR&gt;Next&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;' Matrix U is an orthogonal matrix - its inverse equals&lt;BR&gt;' its transpose&lt;BR&gt;&lt;BR&gt;' Finally the X pseudoinverse becomes :&lt;BR&gt;Xinv = V.Times(S).Times(U.Transpose)&lt;BR&gt;&lt;BR&gt;' and the C matrix containg the coeficients :&lt;BR&gt;&lt;BR&gt;C = Xinv.Times(Y)&lt;BR&gt;'now C(0,0) contains a&lt;BR&gt;' C(1,0) contains b&lt;BR&gt;' C(2,0) contains c&lt;BR&gt;' C(3,0) contains d&lt;BR&gt;&lt;BR&gt;' In order to generate y based on C coeficients&lt;BR&gt;' we just need to multiply matrix X by C&lt;BR&gt;Dim nY As Matrix = X.Times(C)&lt;BR&gt;&lt;BR&gt;' calculating DY = Y - nY&lt;BR&gt;Dim dY As Matrix = Y.Minus(nY)&lt;BR&gt;&lt;BR&gt;' calculating the sum of squares&lt;BR&gt;Dim sumDY2 As Double = dY.ColsDotProduct(0, 0)&lt;BR&gt;&lt;BR&gt;'Writing down the results:&lt;BR&gt;Console.WriteLine("Dataset:")&lt;BR&gt;Console.WriteLine("- x - - y -")&lt;BR&gt;For i = 0 To 5&lt;BR&gt;Console.WriteLine(String.Format(" x{0}={1} y{0}={2}", i, X(i, 1), Y(i, 0)))&lt;BR&gt;Next&lt;BR&gt;&lt;BR&gt;Console.WriteLine()&lt;BR&gt;Console.WriteLine("3rd order polynomial coeficients.")&lt;BR&gt;Console.WriteLine(String.Format("a= {0:+0.0000;-0.0000}", C(0, 0)))&lt;BR&gt;Console.WriteLine(String.Format("a= {0:+0.0000;-0.0000}", C(1, 0)))&lt;BR&gt;Console.WriteLine(String.Format("a= {0:+0.0000;-0.0000}", C(2, 0)))&lt;BR&gt;Console.WriteLine(String.Format("a= {0:+0.0000;-0.0000}", C(3, 0)))&lt;BR&gt;&lt;BR&gt;Console.WriteLine()&lt;BR&gt;Console.WriteLine("3rd order polynomial.")&lt;BR&gt;Console.WriteLine(String.Format("{0: + 0.0000}{1: + 0.0000; - 0.0000}*x{2: + 0.0000; - 0.0000}*x^2{3: + 0.0000; - 0.0000}*x^3", C(0, 0), C(1, 0), C(2, 0), C(3, 0)))&lt;BR&gt;&lt;BR&gt;Console.WriteLine()&lt;BR&gt;Console.WriteLine(" = Y values =")&lt;BR&gt;Console.WriteLine("Observed Estimated Error")&lt;BR&gt;For i = 0 To 5&lt;BR&gt;Console.WriteLine(String.Format(" {0:+0.0000;-0.0000} {1:+0.0000;-0.0000} {2:+0.0000;-0.0000}", Y(i, 0), nY(i, 0), dY(i, 0)))&lt;BR&gt;Next&lt;BR&gt;Console.WriteLine()&lt;BR&gt;Console.WriteLine(String.Format("Sum of error squares : {0:+0.0000;-0.0000}", sumDY2))&lt;BR&gt;&lt;BR&gt;Console.Read()&lt;BR&gt;End Sub&lt;BR&gt;&lt;BR&gt;End Module&lt;BR&gt;[/code]&lt;BR&gt;Free Trial edition will let you to use up to 6x6 matrices, meaning that you can test up to 6 (x,y) pairs, but I think this is enough for evaluation.&lt;BR&gt;&lt;BR&gt;You will need to buy Advanced or Advanced II edition of MaXC since SVD method is not supported in Standard edition.&lt;BR&gt;&lt;BR&gt;There are not any limitations, you'll be able to calculate linear regressions for thousands lines of data and that will be done very fast.&lt;BR&gt;</description><pubDate>Wed, 17 Mar 2004 23:32:23 GMT</pubDate><dc:creator>Trifon</dc:creator></item><item><title>Least Squares Fit of a 3rd order polynomial</title><link>http://www.bluebit.gr/forum/Topic12-1-1.aspx</link><description>I am interested in using your matrix component in a project and would like to know if it will do what is needed.Project: Using linear regression, extract roots or coeficients from a set of ordered pairs (x,y).  These coeficients will then be used in a 3rd order polynomial to generate y based on x. The platform is Visual Basic .NET.Questions:1. Will your software do this? 2. Do you have any example code or suggestions?3. What are the limitations.Thanks</description><pubDate>Wed, 17 Mar 2004 23:23:43 GMT</pubDate><dc:creator>anonymous</dc:creator></item></channel></rss>
