forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 7:04 pm

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  [ 3 posts ] 
Author Message
 Post subject: Need help with scaling
PostPosted: Thu Oct 06, 2011 7:33 am 
Offline

Joined: Thu Oct 06, 2011 6:51 am
Posts: 2
Hi good people,

I work at univeristy's project, and I need help about variable scaling. Can anyone explain me how lsfitsetscale() works and how to set diffstep,epsf,epsx,maxits (how to know if diffstep is not too large or too small, etc.).

I have:
1.Function: func = x[0] * Math.Tan(a + (b / (1 + (x[0] / c)))*Math.PI/180);
2. starting parameters a,b,c: double[] initialTestGuess = { 10.2, 28.81, 250.98 }; //respectively a,b,c
3. bounds: double[] bndl = new double[] { 0, 0, 0 }
double[] bndu = new double[] { 10.2, 28.81, 250.98 };

It's clear that these parameters have different scale, but I dont know how lsfitsetscale() works in alglib and how to set scale for those parameters. I need to understand how all these stuff works because I have whole bunch of different starting parameters. I've attached my testing file, where you can see my code and what are targetValues and x[0] values.

Program is written using C#. Using Excel Solver for values in attached .txt file, I've seen that optimal results are (3.146, 22.167, 250.976).

Thank you very much


Attachments:
Program.txt [4.47 KiB]
Downloaded 683 times
Top
 Profile  
 
 Post subject: Re: Need help with scaling
PostPosted: Sun Oct 09, 2011 11:08 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Hello!

You can set following scales for your parameters: {1, 20, 200}. The idea is that a is about 1 in magnitude, while b is slightly larger and c is the largest one. Thus 1) steps for c will generally be larger than steps for a, and 2) stopping criteria for c will be looser that ones used for a. I recommend you to start from setting epsx=0.0001 as stopping condition, leaving other criteria zero. As for diffstep - again, 0.0001 can be good value to start with.

BTW, have you noticed that your function have singularity for some values of a/b/c/x? ALGLIB can handle it as long as you won't return infinity or overflow.


Top
 Profile  
 
 Post subject: Re: Need help with scaling
PostPosted: Mon Oct 10, 2011 11:38 am 
Offline

Joined: Thu Oct 06, 2011 6:51 am
Posts: 2
Hi,

I've tried as you suggested, but results I get are not even near as I would like (values i got were (9.485, 11.471, 209.150)). Excel Solver did very good fit with values (3.146, 22.167, 250.976). Do you have any idea what's going wrong?

In the attachment I've added Excel file with values and with graph diagrams. Excel Solver is also configured, so all you need to do is to press SOLVE button. Agenda:
1. Black line - beginning function
2. Green line - reduced function (reduced function = beginning function/SRF)
3. Red line - estimated function (good solution is when green and red lines are lapped)
When you get values from ALGLIB fitter put them in I10 ,I11 ,I12 cells so u can compare graphs (it's respectively A,B,C).

This is whole code of my program:
Code:
using System;
using System.Collections.Generic;
using System.Text;


namespace FittingExample
{
    class Program
    {
        public static void function_cx_1_func(double[] parameters, double[] x, ref double func, object obj)
        {
           
            double a = parameters[0];
            double b = parameters[1];
            double c = parameters[2];

           func = x[0] * Math.Tan(a + (b / (1 + (x[0] / c)))*Math.PI/180);
        }

        static void Main(string[] args)
        {
            double[,] sigValues = {{0.000},{50.000},{100.000},{150.000},{200.000},{250.000},{300.000},
                                      {350.000},{400.000},{450.000},{500.000},{550.000},{600.000},{650.000},
                                      {700.000},{750.000},{800.000},{850.000},{900.000},{950.000},{1000.000}};

            double[] tauValues = {0.000,34.011,59.616,80.539,98.557,114.634,129.346,143.057,156.013,168.384,180.291,191.825,
                                     203.053,214.029,224.792,235.376,245.806,256.105,266.289,276.373,286.369};

            double[] tauReduced = new double[tauValues.Length];

            double sr = 3;

            for (int i = 0; i < tauReduced.Length; i++)
            {
                tauReduced[i] = tauValues[i] / sr;
            }

            double[] initialGuess = { 10.2, 28.81, 250.988 };           
            double[] bndl = new double[] { 0.0001, 0.0001, 0.0001};
            double[] bndu = new double[] { 10.2, 28.81, 250.988 };
            double[] scale = new double[] { 1, 20, 200 };
            double epsf = 0;
            double epsx = 0.0001;
            int maxits = 0;           
            int info;
            alglib.lsfitstate state;
            alglib.lsfitreport rep;
            double diffstep = 0.0001;

            try
            {
                alglib.lsfitcreatef(sigValues, tauReduced, initialGuess, diffstep, out state);
                alglib.lsfitsetcond(state, epsf, epsx, maxits);
                alglib.lsfitsetbc(state, bndl, bndu);
                alglib.lsfitsetscale(state, scale);
               
                alglib.lsfitsetscale(state, scale);
                alglib.lsfitfit(state, function_cx_1_func, null, null);
                alglib.lsfitresults(state, out info, out initialGuess, out rep);
             
                Console.WriteLine("Without gradient: ");
                Console.WriteLine("A= " + initialGuess[0]);
                Console.WriteLine("B= " + initialGuess[1]);
                Console.WriteLine("C= " + initialGuess[2]);

                Console.WriteLine("RMS: "+ rep.rmserror);
                Console.WriteLine("AVG Error: " + alglib.ap.format(initialGuess, 3));

                System.Console.ReadLine();
            }
            catch (Exception ex)
            {
               
                Console.WriteLine(ex.Message);
            }
        }
    }
}



Attachments:
Maksimovic.xlsx [15.41 KiB]
Downloaded 525 times
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 19 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group