ALGLIB forum
It is currently Wed Jul 06, 2022 4:08 am

All times are UTC

Forum rules

1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: how to formulate least squares problem
PostPosted: Sat Nov 03, 2012 10:16 pm 

Joined: Mon Apr 16, 2012 12:24 pm
Posts: 5
I am trying to understand how to formualte a problem using Alglib that I hope I may be able to solve with the lsfit routines. so I am trying to start with a very simple example to help me understand how to formulate it.

In my simple example I wish to fit/model a curve of 3 points, thier values are 1,7,4
I am trying to fit them as a combination of two curves of three points 0,3.5,1 and 1,0,2

I have tried to modify the example for lsfit_d_nlf example.

Maybe my function is incorrect, maybe I have mixed up what should be the y values and what should be the X, I have tried several different varaitions of where the inputs should go but without success.

So I would be very greatful if someone can help me, and show me teh correct way to formulate this?

thanks in advance

Module Module1


    Public Sub function_cx_1_func(c As Double(), x As Double(,), ByRef func As Double, obj As Object)
        func = (c(0) * x(0, 0)) + (c(0) * x(0, 1)) + (c(0) * x(0, 2)) + (c(1) * x(0, 1)) + (c(1) * x(1, 1)) + (c(1) * x(2, 1))

    End Sub

    Public Sub Main()
        '  Gradient is estimated using combination of numerical differences
        '  and secant updates. diffstep variable stores differentiation step
        '  (we have to tell algorithm what step to use).
        Dim x(,) As Double = New Double(,) {{0.0, 1.0}, {3.5, 0.0}, {1.0, 2.0}}
        Dim y() As Double = New Double() {1, 4, 7}
        Dim c() As Double = New Double() {0.6, 0.3}
        Dim epsf As Double = 0
        Dim epsx As Double = 0.000001
        Dim maxits As Integer = 0
        Dim info As Integer
        Dim state As lsfitstate = New XAlglib.lsfitstate() ' initializer can be dropped, but compiler will issue warning
        Dim rep As lsfitreport = New XAlglib.lsfitreport() ' initializer can be dropped, but compiler will issue warning
        Dim diffstep As Double = 0.0001

        '  Fitting without weights
        XAlglib.lsfitcreatef(x, y, c, diffstep, state)

        xalglib.lsfitsetcond(state, epsf, epsx, maxits)
        XAlglib.lsfitfit(state, AddressOf function_cx_1_func, Nothing, Nothing)
        xalglib.lsfitresults(state, info, c, rep)
        System.Console.WriteLine("{0}", info) ' EXPECTED: 2
        System.Console.WriteLine("{0}", alglib.ap.format(c, 1)) ' EXPECTED: 2,1

    End Sub
End Module

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group