forum.alglib.net

ALGLIB forum
 It is currently Thu Jul 07, 2022 4:48 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.

 Page 1 of 1 [ 1 post ]
 Print view Previous topic | Next topic
Author Message
 Post subject: how to formulate least squares problemPosted: 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?

Code:
Module Module1

'changed EXAMPLE FROM ALGLIB PACKAGE

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.lsfitresults(state, info, c, rep)
System.Console.WriteLine("{0}", info) ' EXPECTED: 2
System.Console.WriteLine("{0}", alglib.ap.format(c, 1)) ' EXPECTED: 2,1

Environment.Exit(0)
End Sub
End Module

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 1 post ]

 All times are UTC

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

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

Search for: