/* RegressionExampleSix Example of the use of the class Regression demonstrating the use of the non-linear regression method, Regression.simplexPlot in fitting data to the shifted rectangular hyperbola function, y = Ao.x/(theta = x) + alpha with Ao, theta and alpha unknown The weighted regression uses both x and y experimental errors Michael Thomas Flanagan http://www.ee.ucl.ac.uk/~mflanaga/java/Regression.html March 2012 Copyright (c) 2012 - 2014 * PERMISSION TO COPY: * Permission to use, copy and modify this software and its documentation for * NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement * to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, * appears in all copies and associated documentation or publications. * * Public listing of the source codes on the internet is not permitted. * * Redistribution of the source codes or of the flanagan.jar file is not permitted. * * Redistribution in binary form of all or parts of these classes is not permitted. * * Dr Michael Thomas Flanagan makes no representations about the suitability * or fitness of the software for any or for a particular purpose. * Dr Michael Thomas Flanagan shall not be liable for any damages suffered * as a result of using, modifying or distributing this software or its derivatives. * ***************************************************************************************/ import flanagan.analysis.*; public class RegressionExampleSix{ public static void main(String[] args){ // x and y data double[] x = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; double[] y = {0.11, 0.51, 0.83, 1.15, 1.24, 1.38, 1.36, 1.49, 1.67, 1.66, 1.70}; // x and y measurement errors double[] xErrors = {0.005, 0.024, 0.037, 0.059, 0.083, 0.106, 0.119, 0.142, 0.157, 0.177, 0.203}; double[] yErrors = {0.051, 0.048, 0.049, 0.052, 0.051, 0.049, 0.053, 0.048, 0.049, 0.051, 0.050}; // Number of data points int nPoints = 11; // Create instance of class to evaluate the rectangulat hyperbola regression function RectHypFunc func = new RectHypFunc(); func.setXerrors(xErrors); func.setYerrors(yErrors); // Create instance of Regression Regression reg = new Regression(x, y, xErrors, yErrors); // Initial estimates for the Nelder-Mead simplex regression double[] start = new double[3]; start[0] = 2.0; // initial estimate of A0 start[1] = 3.5; // initial estimate of theta start[2] = 0.1; // initial estimate of alpha // Initial step sizes double[] step = new double[3]; step[0] = 0.4; step[1] = 0.7; step[2] = 0.05; // Call simplex non-linear regression method with plotting option // using default tolerance anddefault maximum iterations reg.simplexPlot(func, start, step); } } class RectHypFunc implements RegressionFunction3{ private double[] xErrors = null; private double[] yErrors = null; public double[] function(double[] p, double[] x, int ii){ double[] y = new double[2]; double hold = p[1] + x[0]; // Calculated y value y[0] = p[0]*x[0]/hold + p[2]; // Calculated the square of the weight y[1] = p[0]*p[1]/(hold*hold); // dy/dx y[1] = this.yErrors[ii]*this.yErrors[ii] + y[1]*y[1]*this.xErrors[ii]*this.xErrors[ii]; // w*w return y; } public void setXerrors(double[] xErrors){ this.xErrors = xErrors; } public void setYerrors(double[] yErrors){ this.yErrors = yErrors; } }