using System; using System.Collections.Generic; using System.Text; using System.Linq; 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); } /* public static void function_cx_1_grad(double[] parameters, double[] x, ref double func, double[] grad, 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)))); grad[0] = x[0] / (Math.Pow(Math.Cos((a * x[0] + (a + b) * c) / (x[0] + c)), 2)); //ovo je "df/da" grad[1] = c * x[0] / (x[0] + c) * (Math.Pow(Math.Cos((a * x[0] + (a + b) * c) / (x[0] + c)), 2)); //ovo je "df/db" grad[2] = b * x[0] * x[0] / (Math.Pow((x[0] + c), 2)) * (Math.Pow(Math.Cos((a * x[0] + (a + b) * c) / (x[0] + c)), 2)); //ovo je "df/dc" } */ public static double[] GetScaling(double[] arr, double min, double max) { double m = (max - min) / (arr.Max() - arr.Min()); double c = min - arr.Min() * m; var newarr = new double[arr.Length]; for (int i = 0; i < newarr.Length; i++) newarr[i] = m * arr[i] + c; return newarr; } static void Main(string[] args) { //double[,] sigmaValues = new double[,]{ {0.016}, {0.032}, {0.048}, {0.064}, {0.08}, {0.096}, {0.112}, {0.128}, {0.144}, {0.16}, {0.176} }; double[,] sigTestValues = new double[,] { { 0 }, { 50 }, { 100 }, { 150 }, { 200 }, { 250 }, { 300 }, { 350 }, { 400 }, { 450 }, { 500 }, { 550 }, { 600 }, { 650 }, { 700 }, { 750 }, { 800 }, { 850 }, { 900 }, { 950 }, { 1000 } }; 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 sr = 3.0; double[] tarValues = new double[tauValues.Length]; for (int i = 0; i < tauValues.Length; i++) { tarValues[i] = tauValues[i] / sr; } double[] initialTestGuess = { 10.2, 28.81, 250.98 }; //double[] scaledGuess = GetScaling(initialTestGuess, 0.3, 0.5); double[] bndl = new double[] { 0, 0, 0 };//0.0001 double[] bndu = new double[] { 10.2, 28.81, 250.98 }; double[] s = new double[] { 1e7, 1e7, 1e8 };//skaliranje vrednosti koje su date u initialGuess parametru 1e7,1e7,1e8 double epsf = 0; double epsx = 0.000001; //double epsx = 0; int maxits = 0; //napomena: Ako je epsf=epsx=maxits=0, tada se koriste podrazumevane vrednosti za zaustavljanje int info; alglib.lsfitstate state; alglib.lsfitreport rep; double diffstep = 0.00001; //0.00001; try { alglib.lsfitcreatef(sigTestValues, tarValues, initialTestGuess, diffstep, out state); alglib.lsfitsetcond(state, epsf, epsx, maxits); alglib.lsfitsetbc(state, bndl, bndu); alglib.lsfitsetscale(state, s); alglib.lsfitfit(state, function_cx_1_func, null, null); alglib.lsfitresults(state, out info, out initialTestGuess, out rep); Console.WriteLine("Without gradient: "); Console.WriteLine("A(Degrees)= " + initialTestGuess[0]); Console.WriteLine("B(Degrees)= " + initialTestGuess[1] ); Console.WriteLine("C(kPA)= " + initialTestGuess[2]); Console.WriteLine("RMS: "+ rep.rmserror); Console.WriteLine("AVG Error: " + alglib.ap.format(initialTestGuess, 3)); System.Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }