Sunteți pe pagina 1din 913

NUMERICAL ANALYSIS WITH JAVA EXAMPLES

Dr. M. Turhan OBAN EGE NVERSTES Mhendislik Fakultesi Makine Mhendislii Bornova, ZMR, TURKEY turhan_coban@yahoo.com

INDEX 1. Introductory Concepts 1.1 Java programing langage as programming base for numerical analysis 1.2 Abstract root functions and Mathd library 1.3 Plotting two dimensional data and functions 1.4 Plotting three dimensional data and functions 1.5 Real Time Grapic Environment 1.6 Reading data from graphic environment 1.7 Taylor series and errorterm in the equations 2. Finding the roots of non linear equations of one independent variable 2.1 Bisection Method 2.2 False position method (regula falsi) 2.3 Fxed position method wth aitken extrapolation (stephensen method) 2.4 Newton-Raphson method 2.5 Secant method 2.6 Muller method 2.7 Newton-raphson (secant) bisection combined method 2.8 Inverse quadratic lagrange interpolation method 2.9 Brent method (nverse quadratic lagrange interpolaton bisecton and sekant combined method) 2.10 Roots of a complex number by newton_raphson method 2.11 Newton-raphson method wth second dervatve 2.12 Finding the roots limits 2.13 Ridder method 2.14 Roots of the second and third degree polynomials 2.15 Complex roots of the polynomals by le guerre method 2.16 Problems 3. Lineer system of equations 3.1 Lineer system of equations, general information 3.2 Gauss elimination method 3.3 Gauss elimination with partial pivoting 3.4 Gauss-Jordan method 3.5 Gauss-Jordan method with partial pivoting
3.6 LU-decomposition- gauss (dolittle) method 3.7 Crut LU decomposition method 3.8 Cholesky symmetric matrix LU decomposition method 3.9 Jacobi gauss -Seidel and successive relaxation methods 3.10 Methods for band marices 3.11 Conjugate gradient method 3.12 Norms 3.13 Unit matrix, transpose matrix and matri invesion 3.14 Problems

4.

Nonlinear system of equations (roots of system of equations) 4.1 Newton-Raphson method 4.2 Broyden secant method with Sharman-Morrison formula

4.3 homotopy or continuation methods

4.4 Problems
5. Optimisation 5.1 Definition of optimization and graphic methods 5.2 one dmensonal non-lnear fbonnach search (golden search) method 5.3 One dimensional non-linear quadratic polynomial method 5.4 One dimensional non-linear cubic polynomial method 5.5 One dimensional non-linear Newton-Raphson method 5.6 One dimensional non-linear Brent method 5.7 Improving one dimensional optimization bracket values 5.8 Newton-Raphson method for nonlinear multivariable optimization 5.9 Nelder Mead simplex nonlinear mulitivariable optimization method 5.10 Non-linear multidimensional steepest descent method 5.11 Modified newton method(Newton-Raphson steepest descent combined method) 5.12 Non-linear multidimensional Fletcher-Reeves and Polak-Ribiere conjugate gradient method 5.13 Non-linear multidimensional Polak-Ribiere conjugate gradient method 5.14 Non-linear multidimensional nonlinear Davidon-Fletcher-Powell method
5.15 Multivariable non-linear Broyden-Fletcher-Goldberg-Shanno (bfgs) method 5.16 Homotopy or continuation methods 5.17 Multivariable nonlinear broyden secant method with Sherman Morrison formula 5.18 Scholastic methods : Genetic algorithms 5.19 Scholastic methods: Monte-Carlo method 5.20 Stochastic methods: iterative hill climbing 5.21 Stochastic methods: simulated annealing 5.22 Non linear optimization with boundary restrictions 5.23 Travelling salesman problem (TSP) 5.24 Lineer multivariable optimization, Simplex method 5.25 Solution of the nonlinear system of equations by using optimization algorithms 5.26 Problems

6. Curve fitting 6.1 Linear curve fitting : least square methods with one variable 6.2 Ortogonal polynomial least square 6.3 Curve fitting : least square method with multi variables 6.4 Interpolation 6.5 Cubic spline interpolation polynomials 6.6 B-spline interpolation 6.7 Least square curve fitting of nonlinear equations 6.8 Usage of parametric variables in curve fitting 6.9 Moving least squares 6.10 Bernstein polinomials and Bezier curves 6.11 Problems 7. Integration and derivatives 7.2 Newton-Cotes formulations : Trapezoidal, Bole, Simpson 1/3 ve 3/8 method 7.2 Richardson extrapolation (Romberg integration) formula 7.3 Aitken extrapolation with Newton Cotes integration formulas 7.4 Quadratures: Gauss-Legendre, Gauss - Chebychev, Gauss -Jacobi, Gauss -Hermit, GaussLequerre Integration formulas

7.5 Integration formulation with adjustable error 7.6 One and multi-dimensional Monte-Carlo integral 7.7 Multidimensional integration by parametric methods 7.8 Direct integration of data 7.9 Derivation 7.10 Pade Approximation 7.11 Direct derivation of the numerical data 7.12 Problems 8. Differential equations 8.1 Euler equation 8.2 Heun Equation 8.3 Runge-Kutta Methods 8.4 Multistep (open) Methods 8.5 Differential equations with more then one set of equation 8.6 Boundary value problems and shooting methods n differental equations 8.7 Problems 9. Eigenvalue problem 9.1 Introduction to eigenvalue problems 9.2 Dominant eigenvalue calculation (Power method) 9.3 Smallest magnitude eigen value calculation (inverse power method) 9.4 Jacobi method and Householder transformation 9.5 QL Algorithm 9.6 QR algorithm 9.7 Roots of polynomials and companion matrix 9.8 Problems 10. HOMEWORK PROBLEMS AND EXAMS 11. REFERENCES

1 INTRODUCTORY CONCEPTS 1.1 JAVA PROGRAMMING LANGUAGE AS PROGRAMMING BASE FOR NUMERICAL ANALYSIS Examples and some of the exercises are given as java programs in this book. Java as one of the modern programming language with good graphic capabilities and capabilities of running in internet environment and getting compiler free for the use of students made it my choice of programming language. In a paralel book, details of java programming is given. User without basic nowledge of the languge can be referred to this book or several other similar ones around before going further through the exercises. 1.2 ABSTRACT ROOT FUNCTIONS AND MATHD LIBRARY In numerical analysis, seeing behavior of a function gives us clues for several investigation purpuses. Class plot is defined to be used to plot function and data in 2 dimension problems. In order to write computer codes in this book all the functions are defined as defined from an abstract form. Abstract classes are empty root classes to reference tor he classes to be written inlater stages. The abstract root function is defined in class f_x. This definition is used both in plot class and numerical analysis classes requires.

Program 1.2-1 Abstract function f_x


import static java.lang.Math.*; abstract public class f_x extends Mathd { // single function single independent variable // example f=x*x abstract public double func(double x); }

It is also possible to use multidemensional function with one dimensional input abstract function fi_x with the plot class. Program 1.2-2 Abstract function fi_x
abstract class fi_x extends Mathd { // multifunction single independent variable // vector of dependent variables are returned // example f[0]=x+sin(x) // f[1]=x*x-x // func(x) returns the value of f[0] and f[1] // as a two dimensional vector abstract double[] func(double x); }

By using the abstract class concept the actual function can be defined as a new class and be used directly with the plot class to plot 2D functions. It should be also note that abstract classes are derived from class Mathd. This is an addtional function library that defines functions frequently use in engineering, but not included standard java Math library such as Bessel functions, error functions, gamma functions etc. A further plus of extending root function classes from Mathd class is ability to utilise method names directly. For example instead of using Math.sin(x) required in java sin(x) can be used. The Mathd library is listed

below:
Program 1.1-1 Mathd library of functions import static java.lang.Math.*; public class Mathd { public static double PI=Math.PI; public static double E=Math.E; //C Euler constant public static double C=0.577215664901532860606512090082402431; public static double abs(double a) {return Math.abs(a);} public static double fabs(double a) {return Math.abs(a);} public static int abs(int a) {return Math.abs(a);} public static double sin(double a) {return Math.sin(a);} public static double sinh(double a) {return Math.sinh(a);} public static double cosh(double a) {return Math.cosh(a);} public static double tanh(double a) {return Math.tanh(a);} public static double cos(double a) {return Math.cos(a);} public static double tan(double a) {return Math.tan(a);} public static double asin(double a) {return Math.asin(a);} public static double acos(double a) {return Math.asin(a);} public static double atan(double a) {return Math.atan(a);} public static double atan2(double a,double b) {return Math.atan2(a,b);} public static double sqrt(double a) {return Math.sqrt(a);} public static double cbrt(double a) {return Math.cbrt(a);} public static double ln(double a) {return Math.log(a);} public static double log(double a) {return Math.log(a);} public static double log10(double a) {return Math.log10(a);} public static double log1p(double a) {return Math.log1p(a);} public static double hypot(double a,double b) {return Math.hypot(a,b);} public static double max(double a,double b) {return Math.max(a,b);} public static int max(int a,int b) {return Math.max(a,b);} public static long max(long a,long b) {return Math.max(a,b);} public static double exp(double a) {return Math.exp(a);} public static double pow(double a,double b) {return Math.pow(a,b);} public static double us(double a,double b) {return Math.pow(a,b);} public static double toRadians(double a) {return Math.toRadians(a);} public static double toDegrees(double a)

{return Math.toDegrees(a);} public static double radyanacevir(double a) {return Math.toRadians(a);} public static double dereceyecevir(double a) {return Math.toDegrees(a);} public static double sind(double a) { double r=(a/180.0)*PI; return Math.sin(r); } public static double cosd(double a) { double r=(a/180.0)*PI; return Math.cos(r); } public static double tand(double a) { double r=(a/180.0)*PI; return Math.tan(r); } public static double random() { return Math.random(); } public static double asind(double b) { return Math.asin(b)*180.0/PI; } public static double acosd(double b) { return Math.acos(b)*180/PI; } public static double atand(double b) { return Math.atan(b)*180/PI; } public static double atan2d(double a,double b) { return Math.atan2(a,b)*180.0/PI; } public static double gamma(double xx) { //Gamma fonction double a[]=lngamma(xx); return a[1]*Math.exp(a[0]); } public static double [] lngamma(double x) { // logarithmic gamma function // xx[0] logarithm of gamma function // xx[1] sign of gamma function double xx[]=new double[2]; double result; double a; double b; double c; double p; double q; double u; double w; double z; int i; double logpi; double ls2pi; double tmp; double sgngam = 1; logpi = 1.14472988584940017414;

ls2pi = 0.91893853320467274178; if( x<-34.0 ) { q = -x; double yy[]=lngamma(q); w = yy[0]; tmp=yy[1]; p = Math.floor(q); i = (int)Math.round(p); if( i%2==0 ) { sgngam = -1; } else { sgngam = 1; } z = q-p; if( z>0.5 ) { p = p+1; z = p-q; } z = q*Math.sin(Math.PI*z); result = logpi-Math.log(z)-w; xx[0]=result; xx[1]=sgngam; return xx; } if( x<13 ) { z = 1; p = 0; u = x; while(u>=3) { p = p-1; u = x+p; z = z*u; } while(u<2) { z = z/u; p = p+1; u = x+p; } if( z<0 ) { sgngam = -1; z = -z; } else { sgngam = 1; } if( u==2 ) { result = Math.log(z); //return result; xx[0]=result; xx[1]=sgngam; return xx; } p = p-2; x = x+p; b = -1378.25152569120859100; b = -38801.6315134637840924+x*b;

b = -331612.992738871184744+x*b; b = -1162370.97492762307383+x*b; b = -1721737.00820839662146+x*b; b = -853555.664245765465627+x*b; c = 1; c = -351.815701436523470549+x*c; c = -17064.2106651881159223+x*c; c = -220528.590553854454839+x*c; c = -1139334.44367982507207+x*c; c = -2532523.07177582951285+x*c; c = -2018891.41433532773231+x*c; p = x*b/c; result = Math.log(z)+p; //return result; xx[0]=result; xx[1]=sgngam; return xx; } q = (x-0.5)*Math.log(x)-x+ls2pi; if( x>100000000 ) { result = q; //return result; xx[0]=result; xx[1]=sgngam; return xx; } p = 1/(x*x); if( x>=1000.0 ) { q = q+((7.9365079365079365079365*0.0001*p2.7777777777777777777778*0.001)*p+0.0833333333333333333333)/x; } else { a = 8.11614167470508450300*0.0001; a = -5.95061904284301438324*0.0001+p*a; a = 7.93650340457716943945*0.0001+p*a; a = -2.77777777730099687205*0.001+p*a; a = 8.33333333333331927722*0.01+p*a; q = q+a/x; } xx[0]= q; xx[1]=sgngam; return xx; } public static double C(long n) { double cc=0; for(double i=1;i<=n;i++) {cc+=1.0/i;} cc-=Math.log(n); return cc; } public static double factrl(int n) { // n! factorial of an integer number int ntop=5; double a[]=new double[50]; a[0]=1.0; a[1]=1.0; a[2]=2.0; a[3]=6.0; a[5]=24.0;

int j; if (n < 0) System.out.println("Negative factorial in routine FACTRL"); if (n > 50) gamma(n+1.0); while (ntop<=n) { j=ntop-1; a[ntop]=a[j]*ntop; ntop++; } return a[n]; } public static double factln(int n) { //Math.log(factorial) of an integer number double a[]={0.0,0.0,0.6931471805599453,1.0986122886681098,1.3862943611198906, 1.6094379124341003,1.791759469228055,1.9459101490553132,2.0794415416798357, 2.1972245773362196,2.302585092994046,2.3978952727983707,2.4849066497880004, 2.5649493574615367,2.6390573296152584,2.70805020110221,2.772588722239781, 2.833213344056216,2.8903717578961645,2.9444389791664403,2.995732273553991, 3.044522437723423,3.091042453358316,3.1354942159291497,3.1780538303479458, 3.2188758248682006,3.258096538021482,3.295836866004329,3.332204510175204, 3.367295829986474,3.4011973816621555,3.4339872044851463,3.4657359027997265, 3.4965075614664802,3.5263605246161616,3.5553480614894135,3.58351893845611, 3.6109179126442243,3.6375861597263857,3.6635616461296463,3.6888794541139363, 3.713572066704308,3.7376696182833684,3.7612001156935624,3.784189633918261, 3.8066624897703196,3.828641396489095,3.8501476017100584,3.871201010907891, 3.8918202981106265,3.912023005428146,3.9318256327243257,3.9512437185814275, 3.970291913552122,3.9889840465642745,4.007333185232471,4.02535169073515, 4.04305126783455,4.060443010546419,4.07753744390572,4.0943445622221,4.110873864173311, 4.127134385045092,4.143134726391533,4.1588830833596715,4.174387269895637,4.189654742026425, 4.204692619390966,4.219507705176107,4.23410650459726,4.248495242049359,4.2626798770413155, 4.276666119016055,4.290459441148391,4.30406509320417,4.31748811353631,4.330733340286331, 4.343805421853684,4.356708826689592,4.3694478524670215,4.382026634673881,4.394449154672439, 4.406719247264253,4.418840607796598,4.430816798843313,4.442651256490317,4.454347296253507, 4.465908118654584,4.477336814478207,4.48863636973214,4.499809670330265,4.51085950651685, 4.5217885770490405,4.532599493153256,4.543294782270004,4.553876891600541,4.564348191467836, 4.574710978503383,4.584967478670572,4.59511985013459,4.605170185988092}; if (n < 0) {System.out.println("Negative factorial in routine FACTLN"); return 0.0;} else if(n > 100) {double b[]=lngamma(n+1.0);return b[0];} else {return a[n];} } public static double PI() { //Ramanujan formula double p=0,pi=0.0; double c3=2.0*Math.sqrt(2.0)/9801.0; double c,c1,c2; for(int n=0;n<20;n++) {c1=factln(n);c=factln(4*n)-c1*c1*c1*c1+Math.log(1103.0+26390.0*n)-4.*n*Math.log(396);c2=c3*Math.exp(c); p+=c2; pi=1.0/p; //System.out.println("c="+c+"c2="+c2+"pi="+pi); } return pi; } public static double bico(int n,int k) { //binomial coefficients return Math.floor(0.5+Math.exp(factln(n)-factln(k)-factln(n-k))); } public static double beta(double z,double w) { //beta function //gamma(z)*gamma(w)/gamma(z+w); return Math.exp(lngamma(z)[0]+lngamma(w)[0]-lngamma(z+w)[0]); }

public static double gammp(double a,double x) { //incomplete gamma function P(a,x) double gamser,gammcf; if (x < 0.0 || a <= 0.0) System.out.println("Invalid arguments in routine GAMMP"); if (x < (a+1.0)) { gamser=gser(a,x); return gamser; } else { gammcf=gcf(a,x); return 1.0-gammcf; } } public static double gammq(double a,double x) { //incomplete gamma function Q(a,x)=1-P(a,x); double gamser,gammcf; if (x < 0.0 || a <= 0.0) System.out.println("Invalid arguments in routine GAMMQ"); if (x < (a+1.0)) { gamser=gser(a,x); return 1.0-gamser; } else { gammcf=gcf(a,x); return gammcf; } } public static double gser(double a,double x) { int ITMAX=100; double EPS=3.0e-7; int n; double sum,del,ap; double gln=lngamma(a)[0]; double gamser=0; if (x <= 0.0) { if (x < 0.0) System.out.println("x less than 0 in routine GSER"); gamser=0.0; return gamser; } else { ap=a; del=sum=1.0/a; for (n=1;n<=ITMAX;n++) { ap += 1.0; del *= x/ap; sum += del; if (Math.abs(del) < Math.abs(sum)*EPS) { gamser=sum*Math.exp(-x+a*Math.log(x)-(gln)); return gamser; } } System.out.println("a too large, ITMAX too small in routine GSER"); return gamser; } } public static double gcf(double a,double x) { int ITMAX=100; double EPS=3.0e-7; int n; double gln; double gammcf=0; double gold=0.0,g,fac=1.0,b1=1.0; double b0=0.0,anf,ana,an,a1,a0=1.0;

gln=lngamma(a)[0]; a1=x; for (n=1;n<=ITMAX;n++) { an=(double) n; ana=an-a; a0=(a1+a0*ana)*fac; b0=(b1+b0*ana)*fac; anf=an*fac; a1=x*a0+anf*a1; b1=x*b0+anf*b1; if (a1!=0) { fac=1.0/a1; g=b1*fac; if (Math.abs((g-gold)/g) < EPS) { gammcf=Math.exp(-x+a*Math.log(x)-gln)*g; return gammcf; } gold=g; } } System.out.println("a too large, ITMAX too small in routine GCF"); return gammcf; } public static double erf(double x) { //error function return x < 0.0 ? -gammp(0.5,x*x) : gammp(0.5,x*x); } public static double erfc(double x) { //complimentary error function return x < 0.0 ? 1.0+gammp(0.5,x*x) : gammq(0.5,x*x); } public static double erfcc(double x) { double t,z,ans; z=Math.abs(x); t=1.0/(1.0+0.5*z); ans=t*Math.exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+ t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+ t*(-0.82215223+t*0.17087277))))))))); return x >= 0.0 ? ans : 2.0-ans; } public static double betacf(double a,double b,double x) { int ITMAX=100; double EPS=3.0e-7; double qap,qam,qab,em,tem,d; double bz,bm=1.0,bp,bpp; double az=1.0,am=1.0,ap,app,aold; int m; qab=a+b; qap=a+1.0; qam=a-1.0; bz=1.0-qab*x/qap; for (m=1;m<=ITMAX;m++) { em=(double) m; tem=em+em; d=em*(b-em)*x/((qam+tem)*(a+tem)); ap=az+d*am; bp=bz+d*bm; d = -(a+em)*(qab+em)*x/((qap+tem)*(a+tem)); app=ap+d*az;

bpp=bp+d*bz; aold=az; am=ap/bpp; bm=bp/bpp; az=app/bpp; bz=1.0; if (Math.abs(az-aold) < (EPS*Math.abs(az))) return az; } System.out.println("a or b too big, or ITMAX too small in BETACF"); return az; } public static double betai(double a,double b,double x) { //incomplete beta function double bt; if (x < 0.0 || x > 1.0) System.out.println("Bad x in routine BETAI"); if (x == 0.0 || x == 1.0) bt=0.0; else bt=Math.exp(lngamma(a+b)[0]-lngamma(a)[0]-lngamma(b)[0]+a*Math.log(x)+b*Math.log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; } public static double J(double v,double x) { //J Bessel function double JJ=0;double a=1.0;int fact=1;double b; if( x <=20) { double x1,x2,x3,x4; long x5=1; double lna=0.0; x1=v*Math.log(0.5*x); double lna1=Math.log(0.25*x*x); for(int k=0;k<100;k++) { x2=lna;//Math.log(a); x3=lngamma(k+1)[0]; x4=lngamma(v+k+1)[0]; b=x1+x2-x3-x4; b=x5*Math.exp(b); lna+=lna1; x5*=-1; JJ+=b; } if(v==0 && x==0) JJ=1; //if(v==1 && x==0) JJ=0; } else //x>30 {JJ=Math.sqrt(2.0/Math.PI/x)*Math.cos(x-0.5*v*Math.PI-0.25*Math.PI);} return JJ; } public static double Y(double v,double x) { //Y Besselfunction double YY=0; int vtest=(int)(v*10)/10; if(v==0 && v==(double)vtest) YY=bessy0(x); else if(v==1 && v==(double)vtest) YY=bessy1(x); else if (v==(double)vtest) YY=bessy((int)v,x); else {YY=(J(v,x)*Math.cos(v*Math.PI)-J(-v,x))/Math.sin(v*Math.PI);} return YY; } public static double K(double v,double x) {

double YY=0; int vtest=(int)(v*10)/10; if(v==0 && v==(double)vtest) YY=bessk0(x); else if(v==1 && v==(double)vtest) YY=bessk1(x); else if (v==(double)vtest) YY=bessk((int)v,x); else {YY=Math.PI/2.0*(I(-v,x)-I(v,x))/Math.sin(v*Math.PI);} return YY; } public static double I(double v,double x) { //I Bessel function double JJ=0;double a=1.0;int fact=1;double b; { double x1,x2,x3,x4; long x5=1; double lna=0.0; x1=v*Math.log(0.5*x); double lna1=Math.log(0.25*x*x); for(int k=0;k<100;k++) { x2=lna;//Math.log(a); x3=lngamma(k+1)[0]; x4=lngamma(v+k+1)[0]; b=x1+x2-x3-x4; b=Math.exp(b); lna+=lna1; JJ+=b; } if(v==0 && x==0) JJ=1; //if(v==1 && x==0) JJ=0; } return JJ; } public static double bessj0(double x) { J(0,x) bessel function double ax,z; double xx,y,ans,ans1,ans2; if ((ax=Math.abs(x)) < 8.0) { y=x*x; ans1=57568490574.0+y*(-13362590354.0+y*(651619640.7 +y*(-11214424.18+y*(77392.33017+y*(-184.9052456))))); ans2=57568490411.0+y*(1029532985.0+y*(9494680.718 +y*(59272.64853+y*(267.8532712+y*1.0)))); ans=ans1/ans2; } else { z=8.0/ax; y=z*z; xx=ax-0.785398164; ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4 +y*(-0.2073370639e-5+y*0.2093887211e-6))); ans2 = -0.1562499995e-1+y*(0.1430488765e-3 +y*(-0.6911147651e-5+y*(0.7621095161e-6 -y*0.934935152e-7))); ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2); } return ans; } public static double bessj1(double x) { J(1,x) bessel function double ax,z; double xx,y,ans,ans1,ans2; if ((ax=Math.abs(x)) < 8.0) { y=x*x;

ans1=x*(72362614232.0+y*(-7895059235.0+y*(242396853.1 +y*(-2972611.439+y*(15704.48260+y*(-30.16036606)))))); ans2=144725228442.0+y*(2300535178.0+y*(18583304.74 +y*(99447.43394+y*(376.9991397+y*1.0)))); ans=ans1/ans2; } else { z=8.0/ax; y=z*z; xx=ax-2.356194491; ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4 +y*(0.2457520174e-5+y*(-0.240337019e-6)))); ans2=0.04687499995+y*(-0.2002690873e-3 +y*(0.8449199096e-5+y*(-0.88228987e-6 +y*0.105787412e-6))); ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2); if (x < 0.0) ans = -ans; } return ans; } public static double bessj(int n, double x) { J(n,x) bessel function double ACC=40.0; double BIGNO=1.0e10; double BIGNI=1.0e-10; int j,m; boolean jsum=true; double ax,bj,bjm,bjp,sum,tox,ans; if (n < 2) System.out.println("Index n less than 2 in bessj"); ax=Math.abs(x); if (ax == 0.0) return 0.0; else if (ax > (double) n) { tox=2.0/ax; bjm=bessj0(ax); bj=bessj1(ax); for (j=1;j<n;j++) { bjp=j*tox*bj-bjm; bjm=bj; bj=bjp; } ans=bj; } else { tox=2.0/ax; m=2*((n+(int) sqrt(ACC*n))/2); jsum=false; bjp=ans=sum=0.0; bj=1.0; for (j=m;j>0;j--) { bjm=j*tox*bj-bjp; bjp=bj; bj=bjm; if (Math.abs(bj) > BIGNO) { bj *= BIGNI; bjp *= BIGNI; ans *= BIGNI; sum *= BIGNI; } if (jsum) sum += bj; jsum=!jsum; if (j == n) ans=bjp; } sum=2.0*sum-bj; ans /= sum; } double JJ=0; if(x < 0.0 ) {JJ=-ans;}

else {JJ=ans;} return ans; } public static double bessi0(double x) { double ax,ans; double y; if ((ax=Math.abs(x)) < 3.75) { y=x/3.75; y*=y; ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492 +y*(0.2659732+y*(0.360768e-1+y*0.45813e-2))))); } else { y=3.75/ax; ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1 +y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2 +y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1 +y*0.392377e-2)))))))); } return ans; } public static double bessi1(double x) { double ax,ans; double y; if ((ax=Math.abs(x)) < 3.75) { y=x/3.75; y*=y; ans=ax*(0.5+y*(0.87890594+y*(0.51498869+y*(0.15084934 +y*(0.2658733e-1+y*(0.301532e-2+y*0.32411e-3)))))); } else { y=3.75/ax; ans=0.2282967e-1+y*(-0.2895312e-1+y*(0.1787654e-1 -y*0.420059e-2)); ans=0.39894228+y*(-0.3988024e-1+y*(-0.362018e-2 +y*(0.163801e-2+y*(-0.1031555e-1+y*ans)))); ans *= (exp(ax)/sqrt(ax)); } return x < 0.0 ? -ans : ans; } public static double bessi(int n, double x) { // I(n,x) bessel function double ACC=40.0; double BIGNO=1.0e10; double BIGNI=1.0e-10; int j; double bi,bim,bip,tox,ans; if (n < 2) System.out.println("Index n less than 2 in bessi"); if (x == 0.0) return 0.0; else { tox=2.0/Math.abs(x); bip=ans=0.0; bi=1.0; for (j=2*(n+(int) sqrt(ACC*n));j>0;j--) { bim=bip+j*tox*bi; bip=bi; bi=bim; if (Math.abs(bi) > BIGNO) { ans *= BIGNI; bi *= BIGNI;

bip *= BIGNI; } if (j == n) ans=bip; } ans *= bessi0(x)/bi; if(x<0) return -ans; else return ans; } } public static double bessy0(double x) { Y(0,x) bessel function double z; double xx,y,ans,ans1,ans2; if (x < 8.0) { y=x*x; ans1 = -2957821389.0+y*(7062834065.0+y*(-512359803.6 +y*(10879881.29+y*(-86327.92757+y*228.4622733)))); ans2=40076544269.0+y*(745249964.8+y*(7189466.438 +y*(47447.26470+y*(226.1030244+y*1.0)))); ans=(ans1/ans2)+0.636619772*bessj0(x)*log(x); } else { z=8.0/x; y=z*z; xx=x-0.785398164; ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4 +y*(-0.2073370639e-5+y*0.2093887211e-6))); ans2 = -0.1562499995e-1+y*(0.1430488765e-3 +y*(-0.6911147651e-5+y*(0.7621095161e-6 +y*(-0.934945152e-7)))); ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2); } return ans; } public static double bessy1(double x) { //Y(1,x) bessel function double z; double xx,y,ans,ans1,ans2; if (x < 8.0) { y=x*x; ans1=x*(-0.4900604943e13+y*(0.1275274390e13 +y*(-0.5153438139e11+y*(0.7349264551e9 +y*(-0.4237922726e7+y*0.8511937935e4))))); ans2=0.2499580570e14+y*(0.4244419664e12 +y*(0.3733650367e10+y*(0.2245904002e8 +y*(0.1020426050e6+y*(0.3549632885e3+y))))); ans=(ans1/ans2)+0.636619772*(bessj1(x)*log(x)-1.0/x); } else { z=8.0/x; y=z*z; xx=x-2.356194491; ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4 +y*(0.2457520174e-5+y*(-0.240337019e-6)))); ans2=0.04687499995+y*(-0.2002690873e-3 +y*(0.8449199096e-5+y*(-0.88228987e-6 +y*0.105787412e-6))); ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2); } return ans; } public static double bessy(int n, double x) { //Y(n,x) bessel function int j; double by,bym,byp,tox;

if (n < 2) System.out.println("Index n less than 2 in bessy"); tox=2.0/x; by=bessy1(x); bym=bessy0(x); for (j=1;j<n;j++) { byp=j*tox*by-bym; bym=by; by=byp; } return by; } public static double bessk0(double x) { double y,ans; if (x <= 2.0) { y=x*x/4.0; ans=(-log(x/2.0)*bessi0(x))+(-0.57721566+y*(0.42278420 +y*(0.23069756+y*(0.3488590e-1+y*(0.262698e-2 +y*(0.10750e-3+y*0.74e-5)))))); } else { y=2.0/x; ans=(exp(-x)/sqrt(x))*(1.25331414+y*(-0.7832358e-1 +y*(0.2189568e-1+y*(-0.1062446e-1+y*(0.587872e-2 +y*(-0.251540e-2+y*0.53208e-3)))))); } return ans; } public static double bessk1(double x) { double y,ans; if (x <= 2.0) { y=x*x/4.0; ans=(log(x/2.0)*bessi1(x))+(1.0/x)*(1.0+y*(0.15443144 +y*(-0.67278579+y*(-0.18156897+y*(-0.1919402e-1 +y*(-0.110404e-2+y*(-0.4686e-4))))))); } else { y=2.0/x; ans=(exp(-x)/sqrt(x))*(1.25331414+y*(0.23498619 +y*(-0.3655620e-1+y*(0.1504268e-1+y*(-0.780353e-2 +y*(0.325614e-2+y*(-0.68245e-3))))))); } return ans; } public static double bessk(int n, double x) { int j; double bk,bkm,bkp,tox; if (n < 2) System.out.println("Index n less than 2 in bessk"); tox=2.0/x; bkm=bessk0(x); bk=bessk1(x); for (j=1;j<n;j++) { bkp=bkm+j*tox*bk; bkm=bk; bk=bkp; } return bk; } }

1.3 PLOTTING TWO DIMENSIONAL DATA AND FUNCTIONS Plot program aims a simple yet sophisticated set of plots to be drawn for two dimensional engineering plot applications. Seeing the data is always easier for evaluating the results. There are commercially available plot programs, but the purpose of this package is to create an absolutely free and open code supporting plot definitions. Most of the subprograms in the package is add on methods from the actual requirements arised in numerical analysis and engineering applications.
Program 1.3-1. PlotT1.java sample program
import java.io.*; class PlotT1 { public static void main(String args[]) throws IOException {Plot pp=new Plot("in.txt"); pp.plot();}}

Data in.txt -4 26.0 -3 17.0 -2 10.0 -1 5.0 1 1.0 2 2.0 3 5.0 4 10.0 5 17.0 6 26.0 Program output:

Our first example code reads data from file in.txt and plot it The first line is the constructor line Plot pp=new Plot("in.txt"); it inputs data from file in.txt and pp.plot(); term is actually draw the plot window. This term will always be used to open plot window. In the second page(Control Page 1) of the graphic window several editing features are available such as plot shapes, headings, colors, grid, logarithmic scale, manual maximum and minimum x an y scales. Page can be grided into a desired size, Graphic color can be changed by using standard java JColorChooser class graphic editor. In the third page(Control Page 2) additional labels space in any desired location of the plot. This can be achived as an indivudial line of label or as a label block. In our second example program data in file in.txt is drawn by a single static definition Program 1.3-2 PlotT1A.java sample program
import java.io.*; class plotT1A { public static void main(String args[]) throws IOException {Plot.file("in.txt");} }

Static definitions are usually preferred for one step plot drawing If several different plot jobs will be defined, dynamic definitions should be preferrred. In the next example data is given inside of the program as two double vectors Program 1.3-3 PlotT2.java program plotting directly from arrays
import java.io.*; class PlotT2 { public static void main (String args[]) throws IOException {double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25}; Plot pp=new Plot(xx,yy); pp.plot(); } }

The static version of the same program : Program 1.3-4 PlotT2A, plotting a vector data
import java.io.*; class PlotT2A { public static void main (String args[]) throws IOException {double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25}; Plot.data(xx,yy);}}

The next program first defines the data and then add up cubic spline curve fittings to the given data (2 plot lines are to be drawn) Program 1.3-5 PlotT2B, plotting a vector data and plotting cubic spline curve fitting of the data
import java.io.*; class PlotT2B { public static void main (String args[]) throws IOException {double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25}; Plot pp=new Plot(xx,yy); pp.addSpline(xx,yy,10); pp.plot();}}

In order to add a cubic spline curve fitting, term pp.addSpline(xx,yy,10); was added. This term created a line with 10 additional data points between each original data. Plot data added into the plot window has an index number starting from 0 and goes upwards for every new curve to be added into the plot window. If the original curve index number is known, it can also be used to curve feet additional data. For the details of Cubic Spline curve fitting, refer the curve fitting section. Program 1.3-6 PlotT2B, plotting a vector data and plotting cubic spline curve fitting of the data index number referencing to data is used for spline fitting

import java.io.*; class PlotT2B2 { public static void main (String args[]) throws IOException {double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25}; Plot pp=new Plot(xx,yy); pp.addSpline(0,10); pp.plot();}}

Static definitions can not be applied in the plot drawings with more than one command such as in the above program, but you can always write your user specific static method to collect your own group of commands Program 1.3-7 PlotT2B, plotting a vector data and plotting cubic spline curve fitting of the data user defined static method is used. import java.io.*; class PlotT2B3 { public static void plot(double xx[],double yy[]) {Plot pp=new Plot(xx,yy); pp.addSpline(xx,yy,10); pp.plot(); } public static void main (String args[]) throws IOException {double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25}; plot(xx,yy);} } The same interpolation calculations can also be carried out by B spline interpolation with term pp.addB_Spline(xx,yy,10); Program 1.3-8 PlotT2B1, plotting a vector data and plotting cubic spline curve fitting of the data user defined static method is used.
import java.io.*; class PlotT2B1 { public static void main (String args[]) throws IOException { double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25}; Plot pp=new Plot(xx,yy); pp.addB_Spline(xx,yy,10); pp.plot();}}

If second degree orthogonal polynomial to be used for curve fitting Program 1.3-9 PlotT2C, plotting a vector data and plotting Orthogonal polynomial least square curve fitting of the data.
import java.io.*; class PlotT2C { public static void main (String args[]) throws IOException { double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25};

Plot pp=new Plot(xx,yy); pp.addOPEKK(xx,yy,2,10); pp.plot();}}

if a second degree polynomial is desired from curve fitting f(x)=a0+a1*x+a2*x2 Program 1.3-10 PlotT2D, plotting a vector data and plotting quadratic polynomial least square curve fitting of the data. import java.io.*; class PlotT2D { public static void main (String args[]) throws IOException {double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25}; Plot pp=new Plot(xx,yy); pp.addEKK(xx,yy,2,10); pp.plot();}} Lagrange interpolation can also be used. Program 1.3-11 PlotT2E, plotting a vector data and plotting Lagrange interpolation curve fitting of the data.
import java.io.*; class PlotT2E { public static void main (String args[]) throws IOException {double xx[]={1,2,3,4,5}; double yy[]={1,4,9,16,25}; Plot pp=new Plot(xx,yy); pp.addLagrange(xx,yy,10); pp.plot();}}

The detailed information about interpolation routines are given in the curve fitting chapter. Constructor in the plot program can be given as completely empty. Program 1.3.-12 PlotT01, plotting data from information given in Plot.txt file
import java.io.*; class PlotT01 { public static void main (String args[]) throws java.io.IOException { Plot pp=new Plot(); pp.plot(); }}

Program 1.3-13 PlotT2B1, plotting data from information given in Plot.txt file static version
// (Static call) import java.io.*; class PlotT2B1 { public static void main (String args[]) throws IOException {Plot.file();}}

In this case program will look the data file Plot.txt for data. In Plot.txt Data is given in the following form

Plot label x axis label y axis label 2 in.txt 0 0 0 0 out.txt 3 0 0 255 The meaning of this data is as follows "Plot label" "x axis label" "y axis label" are defined graphic label headings 2 indicates number of data files data to be taken from in.txt and out.txt in this case, the next number is the plot line type and the last 3 numbers are plot color (red green and blue 0 to 255). Plot line type codes are as follows :
1 _________ continious line 2._._._ 3.__..__. 4-------5 ______ 6 __ __ __ __ 7._._._ 8 .__..__. 9----10 * 11 # 12 $ 13 % 14 & 15 16 ~ 17 + 18 x 19 = 20 square 21 filled square 22 circle 23 filled circle 24 triangle 25 diamond 26 pentagon 27 hexagon 28 filled triangle 29 filled diamond 30 filled pentagon 31 filled hexagon 32 3 sided star 33 4 sided star 34 5 sided star 35 6 sided star 36 filled 3 sided star 37 filled 4 sided star 38 filled 5 sided star 39 filled 6 sided star 40 bar graphic 41 filled bar graphic

In engineering and science, seeing behavior of a function gives us clues for several investigation purpuses. By using class plot we can also plot functions directly. In order to write computer codes in this book all the functions are defined as defined from an abstract form. The abstract root function is defined in class f_x. By using the abstract class concept the actual function can be defined as a new class and be used directly with the plot class to plot 2D functions. It should be also note that abstract classes are derived from class Mathd. Some example using of plot class is given below: PROGRAM 1.3-16 Plot class example NA0
import java.io.*; class f3 extends f_x { public double func(double x) { double ff; ff=13*(x - x*x)*Math.exp(-3*x/2); return ff; } } class NA0 {public static void main (String args[]) throws IOException { Plot.func(new f3(),0.0,4.0);}}

The result will be

As an exercise re-write the program to plot function f(x)=x2-1 As a second example Bessel function of order 1 is Plotted by using Mathd function library method J(1,x) PROGRAM 1.3-17 Plot class exampleNA0A
import java.io.*; class fa1 extends f_x { // Bessel function double func (double x) { return Mathd.J(1,x); } } class NA0A { public static void main (String args[]) throws IOException { fa1 ff1=new fa1(); Plot pp=new Plot(ff1,0.0,20.0); pp.plot(); } }

Re-write the program to plot function f(x)=sin(x) If the Bessel function of order 0, order 0.5 and order 1 is to be plotted: PROGRAM 1.3-18 Plot class exampleNA0C
mport java.io.*; class fa1 extends fi_x { double [] func (double x) { double ff[]=new double[3]; ff[0]=J(0,x); ff[1]=J(0.5,x); ff[2]=J(1,x); return ff;}} class NA0C {public static void main (String args[]) throws IOException { fa1 ff1=new fa1(); Plot pp=new Plot(ff1,0.0,20.0,400,3); pp.plot();}}

The same kind of plot can also be achieved by reading different function and add second(third etc) function by using addFunction method. PROGRAM 1.3-19 Plot class exampleNA0D
import java.io.*; class fa0 extends f_x { double func (double x) { return sin(x); } } class fa1 extends f_x { double func (double x) { return cos(x); } }

class NA0E { public static void main (String args[]) throws IOException { fa0 ff0=new fa0(); fa1 ff1=new fa1(); Plot pp=new Plot(ff0,0.0,2.0*Math.PI); pp.addFunction(ff1,0.0,2.0*Math.PI,400); pp.plot(); } }

Last example combines different methods of the plot class together PROGRAM 1.3-20 Plot class examplePlotT3
import java.io.*; class f3 extends fi_x { double[] func(double x) { double f[]=new double[2]; f[0]=Math.sin(x)+x/2; f[1]=Math.sqrt(x); return f; }} class PlotT3 { public static void main (String args[]) throws IOException {//Plot pp=new Plot(); String fi[]={"in.txt","out.txt"}; f3 ff=new f3(); int ipt[]={0,1}; int ir[]={0,0}; int ig[]={0,0}; int ib[]={0,2}; double xx[]={1,2,3,4,5,6}; double yy[]={2,3,4,5,6,7}; Plot pp=new Plot(fi); //read data from fi (in.txt,out.txt data are added up) //pp.addData(fi); pp.setPlotType(0,0);// pp.setPlotType(1,1);// pp.addData(xx,yy,0,0,0,0); //one set of addional data pp.setPlotType(2,41);//bar chart pp.setColor(2,0,0,255); //blue pp.addFunction(ff,0.0,20.0,50,2,ipt,ir,ig,ib); //two additional function pp.setPlabel("Function Plot"); pp.setXlabel("x axis"); pp.setYlabel("y axis"); pp.plot();}}

Grafik Package basic components : Plot.java constructors, input and output methods PlotW.java : basic graphic and control algorithms PlotShapesSW: defines different plot shapes such as line, star,triangle

Plot2D : Converts shapes defined in the PlotShapesSW class into plot screen format Constructor classes : (defines the graphic window and prepare the data to be plotted) public Plot() throws IOException public Plot(String pl,String xl,String yl,int xnt,int ynt,int xgo,int ygo,String fn[],int ipt[],int ir[],int ig[] ,int ib[]) throws IOException public Plot(String fn[],int ipt[],int ir[],int ig[],int ib[]) throws IOException public Plot(String fn[]) throws IOException : Read file names from String fn[] and plot them public Plot(double xi[][],double yi[][],int ipt[],int ir[],int ig[],int ib[]) : read plot data from xi[][] and yi[][] public Plot(double xiyi[][]) : read plot data xi[] and yi[] from xiyi[][] string public Plot(double xi[][],double yi[][]) : read plot data from xi[][] and yi[][] public Plot(double xi[],double yi[]) : read plot data from xi[] and yi[] public Plot(f_x f,double xm,double xma,int N,int ipt,int ir,int ig,int ib) public Plot(f_x f,double xm,double xma,int N) : create plot data from a simple function f_x f public Plot(f_x f,double xm,double xma) : create plot data from a simple function f_x f public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function) Static method definitions : (give short descriptions for one step plotting) public static double[] data(double xmin,double xmax,double dx) public static double[] data(double xmin,double xmax,int n) public static Plot func(f_x ff1,double xmin,double xmax,int n,int pnumber) public static Plot func(f_x ff1,double xmin,double xmax,int n) public static Plot func(f_x ff1,double xmin,double xmax) public static Plot func(fi_x ff1,double xmin,double xmax,int n,int pnumber) public static Plot func(fi_x ff1,double xmin,double xmax,int n) public static Plot func(fi_x ff1,double xmin,double xmax) adding x,y and heading label public void setPlabel(String ip) public void setXlabel(String ix) public void setYlabel(String iy) public void setXYlabel(String ix,String iy) Adding additional data into the graphic window public void addData(double xi[],double yi[]) public void addData(double xi[],double yi[],int iplottype) public void addData(double xi[],double yi[],int iplottype,int ir,int ig,int ib) public void addData(double xi[][],double yi[][]) public void addData(double xi[][],double yi[][],int ip[]) public void addData(double xi[][],double yi[][],int ip[],int ir[],int ig[],int ib[]) public void addFunction(f_x f,double xmi,double xma,int N,int ipt) public void addFunction(f_x f,double xmi,double xma,int N) public void addFunction(f_x f,double xmi,double xma,int N,int ipt,int ir,int ig,int ib) public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function)

public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,int ipt[]) public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function, ,int ipt[]) adding data with curve fit public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi) public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype) public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi) public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype) public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) public void addEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype) public void addEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) public void addLagrange(double xi[],double yi[],int aradegersayisi) public void addLagrange(double xi[],double yi[],int aradegersayisi,int iplottype) public void addLagrange(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) public void addLagrange(int linenumber,int aradegersayisi,int iplottype) public void addLagrange(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) public void addB_Spline(double xi[],double yi[],int nSpline) public void addB_Spline(double ai[][],int nSpline) public void addB_Spline(double xi[],double yi[],int nSpline,int iplottype) public void addB_Spline(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) public void addB_Spline(int linenumber,int nSpline) public void addB_Spline(int linenumber,int nSpline,int iplottype) public void addB_Spline(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) public void addSpline(double xi[],double yi[],int nSpline) public void addSpline(double ai[][],int nSpline) public void addSpline(double xi[],double yi[],int nSpline,int iplottype) public void addSpline(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) public void addSpline(int linenumber,int nSpline) public void addSpline(int linenumber,int nSpline,int iplottype) public void addSpline(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) changing the graphic properties ===========Graphic type===================== public void setPlotType(int plot_type[]) public void setPlotType(int dataset,int plot_no) public void setPlotType(int dataset,char plot_char) ===========x and y grid =========================== public void setXgrid(int igx) public void setYgrid(int igy) public void setGrid(int igx,int igy) public void setXtic(int ixt) public void setYtic(int iyt) public void setXYtic(int ixt,int iyt)

==========Logarithmic scale on/off ========== public void setXlogScaleOn() public void setYlogScaleOn() public void setXlogScaleOff() public void setYlogScaleOff() ==========setColor========== public void setColor(int dataset,int ired,int igreen,int iblue) public void setColor(int dataset,Color c) public void setColor(int ired[],int igreen[],int iblue[]) public void setColor(Color c[]) ================ Setting maximum and minimum values of the plot window manually ==== public void setMinMax() public void setMinMax(double xi[][],double yi[][]) public void setMinMax(double iminx,double imaxx,double iminy,double imaxy) =============== Formatting graphic window ================================== public void set_plotwindow(int width,int height,double xip,double yip,double dxp,double dyp) public void setabsMaxMin(int ixmin,int iymin,int idx,int idy) Drawing the data in graphic format public void plot() Complete Plot graphic codes are given below: PROGRAM 1.3-21 Plot class
//====================================================== // Numerical Analysis package in java // Plot class to plot 2D data // Dr. Turhan Coban // EGE University, Engineering School, Mechanical Eng. Div. // turhan.coban@ege.edu.tr // ===================================================== import java.io.*; import java.awt.Color; import static java.lang.Math.*; //This class organises data and reqired plot classes for actual plot //===== classes used by this class======= // PlotShapesSW // define shapes to draw in plot windows in real coordinate system // Plot2D // Plot and coordinates data // PLotW // defines actual graphic screens and pages //===== Example classes // PlotT0,PlotT01,PlotT02,PlotT03,PlotT1,PlotT2,PlotT3,PlotT4 abstract class f_x extends Mathd { // single function single independent variable // example f=x*x abstract double func(double x); } abstract class fi_x extends Mathd { // multifunction single independent variable // vector of dependent variables are returned // example f[0]=x+sin(x) // f[1]=x*x-x // func(x) returns the value of f[0] and f[1] // as a two dimensional vector

abstract double[] func(double x); } abstract class f_xj extends Mathd { // single function multi independent variable // a single value is returned indiced to equation_ref // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[]); } public class Plot { // This class reads plot input variables // and sets relations between absolute scale and relative scale plot windows public Color bg=Color.blue; public String skutu1[]={"","","","",""}; public String skutu2[]={"","","","",""}; public double x1,y1; public double x2,y2; public double dx1; public double dy1; public double dx2; public double dy2; public double xl[],yl[],dxl[],dyl[]; public String label="y=f(x)"; // Plot Label public String xlabel="x"; // X axis Label public String ylabel="y"; // Y axis Label public String label_at_xy[]; //additional label at x,y point public double label_x[]; //additional label x points public double label_y[]; //additional label x points public String filename; // file name public double xmin,ymin,xmax,ymax; // real max,min scale public int xabsmin,yabsmin,abswidth,absheight; // absolute max,min scale in Pixel dimension public int nmax; // max number of data points public int ni; // data set number with nmax data public int nline; // max number of data sets public double x[][]; // x data public double y[][]; // y=f(x) data public int n[]; // n : number of data in each line public int red[],green[],blue[]; // color code 0-255 public int xgridon=0; // x axis grid line on public int ygridon=0; // y axis grid line on public int xlog=0; // x axis logaritmic scale public int ylog=0; // y axis logaritmic scale public int plottype[]; PlotW p1; public String plotst[]={"0 ______", "1 __ __ ", "2 . _ . ", "3 . _ _ .", "4 - - - ", "5 bold _____", "6 bold __ __", "7 bold . _ . _", "8 bold . _ _ . .", "9 bold - - - ", "10 * " , "11 # ", "12 $ ", "13 % ", "14 & ",

"15 - ", "16 ~ ", "17 + ", "18 x ", "19 = ", "20 square", "21 filled square", "22 circle", "23 filled circle", "24 triangle", "25 equilateral quadrangle", "26 pentagon", "27 hexagon", "28 filled triangle", "28 filled equilateral quadrangle", "30 filled pentagon", "31 fled hexagon", "32 3 sided star", "33 4 sided star", "34 5 sided star", "35 6 dided star", "36 filled 3 sided star", "37 filled 4 sided star", "38 filled 5 sided star", "39 filled 6 sided star", "40 bar graphic", "41 filled bar graphic"}; double xminmaxlog,yminmaxlog; public char ch[]; // plot character (used with plottype 10) public int xntic=10; // number of tics in x axis public int yntic=10; // number of tics in y axis BufferedReader fini; BufferedReader ffile; File plotFile; File lineDataFile; String fn[]; /* ========== constructors ======================= public Plot() throws IOException : Read actual data from file Plot.txt and plot filenames listed in this file public Plot(String pl,String xl,String yl,int xnt,int ynt ,int xgo,int ygo,String fn[],int ipt[],int ir[],int ig[] ,int ib[]) throws IOException : Read file names from String fn[] and plot them public Plot(String fn[],int ipt[],int ir[],int ig[],int ib[]) throws IOException public Plot(String fn[]) throws IOException : Read file names from String fn[] and plot them public Plot(double xi[][],double yi[][],int ipt[],int ir[],int ig[],int ib[]) : read plot data from xi[][] and yi[][] public Plot(double xiyi[][]) : read plot data xi[] and yi[] from xiyi[][] string public Plot(double xi[][],double yi[][]) : read plot data from xi[][] and yi[][] public Plot(double xi[],double yi[]) : read plot data from xi[] and yi[] public Plot(f_x f,double xm,double xma,int N,int ipt,int ir,int ig,int ib) : create plot data from a simple function f_x f public Plot(f_x f,double xm,double xma,int N) : create plot data from a simple function f_x f public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function) : create plot data from an array of simple functions fi_x f ======== Label settings ======================== public void setPlabel(String ip) {label=ip;} public void setXlabel(String ix) {xlabel=ix;} public void setYlabel(String iy) {ylabel=iy;} public void setXYlabel(String ix,String iy) {xlabel=ix;ylabel=iy;} ======== Setting additional data after initial construction ========= This groups's functions are similar to constructor classes public void addData(double xi[],double yi[]) public void addData(double xi[],double yi[],int iplottype) public void addData(double xi[],double yi[],int iplottype,int ir,int ig,int ib) public void addData(double xi[][],double yi[][])

public void addData(double xi[][],double yi[][],int ip[]) public void addData(double xi[][],double yi[][],int ip[],int ir[],int ig[],int ib[]) public void addFunction(f_x f,double xmi,double xma,int N,int ipt) public void addFunction(f_x f,double xmi,double xma,int N) public void addFunction(f_x f,double xmi,double xma,int N,int ipt,int ir,int ig,int ib) public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function) public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,int ipt[]) public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function, ================== Settings of additional plot information ================= ----------Plot Type ---------------------public void setPlotType(int plot_type[]) public void setPlotType(int dataset,int plot_no) public void setPlotType(int dataset,char plot_char) ----------x y grid on-off ---------------public void setXgrid(int igx) public void setYgrid(int igy) public void setGrid(int igx,int igy) public void setXtic(int ixt) public void setYtic(int iyt) public void setXYtic(int ixt,int iyt) public void setColor(int dataset,int ired,int igreen,int iblue) public void setColor(int ired[],int igreen[],int iblue[]) public void setColor(Color c) ================ Setting of Minimum Maximum fuctions =================== public void setMinMax() public void setMinMax(double xi[][],double yi[][]) public void setMinMax(double iminx,double imaxx,double iminy,double imaxy) =============== Plot Window Formatting ================================== public void set_plotwindow(int width,int height,double xip,double yip,double dxp,double dyp) public void setabsMaxMin(int ixmin,int iymin,int idx,int idy) ================ Actual Plotting of constructed data public void plot() */ public static double[] data(double xmin,double xmax,double dx) { int n=(int)((xmax-xmin)/dx)+1; double x[]=new double[n]; for(int i=0;i<n-1;i++) {x[i]=xmin+dx*i;} x[n-1]=xmax; return x; } public static double[] data(double xmin,double xmax,int n) { double dx=(xmax-xmin)/n; double x[]=new double[n]; for(int i=0;i<n;i++) {x[i]=xmin+dx*i;} return x; } public Plot() throws IOException { label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} //read input variables from Plot.txt //adding max-min prompts and input fields //initial values of max and mins // Structure of input file // filename // xlabel

// ylabel // nline // datafilename plottype redcolor greencolor bluecolor // ........ // datafilename plottype redcolor greencolor bluecolor // // A sample data file input : //========================== // filename // xaxislabel // yaxislabel // 4 // b.dat 22 0 0 0 // a.dat 21 0 255 0 // a.dat 0 0 255 0 // b.dat 0 0 0 255 //========================== xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; try{ fini=new BufferedReader(new FileReader("Plot.txt")); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } label=Text.readStringLine(fini); xlabel=Text.readStringLine(fini); ylabel=Text.readStringLine(fini); nline=Text.readInt(fini); n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; ch[10]='*'; ch[11]='#'; ch[12]='$'; ch[13]='%'; ch[14]='&'; ch[15]='@'; ch[16]='~'; ch[17]='+'; ch[18]='x'; ch[19]='='; //read all data to determine limit values String fn[]; fn=new String[nline]; int i,j; for(i=0;i<nline;i++) { plottype[i]=i; if(plottype[i] == 10) ch[i]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[i] == 11) ch[i]='#'; if(plottype[i] == 12) ch[i]='$'; if(plottype[i] == 13) ch[i]='%'; if(plottype[i] == 14) ch[i]='&'; if(plottype[i] == 15) ch[i]='@'; if(plottype[i] == 16) ch[i]='~'; if(plottype[i] == 17) ch[i]='+'; if(plottype[i] == 18) ch[i]='x'; if(plottype[i] == 19) ch[i]='=';

fn[i]=Text.readString(fini); plottype[i]=Text.readInt(fini); if((plottype[i] >= 10)&&(plottype[i] <= 19)) ch[i]=Text.readChar(fini); red[i]=Text.readInt(fini); green[i]=Text.readInt(fini); blue[i]=Text.readInt(fini); j=0; n[i]=0; //open ffile try{ ffile=new BufferedReader(new FileReader(fn[i])); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } try{ while(ffile!=null) { xtemp=Text.readDouble(ffile); ytemp=Text.readDouble(ffile); if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[i]++; } dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; } catch(EOFException e_eof) { //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } //End of EOFException if(i==0) {nmax=n[i];ni=i;} else { if(n[i]>nmax) {nmax=n[i];ni=i;} } } //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } x=new double[nline][nmax]; y=new double[nline][nmax]; //re-read the data again to load it for(i=0;i<nline;i++) { //open ffile try{ ffile=new BufferedReader(new FileReader(fn[i])); } catch(IOException e)

{ System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } for(j=0;j<n[i];j++) { x[i][j]=Text.readDouble(ffile); y[i][j]=Text.readDouble(ffile); } //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } set_plotwindow(500,400,0.1,0.1,0.9,0.9); //close fini try{ fini.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } }

public Plot(String pl,String xl,String yl,int xnt,int ynt ,int xgo,int ygo,String fn[],int ipt[],int ir[],int ig[] ,int ib[]) throws IOException { // String pl : plot label // String x1 : plot x axis label // String y1 : Plot y axis label label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; setXtic(10); setYtic(10); setGrid(0,0); double xtemp,ytemp; setPlabel(pl); setXlabel(xl); setYlabel(yl); n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=fn.length; for(i=0;i<nline;i++) { plottype[i]=ipt[i]; if(plottype[i] == 10) ch[i]='*';//you can change character set by using setPlotType(0,'%');

if(plottype[i] == 11) ch[i]='#'; if(plottype[i] == 12) ch[i]='$'; if(plottype[i] == 13) ch[i]='%'; if(plottype[i] == 14) ch[i]='&'; if(plottype[i] == 15) ch[i]='@'; if(plottype[i] == 16) ch[i]='~'; if(plottype[i] == 17) ch[i]='+'; if(plottype[i] == 18) ch[i]='x'; if(plottype[i] == 19) ch[i]='='; red[i]=ir[i]; green[i]=ig[i]; blue[i]=ib[i]; j=0; n[i]=0; //open ffile System.out.println("isim ="+fn[i]); try{ ffile=new BufferedReader(new FileReader(fn[i])); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } try{ while(ffile!=null) { xtemp=Text.readDouble(ffile); ytemp=Text.readDouble(ffile); if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[i]++; } dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; } catch(EOFException e_eof) { //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } //End of EOFException if(i==0) {nmax=n[i];ni=i;} else { if(n[i]>nmax) {nmax=n[i];ni=i;} } } //System.out.println("nline = "+nline+"nmax="+nmax); x=new double[nline][nmax]; y=new double[nline][nmax]; //re-read the data again to load it for(i=0;i<nline;i++) { //open ffile try{ ffile=new BufferedReader(new FileReader(fn[i])); } catch(IOException e)

{ System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } for(j=0;j<n[i];j++) { x[i][j]=Text.readDouble(ffile); y[i][j]=Text.readDouble(ffile); } //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } set_plotwindow(500,500,0.1,0.1,0.9,0.9); } public Plot(String fn[],int ipt[],int ir[],int ig[],int ib[]) throws IOException { // reading data from file and initilisation label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=fn.length; for(i=0;i<nline;i++) { plottype[i]=ipt[i]; if(plottype[i] == 10) ch[i]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[i] == 11) ch[i]='#'; if(plottype[i] == 12) ch[i]='$'; if(plottype[i] == 13) ch[i]='%'; if(plottype[i] == 14) ch[i]='&'; if(plottype[i] == 15) ch[i]='@'; if(plottype[i] == 16) ch[i]='~'; if(plottype[i] == 17) ch[i]='+'; if(plottype[i] == 18) ch[i]='x'; if(plottype[i] == 19) ch[i]='='; red[i]=ir[i]; green[i]=ig[i]; blue[i]=ib[i]; j=0; n[i]=0; //open ffile //System.out.println("isim ="+fn[i]); try{ ffile=new BufferedReader(new FileReader(fn[i])); } catch(IOException e)

{ System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } try{ while(ffile!=null) { xtemp=Text.readDouble(ffile); ytemp=Text.readDouble(ffile); if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[i]++; } dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; } catch(EOFException e_eof) { //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } //End of EOFException if(i==0) {nmax=n[i];ni=i;} else { if(n[i]>nmax) {nmax=n[i];ni=i;} } } x=new double[nline][nmax]; y=new double[nline][nmax]; //re-read the data again to load it for(i=0;i<nline;i++) { //open ffile try{ ffile=new BufferedReader(new FileReader(fn[i])); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } for(j=0;j<n[i];j++) { x[i][j]=Text.readDouble(ffile); y[i][j]=Text.readDouble(ffile); } //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } set_plotwindow(500,500,0.1,0.1,0.9,0.9); }

public Plot(String fn[]) throws IOException { //reading data from file(s) label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} nline=fn.length; int ipt[]=new int[nline]; int ir[]=new int[nline]; int ig[]=new int[nline]; int ib[]=new int[nline]; xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=fn.length; for(i=0;i<nline;i++) { plottype[i]=ipt[i]; if(plottype[i] == 10) ch[i]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[i] == 11) ch[i]='#'; if(plottype[i] == 12) ch[i]='$'; if(plottype[i] == 13) ch[i]='%'; if(plottype[i] == 14) ch[i]='&'; if(plottype[i] == 15) ch[i]='@'; if(plottype[i] == 16) ch[i]='~'; if(plottype[i] == 17) ch[i]='+'; if(plottype[i] == 18) ch[i]='x'; if(plottype[i] == 19) ch[i]='='; red[i]=ir[i]; green[i]=ig[i]; blue[i]=ib[i]; j=0; n[i]=0; //open ffile //System.out.println("isim ="+fn[i]); try{ ffile=new BufferedReader(new FileReader(fn[i])); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } try{ while(ffile!=null) { xtemp=Text.readDouble(ffile); ytemp=Text.readDouble(ffile); if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[i]++; }

dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; } catch(EOFException e_eof) { //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } //End of EOFException if(i==0) {nmax=n[i];ni=i;} else { if(n[i]>nmax) {nmax=n[i];ni=i;} } } x=new double[nline][nmax]; y=new double[nline][nmax]; //re-read the data again to load it for(i=0;i<nline;i++) { //open ffile try{ ffile=new BufferedReader(new FileReader(fn[i])); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } for(j=0;j<n[i];j++) { x[i][j]=Text.readDouble(ffile); y[i][j]=Text.readDouble(ffile); } //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } set_plotwindow(500,500,0.1,0.1,0.9,0.9); } public Plot(String fn) throws IOException { label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} //reading data from file(s) nline=1; int ipt[]=new int[nline]; int ir[]=new int[nline]; int ig[]=new int[nline]; int ib[]=new int[nline]; xmin=9.99e50; xmax=-9.99e50;

ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=1; for(i=0;i<nline;i++) { plottype[i]=ipt[i]; if(plottype[i] == 10) ch[i]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[i] == 11) ch[i]='#'; if(plottype[i] == 12) ch[i]='$'; if(plottype[i] == 13) ch[i]='%'; if(plottype[i] == 14) ch[i]='&'; if(plottype[i] == 15) ch[i]='@'; if(plottype[i] == 16) ch[i]='~'; if(plottype[i] == 17) ch[i]='+'; if(plottype[i] == 18) ch[i]='x'; if(plottype[i] == 19) ch[i]='='; red[i]=ir[i]; green[i]=ig[i]; blue[i]=ib[i]; j=0; n[i]=0; //open ffile //System.out.println("isim ="+fn[i]); try{ ffile=new BufferedReader(new FileReader(fn)); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } try{ while(ffile!=null) { xtemp=Text.readDouble(ffile); ytemp=Text.readDouble(ffile); if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[i]++; } dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; } catch(EOFException e_eof) { //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } //End of EOFException

if(i==0) {nmax=n[i];ni=i;} else { if(n[i]>nmax) {nmax=n[i];ni=i;} } } x=new double[nline][nmax]; y=new double[nline][nmax]; //re-read the data again to load it for(i=0;i<nline;i++) { //open ffile try{ ffile=new BufferedReader(new FileReader(fn)); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } for(j=0;j<n[i];j++) { x[i][j]=Text.readDouble(ffile); y[i][j]=Text.readDouble(ffile); } //close ffile try{ ffile.close(); } catch(IOException e) { System.err.println("Error Closing File\n"+e.toString()); System.exit(1); } } set_plotwindow(500,500,0.1,0.1,0.9,0.9); }

public Plot(double xi[][],double yi[][],int ipt[],int ir[],int ig[],int ib[]) { pp2(xi,yi,ipt,ir,ig,ib); } public void pp2(double xi[][],double yi[][]) { int ir[]; int ig[]; int ib[]; int ipt[]; int nn=xi.length; ir=new int[nn]; ig=new int[nn]; ib=new int[nn]; ipt=new int[nn]; for(int i=0;i<nn;i++) { ir[i]=0; ig[i]=0; ib[i]=0; ipt[i]=0; } pp2(xi,yi,ipt,ir,ig,ib); } public void pp2(double xi[][],double yi[][],int ipt[]) {

int ir[]; int ig[]; int ib[]; int nn=xi.length; ir=new int[nn]; ig=new int[nn]; ib=new int[nn]; for(int i=0;i<nn;i++) { ir[i]=0; ig[i]=0; ib[i]=0; } pp2(xi,yi,ipt,ir,ig,ib); } public void pp2(double xi[][],double yi[][],int ipt[],int ir[],int ig[],int ib[]) { // reading data from initial array label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=xi.length; for(i=0;i<nline;i++) { plottype[i]=ipt[i]; if(plottype[i] == 10) ch[i]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[i] == 11) ch[i]='#'; if(plottype[i] == 12) ch[i]='$'; if(plottype[i] == 13) ch[i]='%'; if(plottype[i] == 14) ch[i]='&'; if(plottype[i] == 15) ch[i]='@'; if(plottype[i] == 16) ch[i]='~'; if(plottype[i] == 17) ch[i]='+'; if(plottype[i] == 18) ch[i]='x'; if(plottype[i] == 19) ch[i]='='; red[i]=ir[i]; green[i]=ig[i]; blue[i]=ib[i]; nline=xi.length; n[i]=0; for(j=0;j<xi[i].length;j++) { xtemp=xi[i][j]; ytemp=yi[i][j]; if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[i]++; }

dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; if(i==0) {nmax=n[i];ni=i;} else { if(n[i]>nmax) {nmax=n[i];ni=i;} } } //end of for x=new double[nline][nmax]; y=new double[nline][nmax]; for(i=0;i<nline;i++) { for(j=0;j<n[i];j++) { x[i][j]=xi[i][j]; y[i][j]=yi[i][j]; } } set_plotwindow(500,500,0.1,0.1,0.9,0.9); } public void pp2(double ai[][],int ipt[],int ir[],int ig[],int ib[]) { // reading data from initial array label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=ai.length-1; double xi[][]=new double[nline][ai[0].length]; double yi[][]=new double[nline][ai[0].length]; for(i=0;i<nline;i++) { plottype[i]=ipt[i]; if(plottype[i] == 10) ch[i]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[i] == 11) ch[i]='#'; if(plottype[i] == 12) ch[i]='$'; if(plottype[i] == 13) ch[i]='%'; if(plottype[i] == 14) ch[i]='&'; if(plottype[i] == 15) ch[i]='@'; if(plottype[i] == 16) ch[i]='~'; if(plottype[i] == 17) ch[i]='+'; if(plottype[i] == 18) ch[i]='x'; if(plottype[i] == 19) ch[i]='='; red[i]=ir[i]; green[i]=ig[i]; blue[i]=ib[i]; nline=xi.length; n[i]=0; for(j=0;j<xi[i].length;j++)

{ xtemp=ai[0][j]; ytemp=ai[i+1][j]; xi[i][j]=xtemp; yi[i][j]=ytemp; if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[i]++; } dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; if(i==0) {nmax=n[i];ni=i;} else { if(n[i]>nmax) {nmax=n[i];ni=i;} } } //end of for x=new double[nline][nmax]; y=new double[nline][nmax]; for(i=0;i<nline;i++) { for(j=0;j<n[i];j++) { x[i][j]=xi[i][j]; y[i][j]=yi[i][j]; } } set_plotwindow(500,500,0.1,0.1,0.9,0.9); } public void pp2(double ai[][],int ipt[]) { int ir[]; int ig[]; int ib[]; int nn=ai.length-1; ir=new int[nn]; ig=new int[nn]; ib=new int[nn]; for(int i=0;i<nn;i++) { ir[i]=0; ig[i]=0; ib[i]=0; } pp2(ai,ipt,ir,ig,ib); } public void pp2(double ai[][]) { int ir[]; int ig[]; int ib[]; int ipt[]; int nn=ai.length-1; ir=new int[nn]; ig=new int[nn]; ib=new int[nn]; ipt=new int[nn]; for(int i=0;i<nn;i++) { ir[i]=0;

ig[i]=0; ib[i]=0; } pp2(ai,ipt,ir,ig,ib); } public Plot(double ai[][]) { pp2(ai); } public Plot(double xi[][],double yi[][]) { pp2(xi,yi); }

public void pp1( double xi[],double yi[]) { pp1(xi,yi,0,0,0,0); }

public void pp1( double xi[],double yi[],int ipt) { pp1(xi,yi,ipt,0,0,0); } public void ppA( double xi[],double yi[],int ipt,int ir,int ig,int ib) { // reading data from initial array label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=1; plottype[0]=ipt; red[0]=ir; green[0]=ig; blue[0]=ib; nline=1; n[0]=0; plottype[0]=ipt; if(plottype[0] == 10) ch[0]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[0] == 11) ch[0]='#'; if(plottype[0] == 12) ch[0]='$'; if(plottype[0] == 13) ch[0]='%'; if(plottype[0] == 14) ch[0]='&'; if(plottype[0] == 15) ch[0]=''; if(plottype[0] == 16) ch[0]='~'; if(plottype[0] == 17) ch[0]='+'; if(plottype[0] == 18) ch[0]='^'; if(plottype[0] == 19) ch[0]='='; for(j=0;j<xi.length;j++) { xtemp=xi[j]; ytemp=yi[j]; n[0]++;

} nmax=n[0];ni=0; x=new double[nline][nmax]; y=new double[nline][nmax]; i=0; for(j=0;j<n[0];j++) { x[i][j]=xi[j]; y[i][j]=yi[j]; } set_plotwindow(500,500,0.1,0.1,0.9,0.9); } public void pp1( double xi[],double yi[],int ipt,int ir,int ig,int ib) { // reading data from initial array label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=1; plottype[0]=ipt; red[0]=ir; green[0]=ig; blue[0]=ib; nline=1; n[0]=0; plottype[0]=ipt; if(plottype[0] == 10) ch[0]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[0] == 11) ch[0]='#'; if(plottype[0] == 12) ch[0]='$'; if(plottype[0] == 13) ch[0]='%'; if(plottype[0] == 14) ch[0]='&'; if(plottype[0] == 15) ch[0]=''; if(plottype[0] == 16) ch[0]='~'; if(plottype[0] == 17) ch[0]='+'; if(plottype[0] == 18) ch[0]='^'; if(plottype[0] == 19) ch[0]='='; for(j=0;j<xi.length;j++) { xtemp=xi[j]; ytemp=yi[j]; if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[0]++; } dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; nmax=n[0];ni=0;

x=new double[nline][nmax]; y=new double[nline][nmax]; i=0; for(j=0;j<n[0];j++) { x[i][j]=xi[j]; y[i][j]=yi[j]; } set_plotwindow(500,500,0.1,0.1,0.9,0.9); } public void pp1( double yi[],int ipt,int ir,int ig,int ib) { // reading data from initial array label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; n[0]=yi.length; double xi[]=new double[n[0]]; for(int i=0;i<n[0];i++) {xi[i]=i+1;} red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; //read all data to determine limit values int i,j; nline=1; plottype[0]=ipt; red[0]=ir; green[0]=ig; blue[0]=ib; nline=1; n[0]=0; plottype[0]=ipt; if(plottype[0] == 10) ch[0]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[0] == 11) ch[0]='#'; if(plottype[0] == 12) ch[0]='$'; if(plottype[0] == 13) ch[0]='%'; if(plottype[0] == 14) ch[0]='&'; if(plottype[0] == 15) ch[0]=''; if(plottype[0] == 16) ch[0]='~'; if(plottype[0] == 17) ch[0]='+'; if(plottype[0] == 18) ch[0]='^'; if(plottype[0] == 19) ch[0]='='; for(j=0;j<xi.length;j++) { xtemp=xi[j]; ytemp=yi[j]; if(xtemp<xmin) xmin=xtemp; if(xtemp>xmax) xmax=xtemp; if(ytemp<ymin) ymin=ytemp; if(ytemp>ymax) ymax=ytemp; n[0]++; } dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; dx2=(xmax-xmin)/30.0;

dy2=(ymax-ymin)/30.0; nmax=n[0];ni=0; x=new double[nline][nmax]; y=new double[nline][nmax]; i=0; for(j=0;j<n[0];j++) { x[i][j]=xi[j]; y[i][j]=yi[j]; } set_plotwindow(500,500,0.1,0.1,0.9,0.9); } public void pp1( double yi[],int ipt) {pp1(yi,ipt,0,0,0);} public void pp1( double yi[]) {pp1(yi,0,0,0,0);} public Plot(double xi[],double yi[]) { pp1(xi,yi); } public Plot(double yi[]) { pp1(yi); } public Plot(double yi[],int ipt) { pp1(yi,ipt); } public Plot(double yi[],int ipt,int ir,int ig,int ib) { pp1(yi,ipt,ir,ig,ib); } public void pp10(f_xj f,double xmin,double xmax,double ymin,double ymax,int Nx,int Ny, int Nz,int ipt,int ir,int ig,int ib) { double z[][]=new double[Nx][Ny]; double Zc[]=new double[Nz]; double xc[][]; double yc[][]; double zmax=-9e99; double zmin=9e99; double xx=0; double yy=0; double dx=(xmax-xmin)/Nx; double dy=(ymax-ymin)/Ny; double x[]=new double[2]; for(int i=0;i<Nx;i++) {for(int j=0;j<Ny;j++) {x[0]=xmin+dx*i;x[1]=ymin+dy*j;z[i][j]=f.func(x); if(z[i][j]<zmin) zmin=z[i][j]; if(z[i][j]>zmax) zmax=z[i][j]; } } double dz=(zmax-zmin)/Nz; for(int i=0;i<Nz;i++) {Zc[i]=zmin+i*dz;}

} public void pp3(f_x f,double xm,double xma,int N,int ipt,int ir,int ig,int ib) { // reading data from a given function//one data set only

label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} xmin=xm; xmax=xma; nmax=N; ni=0; ymin=9.99e50; ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; double xi[]=new double[N]; double yi[]=new double[N]; double dx=(xmax-xmin)/(N-1); double xx=xmin; for(int j=0;j<N;j++) { xi[j]=xx; yi[j]=f.func(xx); xx+=dx; } pp1(xi,yi,ipt,ir,ig,ib); } public Plot(f_x f,double xm,double xma,int N,int ipt,int ir,int ig,int ib) { pp3(f,xm,xma,N,ipt,ir,ig,ib); } public Plot(f_x f,double xm,double xma,int N,int ipt) { // reading data from a given function//one data set only int ir=0; int ig=0; int ib=0; pp3(f,xm,xma,N,ipt,ir,ig,ib); } public Plot(f_x f,double xm,double xma,int N) { // reading data from a given function//one data set only int ipt=0; int ir=0; int ig=0; int ib=0; pp3(f,xm,xma,N,ipt,ir,ig,ib); } public void pp4(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function,int ipt[],int ir[],int ig[],int ib[]) { // reading data from a given functon set // same xmin xmax and Number_of_data for each function nline=Number_of_Function; label_at_xy=new String[5]; label_x=new double[5]; label_y=new double[5]; for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} xmin=xm; xmax=xma; ymin=9.99e50;

ymax=-9.99e50; double xtemp,ytemp; n=new int[500]; red=new int[500]; green=new int[500]; blue=new int[500]; plottype=new int[500]; ch=new char[500]; int N=Number_of_Data; double dx=(xmax-xmin)/N; double xx=xmin; int NF=Number_of_Function; nline=NF; double xi[][]=new double[NF][N]; double yi[][]=new double[NF][N]; double yy[]=new double[NF]; for(int j=0;j<N;j++) { yy=f.func(xx); for(int i=0;i<NF;i++) { yi[i][j]=yy[i]; xi[i][j]=xx;} xx+=dx; } pp2(xi,yi,ipt,ir,ig,ib); } public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function, int ipt[],int ir[],int ig[],int ib[]) { pp4(f,xm,xma,Number_of_Data,Number_of_Function,ipt,ir,ig,ib); } public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function, int ipt[]) { int ir[]; int ig[]; int ib[]; int nn=Number_of_Function; ir=new int[nn]; ig=new int[nn]; ib=new int[nn]; for(int i=0;i<nn;i++) { ir[i]=0; ig[i]=0; ib[i]=0; } pp4(f,xm,xma,Number_of_Data,Number_of_Function,ipt,ir,ig,ib); } public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function) { int ir[]; int ig[]; int ib[]; int ipt[]; int nn=Number_of_Function; ir=new int[nn]; ig=new int[nn]; ib=new int[nn]; ipt=new int[nn]; for(int i=0;i<nn;i++) { ir[i]=0;

ig[i]=0; ib[i]=0; ipt[i]=0; } pp4(f,xm,xma,Number_of_Data,Number_of_Function,ipt,ir,ig,ib); } public void setPlabel(String ip) {label=ip;} public void setXlabel(String ix) {xlabel=ix;} public void setYlabel(String iy) {ylabel=iy;} public void setXYlabel(String ix,String iy) {xlabel=ix;ylabel=iy;} public void set_plotwindow(int width,int height,double xip,double yip,double dxp,double dyp) { //sets rectangular plot window //function scale input variables : //real scale plot window width //real scale plot window height //xip: minimum real scale x //yip : minimum real scale y //dxp: real scale x width xmax=xip+dxp //dyp: real scale y height ymax=yip+dyp // xabsmin : absolute (pixel) scale minimum x // yabsmin : absolute (pixel) scale minimum y //abswidth : absolute (pixel) scale x width (maximum x=minimum x+abswidth) //absheight: absolute (pixel) scale y height (maximum y=minimum y+absheight) xabsmin = yabsmin = abswidth = absheight = } public void setabsMaxMin(int ixmin,int iymin,int idx,int idy) { xabsmin=ixmin; yabsmin=iymin; abswidth=idx; absheight=idy; } public void addStringwithPlotType(String s[],double x1i,double y1i) { //adding a series of strings with plottype signs at the beginning for(int i=0;i<s.length;i++) {skutu1[i]=s[i];} x1=x1i; y1=y1i; dx1=(xmax-xmin)/30.0; dy1=(ymax-ymin)/30.0; } public void addString(String s[],double x2i,double y2i) { // adding a series of strings for(int i=0;i<s.length;i++) {skutu2[i]=s[i];} x2=x2i; y2=y2i; dx2=(xmax-xmin)/30.0; dy2=(ymax-ymin)/30.0; } public void addString(String s,double x3i,double y3i,int i) { // adding a series of strings if(i>4) i=4; else if(i<0) i=0; (int)(width*(xip+0.2)); (int)(height*(yip+0.2)); (int)(width*dxp); (int)(height*dyp);

label_at_xy[i]=s; label_x[i]=x3i; label_y[i]=y3i; } public void addData(String fn) { //adds data from file double x[]; double y[]; double a[][]=Text.readDoubleT(fn); x=a[0]; y=a[1]; addData(x,y); } public void addData(double xi[],double yi[]) { addData(xi,yi,0,0,0,0); } public void addData(double ai[][]) { pp2(ai); } public void addData(double ai[][],int iplottype[],int ir[],int ig[],int ib[]) {pp2(ai,iplottype,ir,ig,ib);} public void addData(double ai[][],int iplottype[]) {pp2(ai,iplottype);} public void addData(double xi[],double yi[],int iplottype) { addData(xi,yi,iplottype,0,0,0); } public void addData(double xi[],double yi[],int iplottype,int ir,int ig,int ib) { int oldnmax=nmax; int oldnline=nline; nline+=1; n[oldnline]=xi.length; plottype[nline-1]=iplottype; if(plottype[nline-1] == 10) ch[nline-1]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[nline-1] == 11) ch[nline-1]='#'; if(plottype[nline-1] == 12) ch[nline-1]='$'; if(plottype[nline-1] == 13) ch[nline-1]='%'; if(plottype[nline-1] == 14) ch[nline-1]='&'; if(plottype[nline-1] == 15) ch[nline-1]='-'; if(plottype[nline-1] == 16) ch[nline-1]='~'; if(plottype[nline-1] == 17) ch[nline-1]='+'; if(plottype[nline-1] == 18) ch[nline-1]='^'; if(plottype[nline-1] == 19) ch[nline-1]='='; red[nline-1]=ir; green[nline-1]=ig; blue[nline-1]=ib; if(xi.length>nmax) { nmax=xi.length; ni=nline; } double xa[][]=new double[nline][nmax]; double ya[][]=new double[nline][nmax]; for(int i=0;i<oldnline;i++) { for(int j=0;j<n[i];j++)

{ xa[i][j]=x[i][j]; ya[i][j]=y[i][j]; } } for(int j=0;j<n[nline-1];j++) { xa[nline-1][j]=xi[j]; ya[nline-1][j]=yi[j]; } x=xa; y=ya; setMinMax(); } public void addData(double xi[][],double yi[][]) { int ip[]=new int[0]; int ir[]=new int[0]; int ig[]=new int[0]; int ib[]=new int[0]; addData(xi,yi,ip,ir,ig,ib); } public void addData(double xi[][],double yi[][],int ip[]) { int ir[]=new int[0]; int ig[]=new int[0]; int ib[]=new int[0]; addData(xi,yi,ip,ir,ig,ib); } public void addData(double xi[][],double yi[][],int ip[],int ir[],int ig[],int ib[]) { int oldnmax=nmax; int oldnline=nline; nline+=xi.length; for(int i=oldnline;i<nline;i++) { plottype[i]=ip[i-oldnline]; if(plottype[i] == 10) ch[i]='*';//you can change character set by using setPlotType(0,'%'); if(plottype[i] == 11) ch[i]='#'; if(plottype[i] == 12) ch[i]='$'; if(plottype[i] == 13) ch[i]='%'; if(plottype[i] == 14) ch[i]='&'; if(plottype[i] == 15) ch[i]=''; if(plottype[i] == 16) ch[i]='~'; if(plottype[i] == 17) ch[i]='+'; if(plottype[i] == 18) ch[i]='^'; if(plottype[i] == 19) ch[i]='='; red[i]=ir[i-oldnline]; green[i]=ig[i-oldnline]; blue[i]=ib[i-oldnline]; n[i]=xi[i-oldnline].length; if(xi[i-oldnline].length>nmax) { nmax=xi[i-oldnline].length; ni=i; } } double xa[][]=new double[nline][nmax]; double ya[][]=new double[nline][nmax]; for(int i=0;i<oldnline;i++) { for(int j=0;j<n[i];j++) {

xa[i][j]=x[i][j]; ya[i][j]=y[i][j]; } } int k=0; for(int i=oldnline;i<nline;i++) { for(int j=0;j<n[i];j++) { xa[i][j]=xi[i-oldnline][j]; ya[i][j]=yi[i-oldnline][j]; } } x=xa; y=ya; setMinMax(); }

public void addFunction(f_x f,double xmi,double xma,int N,int ipt) { addFunction(f,xmi,xma,N,ipt,0,0,0); } public void addFunction(f_x f,double xmi,double xma,int N) { addFunction(f,xmi,xma,N,0); } public void addFunction(f_x f,double xmi,double xma,int N,int ipt,int ir,int ig,int ib) { double xi[]=new double[N]; double yi[]=new double[N]; double dx=(xma-xmi)/N; double xx=xmi; for(int j=0;j<N;j++) { xi[j]=xx; yi[j]=f.func(xx); xx+=dx; } addData(xi,yi,ipt,ir,ig,ib); } public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function) { int ipt[]=new int[Number_of_Function]; int ir[]=new int[Number_of_Function]; int ig[]=new int[Number_of_Function]; int ib[]=new int[Number_of_Function]; addFunction(f,xmin,xmax,Number_of_Data,Number_of_Function,ipt,ir,ig,ib); } public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,int ipt[]) { int ir[]=new int[Number_of_Function]; int ig[]=new int[Number_of_Function]; int ib[]=new int[Number_of_Function]; addFunction(f,xmin,xmax,Number_of_Data,Number_of_Function,ipt,ir,ig,ib); } public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function, int ipt[],int ir[],int ig[],int ib[]) { int N=Number_of_Data; double dx=(xmax-xmin)/N;

double xx=xmin; int NF=Number_of_Function; double xi[][]=new double[NF][N]; double yi[][]=new double[NF][N]; double yy[]=new double[NF]; for(int j=0;j<N;j++) { yy=f.func(xx); for(int i=0;i<NF;i++) { yi[i][j]=yy[i]; xi[i][j]=xx;} xx+=dx; } addData(xi,yi,ipt,ir,ig,ib); }

public void setPlotType(int plot_type[]) { for(int i=0;i<plot_type.length;i++) { setPlotType(i,plot_type[i]); } setMinMax(); } public void setXlogScaleOn() {xlog=1;xminmaxlog=Math.log10(xmax-xmin+10.0)-1.0;} public void setYlogScaleOn() {ylog=1;yminmaxlog=Math.log10(ymax-ymin+10.0)-1.0;} public void setXlogScaleOff() {xlog=0;} public void setYlogScaleOff() {ylog=0;} public void setPlotType(int dataset,int plot_no) { // plottype = 0 continuous line // plottype = 1 dashed line // plottype = 2 // plottype = 3 // plottype = 10 variable character plot type // plottype = 10 ch[i]='*';//you can change character set by using setPlotType(0,'%'); // plottype = 11 ch[i]='#' // plottype = 12 ch[i]='$' // plottype = 13 ch[i]='%' // plottype = 14 ch[i]='&' // plottype = 15 ch[i]='' // plottype = 16 ch[i]='~' // plottype = 17 ch[i]='+' //plottype = 18 ch[i]='^' //plottype = 19 ch[i]='=' // plottype = 20 rectangle // plottype = 21 filled rectangle // plottype = 22 circle // plottype = 23 filled circle // plottype = 24 triangle // plottype = 25 diamond // plottype = 26 pentagon // plottype = 27 hexagon // plottype = 28 filled triangle // plottype = 29 filled diamond // plottype = 30 filled pentagon

// plottype = 31 filled hexagon // plottype = 32 triangle star // plottype = 33 diamond star // plottype = 34 pentagon star // plottype = 35 hexagon star // plottype = 36 filled triangle star // plottype = 37 filled diamond star // plottype = 38 filled pentagon star // plottype = 39 filled hexagon star // plottype = 40 bar plot // plottype = 41 filled bar plot plottype[dataset]=plot_no; if(plot_no==10) ch[dataset]='*'; //user defined char is defined as * } public void setPlotType(int dataset,char plot_char) { // plottype = 10 user defined character plot plottype[dataset]=10; ch[dataset]=plot_char; }

public void setXgrid(int igx) { xgridon=igx; } public void setYgrid(int igy) { ygridon=igy; } public void setGrid(int igx,int igy) { xgridon=igx; ygridon=igy; } public void setXtic(int ixt) { xntic=ixt; } public void setYtic(int iyt) { yntic=iyt; } public void setXYtic(int ixt,int iyt) { xntic=ixt; yntic=iyt; } public void setColor(int dataset,int ired,int igreen,int iblue) { red[dataset]=ired; green[dataset]=igreen; blue[dataset]=iblue; } public void setColor(int dataset,Color x) { red[dataset]=x.getRed(); green[dataset]=x.getGreen();

blue[dataset]=x.getBlue(); } public void setColor(int ired[],int igreen[],int iblue[]) { for(int i=0;i<nline;i++) { red[i]=ired[i]; green[i]=igreen[i]; blue[i]=iblue[i]; } }

public void setColor(Color x[]) { for(int i=0;i<nline;i++) { red[i]=x[i].getRed(); green[i]=x[i].getGreen(); blue[i]=x[i].getBlue(); } } public void setMinMax() { xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; for(int i=0;i<nline;i++) { for(int j=0;j<n[i];j++) { if(x[i][j]>xmax) xmax=x[i][j]; if(x[i][j]<xmin) xmin=x[i][j]; if(y[i][j]>ymax) ymax=y[i][j]; if(y[i][j]<ymin) ymin=y[i][j]; } } } public void setMinMax(double xi[][],double yi[][]) { xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; for(int i=0;i<nline;i++) { if(xi[i].length>nmax) {nmax=xi[i].length;ni=i;} for(int j=0;j<xi[i].length;j++) { if(xi[i][j]>xmax) xmax=xi[i][j]; if(xi[i][j]<xmin) xmin=xi[i][j]; if(yi[i][j]>ymax) ymax=yi[i][j]; if(yi[i][j]<ymin) ymin=yi[i][j]; } } } public void setMinMax(double iminx,double imaxx,double iminy,double imaxy) { xmin=iminx; ymin=iminy; xmax=imaxx;

ymax=imaxy; } public void plot() { p1=new PlotW(); p1.plot(this); } public void dispose() { p1.pencere.setVisible( false); p1.pencere.dispose(); } public void exit() {System.exit(0);} //==== B-Spline intrepolation============= public static double[] hi(double ti[]) { int n=ti.length-1;//ti 0..n int nn=n+2; double h[]=new double[nn]; for(int i=1;i<=n;i++) {h[i]=ti[i]-ti[i-1]; } h[0]=h[1]; h[n+1]=h[n]; return h; } public static double [] B_Spline(double ti[],double yi[]) { int n=ti.length-1; int nn=n+2; int i; double delta,gamma,p,q,r; double a[]=new double[nn]; double h[]=new double[nn]; h=hi(ti); delta=-1; gamma=2.0*yi[0]; p=delta*gamma; q=2.0; for(i=1;i<=n;i++) {r=h[i+1]/h[i]; delta=-r*delta; gamma=-r*gamma+(r+1)*yi[i]; p=p+gamma*delta; q=q+delta*delta; } a[0]=-p/q; for(i=1;i<=(n+1);i++) {a[i]=((h[i-1]+h[i])*yi[i-1]-h[i]*a[i-1])/h[i-1];} return a; } public static double funcB_Spline(double ti[],double a[],double x) { double h[]=hi(ti); int i; int n=ti.length-1; double nn=n+2; double d,e; for(i=(n-1);i>=1;i--) {if((x-ti[i])>=0) break;}

i++; d=(a[i+1]*(x-ti[i-1])+a[i]*(ti[i]-x+h[i+1]))/(h[i]+h[i+1]); e=(a[i]*(x-ti[i-1]+h[i-1])+a[i-1]*(ti[i-1]-x+h[i]))/(h[i-1]+h[i]); double bs=(d*(x-ti[i-1])+e*(ti[i]-x))/h[i]; return bs; } public static double[][] funcB_Spline(double ti[],double yi[],int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=ti.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double A[]=B_Spline(ti,yi); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=ti[i];z[1][k]=funcB_Spline(ti,A,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(ti[i+1]-ti[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcB_Spline(ti,A,z[0][k]);k++;} } z[0][k]=ti[i];z[1][k]=funcB_Spline(ti,A,z[0][k]); return z; } public void addB_Spline(double xi[],double yi[],int nSpline) { addSpline(xi,yi,nSpline,0,0,0,0); } public void addB_Spline(double ai[][],int nSpline) { addSpline(ai[0],ai[1],nSpline,0,0,0,0); } public void addB_Spline(double xi[],double yi[],int nSpline,int iplottype) { addB_Spline(xi,yi,nSpline,iplottype,0,0,0); } public void addB_Spline(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) { double S[][]=funcB_Spline(xi,yi,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of spline function) } public void addB_Spline(int linenumber,int nSpline) {addB_Spline(linenumber,nSpline,0,0,0,0);} public void addB_Spline(int linenumber,int nSpline,int iplottype) {addB_Spline(linenumber,nSpline,iplottype,0,0,0);} public void addB_Spline(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) { double x2[]=new double[n[linenumber]]; double y2[]=new double[n[linenumber]]; if(linenumber<0) linenumber=0; else if(linenumber>nline) linenumber=nline; for(int j=0;j<n[linenumber];j++) { x2[j]=x[linenumber][j]; y2[j]=y[linenumber][j]; } double S[][]=funcB_Spline(x2,y2,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of spline function)

} //==== spline interpolation=============== public static double [] thomas(double a[][],double r[]) { // int n=a.length; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double x[]=new double[n]; for(int i=0;i<n;i++) {f[i]=a[i][i];} for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];} for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];} for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; } public static double [] thomas(double f[],double e[],double g[],double r[]) { int n=f.length; double x[]=new double[n]; for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; }

public static double [][] cubic_spline(double xi[],double yi[],double c0,double cn) { int n=xi.length; double h[]=new double[n]; double w[]=new double[n]; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double d[]=new double[n]; double x[]=new double[n]; double S[][]=new double[4][n]; int k; for(k=0;k<(n-1);k++) {h[k]=xi[k+1]-xi[k]; w[k]=(yi[k+1]-yi[k])/h[k]; } d[0]=c0; d[n-1]=cn; for(k=1;k<(n-1);k++) {d[k]=6.0*(w[k]-w[k-1]);} f[0]=1.0; f[n-1]=1.0;

g[0]=0.0; g[n-1]=0.0; e[0]=0.0; e[n-1]=0.0; for(k=1;k<(n-1);k++) {f[k]=2.0*(h[k]+h[k-1]);e[k]=h[k-1];g[k]=h[k];} S[2]=thomas(f,e,g,d); S[3]=xi; for(k=0;k<(n-1);k++) {S[0][k]=(6.*yi[k+1]-h[k]*h[k]*S[2][k+1])/(6.0*h[k]); S[1][k]=(6.*yi[k]-h[k]*h[k]*S[2][k])/(6.0*h[k]); } return S; } public static double funcSpline(double S[][],double x) { int n=S[0].length; double xx1=0; double xx2=0; double y=0; double hk=0; for(int k=0;k<(n-1);k++) {if(S[3][k]<=x && x<=S[3][k+1]) {hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); break; } } if(y==0 && S[3][n-2]<=x ) { int k=n-2; hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); } return y; } public static double[][] funcSpline(double xi[],double yi[],int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double S[][]=cubic_spline(xi,yi,0,0); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcSpline(S,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]); return z; }

public void addSpline(double xi[],double yi[],int nSpline) {

addSpline(xi,yi,nSpline,0,0,0,0); } public void addSpline(double ai[][],int nSpline) { addSpline(ai[0],ai[1],nSpline,0,0,0,0); } public void addSpline(double xi[],double yi[],int nSpline,int iplottype) { addSpline(xi,yi,nSpline,iplottype,0,0,0); } public void addSpline(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) { double S[][]=funcSpline(xi,yi,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of spline function) } public void addSpline(int linenumber,int nSpline) {addSpline(linenumber,nSpline,0,0,0,0);} public void addSpline(int linenumber,int nSpline,int iplottype) {addSpline(linenumber,nSpline,iplottype,0,0,0);} public void addSpline(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) { double x2[]=new double[n[linenumber]]; double y2[]=new double[n[linenumber]]; if(linenumber<0) linenumber=0; else if(linenumber>nline) linenumber=nline; for(int j=0;j<n[linenumber];j++) { x2[j]=x[linenumber][j]; y2[j]=y[linenumber][j]; } double S[][]=funcSpline(x2,y2,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of spline function) } //==========orthogonal polynomial interpolation (curve fitting) public static double[][] OPEKK(double xi[],double fi[],int m) //ortogonal polinom en kk kareler metodu //Referans : A First Course in Numerical Analysis // Anthony Ralston,Philip Rabinowitz, Mc Graw Hill ISBN 0-07-051158-6 //m polinom derecesi xi fi girdi verisi { int i,j,k; int n=xi.length; int mp2=n+2; int mp1=n+1; double p[][]=new double[mp2][n]; double gamma[]=new double[mp1]; double beta[]=new double[mp1]; double omega[]=new double[mp1]; double alpha[]=new double[mp1]; double b[]=new double[mp1]; double wi[]=new double[n]; double a[][]=new double[3][mp1]; for(i=0;i<n;i++) { p[1][i]=1.0; p[0][i]=0.0; wi[i]=1.0; } gamma[0]=0; for(i=0;i<n;i++) {

gamma[0]+=wi[i]; } beta[0]=0.0; for(j=0;j<m+1;j++) { omega[j]=0; for(i=0;i<n;i++) {omega[j]+=wi[i]*fi[i]*p[j+1][i];} b[j]=omega[j]/gamma[j]; if( j != m) { alpha[j+1]=0; for(i=0;i<n;i++) { alpha[j+1]+=wi[i]*xi[i]*p[j+1][i]*p[j+1][i]/gamma[j];} for(i=0;i<n;i++) {p[j+2][i]=(xi[i]-alpha[j+1])*p[j+1][i]-beta[j]*p[j][i];} gamma[j+1]=0; for(i=0;i<n;i++) {gamma[j+1]+=wi[i]*p[j+2][i]*p[j+2][i];} beta[j+1]=gamma[j+1]/gamma[j]; } }//end of j for(j=0;j<m+1;j++) { a[0][j]=b[j]; a[1][j]=alpha[j]; a[2][j]=beta[j]; } return a; } public static double funcOPEKK(double a[][],double x) { // polinom deerleri hesaplama fonksiyonu double yy=0; int k; int m=a[0].length-1; int mp2=m+2; double q[]; q=new double[mp2]; //vector<double> q(m+2,0.0); for(k=m-1;k>=0;k--) { q[k]=a[0][k]+(x-a[1][k+1])*q[k+1]-a[2][k+1]*q[k+2]; yy=q[k]; } return yy; } public static double[][] funcOPEKK(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double E[][]=OPEKK(xi,yi,polinomkatsayisi); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) {z[0][k]=xi[i];z[1][k]=funcOPEKK(E,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcOPEKK(E,z[0][k]);k++;} }

z[0][k]=xi[i];z[1][k]=funcOPEKK(E,z[0][k]); return z; } public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi) { addOPEKK(xi,yi,npolinom,aradegersayisi,0,0,0,0); } public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype) { addOPEKK(xi,yi,npolinom,npolinom,iplottype,0,0,0); } public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) { double S[][]=funcOPEKK(xi,yi,npolinom,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of OPEKK function) } public void addOPEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype) {addOPEKK(linenumber,npolinom,aradegersayisi,iplottype,0,0,0);} public void addOPEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) { double x2[]=new double[n[linenumber]]; double y2[]=new double[n[linenumber]]; if(linenumber<0) linenumber=0; else if(linenumber>nline) linenumber=nline; for(int j=0;j<n[linenumber];j++) { x2[j]=x[linenumber][j]; y2[j]=y[linenumber][j]; } double S[][]=funcOPEKK(x2,y2,npolinom,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of OPEKK function) } // en kucuk kareler public static double[] pivotlugauss(double a[][],double b[]) { //ksmi pivotlu gauss eleme yntemi int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss eleme int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //pivotlama p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } //gauss elemeyi zme

for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //geriye doru yerine koyma x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static double[] EKK(double xi[],double yi[],int n) { int l=xi.length; int i,j,k; int np1=n+1; double A[][]; A=new double[np1][np1]; double B[]; B=new double[np1]; double X[]; X=new double[np1]; for(i=0;i<n+1;i++) { for(j=0;j<n+1;j++) {if(i==0 && j==0) A[i][j]=l; else for(k=0;k<l;k++) A[i][j] += Math.pow(xi[k],(i+j)); } for(k=0;k<l;k++) { if(i==0) B[i]+= yi[k]; else B[i] += Math.pow(xi[k],i)*yi[k];} } System.out.println(Matrix.toString(A)); System.out.println(Matrix.toStringT(B)); X=pivotlugauss(A,B); //X=B/A; double max=0; for(i=0;i<n+1;i++) if(Math.abs(X[i]) > max) max = Math.abs(X[i]); for(i=0;i<n+1;i++) if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0; return X; } public static double funcEKK(double e[],double x) { // this function calculates the value of // least square curve fitting function int n=e.length; double ff; if(n!=0.0) { ff=e[n-1]; for(int i=n-2;i>=0;i--) { ff=ff*x+e[i]; } } else ff=0; return ff; }

public static double hata(double x[],double y[],double e[]) { //calculates absolute square root error of a least square approach double n=x.length; int k; double total=0; for(k=0;k<n;k++) { total+=(y[k]-funcEKK(e,x[k]))*(y[k]-funcEKK(e,x[k])); } total=Math.sqrt(total); return total; } public static double[][] funcEKK(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double E[]=EKK(xi,yi,polinomkatsayisi); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) {z[0][k]=xi[i];z[1][k]=funcEKK(E,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcEKK(E,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcEKK(E,z[0][k]); return z; } public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi) { addEKK(xi,yi,npolinom,aradegersayisi,0,0,0,0); } public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype) { addEKK(xi,yi,npolinom,npolinom,iplottype,0,0,0); } public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) { double S[][]=funcEKK(xi,yi,npolinom,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of EKK function) } public void addEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype) {addEKK(linenumber,npolinom,aradegersayisi,iplottype,0,0,0);} public void addEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) { double x2[]=new double[n[linenumber]]; double y2[]=new double[n[linenumber]]; if(linenumber<0) linenumber=0; else if(linenumber>nline) linenumber=nline; for(int j=0;j<n[linenumber];j++) { x2[j]=x[linenumber][j]; y2[j]=y[linenumber][j]; } double S[][]=funcEKK(x2,y2,npolinom,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of EKK function)

} //=====Lagrange interpolasyon public static double[][] Lagrange(double xi[],double yi[]) { int n=xi.length; double L[][]=new double[2][n]; for(int i=0;i<n;i++) {L[0][i]=yi[i]; for(int j=0;j<n;j++) {if(i!=j) L[0][i]/=(xi[i]-xi[j]);} } for(int i=0;i<n;i++) {L[1][i]=xi[i];} return L; } public static double funcLagrange(double L[][],double x) { int n=L[0].length; double carpim=1.0; double toplam=0; for(int i=0;i<n;i++) {carpim=L[0][i]; for(int j=0;j<n;j++) {if(i!=j) carpim*=(x-L[1][j]);} toplam+=carpim; } return toplam; } public static double[][] funcLagrange(double xi[],double yi[],int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double Q[][]=Lagrange(xi,yi); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=xi[i];z[1][k]=funcLagrange(Q,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcLagrange(Q,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcLagrange(Q,z[0][k]); return z; } public void addLagrange(double xi[],double yi[],int aradegersayisi) { addLagrange(xi,yi,aradegersayisi,0,0,0,0); } public void addLagrange(double xi[],double yi[],int aradegersayisi,int iplottype) { addLagrange(xi,yi,aradegersayisi,iplottype,0,0,0); } public void addLagrange(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) { double S[][]=funcLagrange(xi,yi,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of Lagrange function) }

public void addLagrange(int linenumber,int aradegersayisi,int iplottype) {addLagrange(linenumber,aradegersayisi,iplottype,0,0,0);} public void addLagrange(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) { double x2[]=new double[n[linenumber]]; double y2[]=new double[n[linenumber]]; if(linenumber<0) linenumber=0; else if(linenumber>nline) linenumber=nline; for(int j=0;j<n[linenumber];j++) { x2[j]=x[linenumber][j]; y2[j]=y[linenumber][j]; } double S[][]=funcLagrange(x2,y2,aradegersayisi); addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of Lagrange function) } public void setBackground(Color b1) {bg=b1;} public void setBackground(int ir,int ig,int ib) {bg=new Color(ir,ig,ib);} //Static plot functions //=========== public static Plot func(f_x ff1,double xmin,double xmax,int n,int pnumber) { Plot pp=new Plot(ff1,xmin,xmax,n,pnumber); pp.plot(); return pp; } public static Plot func(f_x ff1,double xmin,double xmax,int n) { Plot pp=new Plot(ff1,xmin,xmax,n,0); pp.plot(); return pp; } public static Plot func(f_x ff1,double xmin,double xmax) { Plot pp=new Plot(ff1,xmin,xmax,300,0); pp.plot(); return pp; } public static Plot func(fi_x ff1,double xmin,double xmax,int n,int pnumber) { Plot pp=new Plot(ff1,xmin,xmax,n,pnumber); pp.plot(); return pp; } public static Plot func(fi_x ff1,double xmin,double xmax,int n) { Plot pp=new Plot(ff1,xmin,xmax,n,0); pp.plot(); return pp; } public static Plot func(fi_x ff1,double xmin,double xmax) { Plot pp=new Plot(ff1,xmin,xmax,300,0); pp.plot(); return pp; }

public static Plot func(f_x ff1,f_x ff2,double xmin,double xmax,int n) { Plot pp=new Plot(ff1,xmin,xmax,n); pp.addFunction(ff2,xmin,xmax,n); pp.plot(); return pp; } public static Plot func(f_x ff1,f_x ff2,double xmin,double xmax) { Plot pp=new Plot(ff1,xmin,xmax,300); pp.addFunction(ff2,xmin,xmax,300); pp.plot(); return pp; } public static Plot file(String ff1)throws IOException { Plot pp=new Plot(ff1); pp.plot(); return pp; } public static Plot file()throws IOException { Plot pp=new Plot(); pp.plot(); return pp; } public static Plot file(String ff1,String ff2)throws IOException { String ff3[]={ff1,ff2}; Plot pp=new Plot(ff3); pp.plot(); return pp; } public static Plot file(String ff1,String ff2,String ff3)throws IOException { String ff4[]={ff1,ff2,ff3}; Plot pp=new Plot(ff4); pp.plot(); return pp; } public static Plot file(String ff1,String ff2,String ff4,String ff3)throws IOException { String ff5[]={ff1,ff2,ff3,ff4}; Plot pp=new Plot(ff5); pp.plot(); return pp; } public static Plot file(String ff1[])throws IOException { Plot pp=new Plot(ff1); pp.plot(); return pp; } public static Plot data(double x[],double y[]) { Plot pp=new Plot(x,y); pp.plot();

return pp; } public static Plot data(double x[],double y1[],double y2[]) { Plot pp=new Plot(x,y1); pp.addData(x,y2); pp.plot(); return pp; } public static Plot data(double x[],double y1[],double y2[],double y3[]) { Plot pp=new Plot(x,y1); pp.addData(x,y2); pp.addData(x,y3); pp.plot(); return pp; } public static Plot data(double a[][]) { Plot pp=new Plot(a); pp.plot(); return pp; } }

PROGRAM 1.3-22 PlotW class


//====================================================== // Numerical Analysis package in java // Plot program : Plot2D class // Dr. Turhan Coban // EGE niversitesi Mhendislik Fakultesi, Makina Blm // turhan.coban@ege.edu.tr // ===================================================== import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import java.io.*; import java.awt.print.PrinterJob; import java.awt.print.*; // Plot2D; // PlotShapesSW; // PlotW // BasicWindowMonitor; // PrintUtilities public class PlotW extends JFrame implements ItemListener,ActionListener, MouseListener,MouseMotionListener { private String s=""; //f2 ff=new f2(); boolean inAnApplet = true; BufferedReader fbilgi; PlotW pencere; final static String KONTROLPANEL1 = "Control page 1"; final static String KONTROLPANEL2 = "Control page 2"; final static String PLOTPANEL = "Plot page "; final static String INFOPANEL = "Additional information about class Plot"; Plot2D jta;

double xi1,yi1,xi2,yi2; int n; JLabel altbar; //writing area JLabel promptXmin; // Label prompt in Xmin field JLabel promptXmax; // Label prompt in Xmax field JLabel promptYmin; // Label prompt in Ymin field JLabel promptYmax; // Label prompt in Ymax field JLabel promptLabel; // Label prompt Plot Label JLabel promptXLabel; // Label prompt Plot XLabel JLabel promptYLabel; // Label prompt Plot YLabel JLabel promptXntic; // Label prompt in Xmin field JLabel promptYntic; // Label prompt in Xmax field JLabel promptXgridon; // Label prompt in Ymin field JLabel promptYgridon; // Label prompt in Ymax field JLabel promptXlogon; // x axis log scale on JLabel promptYlogon; // y axis log scale on JLabel pr[]; JTextField inputXmin; // input field Xmin JTextField inputXmax; // input field Xmax JTextField inputYmin; // input field Ymin JTextField inputYmax; // input field Ymax JTextField inputLabel; // input field Label JTextField inputXLabel; // input field XLabel JTextField inputYLabel; // input field YLabel JTextField inputXntic; // input field xntic JTextField inputYntic; // input field yntic JTextArea bilgi; JCheckBox inputXgridon; // input field xgridon JCheckBox inputYgridon; // input field ygridon JCheckBox inputXlogon; // check field xlogon JCheckBox inputYlogon; // check field ylogon JLabel promptAdditionalLabel[]; //to add additional label to the graphic window JTextField inputAdditionalLabel[]; JTextField inputAdditionalXLabel[]; JTextField inputAdditionalYLabel[]; JTextArea inputAdditionalArea[]; JButton printButton; JLabel arkarenk1; JButton arkarenk2; JLabel lab1[]; JLabel lab2[]; JComboBox c1[]; JButton col1[]; JButton but1[]; JScrollPane skrolPane; Color renk1; Color renk2; JPanel pane2; JPanel pane4; public PlotW() { super("Plot"); try{Plot pi=new Plot();plotWkur(pi); } catch(IOException e) { System.err.println("Error Opening File \n"+e.toString()); System.exit(1); } } public PlotW(Plot pi) { super(pi.label); plotWkur(pi); }

public void plotWkur(Plot pi) { jta=new Plot2D(pi); renk2=Color.white; n=pi.nline; lab1=new JLabel[n]; lab2=new JLabel[6]; c1=new JComboBox[n]; col1=new JButton[n]; bilgi=new JTextArea(); promptAdditionalLabel=new JLabel[15]; inputAdditionalLabel= new JTextField[15]; inputAdditionalXLabel= new JTextField[7]; inputAdditionalYLabel=new JTextField[7]; arkarenk1=new JLabel(""); arkarenk2=new JButton("Background color"); arkarenk2.setBackground(renk2); pr=new JLabel[20]; but1=new JButton[7]; for(int i=0;i<5;i++) {promptAdditionalLabel[i]=new JLabel("additional label #"+i+" x,y coordinates"); if(jta.p1.label_at_xy[i]!="") { inputAdditionalLabel[i]=new JTextField(jta.p1.label_at_xy[i]); inputAdditionalXLabel[i]=new JTextField(""+jta.p1.label_x[i]); inputAdditionalYLabel[i]=new JTextField(""+jta.p1.label_y[i]); } else { inputAdditionalLabel[i]=new JTextField(""); inputAdditionalXLabel[i]=new JTextField(""); inputAdditionalYLabel[i]=new JTextField(""); } but1[i]=new JButton("enter additional label coordinates"); } promptAdditionalLabel[5]=new JLabel("additional block label with data types at x,y coordinates"); if(jta.p1.skutu1[0]!="") { for(int i=0;i<5;i++) { inputAdditionalLabel[i+5]=new JTextField(jta.p1.skutu1[i]);} inputAdditionalXLabel[5]=new JTextField(""+jta.p1.xl); inputAdditionalYLabel[5]=new JTextField(""+jta.p1.yl); } else { for(int i=0;i<5;i++) {inputAdditionalLabel[i+5]=new JTextField("");} inputAdditionalXLabel[5]=new JTextField(""); inputAdditionalYLabel[5]=new JTextField(""); } but1[5]=new JButton("enter coordinates for additional labels"); promptAdditionalLabel[6]=new JLabel("additional block label at x,y coordinates"); if(jta.p1.skutu2[0]!="") { for(int i=0;i<5;i++) {inputAdditionalLabel[i+10]=new JTextField(jta.p1.skutu2[i]);} inputAdditionalXLabel[6]=new JTextField(""+jta.p1.xl); inputAdditionalYLabel[6]=new JTextField(""+jta.p1.yl); } else { for(int i=0;i<5;i++) {inputAdditionalLabel[i+10]=new JTextField("");} inputAdditionalXLabel[6]=new JTextField(""); inputAdditionalYLabel[6]=new JTextField(""); }

but1[6]=new JButton("enter coordinates for additional labels"); promptAdditionalLabel[6]=new JLabel("additonal label at x,y corrdinates"); pr[0]=new JLabel(""); pr[1]=new JLabel("x coordinate"); pr[2]=new JLabel("y coordinate"); pr[3]=new JLabel("Heading"); pr[4]=new JLabel("last clicked point"); lab2[0]=new JLabel(""); lab2[1]=new JLabel("line type"); lab2[2]=new JLabel("color selection table"); for(int i=0;i<n;i++) { Color color1=new Color(pi.red[i],pi.green[i],pi.blue[i]); lab1[i]=new JLabel("line "+i+" "); c1[i]=new JComboBox(pi.plotst); c1[i].setSelectedIndex(pi.plottype[i]); col1[i]=new JButton(" "); col1[i].setBackground(color1); } Container contentPane = getContentPane(); renk1=contentPane.getBackground(); JTabbedPane tabbedPane = new JTabbedPane(); printButton=new JButton("print"); promptXmin=new JLabel("Xmin "); inputXmin=new JTextField(10); promptXmax=new JLabel("Xmax "); inputXmax=new JTextField(10); promptYmin=new JLabel("Ymin "); inputYmin=new JTextField(10); promptYmax=new JLabel("Ymax "); inputYmax=new JTextField(10); //******* promptLabel=new JLabel("Plot heading : "); promptXLabel=new JLabel("x axis label : "); promptYLabel=new JLabel("y axis label : "); inputLabel=new JTextField(40); inputXLabel=new JTextField(40); inputYLabel=new JTextField(40); inputLabel.setBackground(renk1); inputXLabel.setBackground(renk1); inputYLabel.setBackground(renk1); //******* promptXntic=new JLabel("x thick no"); inputXntic=new JTextField(10); promptYntic=new JLabel("y thick no"); inputYntic=new JTextField(10); promptXgridon=new JLabel("x grid "); if(pi.xgridon!=0) inputXgridon=new JCheckBox(" ",true); else inputXgridon=new JCheckBox(" ",false); promptYgridon=new JLabel("Y grid"); if(pi.ygridon!=0) inputYgridon=new JCheckBox(" ",true); else inputYgridon=new JCheckBox(" ",false); promptXlogon=new JLabel("x log "); promptYlogon=new JLabel("y log "); if(pi.xlog!=0) inputXlogon=new JCheckBox(" ",true); else inputXlogon=new JCheckBox(" ",false); if(pi.ylog!=0) inputYlogon=new JCheckBox(" ",true); else inputYlogon=new JCheckBox(" ",false); // check field xlogon JPanel pane1 = new JPanel(); JPanel pane3 = new JPanel(); JPanel mpane=new JPanel(); bilgi.setText(bilgioku());

bilgi.setBackground(renk1); pane3.add(bilgi); mpane.setLayout(new GridLayout(6,4)); JPanel p1=new JPanel(); p1.setLayout(new GridLayout(6,5)); JPanel p4=new JPanel(); p4.setLayout(new GridLayout(6,5)); JPanel p5=new JPanel(); p5.setLayout(new GridLayout(1,5)); JPanel p5a=new JPanel(); p5a.setLayout(new GridLayout(6,1)); JPanel p5b=new JPanel(); p5b.setLayout(new BorderLayout()); JPanel p6=new JPanel(); p6.setLayout(new GridLayout(1,5)); JPanel p6a=new JPanel(); p6a.setLayout(new GridLayout(6,1)); JPanel p6b=new JPanel(); p6b.setLayout(new BorderLayout()); mpane.add(promptXmin); inputXmin.setBackground(renk1); inputXmax.setBackground(renk1); inputYmin.setBackground(renk1); inputYmax.setBackground(renk1); inputXntic.setBackground(renk1); inputYntic.setBackground(renk1); mpane.add(inputXmin); mpane.add(promptXmax); mpane.add(inputXmax); mpane.add(promptYmin); mpane.add(inputYmin); mpane.add(promptYmax); mpane.add(inputYmax); mpane.add(promptXntic); mpane.add(inputXntic); mpane.add(promptYntic); mpane.add(inputYntic); mpane.add(promptXgridon); mpane.add(inputXgridon); mpane.add(promptYgridon); mpane.add(inputYgridon); mpane.add(promptXlogon); mpane.add(inputXlogon); mpane.add(promptYlogon); mpane.add(inputYlogon); mpane.add(arkarenk1); mpane.add(arkarenk2); JLabel bos=new JLabel(""); mpane.add(bos); mpane.add(printButton); pane1.add(mpane,BorderLayout.NORTH); JPanel xpane=new JPanel(); xpane.setLayout(new GridLayout((n+1),3)); for(int i=0;i<3;i++) {lab2[i].setBackground(renk1); xpane.add(lab2[i]);} for(int i=0;i<n;i++) { xpane.add(lab1[i]); xpane.add(c1[i]); xpane.add(col1[i]); c1[i].setBackground(renk1); }

JPanel lpane=new JPanel(); lpane.setLayout(new GridLayout(3,2)); lpane.add(promptLabel); lpane.add(inputLabel); lpane.add(promptXLabel); lpane.add(inputXLabel); lpane.add(promptYLabel); lpane.add(inputYLabel); //******** p4.add(pr[0]); p4.add(pr[1]); p4.add(pr[2]); p4.add(pr[3]); p4.add(pr[4]); for(int i=0;i<15;i++) {inputAdditionalLabel[i].setBackground(renk1);} for(int i=0;i<7;i++) { inputAdditionalXLabel[i].setBackground(renk1); inputAdditionalYLabel[i].setBackground(renk1); } for(int i=0;i<5;i++) { p4.add(promptAdditionalLabel[i]); p4.add(inputAdditionalXLabel[i]); p4.add(inputAdditionalYLabel[i]); p4.add(inputAdditionalLabel[i]); p4.add(but1[i]); } JLabel lx1=new JLabel("block label with plot symbol x1 coordinate"); JLabel ly1=new JLabel("block label with plot symbol y1 coordinate"); p5.add(lx1); p5.add(inputAdditionalXLabel[5]); p5.add(ly1); p5.add(inputAdditionalYLabel[5]); p5.add(but1[5]); p5a.add(p5); for(int i=0;i<5;i++) {p5a.add(inputAdditionalLabel[i+5]);} JLabel lx2=new JLabel("block label x2 coordinate"); JLabel ly2=new JLabel("block label with plot symbol y2 coordinate"); p6.add(lx2); p6.add(inputAdditionalXLabel[6]); p6.add(ly2); p6.add(inputAdditionalYLabel[6]); p6.add(but1[6]); p6a.add(p6); for(int i=0;i<5;i++) {p6a.add(inputAdditionalLabel[i+10]);} p5b.add(p5a,BorderLayout.NORTH); p5b.add(p6a,BorderLayout.SOUTH); p6b.add(p4,BorderLayout.NORTH); p6b.add(p5b,BorderLayout.SOUTH); //lpane.add(p1); skrolPane=new JScrollPane(xpane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); pane1.add(lpane,BorderLayout.NORTH); pane1.add(p1,BorderLayout.CENTER); pane1.add(skrolPane,BorderLayout.SOUTH); //********* for(int i=0;i<15;i++) {inputAdditionalLabel[i].addActionListener(this); }

for(int i=0;i<n;i++) { c1[i].addItemListener(this); col1[i].addActionListener(this); } //********** for(int i=0;i<15;i++) {inputAdditionalLabel[i].addActionListener(this);} for(int i=0;i<7;i++) { inputAdditionalXLabel[i].addActionListener(this); inputAdditionalYLabel[i].addActionListener(this); but1[i].addActionListener(this); } printButton.addActionListener(this); inputXmin.addActionListener(this); inputXmax.addActionListener(this); inputYmin.addActionListener(this); inputYmax.addActionListener(this); inputLabel.addActionListener(this); inputXLabel.addActionListener(this); inputYLabel.addActionListener(this); inputXntic.addActionListener(this); inputYntic.addActionListener(this); inputXlogon.addItemListener(this); inputYlogon.addItemListener(this); inputXgridon.addItemListener(this); inputYgridon.addItemListener(this); arkarenk2.addActionListener(this); inputXmin.setText(Double.toString(jta.p1.xmin)); inputXmax.setText(Double.toString(jta.p1.xmax)); inputYmin.setText(Double.toString(jta.p1.ymin)); inputYmax.setText(Double.toString(jta.p1.ymax)); inputXntic.setText(Integer.toString(jta.p1.xntic)); inputYntic.setText(Integer.toString(jta.p1.yntic)); inputLabel.setText(jta.p1.label); inputXLabel.setText(jta.p1.xlabel); inputYLabel.setText(jta.p1.ylabel); pane2 = new JPanel(); pane2.setLayout(new BorderLayout()); pane4 = new JPanel(); pane4.setLayout(new BorderLayout()); altbar=new JLabel(); pane2.add(altbar,BorderLayout.SOUTH); pane2.addMouseListener(this); pane2.addMouseMotionListener(this); pane2.add(jta); pane4.add(p6b); tabbedPane.addTab(PLOTPANEL, pane2); tabbedPane.addTab(KONTROLPANEL1, pane1); tabbedPane.addTab(KONTROLPANEL2, pane4); tabbedPane.addTab(INFOPANEL,new JScrollPane(pane3,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); contentPane.add( tabbedPane, BorderLayout.CENTER); } public void itemStateChanged(ItemEvent e) { inputXmin.setText(Double.toString(jta.p1.xmin)); inputXmax.setText(Double.toString(jta.p1.xmax)); inputYmin.setText(Double.toString(jta.p1.ymin)); inputYmax.setText(Double.toString(jta.p1.ymax)); inputXntic.setText(Integer.toString(jta.p1.xntic)); inputYntic.setText(Integer.toString(jta.p1.yntic));

Object source=e.getItemSelectable(); if(source==inputXgridon) { if (e.getStateChange() == ItemEvent.DESELECTED) {jta.p1.xgridon=0;} else {jta.p1.xgridon=1;} } else if(source==inputYgridon) { if (e.getStateChange() == ItemEvent.DESELECTED) {jta.p1.ygridon=0;} else {jta.p1.ygridon=1;} } else if(source==inputXlogon) { if (e.getStateChange() == ItemEvent.DESELECTED) {jta.p1.setXlogScaleOff();} else {jta.p1.setXlogScaleOn();} } else if(source==inputYlogon) { if (e.getStateChange() == ItemEvent.DESELECTED) {jta.p1.setYlogScaleOff();} else {jta.p1.setYlogScaleOn();} } else { for(int i=0;i<n;i++) { if(source==c1[i]) {jta.p1.plottype[i]=c1[i].getSelectedIndex(); if(jta.p1.plottype[i] == 10) jta.p1.ch[i]='*';//you can change character set by using setPlotType(0,'%'); if(jta.p1.plottype[i] == 11) jta.p1.ch[i]='#'; if(jta.p1.plottype[i] == 12) jta.p1.ch[i]='$'; if(jta.p1.plottype[i] == 13) jta.p1.ch[i]='%'; if(jta.p1.plottype[i] == 14) jta.p1.ch[i]='&'; if(jta.p1.plottype[i] == 15) jta.p1.ch[i]='-'; if(jta.p1.plottype[i] == 16) jta.p1.ch[i]='~'; if(jta.p1.plottype[i] == 17) jta.p1.ch[i]='+'; if(jta.p1.plottype[i] == 18) jta.p1.ch[i]='x'; if(jta.p1.plottype[i] == 19) jta.p1.ch[i]='='; } } inputLabel.setText(jta.p1.label); inputXLabel.setText(jta.p1.xlabel); inputYLabel.setText(jta.p1.ylabel); jta.yenidenciz(); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==printButton) { PrintUtilities.printComponent(pane2); } if (e.getSource()==inputXmin ) {Double valXmin=new Double(inputXmin.getText()); jta.p1.xmin=valXmin.doubleValue();} else if (e.getSource()==inputXmax ) {Double valXmax=new Double(inputXmax.getText()); jta.p1.xmax=valXmax.doubleValue();} else if (e.getSource()==inputYmin ) {Double valYmin=new Double(inputYmin.getText());

jta.p1.ymin=valYmin.doubleValue();} else if (e.getSource()==inputYmax ) {Double valYmax=new Double(inputYmax.getText()); jta.p1.ymax=valYmax.doubleValue();} else if (e.getSource()==inputXntic ) {Integer valXntic=new Integer(inputXntic.getText()); jta.p1.xntic=valXntic.intValue();} else if (e.getSource()==inputYntic ) {Integer valYntic=new Integer(inputYntic.getText()); jta.p1.yntic=valYntic.intValue();} else if (e.getSource()==inputLabel ) {jta.p1.label=inputLabel.getText();} else if (e.getSource()==inputXLabel ) {jta.p1.xlabel=inputXLabel.getText();} else if (e.getSource()==inputYLabel ) {jta.p1.ylabel=inputYLabel.getText();} else if(e.getSource()==arkarenk2) { renk2=JColorChooser.showDialog(null,"grafik arka plan rengi ",renk2 ); jta.setBackground(renk2); arkarenk2.setBackground(renk2); } String s1; for(int i=0;i<7;i++) { if(e.getSource()==but1[i]) { if(i<5) { jta.p1.label_x[i]=xi2; jta.p1.label_y[i]=yi2; } else if(i==5) {jta.p1.x1=xi2; jta.p1.y1=yi2; } else if(i==6) {jta.p1.x2=xi2; jta.p1.y2=yi2; } inputAdditionalXLabel[i].setText(""+xi2); inputAdditionalYLabel[i].setText(""+yi2); } if(e.getSource()==inputAdditionalXLabel[i]) {s1=inputAdditionalXLabel[i].getText(); if(i<5){ if(s1!="") jta.p1.label_x[i]=Double.parseDouble(s1); else jta.p1.label_x[i]=0.0; } else if(i==5) { if(s1!="") jta.p1.x1=Double.parseDouble(s1); else jta.p1.x1=0.0; } else if(i==6) { if(s1!="") jta.p1.x2=Double.parseDouble(s1); else jta.p1.x2=0.0; } } else if(e.getSource()==inputAdditionalYLabel[i]) {s1=inputAdditionalYLabel[i].getText(); if(i<5){ if(s1!="") jta.p1.label_y[i]=Double.parseDouble(s1); else jta.p1.label_y[i]=0.0; } else if(i==5) { if(s1!="") jta.p1.y1=Double.parseDouble(s1); else jta.p1.y1=0.0;

} else if(i==6) { if(s1!="") jta.p1.y2=Double.parseDouble(s1); else jta.p1.y2=0.0; } } } for(int i=0;i<15;i++) { if(e.getSource()==inputAdditionalLabel[i]) { if(i<5) { jta.p1.label_at_xy[i]=inputAdditionalLabel[i].getText(); s1=inputAdditionalXLabel[i].getText(); if(s1!="") jta.p1.label_x[i]=Double.parseDouble(s1); else jta.p1.label_x[i]=0.0; s1=inputAdditionalYLabel[i].getText(); if(s1!="") jta.p1.label_y[i]=Double.parseDouble(s1); else jta.p1.label_y[i]=0.0; s1=jta.p1.label_at_xy[i]; } else if(i>=5 && i<10) { jta.p1.skutu1[i-5]=inputAdditionalLabel[i].getText(); s1=inputAdditionalXLabel[5].getText(); if(s1!="") jta.p1.x1=Double.parseDouble(s1); else jta.p1.x1=0.0; s1=inputAdditionalYLabel[5].getText(); if(s1!="") jta.p1.y1=Double.parseDouble(s1); else jta.p1.y1=0.0; } else if(i>=10 && i<15) { jta.p1.skutu2[i-10]=inputAdditionalLabel[i].getText(); s1=inputAdditionalXLabel[6].getText(); if(s1!="") jta.p1.x2=Double.parseDouble(s1); else jta.p1.x2=0.0; s1=inputAdditionalYLabel[6].getText(); if(s1!="") jta.p1.y2=Double.parseDouble(s1); else jta.p1.y2=0.0; } } } for(int i=0;i<n;i++) { Color color1=new Color(jta.p1.red[i],jta.p1.green[i],jta.p1.blue[i]); Color color2; if (e.getSource()==col1[i] ) { color2=JColorChooser.showDialog(null,"grafik program eri "+i,color1 ); jta.p1.red[i]=color2.getRed();jta.p1.green[i]=color2.getGreen();jta.p1.blue[i]=color2.getBlue(); col1[i].setBackground(color2); } } jta.yenidenciz(); } public void degerleriGir(String e,int x,int y) { s=e; if(jta.p1.xlog!=0) {xi1=jta.p1.xmin+Math.pow(10.0,(double)((x-jta.p1.xabsmin)/(double)jta.p1.abswidth*(double)jta.p1.xminmaxlog+1.0))-10.0; } else {xi1=jta.p1.xmin+(double)(x-jta.p1.xabsmin)/(double)jta.p1.abswidth*(jta.p1.xmax-jta.p1.xmin);} if(jta.p1.ylog!=0) {yi1=jta.p1.ymin-10.0+Math.pow(10.0,((double)(1.0-(double)(y-jta.p1.yabsmin-

jta.p1.absheight)/(double)jta.p1.absheight*(double)jta.p1.yminmaxlog))); } else {yi1=jta.p1.ymin-(double)(y-jta.p1.yabsmin-jta.p1.absheight)/(double)jta.p1.absheight*(jta.p1.ymax-jta.p1.ymin);} altbar.setText(e+" ["+xi1+" , "+yi1+"] "); //jta.yenidenciz(); } public void degerleriGir1(String e,int x,int y) { s=e; if(jta.p1.xlog!=0) {xi2=jta.p1.xmin+Math.pow(10.0,(double)((x-jta.p1.xabsmin)/(double)jta.p1.abswidth*(double)jta.p1.xminmaxlog+1.0))-10.0; } else {xi2=jta.p1.xmin+(double)(x-jta.p1.xabsmin)/(double)jta.p1.abswidth*(jta.p1.xmax-jta.p1.xmin);} if(jta.p1.ylog!=0) {yi2=jta.p1.ymin-10.0+Math.pow(10.0,((double)(1.0-(double)(y-jta.p1.yabsminjta.p1.absheight)/(double)jta.p1.absheight*(double)jta.p1.yminmaxlog))); } else {yi2=jta.p1.ymin-(double)(y-jta.p1.yabsmin-jta.p1.absheight)/(double)jta.p1.absheight*(jta.p1.ymax-jta.p1.ymin);} //System.out.println(""+xi2+" "+yi2); } public void mousePressed(MouseEvent e) { if(e.isMetaDown()) {degerleriGir1("Fare deerleri kaydedildi",e.getX(),e.getY());} //sa tu basldnda else if(e.isAltDown()) //orta tu basldnda {} else { degerleriGir1("Fare deerleri kaydedildi",e.getX(),e.getY());} //sol tu basldnda } public void mouseClicked(MouseEvent e) { degerleriGir1("Kliklendi",e.getX(),e.getY()); } public void mouseReleased(MouseEvent e) { degerleriGir1("mouse released",e.getX(),e.getY()); } public void mouseEntered(MouseEvent e) { setTitle("mouse entered to plot window"); } public void mouseExited(MouseEvent e) { setTitle("mouse exited from the plot window"); degerleriGir("mouse is outside of the plot window",e.getX(),e.getY()); } //MouseMotionListener (fare hareket dinleyicisi) metotlari public void mouseDragged(MouseEvent e) { degerleriGir("mouse dragged",e.getX(),e.getY()); } public void mouseMoved(MouseEvent e) { degerleriGir("mouse moved",e.getX(),e.getY()); } public void plot() { //takes plot sata from Plot.txt file pencere= new PlotW(); pencere.addWindowListener(new BasicWindowMonitor()); pencere.setSize(1000,700); pencere.setVisible(true); pencere.setDefaultCloseOperation(pencere.DISPOSE_ON_CLOSE); }

public void plot(Plot pi) { //take plot data from pi object pencere= new PlotW(pi); pencere.addWindowListener(new BasicWindowMonitor());

pencere.setSize(1000,700); pencere.setVisible(true); pencere.setDefaultCloseOperation(pencere.DISPOSE_ON_CLOSE); } protected void close(){pencere.close();} public static void main(String[] args) { PlotW pencere1= new PlotW(); pencere1.addWindowListener(new BasicWindowMonitor()); pencere1.setSize(1000,700); pencere1.setVisible(true); pencere1.setDefaultCloseOperation(pencere1.DISPOSE_ON_CLOSE); } public String bilgioku() {String s; s= " 1.6.13 JAVA 2 DIMENSIONAL GRAPHIC OUTPUT CLASS : PLOT \n "; s+=" 2 dimensional scientific plot program to be \n"; s+= "====================================================== \n "; s+= " Plot program aims a simple yet sophisticated set of plots \n "; s+= " to be drawn for two dimensional engineering plot applications.\n"; s+= " Seeing the data is always easier for evaluating the results.\n"; s+= " There are commercially available plot programs, but the purpose of\n"; s+= " this package is to create an absolutely free and open code supporting plot\n"; s+= " definitions. Most of the subprograms in the package is add on methods from \n"; s+= " the actual requirements arised in numerical analysis and engineering applications \n"; s+= " If you would like to add into the package you are welcome, but please send me the \n"; s+= " add ups, if they will be usefull, I can make them permanent in the package \n"; s+= " my current email : turhan_coban@yahoo.com. Have a happy plotting\n"; s+= "====================================================== \n "; s+= "Program 1.6.13-1 PlotT1.java SAMPLE PROGRAM \n "; s+= "import java.io.*; \n "; s+= "class PlotT1 \n "; s+= "{ public static void main(String args[]) throws IOException \n "; s+= " {Plot pp=new Plot(\"in.txt\"); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= "Our first example code reads data from file in.txt and plot it \n "; s+= "The first line is the constructor line \n "; s+= "Plot pp=new Plot(\"in.txt\"); it inputs data from file in.txt and pp.plot(); \n "; s+= "term is actually draw the plot window. This term will always be used to \n "; s+= "open plot window\n "; s+= " \n "; s+= "In the second page of the graphic window several editing features are available\n "; s+= "such as plot shapes, headings, colors, grid, logarithmic scale, manual maximum\n "; s+= "and minimum x an y scales, additional heading space in any desired location \n "; s+= "of the plot \n "; s+= "Page can be grided into a desired size, Graphic color can be changed by using\n "; s+= "standard java JColorChooser class graphic editor \n "; s+= " \n "; s+= "In our second example program data in file in.txt is drawn by a single static definition \n "; s+= " \n "; s+= "Program 1.6.13-1A PlotT1.java sample program \n "; s+= "import java.io.*; \n "; s+= "class plotT1 \n "; s+= "{ \n "; s+= " public static void main(String args[]) throws IOException \n "; s+= " {Plot.file(\"in.txt\");} \n "; s+= "} \n "; s+= " \n "; s+= "Static definitions are usually preferred for one step plot drawing \n "; s+= "If several different plot jobs will be defined, dynamic definitions \n "; s+= "should be preferrred. In the next example data is given inside of the program\n "; s+= "as two double vectors \n ";

s+= "Program 1.6.13-2 \n "; s+= "import java.io.*; \n "; s+= "class PlotT2A \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " Plot pp=new Plot(xx,yy); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= "The static version of the same program : \n "; s+= " \n "; s+= "Program 1.6.13-2A \n "; s+= "import java.io.*; \n "; s+= "class PlotT2A \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " Plot.data(xx,yy);}} \n "; s+= " The next program first defines the data and then add up cubic spline \n "; s+= " curve fittings to the given data (2 plot lines are to be drawn) \n "; s+= "Program 1.6.13-3 \n "; s+= "import java.io.*; \n "; s+= "class PlotT2B \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " Plot pp=new Plot(xx,yy); \n "; s+= " pp.addSpline(xx,yy,10); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= " \n "; s+= "In order to do Cubic spline curve fitting adding term pp.addSpline(xx,yy,10); was enough\n "; s+= "10 additional cubic spline fit data is added in between every point\n "; s+= "ara deerleri kbik erit interpolasyonuyla hesaplamamz anlamna gelmektedir. \n "; s+= " If the original curve index number is known, it can also be used to curve feet additional data \n "; s+= " index number start from zero and goes upwards for evry new curve to be added \n "; s+= "Program 1.6.13-3A \n "; s+= "import java.io.*; \n "; s+= "class PlotT2B2 \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " Plot pp=new Plot(xx,yy); \n "; s+= " pp.addSpline(0,10); \n "; s+= " pp.plot();}} \n "; s+= "Static definitions can not be applied in the prot drawings with more than one command such \n "; s+= "as in the above program, but you can always write your user specific static method \n "; s+= "to collect your own group of commands\n "; s+= " \n "; s+= "Program 1.6.13-3B \n "; s+= "import java.io.*; \n "; s+= "class PlotT2B \n "; s+= "{ \n "; s+= " public static void plot(double xx[],double yy[]) \n "; s+= " {Plot pp=new Plot(xx,yy); \n "; s+= " pp.addSpline(xx,yy,10); \n "; s+= " pp.plot();} \n "; s+= " \n "; s+= " public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " plot(xx,yy);}} \n "; s+= " \n "; s+= "The same interpolation calculations can also be carried out by B spline\n"; s+= "interpolation with term pp.addB_Spline(xx,yy,10); \n "; s+= " \n ";

s+= "Program 1.6.13-3C \n "; s+= "import java.io.*; \n "; s+= "class PlotT2B1 \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " Plot pp=new Plot(xx,yy); \n "; s+= " pp.addB_Spline(xx,yy,10); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= " If second degree orthogonal polynomial to be used for curve fitting \n "; s+= " (10 data points in between original points) \n "; s+= " \n "; s+= "Program 1.6.13-4 \n "; s+= "import java.io.*; \n "; s+= "class PlotT2C \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " Plot pp=new Plot(xx,yy); \n "; s+= " pp.addOPEKK(xx,yy,2,10); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= " \n "; s+= "if a second degree polynomial is desired fro corve fitting (fx=a0+a1*x+a2*x2) \n"; s+= " (10 data points in between original points) \n "; s+= "Program 1.6.13-5 \n "; s+= "import java.io.*; \n "; s+= "class PlotT2D \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " Plot pp=new Plot(xx,yy); \n "; s+= " pp.addEKK(xx,yy,2,10); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= " \n "; s+= "Lagrange interpolation can also be used. \n "; s+= "Program 1.6.13-6 \n "; s+= "import java.io.*; \n "; s+= "class PlotT2E \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {double xx[]={1,2,3,4,5}; \n "; s+= " double yy[]={1,4,9,16,25}; \n "; s+= " Plot pp=new Plot(xx,yy); \n "; s+= " pp.addLagrange(xx,yy,10); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= " \n "; s+= "The detailed information about interpolation routines are given in \n "; s+= "Numerical Analysis by using java programs text book \n "; s+= " \n"; s+= "Constructor in the plot program can be given as completely empty. \n "; s+= "Program 1.6.13-7 (Dynamic call) \n "; s+= "import java.io.*; \n "; s+= "class PlotT01 \n "; s+= "{public static void main (String args[]) throws IOException \n "; s+= " {Plot pp=new Plot(); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= "Program 1.6.13-7A (Static call) \n "; s+= "import java.io.*; \n "; s+= "class PlotT2B1 \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {Plot.file();}} \n "; s+= " \n ";

s+= "In this case program will look the data file Plot.txt for data.\n "; s+= "Data is given in the following form in this data\n "; s+= "Plot label \n "; s+= "x axis label \n "; s+= "y axis label \n "; s+= "2 \n "; s+= "in.txt 0 0 0 0 \n "; s+= "out.txt 3 0 0 255 \n "; s+= " \n "; s+= "The meaning of this data is as follows \"Plot label\" \"x axis label\" \n"; s+= " \"y axis label\" \n are defines graphic label headings \n"; s+= "2 indicates number of data files data to be taken from in.txt and out.txt\n "; s+= "in this case, the next number is the plot line type and the last 3 numbers are plot color\n"; s+= "(red green and blue 0 to 255). \n "; s+= "Plot line type codes are as follows : \n "; s+= "\"1 __ __ __ __ \", \n "; s+= "\"2 . _ . _ . _ \", \n "; s+= "\"3 . _ _ . . _ _ .\", \n "; s+= "\"4 - - - - - - - - \", \n "; s+= "\"5 ______\", \n "; s+= "\"6 __ __ __ __ \", \n "; s+= "\"7 . _ . _ . _ \", \n "; s+= "\"8 . _ _ . . _ _ .\", \n "; s+= "\"9 - - - - - \", \n "; s+= "\"10 * \" , \n "; s+= "\"11 # \", \n "; s+= "\"12 $ \", \n "; s+= "\"13 % \", \n "; s+= "\"14 & \", \n "; s+= "\"15 \", \n "; s+= "\"16 ~ \", \n "; s+= "\"17 + \", \n "; s+= "\"18 x \", \n "; s+= "\"19 = \", \n "; s+= "\"20 square\", \n "; s+= "\"21 filled square\", \n "; s+= "\"22 circle\", \n "; s+= "\"23 filled circle\", \n "; s+= "\"24 triangle\", \n "; s+= "\"25 diamond \", \n "; s+= "\"26 pentagon\", \n "; s+= "\"27 hexagon\", \n "; s+= "\"28 filled triangle\", \n "; s+= "\"28 filled diamond\", \n "; s+= "\"30 filled pentagon\", \n "; s+= "\"31 filled hexagon\", \n "; s+= "\"32 3 sided star\", \n "; s+= "\"33 4 sided star\", \n "; s+= "\"34 5 sided star\", \n "; s+= "\"35 6 sided star\", \n "; s+= "\"36 filled 3 sided star\", \n "; s+= "\"37 filled 4 sided star\", \n "; s+= "\"38 filled 5 sided star\", \n "; s+= "\"39 filled 6 sided star\", \n "; s+= "\"40 bar graphic\", \n "; s+= "\"41 filled bar graphic\"}; \n "; s+= ". \n "; s+= " \n "; s+= "Plot class is also draw a given function \n "; s+= "functions to be drawn should be extended to abstract class f_x. \n "; s+= " \n "; s+= "Program 1.6.13-8 (Dynamic version) \n "; s+= "import java.io.*; \n "; s+= "class f3 extends f_x \n "; s+= "{double func(double x) \n "; s+= " {return x*x*x+3.6*x*x-36.4;}} \n ";

s+= " \n "; s+= "class PlotT06 \n "; s+= "{public static void main (String args[]) throws IOException \n "; s+= " {f3 ff2=new f3(); \n "; s+= " Plot pp=new Plot(ff2,0.0,7.0,400,2); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= "Program 1.6.13-8A (Static version) \n "; s+= "import java.io.*; \n "; s+= " \n "; s+= "class f3 extends f_x \n "; s+= "{double func(double x) \n "; s+= " { return x*x*Math.abs(Math.sin(Math.sqrt(x)))-5.0;}} \n "; s+= " \n "; s+= "class PlotT06 \n "; s+= "{ public static void main (String args[]) throws IOException \n "; s+= " {Plot.func(new f3(),0.0,10.0,400,0);} \n "; s+= " \n "; s+= "The function given in the previous program is : f(x)=x3+3.6x2-36.4 \n "; s+= "function is required to be plotted in between 0 and 7 with 400 data points\n "; s+= " \n "; s+= "More than one function can be defined in the same plot window with one \n "; s+= "function class \n "; s+= "Program 1.6.13-9 (Dynamic version) \n "; s+= "import java.io.*; \n "; s+= "class f3 extends fi_x \n "; s+= "{ \n "; s+= " double[] func(double x) \n "; s+= " { double f[]=new double[2]; \n "; s+= " f[0]=x*Math.sin(10.0*Math.PI*x)+1.0; \n "; s+= " f[1]=x*x; \n "; s+= " return f;}} \n "; s+= " \n "; s+= "class PlotT03 \n "; s+= "{public static void main (String args[]) throws IOException \n "; s+= " { f3 ff2=new f3(); \n "; s+= " Plot pp=new Plot(ff2,-2.0,2.0,400,2); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= "Program 1.6.13-9A (Statik versiyon) \n "; s+= "import java.io.*; \n "; s+= " \n "; s+= "class f3 extends fi_x \n "; s+= "{ \n "; s+= " double[] func(double x) \n "; s+= " { double f[]=new double[2]; \n "; s+= " f[0]=x*Math.sin(10.0*Math.PI*x)+1.0; \n "; s+= " f[1]=x*x; \n "; s+= " return f;} \n "; s+= "} \n "; s+= " \n "; s+= "class PlotT03 \n "; s+= "{public static void main (String args[]) throws IOException \n "; s+= " { Plot.func(new f3(),-2.0,2.0,400,2);}} \n "; s+= " \n "; s+= "or the same plot can be achieved by using different plot function classes.\n "; s+= " \n "; s+= "Program 1.6.13-10 \n "; s+= "import java.io.*; \n "; s+= "class f3 extends f_x \n "; s+= "{double func(double x) \n "; s+= " {return x*x;}} \n "; s+= " \n "; s+= "class f4 extends f_x \n "; s+= "{double func(double x) \n ";

s+= " {return x*Math.sin(10.0*Math.PI*x)+1.0;}} \n "; s+= " \n "; s+= "class PlotT07 \n "; s+= "{public static void main (String args[]) throws IOException \n "; s+= " {f3 ff3=new f3(); \n "; s+= " f4 ff4=new f4(); \n "; s+= " Plot pp=new Plot(ff3,-3.0,2.0,400); \n "; s+= " pp.addFunction(ff4,-2.0,2.0,400); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= "Different plot zones can be defined when functions called seperately. \n "; s+= "For example in the previous program second function is called for -2,2 \n "; s+= "zone \n "; s+= " \n "; s+= "pp.addFunction(ff4,-2.0,2.0,400); \n "; s+= " \n "; s+= "method addData is also available it adds additional data points \n "; s+= " \n "; s+= "pp.addData(xx,yy,0,0,0,0); \n "; s+= " \n "; s+= "Last example combines different methods of the plot class together \n "; s+= " \n "; s+= "Program 1.6.13-11 \n "; s+= "import java.io.*; \n "; s+= " \n "; s+= "class f3 extends fi_x \n "; s+= "{ \n "; s+= " double[] func(double x) \n "; s+= " { \n "; s+= " double f[]=new double[2]; \n "; s+= " f[0]=Math.sin(x)+x/2; \n "; s+= " f[1]=Math.sqrt(x); \n "; s+= " return f; \n "; s+= " }} \n "; s+= "class PlotT3 \n "; s+= "{ \n "; s+= " public static void main (String args[]) throws IOException \n "; s+= " {//Plot pp=new Plot(); \n "; s+= " String fi[]={\"in.txt\",\"out.txt\"}; \n "; s+= " f3 ff=new f3(); \n "; s+= " int ipt[]={0,1}; \n "; s+= " int ir[]={0,0}; \n "; s+= " int ig[]={0,0}; \n "; s+= " int ib[]={0,2}; \n "; s+= " double xx[]={1,2,3,4,5,6}; \n "; s+= " double yy[]={2,3,4,5,6,7}; \n "; s+= " Plot pp=new Plot(fi); //read data from fi (in.txt,out.txt data are added up) \n "; s+= " //pp.addData(fi); \n "; s+= " pp.setPlotType(0,0);// \n "; s+= " pp.setPlotType(1,1);// \n "; s+= " pp.addData(xx,yy,0,0,0,0); //one set of addional data \n "; s+= " pp.setPlotType(2,41);//bar chart \n "; s+= " pp.setColor(2,0,0,255); //blue \n "; s+= " pp.addFunction(ff,0.0,20.0,50,2,ipt,ir,ig,ib); //two additional function \n "; s+= " pp.setPlabel(\"Fonksiyon Plot\"); \n "; s+= " pp.setXlabel(\"x ekseni\"); \n "; s+= " pp.setYlabel(\"y ekseni\"); \n "; s+= " pp.plot();}} \n "; s+= " \n "; s+= "Grafik Package basic components : \n "; s+= " \n "; s+= "Plot.java constructors, input and output methods \n "; s+= "PlotW.java : basic graphic and control algorithms \n "; s+= "PlotShapesSW: defines different plot shapes such as line, star,triangle \n "; s+= "Plot2D : Converts shapes defined in the PlotShapesSW class into plot screen \n "; s+= "format \n ";

s+= "\n"; s+= "The list of class Plot constructors \n "; s+= " \n "; s+= "Constructor classes : (defines the graphic window and prepare the data to be plotted) \n "; s+= "public Plot() throws IOException \n "; s+= "public Plot(String pl,String xl,String yl,int xnt,int ynt,int xgo,int ygo,String fn[],int ipt[],int ir[],int ig[] \n "; s+= " ,int ib[]) throws IOException \n "; s+= " public Plot(String fn[],int ipt[],int ir[],int ig[],int ib[]) throws IOException \n "; s+= " public Plot(String fn[]) throws IOException : Read file names from String fn[] and plot them \n "; s+= " public Plot(double xi[][],double yi[][],int ipt[],int ir[],int ig[],int ib[]) : read plot data from xi[][] and yi[][] \n "; s+= " public Plot(double xiyi[][]) : read plot data xi[] and yi[] from xiyi[][] string \n "; s+= " public Plot(double xi[][],double yi[][]) : read plot data from xi[][] and yi[][] \n "; s+= " public Plot(double xi[],double yi[]) : read plot data from xi[] and yi[] \n "; s+= " public Plot(f_x f,double xm,double xma,int N,int ipt,int ir,int ig,int ib) \n "; s+= " public Plot(f_x f,double xm,double xma,int N) : create plot data from a simple function f_x f \n "; s+= " public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function) \n "; s+= " \n "; s+= "Static method definitions : (give short descriptions for one step plotting) \n "; s+= " public static double[] data(double xmin,double xmax,double dx) \n "; s+= " public static double[] data(double xmin,double xmax,int n) \n "; s+= " public static Plot func(f_x ff1,double xmin,double xmax,int n,int pnumber) \n "; s+= " public static Plot func(f_x ff1,double xmin,double xmax,int n) \n "; s+= " public static Plot func(f_x ff1,double xmin,double xmax) \n "; s+= " public static Plot func(fi_x ff1,double xmin,double xmax,int n,int pnumber) \n "; s+= " public static Plot func(fi_x ff1,double xmin,double xmax,int n) \n "; s+= " public static Plot func(fi_x ff1,double xmin,double xmax) \n "; s+= " \n "; s+= " adding x,y and heading label \n "; s+= " public void setPlabel(String ip) {label=ip;} \n "; s+= " public void setXlabel(String ix) {xlabel=ix;} \n "; s+= " public void setYlabel(String iy) {ylabel=iy;} \n "; s+= " public void setXYlabel(String ix,String iy) {xlabel=ix;ylabel=iy;} \n "; s+= " \n "; s+= "Adding additional data into the graphic window \n "; s+= "public void addData(double xi[],double yi[]) \n "; s+= "public void addData(double xi[],double yi[],int iplottype) \n "; s+= "public void addData(double xi[],double yi[],int iplottype,int ir,int ig,int ib) \n "; s+= "public void addData(double xi[][],double yi[][]) \n "; s+= "public void addData(double xi[][],double yi[][],int ip[]) \n "; s+= "public void addData(double xi[][],double yi[][],int ip[],int ir[],int ig[],int ib[]) \n "; s+= "public void addFunction(f_x f,double xmi,double xma,int N,int ipt) \n "; s+= "public void addFunction(f_x f,double xmi,double xma,int N) \n "; s+= "public void addFunction(f_x f,double xmi,double xma,int N,int ipt,int ir,int ig,int ib) \n "; s+= "public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function) \n "; s+= "public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,int ipt[]) \n "; s+= "public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function, ,int ipt[]) \n "; s+= " \n "; s+= "adding data with curve fit \n "; s+= "public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi) \n "; s+= "public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype) \n "; s+= "public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= "public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi) \n "; s+= "public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype) \n "; s+= "public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= "public void addEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype) \n "; s+= "public void addEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= "public void addLagrange(double xi[],double yi[],int aradegersayisi) \n "; s+= "public void addLagrange(double xi[],double yi[],int aradegersayisi,int iplottype) \n "; s+= "public void addLagrange(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= "public void addLagrange(int linenumber,int aradegersayisi,int iplottype) \n "; s+= "public void addLagrange(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= "public void addB_Spline(double xi[],double yi[],int nSpline) \n "; s+= "public void addB_Spline(double ai[][],int nSpline) \n "; s+= "public void addB_Spline(double xi[],double yi[],int nSpline,int iplottype) \n "; s+= "public void addB_Spline(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= "public void addB_Spline(int linenumber,int nSpline) \n ";

s+= "public void addB_Spline(int linenumber,int nSpline,int iplottype) \n "; s+= "public void addB_Spline(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= "public void addSpline(double xi[],double yi[],int nSpline) \n "; s+= "public void addSpline(double ai[][],int nSpline) \n "; s+= "public void addSpline(double xi[],double yi[],int nSpline,int iplottype) \n "; s+= "public void addSpline(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= "public void addSpline(int linenumber,int nSpline) \n "; s+= "public void addSpline(int linenumber,int nSpline,int iplottype) \n "; s+= "public void addSpline(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib) \n "; s+= " \n "; s+= "changing the graphic properties \n "; s+= "===========Grafik tr ===================== \n "; s+= "public void setPlotType(int plot_type[]) \n "; s+= "public void setPlotType(int dataset,int plot_no) \n "; s+= "public void setPlotType(int dataset,char plot_char) \n "; s+= "===========x ve y grid =========================== \n "; s+= "public void setXgrid(int igx) \n "; s+= "public void setYgrid(int igy) \n "; s+= "public void setGrid(int igx,int igy) \n "; s+= "public void setXtic(int ixt) \n "; s+= "public void setYtic(int iyt) \n "; s+= "public void setXYtic(int ixt,int iyt) \n "; s+= "==========logaritmik graphic scale openning and closing ========== \n "; s+= "public void setXlogScaleOn() \n "; s+= "public void setYlogScaleOn() \n "; s+= "public void setXlogScaleOff() \n "; s+= "public void setYlogScaleOff() \n "; s+= "==========setColor========== \n "; s+= "public void setColor(int dataset,int ired,int igreen,int iblue) \n "; s+= "public void setColor(int dataset,Color c) \n "; s+= "public void setColor(int ired[],int igreen[],int iblue[]) \n "; s+= "public void setColor(Color c[]) \n "; s+= "================ Setting maximum and minimum values of the plot window manually ==== \n "; s+= "public void setMinMax() \n "; s+= "public void setMinMax(double xi[][],double yi[][]) \n "; s+= "public void setMinMax(double iminx,double imaxx,double iminy,double imaxy) \n "; s+= "=============== Formatting graphic window ================================== \n "; s+= " public void set_plotwindow(int width,int height,double xip,double yip,double dxp,double dyp) \n "; s+= " public void setabsMaxMin(int ixmin,int iymin,int idx,int idy) \n "; s+= " \n "; s+= "Drawing the data in graphic format \n "; s+= "public void plot() \n "; return s;} }

PROGRAM 1.3.1-22 Plot2D class


import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.awt.print.PrinterJob; import java.awt.print.*; public class Plot2D extends JPanel implements Printable { public Graphics2D g; public Plot p1; final static Color bg = Color.white; final static Color fg = Color.black; final static Color red = Color.red; final static Color white = Color.white; final static BasicStroke stroke = new BasicStroke(1.0f); final static BasicStroke boldStroke = new BasicStroke(2.0f);

PlotShapesSW ps; double xx[],yy[]; int xn; public Plot2D(Plot pi) { //Initialize drawing colors setBackground(Color.white); setForeground(Color.black); //read data from file Plot data previously defined p1=pi; xx=new double[200]; yy=new double[200]; xn=0; } public Plot2D() { //Initialize drawing colors setBackground(Color.white); setForeground(Color.black); //read data from file Plot data previously defined try{p1=new Plot(); } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");} xx=new double[200]; yy=new double[200]; xn=0; } public Plot2D(String pl,String xl,String yl,int xnt,int ynt ,int xgo,int ygo,String ifn[],int ipt[],int ir[],int ig[] ,int ib[]) { //input variables // pl : plot label // xl : x axis label // yl : y axis label //xnt : x axis tic (0=off, any other:on) //ynt : y axis tic (0=off, any other:on) //xgo : x axis grid on (0=off, any other:on) //ygo : y axis grid on (0=off, any other:on) //ifn :file name array variable //ipt : plot type array variable //ir : color red component array variable //ig : color green componenet array variable //Initialize drawing colors setBackground(Color.white); setForeground(Color.black); try{ p1=new Plot(pl,xl,yl,xnt,ynt,xgo,ygo,ifn,ipt,ir,ig,ib); } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");} xx=new double[200]; yy=new double[200]; xn=0; } public void setXY(double ixx[],double iyy[],int ixn) { xn=ixn; for(int i=0;i<xn;i++) {xx[i]=ixx[i];yy[i]=iyy[i];} } public void yenidenPlotDatasiOku() { try{ p1=new Plot(); } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");}

} public void yenidenPlotDatasiOku(Plot pi) { p1=pi; } public void plotVerisiEkle(double xi[],double yi[],int iplottype,int ir,int ig,int ib) { p1.addData(xi,yi,iplottype,ir,ig,ib); } public void plotVerisiEkle(double xi[][],double yi[][],int iplottype[],int ir[],int ig[],int ib[]) { p1.addData(xi,yi,iplottype,ir,ig,ib); } public void plotVerisiEkle(f_x f,double xmin,double xmax,int N,int iplottype,int ir,int ig,int ib) { p1.addFunction(f,xmin,xmax,N,iplottype,ir,ig,ib); } public void yenidenPlotDatasiOku(String pl,String xl,String yl,int xnt,int ynt ,int xgo,int ygo,String ifn[],int ipt[],int ir[],int ig[] ,int ib[]) { //input variables // pl : plot label // xl : x axis label // yl : y axis label //xnt : x axis tic (0=off, any other:on) //ynt : y axis tic (0=off, any other:on) //xgo : x axis grid on (0=off, any other:on) //ygo : y axis grid on (0=off, any other:on) //ifn :file name array variable //ipt : plot type array variable //ir : color red component array variable //ig : color green componenet array variable //ib : color blue componenet //Initialize drawing colors setBackground(Color.white); setForeground(Color.black); try{ p1=new Plot(pl,xl,yl,xnt,ynt,xgo,ygo,ifn,ipt,ir,ig,ib); } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");} } public void yenidenciz() { repaint(); } public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi >= 1) { return Printable.NO_SUCH_PAGE; } super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; drawShapes(g2); return Printable.PAGE_EXISTS; }

public void drawShapes(Graphics2D g2) { g=g2; //draw Plot

Dimension d=getSize(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int gridWidth = d.width; int gridHeight = d.height; int rowspacing=(int)(d.width/100.0); int columnspacing=(int)(d.height/50.0); int rectWidth=gridWidth-columnspacing; int rectHeight=gridHeight-rowspacing; p1.xabsmin=(int)(0.25*rectWidth); p1.yabsmin=(int)(0.2*rectHeight); p1.abswidth=(int)(0.65*rectWidth); p1.absheight=(int)(0.7*rectHeight); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); g2.setStroke(stroke); g2.setPaint(fg); g2.drawRect(p1.xabsmin,p1.yabsmin,p1.abswidth,p1.absheight); ps=new PlotShapesSW(g2,p1.xabsmin,p1.yabsmin, p1.absheight,p1.abswidth,p1.xmin,p1.xmax,p1.ymin,p1.ymax,p1.xlog,p1.ylog); if(p1.xgridon!=0) ps.drawXGrid(p1.xntic); if(p1.ygridon!=0) ps.drawYGrid(p1.yntic); int i,j; ps.drawMouseTic(xx,yy,xn); for(i=0;i<p1.nline;i++) { // Select plot colors g2.setPaint(new Color(p1.red[i],p1.green[i],p1.blue[i])); ps.drawPlotLines(i,p1.plottype,p1.x,p1.y,p1.n,p1.ch); }//end of for(i=0 for(i=0;i<5;i++) { if(p1.label_at_xy[i]!="") ps.drawAdditionalLabel(p1.label_at_xy[i],p1.label_x[i],p1.label_y[i]); } if(p1.skutu1[0]!="") { p1.dx1=(p1.xmax-p1.xmin)/30.0; p1.dy1=(p1.ymax-p1.ymin)/30.0; ps.drawString(p1.skutu1,p1.plottype,p1.x1,p1.y1,p1.dx1,p1.dy1);} if(p1.skutu2[0]!="") { p1.dy2=(p1.ymax-p1.ymin)/30.0; ps.drawString(p1.skutu2,p1.x2,p1.y2,p1.dy2); } g2.setPaint(fg); ps.drawXTic(p1.xntic,(p1.ymax-p1.ymin)/80.0,0); ps.drawYTic(p1.yntic,(p1.xmax-p1.xmin)/80.0,0); ps.drawXNumbers(p1.xntic); ps.drawYNumbers(p1.yntic); ps.drawLabel(p1.label); ps.drawXLabel(p1.xlabel); ps.drawYLabel(p1.ylabel); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; drawShapes(g2); } }

PROGRAM 1.3.1-23 PlotShapesSW class


//====================================================== // Numerical Analysis package in java

// PlotShapes class // This class convert graphic draw methods to // plot coordinates and gives additional plotting methods // Dr. Turhan Coban // ===================================================== import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.awt.font.*; import java.io.*; import java.util.Locale; public class PlotShapesSW { public Graphics2D g; int xabsmin,yabsmin; int absheight,abswidth; double xmin,xmax,ymin,ymax; public int xlog,ylog; double xminmaxlog,yminmaxlog; Font f; final static float dash1[] = {10.0f}; final static float dash2[] = {10.0f,3.0f,3.0f}; final static float dash3[] = {10.0f,3.0f,3.0f}; final static float dash4[] = {5.0f,5.0f,5.0f}; final static BasicStroke d1 = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash1, 2.0f); final static BasicStroke d2 = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash2, 2.0f); final static BasicStroke d3 = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash3, 2.0f); final static BasicStroke d4 = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash4, 2.0f); final static BasicStroke d5 = new BasicStroke(2.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash1, 2.0f); final static BasicStroke d6 = new BasicStroke(2.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash2, 2.0f); final static BasicStroke d7 = new BasicStroke(2.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash3, 2.0f); final static BasicStroke d8 = new BasicStroke(2.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash4, 2.0f);

public PlotShapesSW(Graphics2D gi,int xabsmini ,int yabsmini, int absheighti,int abswidthi, double xmini,double xmaxi, double ymini,double ymaxi, int xlogi,int ylogi) { // xabsmin : absolute(pixel) starting point x axis // yabsmin : absolute(pixel) starting point y axis // absheight : absolute(pixel) height of plotting window // abswidth : absolute(pixel) width of plotting window // xmin : minimum x value (real number) // xmax : maximum x value (real number) // ymin : minimum y value (real number) // ymax : maximum y value (real number)

// g : graphic object that actual drawing is done through g=gi; //Font fonts[]=GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); //f=fonts[2].deriveFont(Font.BOLD,14);//You can change Fony from here //g.setFont(f); xabsmin=xabsmini; yabsmin=yabsmini; absheight=absheighti; abswidth=abswidthi; xmin=xmini; xmax=xmaxi; ymin=ymini; ymax=ymaxi; xlog=xlogi; ylog=ylogi; if(xlog!=0) {xminmaxlog=Math.log10(xmax-xmin+10.0)-1.0;} if(ylog!=0) {yminmaxlog=Math.log10(ymax-ymin+10.0)-1.0;} } public void drawLine(int plottype,double x1,double y1,double x2,double y2) { // draw a line from (x1,y1) to (x2,y2) int xx1,xx2,yy1,yy2; if(xlog!=0) { xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth)); xx2=(int)(xabsmin+((Math.log10(x2-xmin+10.0)-1.0)/xminmaxlog*abswidth)); } else { xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth); xx2=(int)(xabsmin+(x2-xmin)/(xmax-xmin)*abswidth); } if(ylog!=0) { yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight)); yy2=(int)(yabsmin+absheight-((Math.log10(y2-ymin+10.0)-1.0)/yminmaxlog*absheight)); } else { yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight); yy2=(int)(yabsmin+absheight-(y2-ymin)/(ymax-ymin)*absheight); } g.draw(new Line2D.Double( xx1,yy1,xx2,yy2)); } //end of drawLine

public void drawPolyline(int plottype,double x1[],double y1[]) { // draw a line from (x1,y1) to (x2,y2) int points=y1.length; int xPoints[]=new int[points]; int yPoints[]=new int[points]; int xx1,yy1; for(int i=0;i<points;i++) { if(xlog!=0) {xx1=(int)(xabsmin+(Math.log10(x1[i]-xmin+10.0)-1.0)/xminmaxlog*abswidth);} else {xx1=(int)(xabsmin+(x1[i]-xmin)/(xmax-xmin)*abswidth);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-(Math.log10(y1[i]-ymin+10.0)-1.0)/yminmaxlog*absheight);} else {yy1=(int)(yabsmin+absheight-(y1[i]-ymin)/(ymax-ymin)*absheight);} xPoints[i]=xx1; yPoints[i]=yy1;

} g.drawPolyline(xPoints,yPoints,points); } //end of drawPolyLine public void drawChar(char ch,double x1,double y1) { // draws a single character at (x1,y1) char ch1[]=new char[1]; ch1[0]=ch; int h=g.getFontMetrics().getHeight(); int w=h-2; int xx1,yy1; if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-abswidth/200.0));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth);}//-abswidth/200.0);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight))+h/2;} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight);}//+h/2;} g.drawChars(ch1,0,1,xx1,yy1); } //end of PlotShapes.drawChar public void drawChars(char ch[],int firstspace,int numberofchars, double x1,double y1) { int h=g.getFontMetrics().getHeight(); int w=h-2; int xx1,yy1; if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-abswidth/80.0));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth);}//-abswidth/80.0);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight);} g.drawChars(ch,firstspace,numberofchars,xx1,yy1); } //end of PlotShapes.drawChars public void drawString(String s,double x1,double y1) { //draws a String at (x1,y1) int h=g.getFontMetrics().getHeight(); int w=h-2; int xx1,yy1; if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+1.0)-1.0)/xminmaxlog*abswidth));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight);} g.drawString(s,xx1,yy1); } //end of PlotShapes.drawString public void drawString(String s[],double x1,double y1,double dy) { // draws a serial of Strings starting at (x1,y1) // increasing with dy each time int h=g.getFontMetrics().getHeight(); int w=h-2; double xx1,yy1;

int n=s.length; xx1=x1; for(int i=0;i<n;i++) { yy1=y1-i*dy; if(s[i]!="") drawString(s[i],xx1,yy1); else break; } } //end of PlotShapes.drawStrings public void drawString(String s[],int plottypes[],double x1,double y1,double dx,double dy) { // draws a serial of plot characters and then Strings starting at (x1,y1) // increasing with dy each time double x[]=new double[2]; double y[]=new double[2]; int h=g.getFontMetrics().getHeight(); int w=h-2; int xx1,yy1; int n=s.length; double xx2=x1+2*dx; double y2=y[0]; double yy2=0; for(int i=0;i<n;i++) { if(s[i]!="") { x[0]=x1; x[1]=x1+dx; y[0]=y1-i*dy; y[1]=y1-i*dy; yy2=y[0]-(xmax-xmin)/50.0; //drawPlotLine(int plottypei,double xi[],double yi[]) drawPlotLine(plottypes[i],x,y); drawString(s[i],xx2,yy2); } else break; } } //end of PlotShapes.drawStrings public void drawRect(double x1,double y1,int rectwidth,int rectheight) { // draw a rectangle starting at (x1,y1) // with dimensions of (rectwidth,rectheight) int xx1,yy1; if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-rectwidth/2));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth-rectwidth/2);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight-rectheight/2));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight-rectheight/2);} g.draw(new Rectangle2D.Double(xx1,yy1,rectwidth,rectheight)); }//end of drawRect public void drawBar(double x1,double y1) { // draw a rectangle starting at (x1,y1) // with dimensions of (rectwidth,rectheight) int xx1,yy1; double barheight; double barwidth=abswidth/300; if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-barwidth/2.0));} else

{xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth-barwidth/2.0); } if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight)); barheight =(int)((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight); } else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight); barheight =(int)((y1-ymin)/(ymax-ymin)*absheight);} g.draw(new Rectangle2D.Double(xx1,yy1,barwidth,barheight)); }//end of drawBar public void fillBar(double x1,double y1) { int xx1,yy1; double barheight; double barwidth=abswidth/300; if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-barwidth/2.0));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth-barwidth/2.0); } if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight)); barheight =(int)((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight); } else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight); barheight =(int)((y1-ymin)/(ymax-ymin)*absheight);} g.fill(new Rectangle2D.Double(xx1,yy1,barwidth,barheight)); }//end of fillBar public void fillRect(double x1,double y1,int rectwidth,int rectheight) { // draw a rectangle starting at (x1,y1) // with dimensions of (rectwidth,rectheight) // draw a rectangle starting at (x1,y1) // with dimensions of (rectwidth,rectheight) int xx1,yy1; if(xlog!=0) {xx1=(int)(xabsmin+(Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-rectwidth/2);} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth-rectwidth/2);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight-rectheight/2));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight-rectheight/2);} g.fill(new Rectangle2D.Double(xx1,yy1,rectwidth,rectheight)); } public void drawEllipse(double x1,double y1,int width,int height) { // draw an oval with the centre of (x1,y1) // with dimension of (width,height) int xx1,yy1; if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-width/2));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth-width/2);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight-height/2));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight-height/2);} g.draw(new Ellipse2D.Double(xx1,yy1,width,height)); }//end of drawRect

public void fillEllipse(double x1,double y1,int width,int height) { // draw an oval with the centre of (x1,y1) // with dimension of (width,height) int xx1,yy1; if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-width/2));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth-width/2);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight-height/2));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight-height/2);} g.fill(new Ellipse2D.Double(xx1,yy1,width,height)); }//end of drawRect public void drawPolygon(double x1,double y1,int radius,int side) { //draw a polygon of n sides n=3(trinagle),n=4(dimond).... double Pi=Math.PI; int xvalues[]=new int[side+1]; int yvalues[]=new int[side+1]; GeneralPath polygon=new GeneralPath(GeneralPath.WIND_EVEN_ODD, xvalues.length); double angle_increase; double angle; angle_increase=2.0*Pi/side; angle=Pi/2.0; for(int i=0;i<=side;i++) { if(xlog!=0) {xvalues[i]=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth+radius*Math.cos(angle)));} else {xvalues[i]=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth+radius*Math.cos(angle));} if(ylog!=0) {yvalues[i]=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight-radius*Math.sin(angle)));} else {yvalues[i]=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight-radius*Math.sin(angle));} angle+=angle_increase; if(i==0) { xvalues[side]=xvalues[i];yvalues[side]=yvalues[i]; polygon.moveTo(xvalues[0],yvalues[0]); } else { polygon.lineTo(xvalues[i],yvalues[i]); } } g.draw(polygon); }//end of drawPolygon public void fillPolygon(double x1,double y1,int radius,int side) { //draw a polygon of n sides n=3(trinagle),n=4(dimond).... //draw a polygon of n sides n=3(trinagle),n=4(dimond).... double Pi=Math.PI; int xvalues[]=new int[side+1]; int yvalues[]=new int[side+1]; GeneralPath polygon=new GeneralPath(GeneralPath.WIND_EVEN_ODD, xvalues.length); double angle_increase; double angle; angle_increase=2.0*Pi/side; angle=Pi/2.0; for(int i=0;i<=side;i++)

{ if(xlog!=0) {xvalues[i]=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth+radius*Math.cos(angle)));} else {xvalues[i]=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth+radius*Math.cos(angle));} if(ylog!=0) {yvalues[i]=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight-radius*Math.sin(angle)));} else {yvalues[i]=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight-radius*Math.sin(angle));} angle+=angle_increase; if(i==0) { xvalues[side]=xvalues[i];yvalues[side]=yvalues[i]; polygon.moveTo(xvalues[0],yvalues[0]); } else { polygon.lineTo(xvalues[i],yvalues[i]); } } g.fill(polygon); }//end of fillPolygon public void drawStar(double x1,double y1,int radius,int side) { double teta=2.0*Math.PI/side; double R=radius; double r=radius*0.35; int xvalues[]=new int[2*side+2]; int yvalues[]=new int[2*side+2]; GeneralPath polygon=new GeneralPath(GeneralPath.WIND_EVEN_ODD, xvalues.length); double teta1,teta2; for(int i=0;i<=side;i++) { teta1=teta*i+Math.PI/2.0; teta2=teta/2.0+teta1; if(xlog!=0) { xvalues[2*i]= (int)(Math.floor(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/ xminmaxlog*abswidth+R*Math.cos(teta1)))); xvalues[2*i+1]= (int)(Math.floor(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/ xminmaxlog*abswidth+r*Math.cos(teta2)))); } else { xvalues[2*i]= (int)(Math.floor(xabsmin+(x1-xmin)/ (xmax-xmin)*abswidth+R*Math.cos(teta1))); xvalues[2*i+1]= (int)(Math.floor(xabsmin+(x1-xmin)/ (xmax-xmin)*abswidth+r*Math.cos(teta2))); } if(ylog!=0) { yvalues[2*i]= (int)(Math.floor(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/ yminmaxlog*absheight-R*Math.sin(teta1)))); yvalues[2*i+1]= (int)(Math.floor(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/ yminmaxlog*absheight-r*Math.sin(teta2)))); } else { yvalues[2*i]= (int)(Math.floor(yabsmin+absheight-(y1-ymin)/ (ymax-ymin)*absheight-R*Math.sin(teta1))); yvalues[2*i+1]= (int)(Math.floor(yabsmin+absheight-(y1-ymin)/ (ymax-ymin)*absheight-r*Math.sin(teta2))); }

if(i==side) { xvalues[2*i]=xvalues[0]; yvalues[2*i]=yvalues[0]; } } for(int i=0;i<=2*side;i++) { if(i==0) { polygon.moveTo(xvalues[0],yvalues[0]); } else { polygon.lineTo(xvalues[i],yvalues[i]); } } g.draw(polygon); }//end of drawStar public void fillStar(double x1,double y1,int radius,int side) { double teta=2.0*Math.PI/side; double R=radius; double r=radius*0.35; int xvalues[]=new int[2*side+2]; int yvalues[]=new int[2*side+2]; GeneralPath polygon=new GeneralPath(GeneralPath.WIND_EVEN_ODD, xvalues.length); double teta1,teta2; for(int i=0;i<=side;i++) { teta1=teta*i+Math.PI/2.0; teta2=teta/2.0+teta1; if(xlog!=0) { xvalues[2*i]= (int)(Math.floor(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/ xminmaxlog*abswidth+R*Math.cos(teta1)))); xvalues[2*i+1]= (int)(Math.floor(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/ xminmaxlog*abswidth+r*Math.cos(teta2)))); } else { xvalues[2*i]= (int)(Math.floor(xabsmin+(x1-xmin)/ (xmax-xmin)*abswidth+R*Math.cos(teta1))); xvalues[2*i+1]= (int)(Math.floor(xabsmin+(x1-xmin)/ (xmax-xmin)*abswidth+r*Math.cos(teta2))); } if(ylog!=0) { yvalues[2*i]= (int)(Math.floor(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/ yminmaxlog*absheight-R*Math.sin(teta1)))); yvalues[2*i+1]= (int)(Math.floor(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/ yminmaxlog*absheight-r*Math.sin(teta2)))); } else { yvalues[2*i]= (int)(Math.floor(yabsmin+absheight-(y1-ymin)/ (ymax-ymin)*absheight-R*Math.sin(teta1))); yvalues[2*i+1]= (int)(Math.floor(yabsmin+absheight-(y1-ymin)/ (ymax-ymin)*absheight-r*Math.sin(teta2))); } if(i==side)

{ xvalues[2*i]=xvalues[0]; yvalues[2*i]=yvalues[0]; } } for(int i=0;i<=2*side;i++) { if(i==0) { polygon.moveTo(xvalues[0],yvalues[0]); } else { polygon.lineTo(xvalues[i],yvalues[i]); } } g.fill(polygon); }//end of drawStar

public void drawXTic(int ticNumber,double ticHeight,int ticSide) { //draw a series of x axis tics double dtic=(xmax-xmin)/ticNumber; double x1=xmin; double y1=ymin; double dy; dy=ticHeight; int xx1,yy1,yy2,yy3; for(int i=0;i<=ticNumber;i++) { if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth);} yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight); yy2=(int)(yabsmin+absheight-(y1-dy-ymin)/(ymax-ymin)*absheight); yy3=(int)(yabsmin+absheight-(y1+dy-ymin)/(ymax-ymin)*absheight); if(ticSide==0) {g.draw(new Line2D.Double( xx1,yy1,xx1,yy2));} else {g.draw(new Line2D.Double( xx1,yy1,xx1,yy3));} x1=x1+dtic; } }

public void drawYTic(int ticNumber,double ticWidth,int ticSide) { //draw a series of y axis tics double dtic=(ymax-ymin)/ticNumber; double x1=xmin; double y1=ymin; double dx; dx=ticWidth; double xx1,xx2,xx3,yy1; for(int i=0;i<=ticNumber;i++) {if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight);} xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth); xx2=(int)(xabsmin+(x1-dx-xmin)/(xmax-xmin)*abswidth); xx3=(int)(xabsmin+(x1+dx-xmin)/(xmax-xmin)*abswidth); if(ticSide==0) {g.draw(new Line2D.Double( xx1,yy1,xx2,yy1));}

else {g.draw(new Line2D.Double( xx1,yy1,xx3,yy1));} y1=y1+dtic; } } public void drawXGrid(int ticNumber) { //draw x gridlies ||||||| double dtic=(xmax-xmin)/ticNumber; double x1=xmin; for(int i=0;i<=ticNumber;i++) { drawLine(0,x1,ymin,x1,ymax); x1=x1+dtic; } } public void drawYGrid(int ticNumber) { //draw y gridlines ===== double dtic=(ymax-ymin)/ticNumber; double y1=ymin; for(int i=0;i<=ticNumber;i++) { drawLine(0,xmin,y1,xmax,y1); y1=y1+dtic; } } public void drawXNumbers(int ticNumber) { //draw x numbers Locale en=new Locale("US"); double dtic=(xmax-xmin)/ticNumber; double x1=xmin; double y1=ymin; int xx1,yy1; String s; s=" "; for(int i=0;i<=ticNumber;i++) { if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-abswidth/200.0));} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth-abswidth/200.0);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight)+absheight/20.0);} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight+absheight/20.0);} s=String.format(en,"%4.2g",x1); g.drawString(s,xx1,yy1); x1=x1+dtic; } }

public void drawYNumbers(int ticNumber) { // draw y numbers Locale en=new Locale("US"); double dtic=(ymax-ymin)/ticNumber; double x1=xmin; double y1=ymin; int xx1,yy1; String s;

s=" "; for(int i=0;i<=ticNumber;i++) { if(xlog!=0) {xx1=(int)(xabsmin+((Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth-abswidth/200.0)-abswidth/10.0);} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth/200.0-abswidth/10.0);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight)+5;} s=String.format(en,"%5.3g",y1); g.drawString(s,xx1,yy1); y1=y1+dtic; } } public void drawXLabel(String xLabel) { // draw x labels double x1=xmin+(xmax-xmin)/2.0; double y1=ymin; int xx1,yy1; if(xlog!=0) {xx1=(int)(xabsmin+(Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth);} else {xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-(Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight+abswidth/12.0);} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight+abswidth/12.0);} g.drawString(xLabel,xx1,yy1); } public void drawYLabel(String yLabel) { // draw y labels double x1; double y1=ymax-(ymax-ymin)/2.0; int xx1,yy1; if(xlog!=0) {x1=xmin-2.7;xx1=(int)(xabsmin+(Math.log10(x1-xmin+10.0)-1.0)/xminmaxlog*abswidth);} else {x1=xmin-(xmax-xmin)/6.0;xx1=(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth);} if(ylog!=0) {yy1=(int)(yabsmin+absheight-((Math.log10(y1-ymin+10.0)-1.0)/yminmaxlog*absheight));} else {yy1=(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight);} int n=yLabel.length(); char ch[]=new char[n]; yLabel.getChars(0,n,ch,0); TextLayout t=new TextLayout(yLabel,g.getFont(),g.getFontRenderContext()); AffineTransform at=AffineTransform.getRotateInstance(3.0*Math.PI/2.0,xx1,yy1); g.setTransform(at); t.draw(g,xx1,yy1); at=AffineTransform.getRotateInstance(0,xx1,yy1); g.setTransform(at); } public void drawLabel(String Label) { //draw graphic label double x1=xmin+(xmax-xmin)/2.0; double y1=ymax; g.drawString(Label,(int)(xabsmin+(x1-xmin)/(xmax-xmin)*abswidth),

(int)(yabsmin+absheight-(y1-ymin)/(ymax-ymin)*absheight)-(int)(abswidth/40.0)); } public void drawAdditionalLabel(String s,double x1,double y1) { //draw Additional graphic label drawString(s,x1,y1); } public void drawMouseTic(double xi[],double yi[],int xn) { for(int i=0;i<xn;i++) { if((xi[i]>=xmin && xi[i]<=xmax) && (yi[i]>=ymin && yi[i]<=ymax)) { drawStar(xi[i],yi[i],(int)(abswidth/80.0),5); } } } public void drawPlotLine(int plottypei,double xi[],double yi[],char ch[]) { int i=0; int plottype[]=new int[1]; plottype[0]=plottypei; int ni=xi.length; int n[]=new int[1]; n[0]=ni; double x[][]=new double[1][ni]; double y[][]=new double[1][ni]; for(int j=0;j<ni;j++) {x[0][j]=xi[j];y[0][j]=yi[j];} drawPlotLines(i,plottype,x,y,n,ch); } public void drawPlotLine(int plottypei,double xi[],double yi[]) { char ch[]=new char[1]; ch[0]='+'; int i=0; int plottype[]=new int[1]; plottype[0]=plottypei; int ni=xi.length; int n[]=new int[1]; n[0]=ni; double x[][]=new double[1][ni]; double y[][]=new double[1][ni]; for(int j=0;j<ni;j++) {x[0][j]=xi[j];y[0][j]=yi[j];} drawPlotLines(i,plottype,x,y,n,ch); } public void drawPlotLines(int i,int plottype[],double x[][],double y[][],int n[],char ch[]) { int j; //draw lines if((plottype[i] >= 0) && (plottype[i] < 10) ) { switch ( plottype[i] ) { case 0 : g.setStroke(new BasicStroke(1.0f));break; case 1 : g.setStroke(d1);break; case 2 : g.setStroke(d2);break; case 3 : g.setStroke(d3);break; case 4 : g.setStroke(d4);break; case 5 : g.setStroke(new BasicStroke(2.0f));break; case 6 : g.setStroke(d5);break; case 7 : g.setStroke(d6);break; case 8 : g.setStroke(d7);break;

case 9 : g.setStroke(d8);break; }; for(j=0;j<n[i]-1;j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { if((x[i][j+1]>=xmin && x[i][j+1]<=xmax) && (y[i][j+1]>=ymin && y[i][j+1]<=ymax)) { drawLine(0,x[i][j],y[i][j],x[i][j+1],y[i][j+1]); } else if(x[i][j+1]>xmax) { double b=(y[i][j+1]-y[i][j])/(x[i][j+1]-x[i][j]); double a=y[i][j]-b*x[i][j]; drawLine(0,x[i][j],y[i][j],xmax,(a+b*xmax)); } else if(y[i][j+1]>ymax) { double b=(y[i][j+1]-y[i][j])/(x[i][j+1]-x[i][j]); double a=y[i][j]-b*x[i][j]; drawLine(0,x[i][j],y[i][j],(ymax-a)/b,ymax); } else if(x[i][j+1]>xmax && y[i][j+1]>ymax) { double b=(y[i][j+1]-y[i][j])/(x[i][j+1]-x[i][j]); double a=y[i][j]-b*x[i][j]; drawLine(0,x[i][j],y[i][j],(ymax-a)/b,(a+b*xmax)); } } else if((x[i][j+1]>=xmin && x[i][j+1]<=xmax ) && (y[i][j+1]>=ymin && y[i][j+1]<=ymax)) { if(x[i][j]<xmin) { double b=(y[i][j+1]-y[i][j])/(x[i][j+1]-x[i][j]); double a=y[i][j+1]-b*x[i][j+1]; drawLine(0,xmin,(a+b*xmin),x[i][j+1],y[i][j+1]); } if(y[i][j]<ymin) { double b=(y[i][j+1]-y[i][j])/(x[i][j+1]-x[i][j]); double a=y[i][j+1]-b*x[i][j+1]; drawLine(0,(ymin-a)/b,ymin,x[i][j+1],y[i][j+1]); } } }//end of for(j=0 g.setStroke(new BasicStroke(1.0f)); }//end of if(plottype //draw characters else if(plottype[i]>=10 && plottype[i]<=19) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { drawChar(ch[i],x[i][j],y[i][j]); } }//end of for(j=0; }//end else if(plottype[i]==10..19) //draw rectangles else if(plottype[i]==20) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax )

&& (y[i][j]>=ymin && y[i][j]<=ymax)) { drawRect(x[i][j],y[i][j],abswidth/100,absheight/80); } }//end of for(j=0; }//end else if(plottype[i]==20) else if(plottype[i]==21) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { fillRect(x[i][j],y[i][j],abswidth/100,abswidth/80); } }//end of for(j=0; }//end else if(plottype[i]==20) //draw circle else if(plottype[i]==22) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { drawEllipse(x[i][j],y[i][j],abswidth/100,absheight/80); } }//end of for(j=0; }//end else if(plottype[i]==22) else if(plottype[i]==23) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { fillEllipse(x[i][j],y[i][j],abswidth/100,absheight/80); } }//end of for(j=0; }//end else if(plottype[i]==21) else if(plottype[i]>=24 && plottype[i]<=27) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { drawPolygon(x[i][j],y[i][j],abswidth/100,(plottype[i]-21)); } }//end of for(j=0; }//end else if(plottype[i]==24..27) else if(plottype[i]>=28 && plottype[i]<=31) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { fillPolygon(x[i][j],y[i][j],abswidth/100,(plottype[i]-25)); } }//end of for(j=0; }//end else if(plottype[i]==24..27) else if(plottype[i]>=32 && plottype[i]<=35) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { drawStar(x[i][j],y[i][j],abswidth/100,(plottype[i]-29)); } }//end of for(j=0; } else if(plottype[i]>=36 && plottype[i]<=39)

{ for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { fillStar(x[i][j],y[i][j],abswidth/100,(plottype[i]-33)); } }//end of for(j=0; } else if(plottype[i]==40) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { drawBar(x[i][j],y[i][j]); } }//end of for(j=0; } else if(plottype[i]==41) { for(j=0;j<n[i];j++) { if((x[i][j]>=xmin && x[i][j]<=xmax ) && (y[i][j]>=ymin && y[i][j]<=ymax)) { fillBar(x[i][j],y[i][j]); } }//end of for(j=0; } } }

1.4 PLOTTING THREE DIMENSIONAL DATA AND FUNCTIONS

1.4.1 3D PLOTS BY USING VISAD PACKAGE


In order to create 3 dimensional plots in java a free distributed graphic package VisAD is used. VisAD is a professional level graphic package for 3 dimensional graphic applications. You can download the latest VisAD package from internet adres http://www.ssec.wisc.edu/~billh/visad.html. Open codes are available if you desire the compile the package yourself or you can used precompiled jar file. For your compiler to see and use the package copy visad.jar file into jre/lib/ext directory of your java compiler. To utilise this program you also need java 3D graphic package. This package can be obtained from http://www.java.sun.com. Java 3D package use openGL graphic library as default. In some older computers, your graphic card may not support openGL library, if your card is not supporting openGL, you can use DirectX graphic system. In this case you should tell java compiler you are using directX. You achive this by using java command as : java -Dj3d.rend=d3d classname You can find the latest directX version from the adres http://www.microsoft.com/windows/directx/. An interphase program to run our standart 3D function definitions in VisAD graphic environment, an interphase program is developed. Program 1.4-1 Plot3D VisAD graphic package interphase
import visad.*; import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D; import java.rmi.RemoteException; import java.awt.*; import javax.swing.*; import java.awt.event.*;

class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+0.01*(x[0]-1.0)*(x[0]-1.0); return -ff; } } public class Plot3D{ public static double[] grid(f_xj f,double x1min,double x1max,double x2min,double x2max,int NCOLS,int NROWS) { //create 1D Z grid for Z value from a given function value float[][] zi = new float[2][NCOLS * NROWS]; float xx[][]=new float[2][NCOLS * NROWS]; double zz[]=new double[NCOLS * NROWS]; double x[]=new double[2]; for(int c = 0; c < NCOLS; c++) for(int r = 0; r < NROWS; r++) { xx[0][ c * NROWS + r ]=(float)(x1min+(x1max-x1min)*c/(float)(NCOLS-1)); x[0]=(x1min+(x1max-x1min)*c/(float)(NCOLS-1)); xx[1][ c * NROWS + r ]=(float)(x2min+(x2max-x2min)/(float)(NROWS-1)*r); x[1]=(x2min+(x2max-x2min)*r/(float)(NROWS-1)); zz[ c * NROWS + r ]=f.func(x); } return zz; } public static double[][] grid2D(f_xj f,double x1min,double x1max,double x2min,double x2max,int NCOLS,int NROWS) { //create 2D Z grid for Z value from a given function value float[][] zi = new float[2][NCOLS * NROWS]; float xx[][]=new float[2][NCOLS * NROWS]; double zz[][]=new double[NCOLS][NROWS]; double x[]=new double[2]; for(int c = 0; c < NCOLS; c++) for(int r = 0; r < NROWS; r++) { xx[0][ c * NROWS + r ]=(float)(x1min+(x1max-x1min)*c/(float)(NCOLS-1)); x[0]=(x1min+(x1max-x1min)*c/(float)(NCOLS-1)); xx[1][ c * NROWS + r ]=(float)(x2min+(x2max-x2min)/(float)(NROWS-1)*r); x[1]=(x2min+(x2max-x2min)*r/(float)(NROWS-1)); zz[c][r]=f.func(x); } return zz; } public static void plot3D_CD2(double zz[][],double x1min,double x1max,double x2min,double x2max,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { RealType longitude, latitude; RealType altitude; // Tuple to pack longitude and latitude together RealTupleType domain_tuple; // The function (domain_tuple -> altitude ) FunctionType func_domain_alt; // Our Data values for the domain are represented by the Set Set domain_set; // The Data class FlatField FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref; // The 2D display, and its the maps DisplayImpl display;

ScalarMap latMap, lonMap; ScalarMap altMap, altRGBMap; ScalarMap altAlphaMap; ColorControl colCont; // Our color table float[][] myColorTable; // Create the quantities // Use RealType(String name, Unit unit, Set set); latitude = RealType.getRealType(sx, SI.meter, null); longitude = RealType.getRealType(sy, SI.meter, null); altitude = RealType.getRealType("z", SI.meter, null); domain_tuple = new RealTupleType(latitude, longitude);

// Create a FunctionType (domain_tuple -> range_tuple ) // Use FunctionType(MathType domain, MathType range) func_domain_alt = new FunctionType( domain_tuple, altitude); // Create the domain Set, with 12 columns and 6 rows, using an // LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) // note the "inverted" first and last values of latitude int NCOLS = zz[0].length;; int NROWS = zz.length; domain_set = new Linear2DSet(domain_tuple, x1min, x1max, NROWS, x2min, x2max, NCOLS);

// The actal altitudes values // ordered as float[ NROWS ][ NCOLS ] double[][] alt_samples = zz;

// Our 'flat' array double[][] flat_samples = new double[1][NCOLS * NROWS]; // Fill our 'flat' array with the altitude values // by looping over NCOLS and NROWS // Note the use of an index variable, indicating the order of the samples int index = 0; double zmin=2.0e60,zmax=-2e60;; for(int c = 0; c < NCOLS; c++) for(int r = 0; r < NROWS; r++) { flat_samples[0][ index ] = alt_samples[c][r]; if(flat_samples[0][ index ]<zmin) zmin=flat_samples[0][ index ]; if(flat_samples[0][ index ]>zmax) zmax=flat_samples[0][ index ]; index++; } // Create a FlatField // Use FlatField(FunctionType type, Set domain_set)

vals_ff = new FlatField( func_domain_alt, domain_set);

// ...and put the altitude values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( flat_samples , false );

// Create Display and its maps display = new DisplayImplJ3D("display1"); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true); // Also enable Texture //dispGMC.setTextureEnable(false); dispGMC.setTextureEnable(true); // Create the ScalarMaps: latitude to XAxis, longitude to YAxis and // altitude to ZAxis and to RGB // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( latitude, Display.XAxis ); lonMap = new ScalarMap( longitude, Display.YAxis );

altRGBMap = new ScalarMap( altitude, Display.RGB ); altMap = new ScalarMap( altitude, Display.ZAxis ); altAlphaMap = new ScalarMap( altitude, Display.Alpha ); // Add maps to display display.addMap( latMap ); display.addMap( lonMap ); display.addMap( altMap ); display.addMap( altRGBMap ); display.addMap( altAlphaMap ); // Create a different color table // Note: table has red, green and blue components // and is 10 units long, i.e float[3][10]

int tableLength = 10; myColorTable = new float[3][tableLength]; for(int i=0;i<tableLength;i++){ myColorTable[0][i]= (float) 1.0f - (float)i / ((float)tableLength-1.0f); // red component myColorTable[1][i]= (float) (float)i / ((float)tableLength-1.0f); // green component myColorTable[2][i]= (float) 0.50f; // blue component } // Force top of table to be white

myColorTable[0][9]=1.0f; myColorTable[1][9]=1.0f; myColorTable[2][9]=1.0f; // Get the ColorControl from the altitude RGB map colCont = (ColorControl) altRGBMap.getControl(); // Set the table colCont.setTable(myColorTable );

// Create a data reference and set the FlatField as our data data_ref = new DataReferenceImpl("data_ref"); data_ref.setData( vals_ff );

display.addReference( data_ref); // Set maps ranges latMap.setRange(x1min, x1max); altMap.setRange(zmin, zmax);

// Create application window and add display to window JFrame jframe = new JFrame("Plot3D"); jframe.getContentPane().add(display.getComponent()); // Add the LabeledColorWidget to the frame

// Set window size and make it visible jframe.setSize(300, 300); jframe.setVisible(true); } public static void plot3D_CD1(f_xj f,double x1min,double x1max,double x2min,double x2max,int NCOLS,int NROWS,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { plot3D_CD1(grid2D(f,x1min,x1max,x2min,x2max,NCOLS, NROWS),x1min,x1max,x2min,x2max,NCONTOUR,sx,sy,sz);} public static void plot3D_CD1(double zz[][],double x1min,double x1max,double x2min,double x2max,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { RealType longitude, latitude; RealType altitude; // Tuple to pack longitude and latitude together RealTupleType domain_tuple;

// The function (domain_tuple -> altitude ) FunctionType func_domain_alt;

// Our Data values for the domain are represented by the Set Set domain_set;

// The Data class FlatField

FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref; // The 2D display, and its the maps DisplayImpl display; ScalarMap latMap, lonMap; ScalarMap altMap, altRGBMap; ColorControl colCont; // Our color table float[][] myColorTable; // Create the quantities // Use RealType(String name, Unit unit, Set set); latitude = RealType.getRealType(sx, SI.meter); longitude = RealType.getRealType(sy, SI.meter); altitude = RealType.getRealType("z", SI.meter); domain_tuple = new RealTupleType(latitude, longitude); //String ss=sz; //altitude = RealType.getRealType("z", SI.meter);

// Create a FunctionType (domain_tuple -> range_tuple ) // Use FunctionType(MathType domain, MathType range) func_domain_alt = new FunctionType( domain_tuple, altitude); // Create the domain Set, with 12 columns and 6 rows, using an // LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) // note the "inverted" first and last values of latitude int NCOLS = zz.length; int NROWS = zz[0].length; domain_set = new Linear2DSet(domain_tuple, x1min,x1max, NROWS, x2min, x2max, NCOLS); // The actal altitudes values // ordered as float[ NROWS ][ NCOLS ] double[][] alt_samples = zz; double[][] flat_samples = new double[1][NCOLS * NROWS]; int index = 0; double zmin=2.0e60,zmax=-2e60;; for(int c = 0; c < NCOLS; c++) for(int r = 0; r < NROWS; r++) { flat_samples[0][ index ] = alt_samples[c][r]; if(flat_samples[0][ index ]<zmin) zmin=flat_samples[0][ index ]; if(flat_samples[0][ index ]>zmax) zmax=flat_samples[0][ index ]; index++; } // Create a FlatField // Use FlatField(FunctionType type, Set domain_set) vals_ff = new FlatField( func_domain_alt, domain_set);

FlatField der_vals_ff ;//= new FlatField( func_domain_alt, domain_set); // ...and put the altitude values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( flat_samples , false ); der_vals_ff = (FlatField) vals_ff.derivative( longitude, Data.NO_ERRORS );//.setSamples( flat_samples , false ); // Create Display and its maps display = new DisplayImplJ3D("display1"); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true); // Also enable Texture dispGMC.setTextureEnable(false); // Create the ScalarMaps: latitude to XAxis, longitude to YAxis and // altitude to ZAxis and to RGB // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( latitude, Display.XAxis ); lonMap = new ScalarMap( longitude, Display.YAxis );

altRGBMap = new ScalarMap( altitude, Display.RGB ); altMap = new ScalarMap( altitude, Display.ZAxis ); // Add maps to display display.addMap( latMap ); display.addMap( lonMap ); display.addMap( altMap ); display.addMap( altRGBMap ); // Create a color table // Note: table has red, green and blue components // and is 8 units long, i.e float[3][8] myColorTable = new float[][]{{0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f}, // red component {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f}, // green component {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f}}; // blue component // Get the ColorControl from the altitude RGB map colCont = (ColorControl) altRGBMap.getControl(); // Set the table colCont.setTable(myColorTable ); // Create a data reference and set the FlatField as our data data_ref = new DataReferenceImpl("data_ref"); data_ref.setData( vals_ff );

display.addReference( data_ref); // Set maps ranges latMap.setRange(x1min, x1max); lonMap.setRange(x2min, x2max); altMap.setRange(zmin, zmax);

// Create application window and add display to window JFrame jframe = new JFrame(sz); jframe.getContentPane().add(display.getComponent()); // Add the LabeledColorWidget to the frame

// Set window size and make it visible

jframe.setSize(300, 300); jframe.setVisible(true); } public static void plot3D_CD(f_xj f,double x1min,double x1max,double x2min,double x2max,int NCOLS,int NROWS,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { plot3D_CD(grid2D(f,x1min,x1max,x2min,x2max,NCOLS, NROWS),x1min,x1max,x2min,x2max,NCONTOUR,sx,sy,sz);}

public static void plot3D_CD(double zz[][],double x1min,double x1max,double x2min,double x2max,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { //=============== // Declare variables // The domain quantities longitude and latitude // and the dependent quantity altitude RealType longitude, latitude; RealType altitude; // Tuple to pack longitude and latitude together RealTupleType domain_tuple;

// The function (domain_tuple -> altitude ) FunctionType func_domain_alt;

// Our Data values for the domain are represented by the Set Set domain_set;

// The Data class FlatField FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref; // The 2D display, and its the maps DisplayImpl display; ScalarMap latMap, lonMap; ScalarMap altMap, altRGBMap; ColorControl colCont; // Our color table float[][] myColorTable;

// Create the quantities // Use RealType(String name, Unit unit, Set set); latitude = RealType.getRealType(sx, SI.meter, null); longitude = RealType.getRealType(sy, SI.meter, null); domain_tuple = new RealTupleType(latitude, longitude); altitude = RealType.getRealType("z", SI.meter, null); // Create a FunctionType (domain_tuple -> range_tuple )

// Use FunctionType(MathType domain, MathType range) func_domain_alt = new FunctionType( domain_tuple, altitude); // Create the domain Set, with 12 columns and 6 rows, using an // LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) // note the "inverted" first and last values of latitude // The actal altitudes values // ordered as float[ NROWS ][ NCOLS ] double[][] alt_samples =zz; int NROWS=alt_samples.length; int NCOLS=alt_samples[0].length; domain_set = new Linear2DSet(domain_tuple, x1min,x1max, NROWS, x2min, x2max, NCOLS); // Our 'flat' array double[][] flat_samples = new double[1][NCOLS * NROWS]; // Fill our 'flat' array with the altitude values // by looping over NCOLS and NROWS // Note the use of an index variable, indicating the order of the samples int index = 0; for(int c = 0; c < NCOLS; c++) for(int r = 0; r < NROWS; r++){ // set altitude altitude flat_samples[0][ index ] = alt_samples[r][c]; // increment index index++; } // Create a FlatField // Use FlatField(FunctionType type, Set domain_set) vals_ff = new FlatField( func_domain_alt, domain_set); // ...and put the altitude values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( flat_samples , false );

// Code for slope data // Data in a FlatField FlatField slope_vals_ff ; // Calculate derivative of altitude with respect to longitude; assume no errors slope_vals_ff = (FlatField) vals_ff.derivative( longitude, Data.NO_ERRORS );

// Get the funtionc from the FlatField for slope FunctionType func_domain_slope = ((FunctionType)slope_vals_ff.getType());

// "slope" is a RealType; RealType slope = (RealType) func_domain_slope.getRange();

// Create a ScalarMap to color slope's surface ScalarMap slopeRGBMap = new ScalarMap( slope, Display.RGB );

// Create Display and its maps

display = new DisplayImplJ3D("display1"); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true); // Also enable Texture dispGMC.setTextureEnable(false);

// Create the ScalarMaps: latitude to XAxis, longitude to YAxis and // altitude to ZAxis and to RGB // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( latitude, Display.XAxis ); lonMap = new ScalarMap( longitude, Display.YAxis );

altRGBMap = new ScalarMap( altitude, Display.RGB );

altMap = new ScalarMap( altitude, Display.ZAxis );

// Add maps to display display.addMap( latMap ); display.addMap( lonMap ); display.addMap( altMap ); display.addMap( altRGBMap ); display.addMap( slopeRGBMap ); // Create a color table // Note: table has red, green and blue components // and is 8 units long, i.e float[3][8] myColorTable = new float[][]{{0, 1, 0, 0, 0, 1, 1, 1}, // red component {0, 0, 1, 0, 1, 0, 1, 1}, // green component {0, 0, 0, 1, 1, 1, 0, 1}}; // blue component

// Get the ColorControl from the altitude RGB map colCont = (ColorControl) altRGBMap.getControl(); // Set the table colCont.setTable(myColorTable );

// Create a data reference and set the FlatField as our data

data_ref = new DataReferenceImpl("data_ref"); data_ref.setData( vals_ff ); // Data reference for slope DataReferenceImpl data_ref2 = new DataReferenceImpl("data_ref2"); data_ref2.setData( slope_vals_ff ); // Add reference to display ConstantMap[] constAlpha_CMap = { new ConstantMap( 0.50f, Display.Alpha)};

ConstantMap[] constZ_CMap = { new ConstantMap( -1.0f, Display.ZAxis)}; // Add references to display // order MATTERS! display.addReference( data_ref2, constZ_CMap ); display.addReference( data_ref, constAlpha_CMap ); // Set maps ranges latMap.setRange(x1min, x1max); lonMap.setRange(x2min, x2max); //altMap.setRange(zmin, zmax); // Create application window and add display to window JFrame jframe = new JFrame(sz); jframe.getContentPane().add(display.getComponent()); // Add the LabeledColorWidget to the frame

// Set window size and make it visible jframe.setSize(300, 300); jframe.setVisible(true); } //================ public static void plot3D_RGB(double zz[][],double x1min,double x1max,double x2min,double x2max,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { int NROWS=zz[0].length; int NCOLS=zz.length; RealType x1, x2; RealType z, temperature, precipitation; // Two Tuples: one to pack longitude and latitude together, as the domain // and the other for the range (altitude, temperature) RealTupleType domain_tuple, range_tuple; // The function (domain_tuple -> range_tuple ) FunctionType func_domain_range; // Our Data values for the domain are represented by the Set Set domain_set; // The Data class FlatField FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref; // The 2D display, and its the maps DisplayImpl display; ScalarMap latMap, lonMap; ScalarMap altZMap, tempRGBMap; ScalarMap tempZMap, altRGBMap; ScalarMap tempIsoMap; // Create the quantities // Use RealType(String name);

x1 = RealType.getRealType(sx,null,null); x2 = RealType.getRealType(sy,null,null); domain_tuple = new RealTupleType(x1, x2); temperature = RealType.getRealType("temperature",null,null); z = RealType.getRealType("z",null,null); range_tuple = new RealTupleType( z, temperature ); // Create a FunctionType (domain_tuple -> range_tuple ) // Use FunctionType(MathType domain, MathType range) func_domain_range = new FunctionType( domain_tuple, range_tuple); // Create the domain Set // Use LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) domain_set = new Linear2DSet(domain_tuple, x1min, x1max, NROWS, x2min, x2max, NCOLS); // Get the Set samples to facilitate the calculations float[][] x_j = domain_set.getSamples( true ); // We create another array, with the same number of elements of // altitude and temperature, but organized as // float[2][ number_of_samples ] float[][] zi = new float[2][NCOLS*NROWS]; // ...and then we fill our 'flat' array with the generated values // by looping over NCOLS and NROWS double xx[]=new double[2]; for(int c = 0; c < NCOLS; c++) for(int r = 0; r < NROWS; r++) {xx[0]=(float)(x1min+(x1max-x1min)*c/(float)(NCOLS-1)); xx[1]=(float)(x2min+(x2max-x2min)*r/(float)(NROWS-1)); zi[0][ c * NROWS + r ] = (float)zz[c][r]; zi[1][ c * NROWS + r ] = (float)xx[1]; } // Create a FlatField // Use FlatField(FunctionType type, Set domain_set) vals_ff = new FlatField( func_domain_range, domain_set); // ...and put the values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( zi , false ); // Create Display and its maps // A 2D display display = new DisplayImplJ3D(sz); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true); // Create the ScalarMaps: latitude to YAxis, longitude to XAxis and // altitude to ZAxis and temperature to RGB // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( x1,Display.XAxis ); lonMap = new ScalarMap( x2,Display.YAxis ); // Add maps to display display.addMap( latMap ); display.addMap( lonMap ); // altitude to z-axis and temperature to color altZMap = new ScalarMap( z, Display.ZAxis ); tempRGBMap = new ScalarMap( temperature, Display.RGB ); tempIsoMap = new ScalarMap( temperature, Display.IsoContour ); display.addMap( tempRGBMap ); display.addMap( tempIsoMap ); // Add maps to display display.addMap( altZMap ); // Uncomment following lines to have different data depiction // temperature to z-axis and altitude to color //altRGBMap = new ScalarMap( zi, Display.RGB ); //tempZMap = new ScalarMap( temperature, Display.ZAxis ); //display.addMap( altRGBMap ); //display.addMap( tempZMap );

// Create a data reference and set the FlatField as our data data_ref = new DataReferenceImpl("data_ref"); data_ref.setData( vals_ff ); // Add reference to display display.addReference( data_ref ); // Create application window and add display to window JFrame jframe = new JFrame(sz); jframe.getContentPane().add(display.getComponent()); // Set window size and make it visible jframe.setSize(300, 300); jframe.setVisible(true); } public static void plot3D_RGB(double zz[],double x1min,double x1max,double x2min,double x2max,int NCOLS,int NROWS,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { RealType x1, x2; RealType z, temperature, precipitation; // Two Tuples: one to pack longitude and latitude together, as the domain // and the other for the range (altitude, temperature) RealTupleType domain_tuple, range_tuple; // The function (domain_tuple -> range_tuple ) FunctionType func_domain_range; // Our Data values for the domain are represented by the Set Set domain_set; // The Data class FlatField FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref; // The 2D display, and its the maps DisplayImpl display; ScalarMap latMap, lonMap; ScalarMap altZMap, tempRGBMap; ScalarMap tempZMap, altRGBMap; ScalarMap tempIsoMap; // Create the quantities // Use RealType(String name); x1 = RealType.getRealType(sx,null,null); x2 = RealType.getRealType(sy,null,null); domain_tuple = new RealTupleType(x1, x2); temperature = RealType.getRealType("temperature",null,null); z = RealType.getRealType("z",null,null); range_tuple = new RealTupleType( z, temperature ); // Create a FunctionType (domain_tuple -> range_tuple ) // Use FunctionType(MathType domain, MathType range) func_domain_range = new FunctionType( domain_tuple, range_tuple); // Create the domain Set // Use LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) domain_set = new Linear2DSet(domain_tuple, x1min, x1max, NROWS, x2min, x2max, NCOLS); // Get the Set samples to facilitate the calculations float[][] x_j = domain_set.getSamples( true ); // We create another array, with the same number of elements of // altitude and temperature, but organized as // float[2][ number_of_samples ] float[][] zi = new float[2][NCOLS*NROWS]; // ...and then we fill our 'flat' array with the generated values // by looping over NCOLS and NROWS double xx[]=new double[2]; for(int c = 0; c < NCOLS; c++) for(int r = 0; r < NROWS; r++) {xx[0]=(float)(x1min+(x1max-x1min)*c/(float)(NCOLS-1)); xx[1]=(float)(x2min+(x2max-x2min)*r/(float)(NROWS-1)); zi[0][ c * NROWS + r ] = (float)zz[ c * NROWS + r ];

zi[1][ c * NROWS + r ] = (float)xx[1]; } // Create a FlatField // Use FlatField(FunctionType type, Set domain_set) vals_ff = new FlatField( func_domain_range, domain_set); // ...and put the values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( zi , false ); // Create Display and its maps // A 2D display display = new DisplayImplJ3D(sz); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true); // Create the ScalarMaps: latitude to YAxis, longitude to XAxis and // altitude to ZAxis and temperature to RGB // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( x1,Display.YAxis ); lonMap = new ScalarMap( x2,Display.XAxis ); // Add maps to display display.addMap( latMap ); display.addMap( lonMap ); // altitude to z-axis and temperature to color altZMap = new ScalarMap( z, Display.ZAxis ); tempRGBMap = new ScalarMap( temperature, Display.RGB ); tempIsoMap = new ScalarMap( temperature, Display.IsoContour ); display.addMap( tempRGBMap ); display.addMap( tempIsoMap ); // Add maps to display display.addMap( altZMap ); // Uncomment following lines to have different data depiction // temperature to z-axis and altitude to color altRGBMap = new ScalarMap( z, Display.RGB ); tempZMap = new ScalarMap( temperature, Display.ZAxis ); display.addMap( altRGBMap ); display.addMap( tempZMap ); // Create a data reference and set the FlatField as our data data_ref = new DataReferenceImpl("data_ref"); data_ref.setData( vals_ff ); // Add reference to display display.addReference( data_ref ); // Create application window and add display to window JFrame jframe = new JFrame(sz); jframe.getContentPane().add(display.getComponent()); // Set window size and make it visible jframe.setSize(300, 300); jframe.setVisible(true); } public static void plot3D_RGB(f_xj f,double x1min,double x1max,double x2min,double x2max,int NCOLS,int NROWS,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { RealType x1, x2; RealType z, temperature, precipitation; // Two Tuples: one to pack longitude and latitude together, as the domain // and the other for the range (altitude, temperature) RealTupleType domain_tuple, range_tuple; // The function (domain_tuple -> range_tuple ) FunctionType func_domain_range; // Our Data values for the domain are represented by the Set Set domain_set; // The Data class FlatField FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref;

// The 2D display, and its the maps DisplayImpl display; ScalarMap latMap, lonMap; ScalarMap altZMap, tempRGBMap; ScalarMap tempZMap, altRGBMap; ScalarMap tempIsoMap; // Create the quantities // Use RealType(String name); x1 = RealType.getRealType(sx,null,null); x2 = RealType.getRealType(sy,null,null); domain_tuple = new RealTupleType(x1, x2); temperature = RealType.getRealType("temperature",null,null); z = RealType.getRealType("z",null,null); range_tuple = new RealTupleType( z, temperature ); // Create a FunctionType (domain_tuple -> range_tuple ) // Use FunctionType(MathType domain, MathType range) func_domain_range = new FunctionType( domain_tuple, range_tuple); // Create the domain Set // Use LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) domain_set = new Linear2DSet(domain_tuple, x1min, x1max, NROWS, x2min, x2max, NCOLS); // Get the Set samples to facilitate the calculations float[][] x_j = domain_set.getSamples( true ); // We create another array, with the same number of elements of // altitude and temperature, but organized as // float[2][ number_of_samples ] float[][] zi = new float[2][NCOLS * NROWS]; // ...and then we fill our 'flat' array with the generated values // by looping over NCOLS and NROWS double xx[]=new double[2]; for(int c = 0; c < NCOLS; c++) for(int r = 0; r < NROWS; r++) {xx[0]=x_j[0][ c * NROWS + r ]; xx[1]=x_j[1][ c * NROWS + r ]; zi[0][ c * NROWS + r ] = (float)f.func(xx); zi[1][ c * NROWS + r ] = x_j[1][ c * NROWS + r ]; } // Create a FlatField // Use FlatField(FunctionType type, Set domain_set) vals_ff = new FlatField( func_domain_range, domain_set); // ...and put the values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( zi , false ); // Create Display and its maps // A 2D display display = new DisplayImplJ3D(sz); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true); // Create the ScalarMaps: latitude to YAxis, longitude to XAxis and // altitude to ZAxis and temperature to RGB // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( x1,Display.YAxis ); lonMap = new ScalarMap( x2,Display.XAxis ); // Add maps to display display.addMap( latMap ); display.addMap( lonMap ); // altitude to z-axis and temperature to color altZMap = new ScalarMap( z, Display.ZAxis ); tempRGBMap = new ScalarMap( temperature, Display.RGB ); //tempIsoMap = new ScalarMap( temperature, Display.IsoContour ); display.addMap( tempRGBMap ); //display.addMap( tempIsoMap ); // Add maps to display display.addMap( altZMap );

// Uncomment following lines to have different data depiction // temperature to z-axis and altitude to color //altRGBMap = new ScalarMap( zi, Display.RGB ); //tempZMap = new ScalarMap( temperature, Display.ZAxis ); //display.addMap( altRGBMap ); //display.addMap( tempZMap ); // Create a data reference and set the FlatField as our data data_ref = new DataReferenceImpl("data_ref"); data_ref.setData( vals_ff ); // Add reference to display display.addReference( data_ref ); // Create application window and add display to window JFrame jframe = new JFrame(sz); jframe.getContentPane().add(display.getComponent()); // Set window size and make it visible jframe.setSize(400, 400); jframe.setVisible(true); } public static void contour_Plot1(f_xj f, double x1min,double x1max,double x2min,double x2max,int NROWS,int NCOLS,int NCONTOUR,String s1,String s2,String s3) throws RemoteException, VisADException { // Declare variables // The domain quantities longitude and latitude // and the dependent quantity temperature RealType longitude, latitude; RealType temperature;

// Tuple to pack longitude and latitude together, as the domain RealTupleType domain_tuple;

// The function (domain_tuple -> temperature ) // Remeber, range is only "temperature" FunctionType func_domain_temp;

// Our Data values for the domain are represented by the Set Set domain_set;

// The Data class FlatField FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref; // The 2D display, and its the maps DisplayImpl display; ScalarMap latMap, lonMap; ScalarMap tempIsoMap, tempRGBMap; // These objects are for drawing isocontours RealType isoTemperature; FunctionType func_domain_isoTemp;

FlatField iso_vals_ff; DataReferenceImpl iso_data_ref; // Create the quantities // Use RealType(String name); latitude = RealType.getRealType(s1); longitude = RealType.getRealType(s2); domain_tuple = new RealTupleType(latitude, longitude); temperature = RealType.getRealType("temperature", SI.kelvin, null); isoTemperature = RealType.getRealType("isoTemperature", SI.kelvin, null); // Create a FunctionType (domain_tuple -> temperature ) // Use FunctionType(MathType domain, MathType range) func_domain_temp = new FunctionType( domain_tuple, temperature); // ... the same for isoTemperature func_domain_isoTemp = new FunctionType( domain_tuple, isoTemperature); // Create the domain Set // Use LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) domain_set = new Linear2DSet(domain_tuple, x1min, x1max, NROWS, x2min, x2max, NCOLS); // Get the Set samples to facilitate the calculations float[][] set_samples = domain_set.getSamples( true ); // The actual temperature values are stored in this array // float[1][ number_of_samples ] double x[]=new double[2]; float[][] z_j = new float[1][NCOLS * NROWS]; // We fill our 'flat' array with the generated values // by looping over NCOLS and NROWS for(int c = 0; c < NCOLS; c++) {x[0]=x1min+(x1max-x1min)/(double)NCOLS*(double)c; for(int r = 0; r < NROWS; r++) {x[1]=x2min+(x2max-x2min)/(double)NROWS*(double)r; z_j[0][ c * NROWS + r ] = (float)f.func(x); } } // Create the FlatFields // Use FlatField(FunctionType type, Set domain_set) // For the colored image vals_ff = new FlatField( func_domain_temp, domain_set); // ...and put the values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( z_j , false );

// ...and for the isocontours iso_vals_ff = new FlatField( func_domain_isoTemp, domain_set);

// Get the values from the temperature FlatField // create flat_isoVals array for clarity's sake // "false" argument means "don't copy" float[][] flat_isoVals = vals_ff.getFloats(false);

// ...and put the values above into it // Note the argument false, meaning that the array won't be copied again

iso_vals_ff.setSamples( flat_isoVals , false );

// Create Display and its maps // A 2D display display = new DisplayImplJ2D(s3); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true);

// Create the ScalarMaps: latitude to YAxis, longitude to XAxis and // temperature to RGB and // isoTemperature to IsoContour // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( latitude, Display.XAxis ); lonMap = new ScalarMap( longitude, Display.YAxis ); tempIsoMap = new ScalarMap( isoTemperature, Display.IsoContour ); tempRGBMap = new ScalarMap( temperature, Display.RGB );

// Add maps to display display.addMap( latMap ); display.addMap( lonMap ); display.addMap( tempIsoMap ); display.addMap( tempRGBMap ); // The ContourControl // Note that we get the control from the IsoContour map ContourControl isoControl = (ContourControl) tempIsoMap.getControl(); // Define some parameters for contour lines float interval = (float)((x1max-x1min)/NCONTOUR); // interval between lines float lowValue = (float)x1min; // lowest value float highValue = (float)x1max; float base = (float)x1min; // highest value

// starting at this base value

// ...and set the lines with the method isoControl.setContourInterval(interval, lowValue, highValue, base); isoControl.enableLabels(true); // Create data references and set the FlatField as our data data_ref = new DataReferenceImpl("data_ref"); iso_data_ref = new DataReferenceImpl("iso_data_ref"); data_ref.setData( vals_ff ); iso_data_ref.setData( iso_vals_ff );

// Add reference to display

display.addReference( iso_data_ref ); display.addReference( data_ref ); // Create application window and add display to window JFrame jframe = new JFrame(s3); jframe.getContentPane().add(display.getComponent());

// Set window size and make it visible jframe.setSize(400, 400); jframe.setVisible(true); } public static void contour_Plot(f_xj f, double x1min,double x1max,double x2min,double x2max,int NROWS,int NCOLS,int NCONTOUR,String s1,String s2,String s3) throws RemoteException, VisADException { RealType longitude, latitude; RealType temperature; // Tuple to pack longitude and latitude together, as the domain RealTupleType domain_tuple; // The function (domain_tuple -> temperature ) // Remeber, range is only "temperature" FunctionType func_domain_range; // Our Data values for the domain are represented by the Set Set domain_set; // The Data class FlatField FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref; // The 2D display, and its the maps DisplayImpl display; ScalarMap latMap, lonMap; ScalarMap tempIsoMap, tempRGBMap; // Create the quantities // Use RealType(String name); latitude = RealType.getRealType(s1); longitude = RealType.getRealType(s2);

domain_tuple = new RealTupleType(latitude, longitude); temperature = RealType.getRealType("temperature"); // Create a FunctionType (domain_tuple -> range_tuple ) // Use FunctionType(MathType domain, MathType range) func_domain_range = new FunctionType( domain_tuple, temperature); // Create the domain Set // Use LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) //int NCOLS = 50; //int NROWS = NCOLS; domain_set = new Linear2DSet(domain_tuple, x1min, x1max, NROWS, x2min, x2max, NCOLS); // Get the Set samples to facilitate the calculations float[][] set_samples = domain_set.getSamples( true );

// The actual temperature values are stored in this array // float[1][ number_of_samples ] double x[]=new double[2]; float[][] z_j = new float[1][NCOLS * NROWS]; // We fill our 'flat' array with the generated values // by looping over NCOLS and NROWS for(int c = 0; c < NCOLS; c++) {x[0]=x1min+(x1max-x1min)/(double)NCOLS*(double)c; for(int r = 0; r < NROWS; r++) {x[1]=x2min+(x2max-x2min)/(double)NROWS*(double)r; z_j[0][ c * NROWS + r ] = (float)f.func(x); } } // Create a FlatField // Use FlatField(FunctionType type, Set domain_set) vals_ff = new FlatField( func_domain_range, domain_set); // ...and put the values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( z_j , false ); // Create Display and its maps // A 2D display display = new DisplayImplJ2D(s3); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true);

// Create the ScalarMaps: latitude to YAxis, longitude to XAxis and // temperature to IsoContour and eventualy to RGB // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( latitude, Display.XAxis ); lonMap = new ScalarMap( longitude, Display.YAxis ); // This is new! tempIsoMap = new ScalarMap( temperature, Display.IsoContour ); // this ScalarMap will color the isolines // don't foget to add it to the display tempRGBMap = new ScalarMap( temperature, Display.RGB );

// Add maps to display display.addMap( latMap ); display.addMap( lonMap ); display.addMap( tempIsoMap ); display.addMap( tempRGBMap );

// Create a data reference and set the FlatField as our data data_ref = new DataReferenceImpl("data_ref"); data_ref.setData( vals_ff ); // Add reference to display display.addReference( data_ref ); // Create application window and add display to window JFrame jframe = new JFrame(s3); jframe.getContentPane().add(display.getComponent()); jframe.setSize(300, 300); jframe.setVisible(true); } public static void contour_Plot(double a[][], double x1min,double x1max,double x2min,double x2max,int NROWS,int NCOLS,int NCONTOUR,String s1,String s2,String s3) throws RemoteException, VisADException { RealType longitude, latitude; RealType temperature; // Tuple to pack longitude and latitude together, as the domain RealTupleType domain_tuple; // The function (domain_tuple -> temperature ) // Remeber, range is only "temperature" FunctionType func_domain_range; // Our Data values for the domain are represented by the Set Set domain_set; // The Data class FlatField FlatField vals_ff; // The DataReference from data to display DataReferenceImpl data_ref; // The 2D display, and its the maps DisplayImpl display; ScalarMap latMap, lonMap; ScalarMap tempIsoMap, tempRGBMap; // Create the quantities // Use RealType(String name); latitude = RealType.getRealType(s1); longitude = RealType.getRealType(s2);

domain_tuple = new RealTupleType(latitude, longitude); temperature = RealType.getRealType("temperature"); // Create a FunctionType (domain_tuple -> range_tuple ) // Use FunctionType(MathType domain, MathType range) func_domain_range = new FunctionType( domain_tuple, temperature); // Create the domain Set // Use LinearDSet(MathType type, double first1, double last1, int lengthX, // double first2, double last2, int lengthY) //int NCOLS = 50; //int NROWS = NCOLS; domain_set = new Linear2DSet(domain_tuple, x1min, x1max, NROWS, x2min, x2max, NCOLS); // Get the Set samples to facilitate the calculations float[][] set_samples = domain_set.getSamples( true );

// The actual temperature values are stored in this array

// float[1][ number_of_samples ] double x[]=new double[2]; float[][] z_j = new float[1][NCOLS * NROWS]; // We fill our 'flat' array with the generated values // by looping over NCOLS and NROWS for(int c = 0; c < NCOLS; c++) {x[0]=x1min+(x1max-x1min)/(double)NCOLS*(double)c; for(int r = 0; r < NROWS; r++) {x[1]=x2min+(x2max-x2min)/(double)NROWS*(double)r; z_j[0][ c * NROWS + r ] = (float)a[r][c];//(float)f.func(x); } } // Create a FlatField // Use FlatField(FunctionType type, Set domain_set) vals_ff = new FlatField( func_domain_range, domain_set); // ...and put the values above into it // Note the argument false, meaning that the array won't be copied vals_ff.setSamples( z_j , false ); // Create Display and its maps // A 2D display display = new DisplayImplJ2D(s3); // Get display's graphics mode control and draw scales GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl(); dispGMC.setScaleEnable(true);

// Create the ScalarMaps: latitude to YAxis, longitude to XAxis and // temperature to IsoContour and eventualy to RGB // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) latMap = new ScalarMap( latitude, Display.XAxis ); lonMap = new ScalarMap( longitude, Display.YAxis ); // This is new! tempIsoMap = new ScalarMap( temperature, Display.IsoContour ); // this ScalarMap will color the isolines // don't foget to add it to the display tempRGBMap = new ScalarMap( temperature, Display.RGB );

// Add maps to display display.addMap( latMap ); display.addMap( lonMap ); display.addMap( tempIsoMap ); display.addMap( tempRGBMap );

// Create a data reference and set the FlatField as our data data_ref = new DataReferenceImpl("data_ref");

data_ref.setData( vals_ff ); // Add reference to display display.addReference( data_ref ); // Create application window and add display to window JFrame jframe = new JFrame(s3); jframe.getContentPane().add(display.getComponent()); jframe.setSize(300, 300); jframe.setVisible(true); } public static void contour(f_xj f, double x1min,double x1max,double x2min,double x2max,int NROWS,int NCOLS,int NCONTOUR,String s1,String s2,String s3) throws RemoteException, VisADException {contour_Plot(f,x1min,x1max,x2min,x2max,NROWS,NCOLS,NCONTOUR,s1,s2,s3);} public static void contour(f_xj f, double x1min,double x1max,double x2min,double x2max,String s1,String s2,String s3) throws RemoteException, VisADException {contour_Plot(f,x1min,x1max,x2min,x2max,200,200,1,s1,s2,s3);} public static void contour(f_xj f, double x1min,double x1max,double x2min,double x2max) throws RemoteException, VisADException {contour_Plot(f,x1min,x1max,x2min,x2max,200,200,1,"x","y","z");} public static void contour(double a[][], double x1min,double x1max,double x2min,double x2max,int NROWS,int NCOLS,int NCONTOUR,String s1,String s2,String s3) throws RemoteException, VisADException {contour_Plot(a,x1min,x1max,x2min,x2max,NROWS,NCOLS,NCONTOUR,s1,s2,s3);} public static void contour(double a[][], double x1min,double x1max,double x2min,double x2max,String s1,String s2,String s3) throws RemoteException, VisADException {contour_Plot(a,x1min,x1max,x2min,x2max,200,200,1,s1,s2,s3);} public static void contour(double a[][], double x1min,double x1max,double x2min,double x2max) throws RemoteException, VisADException {contour_Plot(a,x1min,x1max,x2min,x2max,200,200,1,"x","y","z");} public static void func(f_xj f,double x1min,double x1max,double x2min,double x2max,int NCOLS,int NROWS,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { plot3D_CD1(grid2D(f,x1min,x1max,x2min,x2max,NCOLS, NROWS),x1min,x1max,x2min,x2max,NCONTOUR,sx,sy,sz);} public static void func(f_xj f,double x1min,double x1max,double x2min,double x2max,String sx,String sy,String sz) throws RemoteException, VisADException { plot3D_CD1(grid2D(f,x1min,x1max,x2min,x2max,200, 200),x1min,x1max,x2min,x2max,1,sx,sy,sz);} public static void func(f_xj f,double x1min,double x1max,double x2min,double x2max) throws RemoteException, VisADException { plot3D_CD1(grid2D(f,x1min,x1max,x2min,x2max,200, 200),x1min,x1max,x2min,x2max,1,"x","y","z1");} public static void data(double zz[][],double x1min,double x1max,double x2min,double x2max,int NCONTOUR,String sx,String sy,String sz) throws RemoteException, VisADException { plot3D_CD1(zz,x1min,x1max,x2min,x2max,NCONTOUR,sy,sx,sz);} public static void data(double zz[][],double x1min,double x1max,double x2min,double x2max,String sx,String sy,String sz) throws RemoteException, VisADException { plot3D_CD1(zz,x1min,x1max,x2min,x2max,1,sy,sx,sz);} public static void data(double zz[][],double x1min,double x1max,double x2min,double x2max) throws RemoteException, VisADException { plot3D_CD1(zz,x1min,x1max,x2min,x2max,1,"y","x","z");} public static void main(String[] args) throws RemoteException, VisADException { f1 f=new f1(); double a[][]=grid2D(f,-1,1,-1,1,20,20); contour(f,-1.0,1.0,-1.0,1.0,100,100,100,"x","y","f(x,y)"); } } //end of Plot3D

Utilisation of the program in java environment can be shown with few examples Program 1.4-2 Plot3DT1 3 dimensional graphic example program
import visad.*; import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D; import java.rmi.RemoteException;

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Plot3DT1 { public static void main(String[] args) throws RemoteException, VisADException { double a[][]=new double[][]{ {3000.0, 3000.0, 6500.0, 4000.0, 3500.0, 4000.0, 5500.0, 4000.0, 4000.0, 4000.0, 5000.0, 1000 }, {1000.0, 1500.0, 4000.0, 3500.0, 6300.0, 4500.0, 4000.0, 3800.0, 3800.0, 3800.0, 5000.0, 6400 }, { 0.0, 0.0, 0.0, 1500.0, 3000.0, 6500.0, 4500.0, 5000.0, 4000.0, 3800.0, 3800.0, 6200 }, {-3000.0, -2000.0, -1000.0, 0.0, 1500.0, 1000.0, 4000.0, 5800.0, 4000.0, 4000.0, 3900.0, 3900 }, {-3000.0, -4000.0, -2000.0, -1000.0, 0.0, 1000.0, 1500.0, 4000.0, 5700.0, 4500.0, 4000.0, 4000.0 }, {-6500.0, -6000.0, -4000.0, -3000.0, 0.0, 100.0, 1500.0, 4500.0, 6000.0, 4000.0, 4000.0, 4000.0 }}; Plot3D.plot3D_CD1(a,-1,2.0,-1.0,1.0,40,"x","y","z=f(x,y)"); } }

Output 1.3-1 Plot3D 3 scientific plotting example

As it seen from the program, it is quite simple to plot the data. The next example is plotting a function Program 1.4 -3 Plot3DT2 3 scientific plotting example
import visad.*; import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D; import java.rmi.RemoteException; import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_xj {

public double func(double x[]) { //zm istenen fonksiyon double ff; ff=(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+0.01*(x[0]-1.0)*(x[0]-1.0); return -ff; } } public class Plot3DT2 { public static void main(String[] args) throws RemoteException, VisADException { f1 f=new f1(); Plot3D.plot3D_CD1(f,-1,1,-1,1.0,100,100,40,"x","y","z=f(x,y)"); } }

The next example draw a contour graphic Program 1.4-4 Plot3D 3 dimensional graphic program CONTOUR graphic example
import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+0.01*(x[0]-1.0)*(x[0]-1.0); return -ff; }} public class Plot3DMT3 { public static void main(String[] args) { f1 fa=new f1(); Plot3D_JM pp=new Plot3D_JM(fa,-10,10,0.0,20); pp.plot(); }}

Contour graphics are 2 dimensional mapping type of equal height graphics (isohips) of 3 dimensional shapes Output 1.4-4 Plot3D 3 dimensional graphic program CONTOUR graphic example

Program 1.4-5 Plot3D 3 dimensional plot by using plot_RGB graph method


import visad.*; import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D; import java.rmi.RemoteException; import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+0.01*(x[0]-1.0)*(x[0]-1.0); return -ff; } } public class Plot3DT5 { public static void main(String[] args) throws RemoteException, VisADException { f1 f=new f1(); Plot3D.plot3D_RGB(f,-1.0,1.0,-1.0,1.0,100,100,80,"x","y","z=f(x,y)"); } }

output 1.3-5 Plot3D 3 dimensional plot by using plot_RGB graph method

1.4.2 JAVA 3 DIMENSIONAL GRAPHIC BY USING JMATH CLASS : PLOT3D_JM When I search free internet graphic packages in Java, I found a packg with very simple usage : JMath. So in addition to VisAD, I also preperade an interphase to use JMath Package, as a standart graphic package in Numerical analysis applications. This package does not have the rofessional quality of VisAD, but this might be a plus for some of the users. You can obtain a copy of JMath package from http://jmathtools.sourceforge.net/doku.php. Three files you will obtain from this adres jmathio.jar, jmathplot.jar, ve jmatharray.jar should be copied into JAVAHOME/jre/lib/ext directory. Program 1.4-6 Plot3D_JM 3 dimensional scientific graphic packge
//====================================================== // Numerical Analysis package in java // Plot3D_JM class to plot 3D data // Dr. Turhan Coban // EGE University, Engineering School, Mechanical Eng. Div. // turhan.coban@ege.edu.tr // ===================================================== // utilizes JMath 3D Plot // http://jmathtools.sourceforge.net/doku.php // to use this program please add to : javahome/jre/lib/ext // jmathio.jar jmathplot.jar ve jmatharray.jar jar files import javax.swing.*; import org.math.plot.*; import static java.lang.Math.*; import static org.math.array.DoubleArray.*; import java.awt.*;

import java.awt.event.*; class f2 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=7.9+0.13*x[0]+0.21*x[1]-0.05*x[0]*x[0]-0.016*x[1]*x[1]-0.007*x[0]*x[1]; //ff=sin(x[0])*cos(x[1]); return ff; } } public class Plot3D_JM{ Plot3DPanel plot; public Plot3D_JM(f_xj f, double x1min,double x1max,double x2min,double x2max,int NX,int NY,String sz,Color c) { plot = new Plot3DPanel("SOUTH"); add(f,x1min,x1max,x2min,x2max,NX,NY,sz,c); } public Plot3D_JM(f_xj f, double x1min,double x1max,double x2min,double x2max,int NX,int NY) { plot = new Plot3DPanel("SOUTH"); add(f,x1min,x1max,x2min,x2max,NX,NY,"z=f(x,y)",Color.blue); } public Plot3D_JM(f_xj f, double x1min,double x1max,double x2min,double x2max) { plot = new Plot3DPanel("SOUTH"); add(f,x1min,x1max,x2min,x2max,20,20,"z=f(x,y)",Color.blue); } public Plot3D_JM(f_xj f, double x1min,double x1max,double x2min,double x2max,String sz) { plot = new Plot3DPanel("SOUTH"); add(f,x1min,x1max,x2min,x2max,20,20,sz,Color.blue); } public void add(f_xj f, double x1min,double x1max,double x2min,double x2max,int NX,int NY,String sz,Color c) { double dx1=(x1max-x1min)/(double)NX; double dx2=(x2max-x2min)/(double)NY; ; double[] xx0 = increment(x1min, dx1, x1max); double[] xx1 = increment(x2min, dx2, x2max); double x[]=new double[2]; double[][] z = new double[NY][NX]; for (int i = 0; i < NX; i++) for (int j = 0; j < NY; j++) {x[0]=xx0[i];x[1]=xx1[j];z[j][i] = f.func(x);} // add grid plot to the PlotPanel plot.addGridPlot(sz,c, xx0, xx1, z); } public void add(f_xj f, double x1min,double x1max,double x2min,double x2max,String sz,Color c) {add(f,x1min,x1max,x2min,x2max,20,20,sz,c);} public void add(f_xj f, double x1min,double x1max,double x2min,double x2max,String sz) {add(f,x1min,x1max,x2min,x2max,20,20,sz,Color.blue);} public void add(f_xj f, double x1min,double x1max,double x2min,double x2max) {add(f,x1min,x1max,x2min,x2max,20,20,"z=f(x,y)",Color.blue);} public void plot(String sz) {JFrame frame = new JFrame(sz); frame.setSize(600,600); frame.setContentPane(plot); frame.setVisible(true); } public void plot() {plot("z=f(x,y)");}

public static void func(f_xj f, double x1min,double x1max,double x2min,double x2max,int NX,int NY,String sz) { double dx1=(x1max-x1min)/(double)NX; double dx2=(x2max-x2min)/(double)NY; ; double[] xx0 = increment(x1min, dx1, x1max); double[] xx1 = increment(x2min, dx2, x2max); double x[]=new double[2]; double[][] z = new double[NY][NX]; for (int i = 0; i < NX; i++) for (int j = 0; j < NY; j++) {x[0]=xx0[i];x[1]=xx1[j];z[j][i] = f.func(x);} Plot3DPanel plot = new Plot3DPanel("SOUTH"); // add grid plot to the PlotPanel plot.addGridPlot("z=f(x,y)",Color.black, xx0, xx1, z); JFrame frame = new JFrame(sz); frame.setSize(600,600); frame.setContentPane(plot); frame.setVisible(true); } public static void func(f_xj f, double x1min,double x1max,double x2min,double x2max) {func(f, x1min,x1max,x2min,x2max,40,40,"z=f(x,y)");} public static void func(f_xj f, double x1min,double x1max,double x2min,double x2max,String sz) {func(f, x1min,x1max,x2min,x2max,40,40,sz);} public static void main(String[] args) { f2 f=new f2(); func(f,-10.0,10.0,0.0,20.0,"fx"); } } //end of Plot3D

Output 1.4-1 Plot3D_JM 3 dimensional graphic package

This program can be use as stand alone program, by chaging the input function or can also be called from your own program and plot te 3D output. The JMath package has some interesting facilities that you might enjoy using. For example you can change the maximum or minimum (scale of the plot) or you can plot the data in logarithmic scale.

The interphase program is very similar to VisAD interphase program to make a simple usage. An example code calling the program. Program 1.4-7 Plot3D_JM 3 dimensional plot
import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+0.01*(x[0]-1.0)*(x[0]-1.0); return -ff; }} public class Plot3DMT3 { public static void main(String[] args) { f1 fa=new f1(); Plot3D_JM pp=new Plot3D_JM(fa,-10,10,0.0,20); pp.plot(); }}

1.4.3 JAVA 3 DIMENSIONAL CONTOUR GRAPHIC BY USING JOSEPH A. HUWALDT PLOT PACKAGE The package developed by Joseph A. Huwaldth has capability of plotting 2D and contour graphics. The contour graphic program in the package ContourPlot.java is modified by the author to fit into the abstract function structure to plot function contour plots. Program is given nice contour plots, so it can be preferred to the Visad contour plots. The modified code is as follows:
Program 1.4-8 ContourPlot.java
/* * ContourPlot -- A simple 2D contour plot of gridded 3D data. * * Copyright (C) 2000-2002 by Joseph A. Huwaldt <jhuwaldt@knology.net>. * All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * additions are made to this package by M. Turhan Coban * to run plot windows with functions directly * turhan_coban@yahoo.com **/ import java.awt.*; import javax.swing.*; import java.util.*; import java.text.NumberFormat; import java.text.*; /** * <p> An object that represents a simple 2D contour plot * of 3D data. * </p> * <p> Grid styles, axis formats, and axis labels can * be changed by accessing this object's axes and * making the changes there. Then call repaint() * on the component containing this plot. * </p> * <p> This object's run list contains the contour lines * being plotted. Each run represents a contour level. * </p> * * <p> Modified by: Joseph A. Huwaldt </p> * * @author Joseph A. Huwaldt Date: November 12, 2000 * @version November 20, 2000

**/ public class ContourPlot extends SimplePlotXY { double xArr[][]; double yArr[][]; double zArr[][]; // Debug flag. private static final boolean DEBUG = false; // The contour paths displayed in this plot. private ContourPath[] paths = null; NumberFormat xFormat= NumberFormat.getInstance(Locale.US); NumberFormat yFormat= NumberFormat.getInstance(Locale.US); //------------------------------------------------------------------------/** * Only subclasses can access the default constructor. **/ protected ContourPlot() { } /** * Creates an contour plot of the specified gridded, 3D, data. * * @param xArr A 2D array of X coordinate values. * @param yArr A 2D array of Y coordinate values. * @param zArr A 2D array of Z coordinate values. * @param nc The number of contour levels to plot. * @param intType Indicates if the intervals should be log (true), * or linear (false). * @param title The title to be displayed across the top * of the plot. * @param xLabel The label to be displayed along the X-axis. * @param yLabel The label to be displayed along the Y-axis. * @param xFormat The number format to be used for the X axis * tick mark labels. * @param yFormat The number format to be used for the Y axis * tick mark labels. **/ public ContourPlot(f_xj f,double x0,double xn,int Nx,double y0,double yn,int Ny,int nc, boolean intType, String title, String xLabel, String yLabel) {xArr=new double[Ny][Nx]; yArr=new double[Ny][Nx]; zArr=new double[Ny][Nx]; double dx=(xn-x0)/(Nx-1); double dy=(yn-y0)/(Ny-1); double a[]=new double[2]; for (int i=0; i < Ny; ++i) { for (int j=0; j < Nx; ++j) { xArr[i][j] = x0+j*dx; yArr[i][j] = y0+i*dy; a[0]=xArr[i][j]; a[1]=yArr[i][j]; zArr[i][j] = f.func(a); } } this.setTitle(title); createPlot(xArr, yArr, zArr, xLabel, yLabel, nc, intType); colorizeContours(Color.blue, Color.red); //PlotSymbol symbol = new CircleSymbol(); //symbol.setBorderColor(Color.gray); //symbol.setSize(4); PlotRun run = new PlotRun(); for (int i=0; i < Ny; ++i) { for (int j=0; j < Nx; ++j) { run.add( new PlotDatum(xArr[i][j], yArr[i][j], false, null) ); } } getRuns().add(run); } public ContourPlot(f_xy f,double x0,double xn,int Nx,double y0,double yn,int Ny,int nc, boolean intType, String title, String xLabel, String yLabel) {xArr=new double[Ny][Nx]; yArr=new double[Ny][Nx]; zArr=new double[Ny][Nx]; double dx=(xn-x0)/(Nx-1);

double dy=(yn-y0)/(Ny-1); //NumberFormat xFormat= NumberFormat.getInstance(Locale.US); //NumberFormat yFormat= NumberFormat.getInstance(Locale.US); for (int i=0; i < Ny; ++i) { for (int j=0; j < Nx; ++j) { xArr[i][j] = x0+j*dx; yArr[i][j] = y0+i*dy; zArr[i][j] = f.func(xArr[i][j], y0+i*dy); } } this.setTitle(title); createPlot(xArr, yArr, zArr, xLabel, yLabel, nc, intType); colorizeContours(Color.blue, Color.red); //PlotSymbol symbol = new CircleSymbol(); //symbol.setBorderColor(Color.gray); //symbol.setSize(4); PlotRun run = new PlotRun(); for (int i=0; i < Ny; ++i) { for (int j=0; j < Nx; ++j) { run.add( new PlotDatum(xArr[i][j], yArr[i][j], false, null) ); } } getRuns().add(run); } public ContourPlot( double[][] xArri, double[][] yArri, double[][] zArri, int nc, boolean intType, String title, String xLabel, String yLabel) { xArr=xArri; yArr=yArri; zArr=zArri; int Nx=zArr[0].length; int Ny=zArr.length; this.setTitle(title); createPlot(xArr, yArr, zArr, xLabel, yLabel, nc, intType); colorizeContours(Color.blue, Color.red); PlotSymbol symbol = new CircleSymbol(); symbol.setBorderColor(Color.gray); symbol.setSize(4); PlotRun run = new PlotRun(); for (int i=0; i < Ny; ++i) { for (int j=0; j < Nx; ++j) { run.add( new PlotDatum(xArr[i][j], yArr[i][j], false, symbol) ); } } getRuns().add(run); } public void plot() { PlotPanel panel = new PlotPanel(this); panel.setBackground( Color.white ); PlotWindow window = new PlotWindow("ContourPlot", panel); window.setSize(500, 500); window.setLocation(100,100); window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); window.setVisible(true); } private void createPlot( double[][] xArr, double[][] yArr, double[][] zArr, String xLabel, String yLabel, int nc, boolean logIntervals) { try { // Generate the contours. ContourGenerator cg = new ContourGenerator(xArr, yArr, zArr, nc, logIntervals); paths = cg.getContours(); int npaths = paths.length; if (DEBUG) { System.out.println("Number of contours = " + nc); System.out.println("Number of contour paths = " + npaths); } // Get this plots list of runs. PlotRunList runs = this.getRuns(); runs.clear();

// Create an empty run for each contour level. for (int i=0; i < nc; ++i) runs.add( new PlotRun() ); // Loop over all the contour paths, adding them to the appropriate // contour level. for (int j=0; j < npaths; ++j) { // Retrieve the contour path data. double[] xData = paths[j].getAllX(); double[] yData = paths[j].getAllY(); int levelIndex = paths[j].getLevelIndex(); if (DEBUG) { System.out.println(); System.out.println("LevelIdx = " + levelIndex); } // Retrieve the appropriate run. PlotRun run = (PlotRun)runs.get(levelIndex); // Add this path the the retrieved run, one data point at a time. int numPoints = xData.length; for (int i=0; i < numPoints; ++i) { run.add( new PlotDatum(xData[i], yData[i], i != 0) ); if (DEBUG) System.out.println("X = " + (float)xData[i] + ", Y = " + (float)yData[i]); } } } catch (InterruptedException e) { // Shouldn't be possible here. e.printStackTrace(); } // Create our plot axes and add them to this plot. PlotAxis axis = new PlotXAxis(xLabel, null, xFormat, PlotAxis.kMajorGrid); this.setHorizontalAxis(axis); axis = new PlotYAxis(yLabel, null, yFormat, PlotAxis.kMajorGrid); this.setVerticalAxis(axis); } /** * Colorize the contours by linearly interpolating between * the specified colors for this plot's range of contour levels. **/ public void colorizeContours(Color lowColor, Color highColor) { // Find the range of levels in the contours. double minLevel = Double.MAX_VALUE; double maxLevel = -minLevel; int npaths = paths.length; for (int i=0; i < npaths; ++i) { double level = paths[i].getAttributes().getLevel(); minLevel = Math.min(minLevel, level); maxLevel = Math.max(maxLevel, level); } // Now assign the colors. PlotRunList runs = getRuns(); for (int i=0; i < npaths; ++i) { // Extract contour path information. double level = paths[i].getAttributes().getLevel(); int levelIndex = paths[i].getLevelIndex(); // Retrieve the appropriate run. PlotRun run = (PlotRun)runs.get(levelIndex); // Colorize the run. run.setLineColor(interpColors(lowColor, highColor, minLevel, maxLevel, level)); } } /** * Interpolate the colors for the contour level. **/

private Color interpColors(Color lowColor, Color highColor, double minLevel, double maxLevel, double level) { level -= minLevel; double range = maxLevel - minLevel; double temp = range - level; Color color = new Color( (int)(( temp*lowColor.getRed() + level*highColor.getRed() )/range), (int)(( temp*lowColor.getGreen() + level*highColor.getGreen() )/range), (int)(( temp*lowColor.getBlue() + level*highColor.getBlue() )/range) ); return color; } /** * Make a copy of this ContourPlot object. * * @return Returns a clone of this object. **/ public Object clone() { ContourPlot newObject = null; // Make a shallow copy of this object. newObject = (ContourPlot) super.clone(); // Make a copy of this object's data structures. int length = this.paths.length; newObject.paths = new ContourPath[length]; for (int i=0; i < length; ++i) newObject.paths[i] = (ContourPath)this.paths[i].clone(); // Output the newly cloned object. return newObject; } }

Contour plot program can use f_xj abstract class defined above as class input point, in addition an abstract class f_xy is also defined as addition. This two form can be converted to each other easily, but it is defined for convinience nevertheless. Program 1.4-9 Abstract entry function f_xy
abstract class f_xy extends Mathd { // single function multi independent variable // a single value is returned indiced to equation_ref abstract double func(double x,double y); }

In order to see how this package will plot an example code is given below
Program 1.4-10 ContourPlot example Plot3DT2.java
import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_xy { public double func(double x,double y) {//required function a series solution of a two dimensional temperature profile of a plate double ff; int artieksi=-1; double L=1.0;; double W=1.0; double toplam=0; for(int n=1;n<200;n++) { artieksi*=-1; toplam+=(artieksi+1)/n*Math.sin(n*Math.PI*x/L)*Math.sinh(n*Math.PI*y/L)/Math.sinh(n*Math.PI*W/L);} toplam*=(2.0/Math.PI);

return toplam; }} public class Plot3DT2 { public static void main(String[] args) { f1 fa=new f1(); ContourPlot pp = new ContourPlot(fa,0.0,1.0,20,0.0,1.0,20,20, false,"f(x,y)", "x", "y); pp.plot();}}

If the function is given as f_xj abstract class extended form :


import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_xj { public double func(double w[]) {//required function a series solution of a two dimensional temperature profile of a plate double x=w[0]; double y=w[1]; double ff; int artieksi=-1; double L=1.0;; double W=1.0; double toplam=0; for(int n=1;n<200;n++) { artieksi*=-1; toplam+=(artieksi+1)/n*Math.sin(n*Math.PI*x/L)*Math.sinh(n*Math.PI*y/L)/Math.sinh(n*Math.PI*W/L);} toplam*=(2.0/Math.PI); return toplam; }} public class Plot3DT6 { public static void main(String[] args) { f1 fa=new f1(); ContourPlot pp = new ContourPlot(fa,0.0,1.0,20,0.0,1.0,20,20, false, "f(x,y)", "x", "y"); pp.plot();}}

The results will be same. 1.5 REAL TIME GRAPHIC ENVIRONMENT


The programs listed in here will utilize real time graphic environment. The first case of the real time applications will be time dependent function plots. The 2D plot package defined above will be used as

the base of the real time plotting. Plotting will bedone as two dimensional (x axis and time) the time dimension will be shown as seperate plots showing in time scale. The plotting time scale can be different than actual time step so that actin can be slow down or accelerate in time. Program 1.5-1 time dependent plotting class timeplot_n
abstract class fi_xt extends Mathd { // n function function two independent variable : x,t // n value is returned abstract double[] func(double x,double t); } abstract class f_xt extends Mathd { // single function two independent variable : x,t // a single value is returned abstract double func(double x,double t); } class timeplot_n extends Thread {int n,nx,nt; double x[][][]; double y[][][]; double dt; double dt_real; double xmin,xmax,ymin,ymax; public timeplot_n(double xi[][][],double yi[][][],double dti,double dt_reali) { nx=xi[0][0].length;nt=xi[0].length;n=xi.length;dt=dti; dt_real=dt_reali; xmin=9.99e50; xmax=-9.99e50; ymin=9.99e50; ymax=-9.99e50; x=new double[n][nt][nx]; y=new double[n][nt][nx]; for(int k=0;k<n;k++) { for(int i=0;i<nt;i++) {for(int j=0;j<nx;j++) {x[k][i][j]=xi[k][i][j]; y[k][i][j]=yi[k][i][j]; if(x[k][i][j]<xmin) xmin=x[k][i][j]; if(x[k][i][j]>xmax) xmax=x[k][i][j]; if(y[k][i][j]<ymin) ymin=y[k][i][j]; if(y[k][i][j]>ymax) ymax=y[k][i][j]; } } } } public timeplot_n(fi_xt f,double x0,double xn,double dx,double t0,double tn,double dti,double dt_reali) {dt=dti; dt_real=dt_reali; nx=(int)((xn-x0)/dx+1); nt=(int)((tn-t0)/dt_real+1); double a[]=f.func(0.0,0.0); n=a.length; xmin=x0; xmax=xn; ymin=9.99e50; ymax=-9.99e50; x=new double[n][nt][nx]; y=new double[n][nt][nx]; double xx=0; double t; for(int i=0;i<nt;i++) {for(int j=0;j<nx;j++) {xx=x0+dx*j; t=t0+dt_real*i; a=f.func(xx,t); for(int k=0;k<n;k++) {x[k][i][j]=xx;

y[k][i][j]=a[k]; if(y[k][i][j]<ymin) ymin=y[k][i][j]; if(y[k][i][j]>ymax) ymax=y[k][i][j]; } } } } public timeplot_n(f_xt f,double x0,double xn,double dx,double t0,double tn,double dti,double dt_reali) {dt=dti; dt_real=dt_reali; nx=(int)((xn-x0)/dx+1); nt=(int)((tn-t0)/dt_real+1); double a[]=new double[1]; n=1; xmin=x0; xmax=xn; ymin=9.99e50; ymax=-9.99e50; x=new double[n][nt][nx]; y=new double[n][nt][nx]; double xx=0; double t; for(int i=0;i<nt;i++) {for(int j=0;j<nx;j++) {xx=x0+dx*j; t=t0+dt_real*i; a[0]=f.func(xx,t); for(int k=0;k<n;k++) {x[k][i][j]=xx; y[k][i][j]=a[k]; if(y[k][i][j]<ymin) ymin=y[k][i][j]; if(y[k][i][j]>ymax) ymax=y[k][i][j]; } } } } public timeplot_n(double yi[][][],double x0,double xn,double dti,double dt_reali) {nx=yi[0].length;nt=yi.length; dt=dti; dt_real=dt_reali; xmin=x0; xmax=xn; ymin=9.99e50; ymax=-9.99e50; x=new double[n][nt][nx]; y=new double[n][nt][nx]; double dx=(xn-x0)/(nx-1); for(int k=0;k<n;k++) { for(int i=0;i<nt;i++) {for(int j=0;j<nx;j++) {x[k][i][j]=x0+dx*j; y[k][i][j]=yi[k][i][j]; if(y[k][i][j]<ymin) ymin=y[k][i][j]; if(y[k][i][j]>ymax) ymax=y[k][i][j]; } } } } public void run() { Plot pp=new Plot(x[0][0],y[0][0]); for(int i=0;i<nt;i++) try { pp.ppA(x[0][i],y[0][i],0,0,0,255); for(int k=1;k<n;k++) {pp.addData(x[k][i],y[k][i]);} pp.setPlabel("t = "+i*dt_real+" s"); pp.setMinMax(xmin,xmax,ymin,ymax); int t=(int)(1000*dt); pp.plot(); Thread.sleep(t); pp.dispose(); }

catch(InterruptedException e) {System.err.println(e.toString());} } public void plot() {start();} }

Program 1.5-2 time dependent plotting class timeplot_n example class timeplottest
class i_wall extends f_xt { public double func(double x,double t) {double Ti=20.0; double Ts=100.0; double alfa=17.7e-4; double T=Ts+(Ti-Ts)*Mathd.erf(x/(2*Math.pow((alfa*t),0.5))); return T; } } public class timeplottest { public static void main(String args[]) { i_wall ff=new i_wall(); timeplot_n p=new timeplot_n(ff,0.0,1.0,0.05,0.1,1000.0,0.5,10.0); p.plot(); } }

The next program utilises real time programming. But it will not plot functions in real time as above program, it will allow you to enter a function in real time, then it will plot the function you enter to the window. In order to achive that program writes the function, compile it by using java compiler than run it using our plot program. You can enter any function you desired to it. As an example function x*x-1 be entered to the plot window. All the code required will not be listed in here but they are existed in the class file folder. Program 1.5-3 real time plot class ce1
import java.io.*;

import javax.swing.*; import javax.tools.*; import java.util.*; import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import java.util.Locale; import java.text.*; import java.util.*; import javax.swing.table.*; import static java.lang.Math.*; class ce1 { String s; public ce1() {input g=new input("f2D"," "); s=g.vericiktisi(); try{ g.finalize();} catch(Throwable e1) {System.err.println("input error.");} } public ce1(String function) { functiongenerator g=new functiongenerator("f2D",function); s=g.createfunction(); try{ g.finalize();} catch(Throwable e1) {System.err.println("input error.");} } public static void main(String arg[]) { //String s1=JOptionPane.showInputDialog(" Fonksiyonu giriniz : "); ce1 x0=new ce1(); double a[]=Text.readDoubleV("Enter minimum and maximum values of the function xmin xmax"); ce3 x1=new ce3(a[0],a[1],400,x0.s); //ce8 x2=new ce8(a[0],a[1],x0.s); } }

Program 1.5-2 real time plot class ce3


import java.io.*; import javax.swing.*; import javax.tools.*; import java.util.*; import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import java.util.Locale; import java.text.*; import java.util.*; import javax.swing.table.*; import static java.lang.Math.*;

class ce3 extends JApplet { public ce3(double x1,double x2,int n,String s) { Plot pp=new Plot(new f2D(),x1,x2,n); pp.setPlabel("f(x)="+s); pp.plot(); } public ce3(double x1,double x2,int n) {Plot pp=new Plot(new f2D(),x1,x2,n); pp.plot(); } public ce3(double x1,double x2) {Plot pp=new Plot(new f2D(),x1,x2,50); pp.plot(); } public static void main(String arg[]) {double a=Double.parseDouble(JOptionPane.showInputDialog(" Fonksiyonun alt snrn giriniz x_minimum : ")); double b=Double.parseDouble(JOptionPane.showInputDialog(" Fonksiyonun st snrn giriniz x_maksimum : ")); ce3 xx=new ce3(a,b,400);} }

Program 1.5-3 real time plot class input


import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.event.*; import java.io.*; import java.util.*;

public class input extends Thread implements ActionListener { Container c; String s=" "; String s1=""; functiongenerator fg; JTextArea jt; JButton bb; boolean basildi; public input(String isim,String fonksiyon) { jt=new JTextArea(s,5,30); jt.setFont(new Font("SansSerif",Font.PLAIN,20)); fg=new functiongenerator(); bb=new JButton("push button after entering the function into to the window : x"); JScrollPane jsp=new JScrollPane(jt); JFrame cerceve=new JFrame("function input window example: Math.sin(x)-1/(x*x-1) "); cerceve.addWindowListener(new BasicWindowMonitor()); c=cerceve.getContentPane(); c.setLayout(new BorderLayout()); c.add(jsp,BorderLayout.NORTH);

c.add(bb,BorderLayout.SOUTH); bb.addActionListener(this); cerceve.pack(); cerceve.setVisible(true); basildi=false; } public String vericiktisi() { while(!basildi) { try {Thread.sleep(100);} catch(InterruptedException e) {System.err.println(e.toString());} } return s1; } protected void finalize() throws Throwable {super.finalize();} public void actionPerformed( ActionEvent e) { if(e.getSource()==bb) {s=jt.getText();fg.addfunction(s);s1=fg.createfunction(); basildi=true; try{fg.finalize();finalize(); } catch(Throwable e1) {System.err.println("girdi hatas.");} } } }

Program 1.5-4 real time plot class functiongenerator


mport java.io.*; import javax.swing.*; import javax.tools.*; import java.util.*; import java.lang.Math.*; import java.lang.*; class functiongenerator { String fonksiyon; String dosyaismi; String sinif; PrintStream fout; PrintStream ferr; public functiongenerator(String dosyaismi,String fonksiyon) { this.fonksiyon=fonksiyon; this.dosyaismi=dosyaismi; try { fout=new PrintStream(new FileOutputStream("print.txt")); ferr=new PrintStream(new FileOutputStream("error.txt")); }catch(FileNotFoundException e1) {System.err.println("dosya bulunamad");} } public functiongenerator(String fonksiyon) {this.fonksiyon=fonksiyon;

dosyaismi="f2D"; } public void addfunction(String fonksiyon) {this.fonksiyon=fonksiyon;} public String toString() {return fonksiyon;} public functiongenerator() {fonksiyon="x*x-4.0"; dosyaismi="f2D"; } public String createfunction() {String isim=dosyaismi+".java"; String s=""; try {PrintWriter ffout=new PrintWriter(new BufferedWriter(new FileWriter(isim))); s+="class "+dosyaismi+" extends f_x\n"; s+=" {\n"; s+=" public double func (double x)\n"; s+="{ double y="+fonksiyon+";\n"; s+=" return y;\n"; s+="}\n"; s+="}\n"; sinif=s; ffout.println(s); //System.out.println(s); ffout.close(); } catch(IOException e1) {System.err.println("girdi hatas.");} catch(NumberFormatException e2){}; try{ JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); int rc = javac.run(null,null,null,isim);} catch(NullPointerException e1) {System.err.println("NULL POINTER");} return fonksiyon; } protected void finalize() throws Throwable {super.finalize();} }

Real time program can be run directly from your screen by writing a small console program (a bat file for window dos system). That way you could have a direct print type function plot generator will be available to you. Program 1.5-5 real time Plot class G2D.bat java ce1

The same kind of real time program can be achived also for 3D plot environment Program 1.5-6 real time 3D Plot class ce5
import java.io.*; import javax.swing.*; import javax.tools.*; import java.util.*; import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import java.util.Locale; import java.text.*; import java.util.*; import javax.swing.table.*; import visad.*; import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D; import java.rmi.RemoteException; class ce5 extends JApplet

{ public ce5() { input3 g=new input3("f3D"," "); String s=g.vericiktisi(); try{g.finalize();} catch(Throwable e1) {System.err. println("input hatas1");} } public ce5(String fonksiyon) {//Text.create3Dfunction(s1); functiongenerator3D g=new functiongenerator3D(fonksiyon); String s=g.createfunction(); try{ g.finalize(); } catch(Throwable e1) {System.err.println("input hatas1");} } public static void main(String arg[]) throws RemoteException,VisADException { ce5 xx=new ce5(); double a[]=Text.readDoubleV("x_min x_max y_min y_max (enter 4 value)"); ce6 xy=new ce6(a[0],a[1],a[2],a[3],200); } }

Program 1.5-6 real time 3D Plot class ce6


import visad.*; import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D; import java.rmi.RemoteException; import java.awt.*; import javax.swing.*; import java.awt.event.*; class ce6 { public ce6(double xmin,double xmax,double ymin,double ymax,int n)throws RemoteException,VisADException {f3D f=new f3D(); Plot3D.func(f,xmin,xmax,ymin,ymax); } public static void main(String arg[]) throws s RemoteException,VisADException { double a[]=Text.readDoubleV("x_min x_max y_min y_max (4 deer giriniz)"); ce6 xy=new ce6(a[0],a[1],a[2],a[3],200); } }

Program 1.5-7 real time 3D Plot class functiongenerator3D


import java.io.*; import javax.swing.*; import javax.tools.*; import java.util.*; class functiongenerator3D extends Thread { String fonksiyon; String dosyaismi; String sinif; public functiongenerator3D(String dosyaismi,String fonksiyon) { this.fonksiyon=fonksiyon; this.dosyaismi=dosyaismi; }

public functiongenerator3D(String fonksiyon) {this.fonksiyon=fonksiyon; dosyaismi="f3D"; } public functiongenerator3D() {fonksiyon="x[0]*x[0]+x[1]*x[1]-4.0"; dosyaismi="f3D"; } public void addfunction(String fonksiyon) {this.fonksiyon=fonksiyon; } public String toString() {return fonksiyon;} public String createfunction() {String isim=dosyaismi+".java"; String s=""; try {PrintWriter fout=new PrintWriter(new BufferedWriter(new FileWriter(isim))); s+="class "+dosyaismi+" extends f_xj\n"; s+="{\n"; s+="public double func(double w[])\n"; s+="{//zm istenen fonksiyon\n"; s+="double x=w[0];\n"; s+="double y=w[1];\n"; s+="double ff;\n"; s+="ff="+fonksiyon+";\n"; s+="return ff;\n"; ; s+="}\n"; s+="}\n"; sinif=s; fout.println(s); //System.out.println(s); fout.close(); } catch(IOException e1) {System.err.println("girdi hatas.");} catch(NumberFormatException e2){}; JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); int rc = javac.run(null, null, null,isim); return fonksiyon; } protected void finalize() throws Throwable {super.finalize();} }

Program 1.5-8 real time 3D Plot class functiongenerator3D


import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.event.*; import java.io.*; import java.util.*; public class input3 extends Thread implements ActionListener { Container c; String s=" "; String s1=""; functiongenerator3D fg; JTextArea jt; JButton bb;

boolean basildi; public input3(String isim,String fonksiyon) { jt=new JTextArea(s,5,40); jt.setFont(new Font("SansSerif",Font.PLAIN,20)); fg=new functiongenerator3D(); bb=new JButton("3D push the button when you finish writing the function : variables : x and y"); JScrollPane jsp=new JScrollPane(jt); JFrame cerceve=new JFrame("3D function input window : a sample function : Math.sin(x*(10.1*y*Math.random()))*Math.cos(y) "); cerceve.addWindowListener(new BasicWindowMonitor()); c=cerceve.getContentPane(); c.setLayout(new BorderLayout()); c.add(jsp,BorderLayout.NORTH); c.add(bb,BorderLayout.SOUTH); bb.addActionListener(this); cerceve.pack(); cerceve.setVisible(true); basildi=false; } public String vericiktisi() { while(!basildi) { try {Thread.sleep(100);} catch(InterruptedException e) {System.err.println(e.toString());} } return s1; } protected void finalize() throws Throwable {super.finalize();} public void actionPerformed( ActionEvent e) { if(e.getSource()==bb) {s=jt.getText();fg.addfunction(s);s1=fg.createfunction(); basildi=true; try{fg.finalize();finalize(); } catch(Throwable e1) {System.err.println("input error");} } } }

Program 1.5-9 real time 3D Plot bat file G3D.bat javaw ce5

Not that similar sets are available for the real time and contour plot as well as ce1DC (contour plot) and ce1CT(time dependent plot) Program 1.5-10 real time 3D Contour Plot ce1DC.java
class ce1DC { public static void main(String arg[]) { ce5 x0=new ce5(); double a[]=Text.readDoubleV("enter minimum and maximum values of the function xmin xmax ymin ymax"); ce3DC x1=new ce3DC(a,50,x0.s); } }

Program 1.5-11 real time 3D Contour Plot program ce3DC.java


import java.io.*; import javax.swing.*; import javax.tools.*; import java.util.*; import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import java.util.Locale; import java.text.*; import java.util.*; import javax.swing.table.*; import static java.lang.Math.*; class ce3DC extends JApplet { public ce3DC(double a[],int n,String s) { int n1=a.length; f3D fa=new f3D(); // if(n1==6) {ContourPlot pp = new ContourPlot(fa,a[0],a[1],(int)a[5],a[2],a[3],(int)a[6],n, false,s, "x", "y") pp.plot();} else {ContourPlot pp = new ContourPlot(fa,a[0],a[1],100,a[2],a[3],100,n, false,s, "x", "y"); pp.plot();} } }

Program 1.5-13 real time bat file G3DC.bat d:\java\bin\javaw ce1DC

Entering function as a graphic inputs in your own programs simplifies graphic processes and helpfull in numerical analysis processes. Classes above is merged together to make simple to use String inputed plot functions. Program 1.5-14 real time String input function class PlotS.java
//====================================================== // Numerical Analysis package in java // PlotS class to plot 2D data // with real time function generation (function input as // String variable uses Plot class to plot function // Dr. Turhan Coban // EGE University, Engineering School, Mechanical Eng. Div. // turhan.coban@ege.edu.tr // ===================================================== import java.io.*; import java.awt.Color; import static java.lang.Math.*;

public class PlotS { ce1 x0; // write function f2D compile it as a java file ce3 x1; // plot function f2D by using Plot class String ss; double xm,xma; int N,ipt,ir,ig,ib; public PlotS(String s,double xm,double xma,int N,int ipt,int ir,int ig,int ib) { ce1 x0=new ce1(s); ss=s; this.xm=xm; this.xma=xma; this.N=N; this.ipt=ipt; this.ir=ir; this.ig=ig; this.ib=ib; } public PlotS(String s,double xm,double xma,int N,int ipt) { ce1 x0=new ce1(s); ss=s; this.xm=xm; this.xma=xma; this.N=N; this.ipt=ipt; this.ir=0; this.ig=0; this.ib=0; } public PlotS(String s,double xm,double xma,int N) { ce1 x0=new ce1(s); ss=s; this.xm=xm; this.xma=xma; this.N=N; this.ipt=0; this.ir=0; this.ig=0; this.ib=0; } public PlotS(String s,double xm,double xma) { ce1 x0=new ce1(s); ss=s; this.xm=xm; this.xma=xma; this.N=400; this.ipt=0; this.ir=0; this.ig=0; this.ib=0; } public void plot() { x1=new ce3(xm,xma,N,ipt,ir,ig,ib,ss);} public static void func(String s,double xm,double xma,int N,int ipt,int ir,int ig,int ib) {PlotS ps=new PlotS(s,xm,xma,N,ipt,ir,ig,ib); ps.plot(); } public static void func(String s,double xm,double xma,int N,int ipt) {PlotS ps=new PlotS(s,xm,xma,N,ipt,0,0,0);

ps.plot(); } public static void func(String s,double xm,double xma,int N) {PlotS ps=new PlotS(s,xm,xma,N,0,0,0,0); ps.plot(); } public static void func(String s,double xm,double xma) {PlotS ps=new PlotS(s,xm,xma,400,0,0,0,0); ps.plot(); } }

Program 1.5-15 real time String input function class PlotS.java example program NA0S.java (Static function call to plot)
import java.io.*; class NA0S { public static void main (String args[]) throws IOException { PlotS.func("x*x-5*x-3",0.0,10.0);}}

Program 1.5-16 real time String input function class PlotS.java example program NA0S1.java (Dynamic function call to plot)
import java.io.*; class NA0S1 { public static void main (String args[]) throws IOException { PlotS pp=new PlotS("x*x-5*x-3",0.0,10.0); pp.plot();}}

The same concept can also be applied to 3D plots. Program 1.5-17 real time String input function 3D Contour plot class Plot3DCS.java
//====================================================== // Numerical Analysis package in java // PlotS class to plot 3D function // with real time function generation (function input as // String variable uses ContourPlot class // Dr. Turhan Coban // EGE University, Engineering School, Mechanical Eng. Div. // turhan.coban@ege.edu.tr // ===================================================== import java.io.*; import java.awt.Color; import static java.lang.Math.*; public class Plot3DCS { ce5 x0; // write function f3D compile it as a java file

ce3DC x1; // plot function f3D by using ContourPlot class String ss; double a[]; int N,ipt,ir,ig,ib; public Plot3DCS(String s,double ai[],int N) { ce5 x0=new ce5(s); ss=s; a=new double[4]; for(int i=0;i<4;i++) {a[i]=ai[i];} this.N=N; } public void plot() { x1=new ce3DC(a,N,ss);} public static void func(String s,double a[],int N) {Plot3DCS ps=new Plot3DCS(s,a,N); ps.plot(); } public static void func(String s,double a[]) {Plot3DCS ps=new Plot3DCS(s,a,20); ps.plot(); } }

Program 1.5-18 real time String input function class PlotS.java example program NA03DCS.java (Static function call to plot)
import java.io.*; class NA03DCS { public static void main (String args[]) throws IOException { double a[]={-6,6,-6,6}; Plot3DCS.func("sin(x)*cos(y)",a); } }

Program 1.5-19 real time String input function 3D Contour plot class Plot3DS.java (Using visAD package an Plot3D)
import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D;

import java.rmi.RemoteException; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Plot3DS { ce5 x0; // write function f3D compile it as a java file ce6 x1; // plot function f3D by using Plot3D class String ss; double a[]; int N,ipt,ir,ig,ib; public Plot3DS(String s,double ai[],int N) { ce5 x0=new ce5(s); ss=s; a=new double[4]; for(int i=0;i<4;i++) {a[i]=ai[i];} this.N=N; } public void plot() throws RemoteException,VisADException { x1=new ce6(a,N);} public static void func(String s,double a[],int N) throws RemoteException,VisADException {Plot3DS ps=new Plot3DS(s,a,N); ps.plot(); } public static void func(String s,double a[]) throws RemoteException,VisADException {Plot3DS ps=new Plot3DS(s,a,20); ps.plot(); } }

Program 1.5-20 real time String input function class Plot3DSS.java example program NA03DS.java (Static function call to plot)
import javax.tools.*; import java.util.*; import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import java.util.Locale; import java.text.*; import java.util.*; import javax.swing.table.*; import visad.*; import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D; import java.rmi.RemoteException; class NA03DS { public static void main (String args[]) throws RemoteException,VisADException {double a[]={-6,6,-6,6}; Plot3DS.func("sin(x)*y+cos(y)*x",a,20); }}

In order to use all this realtime function graphic programs from one input point a a small user interface RTGraph is developed. Program 1.5-21 real time function graphic environment user interphase RTGraph
import java.io.*; import javax.swing.*; import javax.tools.*; import java.util.*; import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import java.util.Locale; import java.text.*; import java.util.*; import javax.swing.table.*; import visad.*; import visad.java3d.DisplayImplJ3D; import visad.java2d.DisplayImplJ2D; import java.rmi.RemoteException; class RTGraph { public static void main(String args[]) throws RemoteException,VisADException { String s="1 : y=f(x) 2D graph program\n"; s+="2 : z=f(x,y) 3D graph program\n"; s+="3 : z=f(x,y) 3D contour graph program\n"; s+="4 : z=f(x,t) time dependent graph program"; int s1= Integer.parseInt(JOptionPane.showInputDialog(s)); if(s1==2) { ce5 xx=new ce5(); double a[]=Text.readDoubleV("x_min x_max y_min y_max (4 deer giriniz)"); ce6 xy=new ce6(a[0],a[1],a[2],a[3],200); } else if(s1==3) { ce1DC x0=new ce1DC(); double a[]=Text.readDoubleV("Fonksiyonun minimum ve maksimum deerlerini giriniz xmin xmax ymin ymax"); ce3DC x1=new ce3DC(a,50,x0.s); } else if(s1==4) { ce1DT x0=new ce1DT(); double a[]=Text.readDoubleV("Fonksiyonun minimum ve maksimum deerlerini giriniz : xmin xmax tmin tmax"); ce3DT x1=new ce3DT(a,x0.s); } else {ce1 x0=new ce1(); double a[]=Text.readDoubleV("Fonksiyonun minimum ve maksimum deerlerini giriniz xmin xmax"); ce3 x1=new ce3(a[0],a[1],400,x0.s);} } }

It will connect to the programs explained above acording to input selection.

1.6 READING DATA FROM THE GRAPHIC ENVIRONMENT In numerical analysis one of the problem we face is the obtain data from some plot or graphic. In order to that scanning and digitizing of the data is needed. A simple digitizing program is given here. Program 1.6-1 scanP.java
import java.io.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.geom.*; import java.net.URL; import java.awt.image.*; import java.util.*; public class scanP extends JPanel implements MouseListener,MouseMotionListener,MouseWheelListener { int x1[],y1[]; public JLabel jp; public double a[][]; int n; boolean mousemoved; Rectangle2D l; Ellipse2D l1,l2; Color c; Image img; public PrintWriter fout; String dosya; //BufferedWriter fout; double xmin,xmax,ymin,ymax; int xcmin,xcmax,ycmin,ycmax; public scanP(String p1, String idosya,double ixmin,double ixmax,double iymin,double iymax) {super(); dosya=idosya; try {fout=new PrintWriter(new BufferedWriter(new FileWriter(dosya))); } catch(IOException e1) {System.err.println("input error.");} catch(NumberFormatException e2){} URL url = getClass().getResource(p1); img = getToolkit().getImage(url); xmin=ixmin; xmax=ixmax; ymin=iymin; ymax=iymax; jp=new JLabel(); xcmin=(int)xmin; ycmin=(int)ymin; xcmax=(int)xmax; ycmax=(int)ymax; x1=new int[1000]; y1=new int[1000]; a=new double[2][1000]; n=-2; addMouseListener(this); addMouseMotionListener(this); addMouseWheelListener(this); mousemoved=false; }

public void setMinMax(double ixmin,double ixmax,double iymin,double iymax) { xmin=ixmin; xmax=ixmax; ymin=iymin; ymax=iymax; } public JLabel getLabel() { return jp; } public void setDosyaismi(String Di) { dosya=Di; try {fout=new PrintWriter(new BufferedWriter(new FileWriter(dosya))); } catch(IOException e1) {System.err.println("input error.");} catch(NumberFormatException e2){}} public void setResimismi(String Ci) { URL url = getClass().getResource(Ci); img = getToolkit().getImage(url);} public void setCizgi(int x1i,int y1i) {if(n==-2) {xcmin=x1i;ycmin=y1i;} else if(n==-1) {xcmax=x1i;ycmax=y1i;} else {x1[n]=x1i;y1[n]=y1i; a[0][n]=xmin+(xmax-xmin)/(xcmax-xcmin)*(x1i-xcmin); a[1][n]=ymin+(ymax-ymin)/(ycmax-ycmin)*(y1i-ycmin); System.out.println(a[0][n]+" "+a[1][n]); fout.println(a[0][n]+" "+a[1][n]); }} public void paintComponent(Graphics g) { //super.paintComponent(g); Graphics2D g2=(Graphics2D)g; g2.setFont(new Font("Serif",Font.BOLD,24)); g2.setColor(Color.blue); g2.setStroke(new BasicStroke(2.0f)); Dimension d=getSize(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); int dx = d.width; int dy = d.height; g2.drawImage(img, 0, 0,dx,dy, this); if(n>=-1) { l1=new Ellipse2D.Double(xcmin-2,ycmin-2,5,5); l2=new Ellipse2D.Double(xcmax-2,ycmax-2,5,5); g2.draw(l1); g2.draw(l2); } for(int i=0;i<n;i++) {l=new Rectangle2D.Double(x1[i]-1,y1[i]-1,3,3); g2.draw(l); } } //MouseListener (fare dinleyicisi) public void mouseClicked(MouseEvent e) {} public void mouseclose() {if(!mousemoved) {fout.close(); Text.print(dosya,"digitiser output data : "+dosya+" file "); mousemoved=true; } }

public void mousePressed(MouseEvent e) { if(e.isMetaDown()) {mouseclose();} //when right button is pushed else if(e.isAltDown()) //when middle button is pushed {} else { setCizgi(e.getX(),e.getY());n++;repaint();} //when left button is pushed } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} //MouseMotionListener methods public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { double x1,y1; x1=xmin+(xmax-xmin)/(xcmax-xcmin)*(e.getX()-xcmin); y1=ymin+(ymax-ymin)/(ycmax-ycmin)*(e.getY()-ycmin); jp.setText("x = "+x1+"y = "+y1); } public void mouseWheelMoved(MouseWheelEvent e) {} }

Program 1.6-2 digitizer.java


import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.*; public class digitizer extends JApplet implements ActionListener { private JButton B[]; private JLabel l[]; JLabel altbar; private JTextField t[]; private JTextArea ta; private String S[]={"input page","graphic digitizing page"}; private Container c; private JPanel J[]; scanP fp; double xmin,xmax,ymin,ymax; String resim,dosya; JTabbedPane jtp; public void init() { xmin=0.0; xmax=10.0; ymin=0.0; ymax=10.0; resim="kalp.JPG"; dosya="a.txt";

c=getContentPane(); c.setLayout(new BorderLayout(5,5)); B=new JButton[2]; l=new JLabel[8]; t=new JTextField[6]; altbar=new JLabel(); ta=new JTextArea(toString()); jtp=new JTabbedPane(); J=new JPanel[3]; J[0]=new JPanel(); J[0].setLayout(new GridLayout(4,4)); J[1]=new JPanel(); J[2]=new JPanel(); B[0]=new JButton("JFileChoser reading picture"); B[1]=new JButton("JFileChoser output file"); l[0]=new JLabel("minimum x"); l[1]=new JLabel("minimum y"); l[2]=new JLabel("maximum x"); l[3]=new JLabel("maxsimum y"); l[4]=new JLabel("graphic (input) file"); l[5]=new JLabel("graphic (output) file"); l[6]=new JLabel("graphic (input) file"); l[7]=new JLabel("graphic (output) file"); t[0]=new JTextField(""+xmin); t[1]=new JTextField(""+ymin); t[2]=new JTextField(""+xmax); t[3]=new JTextField(""+ymax); t[4]=new JTextField(resim); t[5]=new JTextField(dosya); for(int i=0;i<5;i++) t[i].addActionListener(this); for(int i=0;i<2;i++) B[i].addActionListener(this); J[0].add(l[0]); J[0].add(t[0]); J[0].add(l[1]); J[0].add(t[1]); J[0].add(l[2]); J[0].add(t[2]); J[0].add(l[3]); J[0].add(t[3]); J[0].add(l[4]); J[0].add(t[4]); J[0].add(l[5]); J[0].add(t[5]); J[0].add(l[6]); J[0].add(B[0]); J[0].add(l[7]); J[0].add(B[1]); J[1].add(J[0],BorderLayout.NORTH); J[1].add(ta,BorderLayout.CENTER); jtp.addTab(S[0],J[1]); fp=new scanP(resim,dosya,xmin,xmax,ymin,ymax); //fp.setSize(1000,600); //J[2].setLayout(new FlowLayout()); J[2].add(fp,BorderLayout.CENTER); //J[2].add(fp.jp,BorderLayout.SOUTH); jtp.addTab(S[1],fp); c.add(jtp); } public String toString() {String s; s=" Turhan OBAN EGE niversitesi, Mhendislik Fak. Makina Mh.\n"; s+="=========================================================================================\n"; s+="Command structure : first two tkics to mouse defines minimum and maximum data points\n"; s+="After this each left mouse click converted to x and y coordinates according to given maximum \n"; s+="and minimum values. A picture of the graphics placed on the screen as a picture\n";

s+="when right hand button of mouse is pushed process stops and all the values are recorded \n"; s+="into the given file.\n"; s+="=========================================================================================\n"; return s; } public void actionPerformed(ActionEvent e) { if(e.getSource()==t[0]) { Double V0=new Double(t[0].getText()); xmin=V0.doubleValue(); } else if(e.getSource()==t[1]) { Double V0=new Double(t[1].getText()); ymin=V0.doubleValue(); } else if(e.getSource()==t[2]) { Double V0=new Double(t[2].getText()); xmax=V0.doubleValue(); } else if(e.getSource()==t[3]) { Double V0=new Double(t[3].getText()); ymax=V0.doubleValue(); } else if(e.getSource()==t[4]) { resim=t[4].getText(); fp.setResimismi(resim);} else if(e.getSource()==t[5]) { dosya=t[4].getText(); fp.setDosyaismi(dosya); } else if(e.getSource()==B[0]) { File file=new File("kalp.jpg"); JFileChooser fc=new JFileChooser(); if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {file = fc.getSelectedFile();resim=file.getName();} t[4].setText(resim); fp.setResimismi(resim); } else if(e.getSource()==B[1]) { File file=new File("a.txt"); JFileChooser fc=new JFileChooser(); if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {file = fc.getSelectedFile();dosya=file.getName();} t[5].setText(dosya); fp.setDosyaismi(dosya); } System.out.println("xmin = "+xmin+"xmax="+xmax+"ymin="+ymin+"ymax="+ymax); fp.setMinMax(xmin,xmax,ymin,ymax); repaint(); } public static void main(String s[]) { JFrame f = new JFrame("digitizer program"); JApplet applet = new digitizer(); applet.init(); f.add(applet); f.pack(); f.setSize(800,1000); f.setVisible(true); } }

When the program starts, a JPG Picture of the required graphic input is selected .The first act is to sign minimum and maximum point in the picture. Number equivalents of this points should also be given to the program through the input windows. After this each click of the left Mouse buton will be register a new point in cartesian coordinate system. Points will be recorded in the defined text output file. When the right button is clicked program will stop and bring outs a list of all clicked points as table values.
1.7 TAYLOR SERIES AND ERROR TERM IN THE EQUATIONS Taylor equation can be considered the roots of many numerical analysis methods. It will give us serial. Taylor series is a representation of a function as an infinite sum of terms calculated from the values of its derivatives at a single point. It is named after the English mathematician Brook Taylor. If the series is centered at zero, the series is also called a Maclaurin series, named after the Scottish mathematician Colin Maclaurin. It is common practice to use a finite number of terms of the series to approximate a function. Taylor series around a point a can be written as

f ( x) = f (a ) +

f ' (a) f " (a ) f ( 3) ( a ) ( x a) + ( x a) 2 + ( x a )3 + .. 1! 1! 1!

Or if it is written in summation notation

f ( x) =
n =0

Or if a is replaces with xk and x is replaced with xk+1 Equation becomes

f ( n ) (a) ( x a) n n!

f ( x k +1 ) =
n =0

f ( n ) ( x k ) k +1 ( x x k )n n!

Computers could not calculate anything more complex than addition, the rest usually accomplish through Taylor series approximations. For example function f(x)=ex can be approximated as

f ( x) = e x = 1 +

Some of the formulation used in Numerical analysis is direct derivation from the Taylor series. For example for the Newton root finding equation, above linear terms of the equation is ignorede and function value is replaced with 0

x x 2 x3 x 4 x5 x 2 x3 x 4 x5 + + + + .. = 1 + x + + + + .. 1! 2! 3! 4! 5! 2 6 24 120

f ( x k +1 ) = 0 = f ( x k ) + f ' ( x k )( x k +1 x k )
Will yield to

x k +1 = x k

f (xk ) f '(xk )

Taylor series can also be used to estimate error. The series usually used up to a certain term and error will be proportinal to one upper term in the series. For example If exponetial function s taken as

f ( x) = e x = 1 + x + x5 120

x 2 x3 x 4 + + 2 6 24

The error term will be proportional to the next term of the series

Error

2.0 FINDING THE ROOTS OF EQUATION NON-LINEAR FUNCTIONS WITH ONE VARIABLE Root finding is one of the most used numerical methods. For the one independent variable functions, it requires to find x value where the function is zero f(x)=0. If multidimensional (has more than one independent value) functions are involved, it requires to find x1,x2,x3,.,xn roots of n equation
fi(x1,x2,x3,.,xn)=0 , i=1..n Some of the root finding methods will be investigated .

2.1 BISECTION METHOD


In the bisection method root of the functions is searched in a given region a <= x <= b If a single root is existed in the given region equation f(a)*f(b) < 0 will be satisfied. If this condition is not satisfied, it can be assumed that there is no root in the given region. If the root existed region is divide to two equal parts as p=(a+b)/2 and function is evaluated in the middle. If f(a)*f(p) < 0 root should be located in a-p else it is located in p-b region. Rarely it is also possible that f(a)*f(p)=0 can happen, that will indicate the root. The new root region is used in further iterations. In order to use iterative process an exceptible criteria for error limit can be assumed

(b a) < (b + a)
kiye blme ynteminde f(x) fonksiyonunun blgesinde kk aranr. Eer bu blgede bir kk mevcutsa

Figure 2.1 Convergence of bisection Method

An example code is created for the Bisection method. Program 2.1-1 Bisection method NA1
import javax.swing.*; //============= Function to be define ================ class fb extends f_x {

public double func (double x) { return x*x-2.0;} } //=========================================================== public class NA1 { public static double bisection(f_x f,double xl,double xu) { //bisection root finding method double test; double xr=0; double es,ea; double fxl,fxr; int maxit=100,iter=0; es=0.0000001; ea=1.1*es; while((ea>es)&&(iter<maxit)) { xr=(xl+xu)/2.0; iter++; if((xl+xu)!=0) { ea=Math.abs((xu-xl)/(xu+xl))*100;} fxl= f.func(xl); fxr= f.func(xr); test= fxl*fxr; if(test==0.0) ea=0; else if(test<0.0) xu=xr; else {xl=xr;} } if(iter>=maxit) JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded \n"+ " result might not be valid","MAKSMUM NUMBER OF ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return xr; } public static void main (String args[]) { double a,b; a=Double.parseDouble(JOptionPane.showInputDialog(" INPUT LOWER LMT OF SEARCH AREA a : ")); b=Double.parseDouble(JOptionPane.showInputDialog(" INPUT UPPER LIMIT OF SEARCH AREA b : ")); double r; fb f=new fb(); r= bisection(f,a,b); JOptionPane.showMessageDialog(null," ROOT : "+r+"\nFUNCTION VALUE : "+f.func(r), "bisection root finding method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Steps of bisection method is shown in detail in the following table for function f(x)=x2-2

xl 1 1 1.25 1.375 1.375 1.40625 1.40625 1.4140625 1.4140625 1.4140625 1.4140625 1.4140625 1.4140625 1.41418457

xu 2 1.5 1.5 1.5 1.4375 1.4375 1.421875 1.421875 1.41796875 1.416015625 1.415039063 1.414550781 1.414306641 1.414306641

xr 1.5 1.25 1.375 1.4375 1.40625 1.421875 1.4140625 1.41796875 1.416015625 1.415039063 1.414550781 1.414306641 1.41418457 1.414245605

fxl 1 1 0.4375 0.10938 0.10938 0.02246 0.02246 0.00043 0.00043 0.00043 0.00043 0.00043 0.00043 8.2E05

fxu 2 0.25 0.25 0.25 0.066406 0.066406 0.021729 0.021729 0.010635 0.0051 0.002336 0.000954 0.000263 0.000263

fxr 0.25 0.4375 0.10938 0.066406 0.02246 0.021729 0.00043 0.010635 0.0051 0.002336 0.000954 0.000263 8.2E05 9.06E05

fxr*fxl 0.25 0.4375 0.047852 0.00726 0.002457 0.00049 9.6E06 4.5E06 2.2E06 1E06 4.1E07 1.1E07 3.5E08 7.4E09

2.2 FALSE POSITION METHOD (REGULA FALSI )


In bisection method search region was devided by two in each time. It is caused a relatively long interpolation period. In order to improve interpolation time, two points can be joined with a line and root of the line can be taken as the next iteration point .

p = b f (b)

( a b) ( f (a ) f (b)

Region selection process is same as bisection process. If f(a)*f(p) < 0 root should be located in a-p else it is located in p-b region. The new root region is used in further iterations. In order to use iterative process an exceptible criteria for error limit can be assumed

(b a) < (b + a)

Figure 2.2 Convergence of the False position Method

Program 2.2-1 False position (Regula falsi) NA2


import javax.swing.*; //============= Function to be define ================ class fb extends f_x { public double func (double x) { return x*x-2.0;} } //=========================================================== public class NA2 { public static double false_position(f_x f,double xl,double xu) { //false position root finding method double test; double xr=0; double es,ea; double fxl,fxu,fxr; int maxit=100,iter=0; es=0.1; ea=1.1*es; fxl= f.func(xl); fxu= f.func(xu); while((ea>es)&&(iter<maxit)) { xr=xu-fxu*(xl-xu)/(fxl-fxu); fxr= f.func(xr); iter++; if((xl+xu)!=0) { ea=Math.abs((xu-xl)/(xu+xl))*100;System.out.println("ea="+ea);}

test= fxl*fxr; if(test==0.0) ea=0; else if(test<0.0) {xu=xr;fxu=fxr;} else {xl=xr;fxl=fxr;} } if(iter>=maxit) JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded \n"+ " result might not be valid","MAKSMUM NUMBER OF ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return xr; } public static void main (String args[]) { double a,b; a=Double.parseDouble(JOptionPane.showInputDialog(" INPUT LOWER LMT OF SEARCH AREA a : ")); b=Double.parseDouble(JOptionPane.showInputDialog(" INPUT UPPER LIMIT OF SEARCH AREA b : ")); double r; fb f=new fb(); r= false_position(f,a,b); JOptionPane.showMessageDialog(null," ROOT : "+r+"\nFUNCTION VALUE : "+f.func(r), "false position root finding method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

What went wrong? How come it failed to find root of such a simple function? Let us plot the function iterations and try to see what went wrong.

Figure 2.3 Convergence of the False position Method example program function f(x)=x2-2 Steps of False position method is shown in detail in the following table for function f(x)=x2-2

xl 1 1.333333333 1.4 1.411764706 1.413793103 1.414141414 1.414201183 1.414211438 1.414213198 1.4142135 1.414213552 1.414213561 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562

xu 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

xr 1.333333333 1.4 1.411764706 1.413793103 1.414141414 1.414201183 1.414211438 1.414213198 1.4142135 1.414213552 1.414213561 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562

fxl 1 0.22222 0.04 0.00692 0.00119 0.0002 3.5E05 6E06 1E06 1.8E07 3E08 5.2E09 8.9E10 1.5E10 2.6E11 4.5E12 7.7E13 1.3E13 2.3E14

fxu 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

fxr 0.22222 0.04 0.00692 0.00119 0.0002 3.5E05 6E06 1E06 1.8E07 3E08 5.2E09 8.9E10 1.5E10 2.6E11 4.5E12 7.7E13 1.3E13 2.3E14 4.2E15

fxr*fxl 0.222222 0.008889 0.000277 8.23E06 2.43E07 7.14E09 2.1E10 6.19E12 1.82E13 5.37E15 1.58E16 4.65E18 1.37E19 4.03E21 1.19E22 3.49E24 1.03E25 3.04E27 9.65E29

As it is seen from the plot and table for iteration steps 2.0 is always stays and root search goes on between this two points. Even if solution is close to first point iterations did not cut from the left side of the search area. It cause a long number of iteration steps. If we try another function tan(x-1), it will be seen that result is achieved without difficulty.

In order to overcome the difficulty of one sided cut of the region a bisection step can be put in with some regular intervalse. For example once in every 4 false position steps. This is called modified false position method. The next example looks the root of f(x)=x2-2 with modified false position method. Program 2.2-2 Modified False position (Regula falsi) NA3
import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function to be define ================ class fb extends f_x { public double func (double x) { return x*x-2.0;} } //=========================================================== public class NA3 { public static double false_position(f_x f,double xa,double xu) { //Modified False position root finding double test; double p=0; double es,ea; double fa,fp,fu; int maxit=100,iter=0; es=0.000001; ea=1.1*es; double xold; int ia=0,iu=0; fa=f.func(xa); fu=f.func(xu); p=xu; double xpold; while(iter<maxit && ea>es) { xpold=p; p=xu-fu*(xa-xu)/(fa-fu); fp=f.func(p); iter++; if(p!=0) ea=Math.abs((p-xpold)/p)*100.0; test=fa*fp; if(test<0) {xu=p;fu=f.func(xu);iu=0;ia++;if(ia>=2) fa/=2.0;} else if(test>0) {xa=p;fa=f.func(xa);ia=0;iu+=1;if(iu>=2)fu/=2.0;} else ea=0; } if(iter>=maxit) JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded \n"+ " result might not be valid","MAKSMUM NUMBER OF ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return p; }

public static void main (String args[]) { double a,b; a=Double.parseDouble(JOptionPane.showInputDialog(" INPUT LOWER LMT OF SEARCH AREA a : ")); b=Double.parseDouble(JOptionPane.showInputDialog(" INPUT UPPER LIMIT OF SEARCH AREA b : ")); double r; fb f=new fb(); r= false_position(f,a,b); JOptionPane.showMessageDialog(null," ROOT : "+r+"\nFUNCTION VALUE : "+f.func(r), "false position root finding method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

As it is seen no problem arise finding the root of this function. 2.3 FIXED POSITION METHOD WITH AITKEN EXTRAPOLATION (STEPHENSEN METHOD) Fixed position method is relatively simple. When a function f(x) is given to fined the root, the function f(x) converted with algebraic manupulations and converted to f(x)=g(x) x form. Since this equation can be written in as g(x)=x, starting from an initial guess, iteration continued till x and g(x) gives the same x value. Even though the relative simplicity, this method is not used much due to high failure possibility. The equation can diverge from the root sometimes. In order to overcome this problem aitken extrapoaltion process will be combined with fixed point , iteration. Method is called Stephensen method.

Figure 2.4 Convergence of the Fixed iteration Method

Aitken extrapolation process is a error reduction method. For a very big k number

xk +2 a xk +1 a xk +1 a xk a

k >> 1

Relation can be valid. If the value of fourth point desired to be calculated from this equation

xe =

2 xk xk +2 xk +1 xk 2 xk +1 + xk +2

When 3 points are previously known, a third one can be evaluated xk = xk+1 - xk

2xk = xk+2 - 2xk+1 + xk

(xk ) 2 xe = xk 2 xk
Stephensen method evaluates equation by using an aitken extrapolation step after two step of fixed point iteration. Program 2.3-1 : Fixed point iteration (Stephensen method) :
// Stephensen method x=g(x) import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Tanm gereken fonksiyon ================ class fb extends f_x { //x = g(x) fonction //f(x)=x-Math.exp(-x) public double func (double x) { return Math.exp(-x);} } //=========================================================== public class NA4 { public static double stephensen(f_x f,double x0) { //Stephensen root finding method double es,ea; int maxit=100,iter=0; es=0.000001; ea=1.1*es; double x,x1,x2; x=0; while(iter<maxit && ea>es) { x1=f.func(x0); x2=f.func(x1); x=x0-(x1-x0)*(x1-x0)/(x2-2*x1+x0); ea=Math.abs(x-x0)*100.0; x0=x; iter++; } if(iter>=maxit) JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded \n"+ " result might not be valid","MAKSMUM NUMBER OF ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static void main (String args[]) { fb g=new fb(); double xa,xu; xa=Double.parseDouble(JOptionPane.showInputDialog(" first guess for the root value : ")); double r= stephensen(g,xa); double fr=g.func(r); String s=" root value : "+r+"\ng(x) = "+fr+"\nf(x)=g(x)-x = "+(fr-r); JOptionPane.showMessageDialog(null,s,

"Stephensen root finding method : ",JOptionPane.PLAIN_MESSAGE); } }

2.4NEWTON-RAPHSON METHOD
Taylor Formula can be written as

f(xn+1) = f(xn) + f(xn)( xn+1- xn) + f (xn) ( xn+1- xn)2/2! + f (3)(xn) ( xn+1- xn)3/3! + f (4)(xn) ( xn+1- xn)4/4! + f (5)(xn) ( xn+1- xn)5/5!+. We would like to find f(xn+1) = 0. If We substitude it into the Taylor Formula, and also cancel out terms above
the first degree , The equation becomes 0 = f(xn) + f(xn)( xn+1- xn). If xn+1 taken to left hand side Newton-Raphson Formula is obtained

xn+1 = xn - f(xn) /f(xn)


we can find the root iteratively starting from a first guess. Newton Formula aproached to the root quickly if the first guess is close to the actual root. But if the first gues is far away from the root, it might take longer or it might fail to reach to the root. Another difficulty in Newton-Raphson Formula is the requirement of knowing the actual derivative of the function

Figure 2.5 Convergence of the Newton-Raphson Method

PROGRAM 2.4-1 Newton-Raphson method // // // Newton-Raphson root finding //Numerical Analysis // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= The function ================ class fb extends f_x { public double func (double x) { return x*x*x-6*x*x+11*x-6.1;} } //============= Derivative of the function ================ class dfb extends f_x {

public double func (double x) { return 3.0*x*x-12.0*x+11;} } //=========================================================== public class NA5 { public static double newton(f_x f,f_x df,double x) { int nmax=100; double tolerance=1.0e-10; double fx,dfx; for(int i=0;i<nmax;i++) { fx=f.func(x); dfx=df.func(x); x-=fx/dfx; System.out.println("i="+i+"x="+x+"fx="+fx+"dfx = "+dfx); if(Math.abs(fx)<tolerance) { return x;} } JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+ " zm geerli olmyabilir","MAXMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static void main (String args[]) { fb f=new fb(); dfb df=new dfb(); double x0; x0=Double.parseDouble(JOptionPane.showInputDialog(" First guess for the root : ")); double r= newton(f,df,x0); JOptionPane.showMessageDialog(null," Root of the function : "+r+"\nFunction value : "+f.func(r), "Newton-Raphson root finding method : ",JOptionPane.PLAIN_MESSAGE); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA5 i=0x=1.0499999999999998fx=-0.09999999999999964dfx = 2.0 i=1x=1.054319180087848fx=-0.0073750000000005755dfx = 1.7075000000000014 i=2x=1.0543507243988341fx=-5.30870767248004E-5dfx = 1.68293663944916 i=3x=1.0543507260764082fx=-2.82295076203809E-9dfx = 1.6827576573350296 i=4x=1.0543507260764078fx=8.881784197001252E-16dfx = 1.6827576478166488 > Terminated with exit code 0. In order to see step calculations of Newton-Raphson method, root of f(x)=x2-2 is calculated in the spreadsheet environment as well NewtonRaphsonrootfindingf(x)=x2a a 2 x f(x)=x*xa fx/dx=2*x 1.00000000000000000 1 2 1.50000000000000000 0.25 3 1.41666666666667000 0.006944444 2.833333333 1.41421568627451000 6.0073E06 2.828431373 1.41421356237469000 4.51061E12 2.828427125 1.41421356237310000 0 2.828427125 1.41421356237310000 0 2.828427125 1.41421356237310000 0 2.828427125 As it is seen from the results, convergence is quite fast for this problem. 2.5 SECANT METHOD
One of the difficulty of the Newton-Raphson method is the requirement to find the second derivative. If difference equation (Numerical derivative) is used instead of derivative (tangent to the function), a secant to the function is drawn to the function, so the method is called secant method. If derivative is approximated with a first degree difference equation f ( xn )
'

f ( xn ) f ( xn1 ) ( xn xn1 )

Newton Raphson Formula is converted to

xn +1 = xn

f ( xn )( xn xn1 ) f ( xn ) f ( xn1 )

Two first estimation is needed to start iteration by using this formula f ( x0 ) van f ( x1 ) If we would like to establish a one estimation secant Formula, the previous difference equation can be written for a small x as (central difference Formula)

f ' ( xn )

f ( xn + x) f ( xn x) 2x 2 f ( xn )x f ( xn + x) f ( xn x)

Second equation becomes:

xn+1 = xn

In order to minimize error x should be relatively small, to have a further approximation a second order derivative (difference) Formula can also be used

f ' ( xn )

f ( xn + 2x) + 8 f ( xn + x) 8 f ( xn x) + f ( xn 2x) 12x

Figure 2.6 : Second method approximation with lineer difference

PROBLEM 2.5-1 NA7 : Secant method with two initial root estimation // Numerical Analysis // // // Root finding with secant method // two initia root estimation // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function definition ================ class fb extends f_x { public double func (double x) { return Math.sin(Math.sqrt(x))-x;}

} public class NA7 { public static double secant(f_x f,double x0,double x1) { int nmax=500; double tolerance=1.0e-10; double fx0,fx1; double x; fx0=f.func(x0); for(int i=0;i<nmax;i++) { fx1=f.func(x1); x=x1-fx1*(x1-x0)/(fx1-fx0); x0=x1; fx0=fx1; x1=x; if(Math.abs(fx1)<tolerance) { return x1;} } JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+ " zm geerli olmyabilir","MAXMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return x1; } public static void main (String args[]) { fb f=new fb(); double x0,x1; x0=Double.parseDouble(JOptionPane.showInputDialog(" First guess for the root : ")); x1=Double.parseDouble(JOptionPane.showInputDialog(" Second guess for the root : ")); double r= secant(f,x0,x1); JOptionPane.showMessageDialog(null," the root : "+r+"\nfunction value : "+f.func(r), "Secant root finding method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Re-write the program to find root of f(x)=tan(x-1) In order to see calculation steps of the method a spreadsheet program is introduced, fort he function f(x)=x2-2, initial values x-1=0, x0=1 and f ' ( xn ) Secant1f(x)=x2a

f ( xn ) f ( xn1 ) ( xn xn1 )

a x 0.00000000000000000 1.00000000000000000 2.00000000000000000 1.33333333333333000 1.40000000000000000 1.41463414634146000 1.41421143847487000 1.41421356205732000 1.41421356237310000 1.41421356237310000 In the next java example f ' ( xn )

2 f(x)=x*xa 2 1 2 0.222222222 0.04 0.001189768 6.00729E06 8.93146E10 0 0

fx/dx=(f(xn)f(xn1))/(xnxn1) 1 3 3.333333333 2.733333333 2.814634146 2.828845585 2.828425001 2.828424244

f ( xn + x) f ( xn x) approach is used to estimate the derivative 2x

PROBLEM 2.5-2 NA8 : Secant method with one initial root estimation, lineer difference Formula ///Numerical Analysis // // // Secant method with one root estimation // Linear difference formula // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= function definition ================ class fb extends f_x { public double func (double x) { return x*x-2;} } public class NA8 { public static double secant(f_x f,double x) { int nmax=100; double tolerance=1.0e-10; double fx,dfx; double dx=0.01; for(int i=0;i<nmax;i++) { fx=f.func(x);

dfx=(f.func(x+dx)-f.func(x-dx))/(2.0*dx); x-=fx/dfx; if(Math.abs(fx)<tolerance) { return x;} } JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+ " zm geerli olmyabilir","MAXMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static void main (String args[]) { fb f=new fb(); double x0; x0=Double.parseDouble(JOptionPane.showInputDialog(" First estimation for the root : ")); double r= secant(f,x0); JOptionPane.showMessageDialog(null," the root : "+r+"\nFunction value : "+f.func(r), "root of the function by Secant method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Find roos of f(x)=x*x*x-2 to see calculation steps of the method a spreadsheet program is introduced, for the function f(x)=x2-2, initial values x0=1 , x=0.1 and f ' ( xn )
Secant2f(x)=x*xa a x x 1 1.5 1.416666667 1.414215686 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 1.414213562 2 2 f(x)=x a 0.1 1 0.25 0.00694 6E06 4.5E12 0 0 0 0 0 0 0 x+x 1.1 1.6 1.51667 1.51422 1.51421 1.51421 1.51421 1.51421 1.51421 1.51421 1.51421 1.51421 x-x 0.9 1.4 1.31667 1.31422 1.31421 1.31421 1.31421 1.31421 1.31421 1.31421 1.31421 1.31421

f ( xn + x) f ( xn x) 2x
f(x+x) 0.79 0.56 0.30028 0.29285 0.29284 0.29284 0.29284 0.29284 0.29284 0.29284 0.29284 0.29284 f(x-x) 1.19 0.04 0.26639 0.27284 0.27284 0.27284 0.27284 0.27284 0.27284 0.27284 0.27284 0.27284 f'(x) 2 3 2.83333 2.82843 2.82843 2.82843 2.82843 2.82843 2.82843 2.82843 2.82843 2.82843

PROBLEM 2.5-3 NA8 : Secant method with one initial root estimation, quadratic difference Formula ///Numerical Analysis // // // Secant method with one root estimation // quadratic difference formula // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= function definition ================ class fb extends f_x { public double func (double x) { return Math.log(x*x)-0.7;} } //=========================================================== public class NA9 { public static double df(f_x f,double x,double h) { // derivative formula double hh=1.0/h; return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/12.0*hh; } public static double secant(f_x f,double x) { int nmax=500; double tolerance=1.0e-15; double fx,dfx; for(int i=0;i<nmax;i++) { fx=f.func(x); dfx=df(f,x,0.00001); x-=fx/dfx; if(Math.abs(fx)<tolerance) { return x;} } JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+ " zm geerli olmyabilir","MAXMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static void main (String args[]) { fb f=new fb();

double x0; x0=Double.parseDouble(JOptionPane.showInputDialog(" First estimation for the root : ")); double r= secant(f,x0); JOptionPane.showMessageDialog(null," the root : "+r+"\nFunction value : "+f.func(r), "root of the function by Secant method : ",JOptionPane.PLAIN_MESSAGE); } }

Utilize the method to find root of f(x)=x*x*x-3 In the next example secant solution of the function f(x)=x2-2 will be inroduced with initial values x0=1 , x=0.1 and f ' ( xn )
spreadsheet environment
Secant3f(x)=x a a 2 0.1 x x f(x)=x*xa 1 1 1.334261838 0.21975 1.439846995 0.07316 1.397218859 0.04778 1.422795983 0.02435 1.409099259 0.01444 1.417005353 0.0079 1.412610076 0.00453 1.415108941 0.00253 1.413705498 0.00144 1.414499268 0.00081 1.414052077 0.00046 1.414304575 0.00026 1.414162185 0.00015 1.414242539 8.2E05 1.414197211 4.6E05 1.414222786 2.6E05 1.414208358 1.5E05 1.414216498 8.3E06
2

f ( xn + 2x) + 8 f ( xn + x) 8 f ( xn x) + f ( xn 2x) in the 12x

x+2x 1.2 1.53426 1.63985 1.59722 1.6228 1.6091 1.61701 1.61261 1.61511 1.61371 1.6145 1.61405 1.6143 1.61416 1.61424 1.6142 1.61422 1.61421 1.61422

x+x 1.1 1.43426 1.53985 1.49722 1.5228 1.5091 1.51701 1.51261 1.51511 1.51371 1.5145 1.51405 1.5143 1.51416 1.51424 1.5142 1.51422 1.51421 1.51422

x-x 0.9 1.23426 1.33985 1.29722 1.3228 1.3091 1.31701 1.31261 1.31511 1.31371 1.3145 1.31405 1.3143 1.31416 1.31424 1.3142 1.31422 1.31421 1.31422

x-2x 0.8 1.13426 1.23985 1.19722 1.2228 1.2091 1.21701 1.21261 1.21511 1.21371 1.2145 1.21405 1.2143 1.21416 1.21424 1.2142 1.21422 1.21421 1.21422

f(x+2x) 0.56 0.35396 0.6891 0.55111 0.63347 0.5892 0.61471 0.60051 0.60858 0.60405 0.60661 0.60516 0.60598 0.60552 0.60578 0.60563 0.60572 0.60567 0.60569

f(x+x) 0.79 0.05711 0.37113 0.24166 0.31891 0.27738 0.30131 0.28799 0.29556 0.2913 0.29371 0.29235 0.29312 0.29269 0.29293 0.29279 0.29287 0.29283 0.29285

f(x-x) 1.19 0.4766 0.20481 0.31722 0.25021 0.28626 0.2655 0.27705 0.27049 0.27418 0.27209 0.27327 0.2726 0.27298 0.27277 0.27289 0.27282 0.27286 0.27283

f(x-2x) 1.36 0.71345 0.46278 0.56667 0.50477 0.53808 0.5189 0.52958 0.52351 0.52692 0.52499 0.52608 0.52546 0.52581 0.52562 0.52573 0.52566 0.5257 0.52568

f'(x) 2.99167 2.08121 1.71622 1.86805 1.77768 1.82635 1.79833 1.81393 1.80507 1.81005 1.80724 1.80882 1.80793 1.80843 1.80815 1.80831 1.80822 1.80827 1.80824

2.6 MULLERS METHOD In false position method roots were found by using the root of the lineer line passing throgh the two interval points. The same concept can be use to fit a quadratic curve and find the roots of the quadratic function. But quadratic function fitting is required 3 estimation point. The method consist of deriving the coefficients of a quadratic curve(parabola) that goes through three points. If function evaluation of these 3 points are f(x0), f(x1) and f(x2), enough information will be evailable to make a quadratic curve root estimation. Assuming quadratic function defined b the following equation : f(x)=a(x-x2)2+b(x-x2)+c Evaluation of the function in 3 given points will be

f(x0)=a(x0-x2)2+b(x0-x2)+c f(x1)=a(x1-x2)2+b(x1-x2)+c f(x2)=a(x2-x2)2+b(x2-x2)+c=c Considering the difference of the functions: f(x0)-f(x2)=a(x0-x2)2+b(x0-x2) f(x1)-f(x2)=a(x1-x2)2+b(x1-x2) h0= x1 x0 h1= x2 x1

d0 =
d1 =

f ( x1 ) f ( x0 ) h0
f ( x2 ) f ( x1 ) h1

Figure 2.7 graphic representation of Muller root finding method.

When these are substituted back to the main equation (h0+h1)b - (h0+h1)2a = h0d0 + h1d1 h1 b h12 a = h1d1 is obtained. a and b can be solved from here.
a= d1 d 0 h1 + h0

b = a*h1 + d1 c=f(x2) to find root root :

= b 2 4ac
if

b+ >b e=b+
else

b + b e = b

xr = x 2

2c e

x2 will be substituted with xr and iteration continues. It should be noted that in a quadratic formula complex roots can be located as well as the real roots, therefore this method can be used to find complex roots as well. The program NA10 is developed to calculate real roots by Program 2.6-1 NA10 : Muller method root finding with quadratic formula
//Numerical Analysis // Muller method for root finding import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function definition ================ class fb extends f_x { public double func (double x) { return x*x-2;} } //===========================================================

public class NA10 { public static double muller(f_x f,double x0,double x1,double x2) { //Finding real roots by using Muller method //orta xr noktasndan +/- h kadar giderek dier 2 noktaya ulayoruz. int maxit=5; double xr=x0; int iter=0; double es1=0.001; double es=es1; double ea=1.1*es; double h0,h1; double d0,d1; double a,b,c; double fx0,fx1,fx2; double determinant; double den; double dxr; while(iter<maxit && ea>es) { fx0=f.func(x0); fx1=f.func(x1); fx2=f.func(x2); h0=x1-x0; h1=x2-x1; d0=(fx1-fx0)/h0; d1=(fx2-fx1)/h1; a=(d1-d0)/(h1+h0); b=a*h1+d1; c=fx2; determinant=b*b-4.0*a*c; if(determinant<=0) determinant=0;//we do not want to calculate complex roots here else determinant=Math.sqrt(determinant); if(Math.abs(b+determinant)>Math.abs(b-determinant)) {den=b+determinant;;}

else {den=b-determinant;} dxr=-2*c/den; xr=x2+dxr; System.out.println("a="+a+"b="+b+"c="+c+"disc="+determinant+"xr="+xr+"iter="+iter); ea=Math.abs((xr-x2)/xr)*100; iter++; x0=x1; x1=x2; x2=xr; } if(iter>=maxit) JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+ " zm geerli olmyabilir","MAXMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return xr; } public static void main (String args[]) { fb f=new fb(); double x0,x1,x2; x0=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x0 : ")); x1=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x1 : ")); x2=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x2 : ")); //h=Double.parseDouble(JOptionPane.showInputDialog(" h=(kk bir rakam rnein 0.1")); double r; r= muller(f,x0,x1,x2); JOptionPane.showMessageDialog(null," Value of the root : "+r+"\nFunction value : "+f.func(r), "root estimation with Muller method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Find the root of f(x)=ex-2 by using Muller method 2.7 NEWTON-RAPHSON (SECANT) BISECTION COMBINED METHOD Bisection method is good in getting closer to the root. But when it get close to root it takes a long time to achive the final accuracy. On the other hand, Newton-Raphson method is very efficient when the estimation is close to the actual root but rather poor performer when the root estimation is not very close to the actual root. By combining two methods advantages of both methods will be maximized while inefficiencies will be eliminated. Therefore a combine method is a good candidate for an efficient algorithm. Program 2.7-1 NA11 : Newton-Raphson (Secant)-Bisection combined method
/ // // Newton-Raphson (Secant)-Bisection combined root finding method //

// import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function definition ================ class fb extends f_x { public double func (double x) { return Math.log(x*x)-0.7;} } //=========================================================== public class NA11 { public static double dfx(f_x f,double x,double h) { //Derivative formula double hh=1.0/h; return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/12.0*hh; } public static double newton_bisection(f_x fx, double x1, double x2) { double xacc=1.0e-10; //accuracy double h=0.0001; //derivative function step int MAXIT=100; //Maximumnumber of iterations int j; double dfr,dx,dxold,fr,fh,fl; double temp,b,a,r; fl=fx.func(x1); fh=fx.func(x2); //Check if a root is existed in the given region if ((fl > 0.0 && fh > 0.0) || (fl < 0.0 && fh < 0.0)) System.out.println("There are no root in the given region or double root?"); if (fl == 0.0) return x1; if (fh == 0.0) return x2; if (fl < 0.0) { a=x1;b=x2;} else { b=x1;a=x2;} r=0.5*(x1+x2); //midpoint value dxold=Math.abs(x2-x1); dx=dxold; fr=fx.func(r); //function value at midpoint dfr=dfx(fx,r,h); //derivative of function value at midpoint for (j=1;j<=MAXIT;j++) { if ((((r-b)*dfr-fr)*((r-a)*dfr-fr) > 0.0) || (Math.abs(2.0*fr) > Math.abs(dxold*dfr))) { //bisection step dxold=dx; dx=0.5*(b-a); r=a+dx; if (a == r) return r; //zm!!!! } else{ //Newton-Raphson (Secant) step dxold=dx;

dx=fr/dfr; temp=r; r -= dx; if (temp == r) return r; //solution!!!!! } if (Math.abs(dx) < xacc) return r; //solution!!!!! fr=fx.func(r); dfr=dfx(fx,r,h); if (fr < 0.0) a=r; else b=r; } System.out.println("Maximum number of iterations are exceeded"); return 0.0; //program should never reach here //dummy return } public static void main (String args[]) { fb f=new fb(); double x0,x1; x0=Double.parseDouble(JOptionPane.showInputDialog(" lower root limit : ")); x1=Double.parseDouble(JOptionPane.showInputDialog(" higher root limit : ")); double r= newton_bisection(f,x0,x1); JOptionPane.showMessageDialog(null," root value : "+r+"\nFunction value : "+f.func(r), "Newton-Raphson (Secant) - Bisection combined root finding method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Find the root of f(x)=ln(x)-1

A second problem will be defined as an engineering problem from heat transfer: Time dependent temperature of a flat wall will be given with the differential equation : In this equation T is temperature (degree Kelvin), x is the distance(meter), t is the time(second) T 2T = 2 t x k = C p And is thermal diffusivity, where k is thermal conductivity(W/(mK)), is density (kg/m3) and Cp is specific heat(J/(kgK))

Figure 2.8 geometry of wall in time dependent heat transfer problemT

Boundary conditions: T ( x,0) = Ti T | x =0 x T k | x= L = [h T ( L, t ) T )] x

Converted dimensionless form of the equation and boundary conditions


Boundary conditions:

Where Bi is called Biot Number, and Fo is caled Fourier number. Analytic solution of this differential equation :

Where

Cn =
And

4 sin n 2 n n + sin( 2 n n )

n tan n = Bi

As it is seen from the last equation, this equation can only be solved by using numerical root finding methods. After finding the roots coefficients Cns can be evaluated and heat transfer equation is solved. Program calculated the heat transfer is given as a whole below:

Program 2.7-2 HT_wall_x_t : Newton-Raphson (Secant)-Bisection combined method


import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; public class HT_duvar { public static double fx(double lambda,double Bi) {return lambda*Math.tan(lambda)-Bi;} public static double dfx(double x,double h,double Bi) { //Trev hesab return (-fx((x+2.0*h),Bi)+8.0*fx((x+h),Bi)-8.0*fx((x-h),Bi)+fx((x-2.0*h),Bi))/(12.0*h); } public static double Cn(double ln,double Bi) { return 4.0*Math.sin(ln)/(2.0*ln+Math.sin(2.0*ln)); } public static double lambda(double n,double Bi) { //lineer olmayan denklemin kk : ikiyeblme-Newton Raphson metodu //lambda*tan(lambda)-Bi=0 denkleminin kklerini bulur double x1=(n-1)*Math.PI; double x2= x1+Math.PI/2-0.000001; double xacc=1.0e-10; //iterasyonlar arasndaki fark, bu rakamdan // kk olduunda zme ulatmz kabul edeceiz double h=0.0001; //trev step bykl int MAXIT=100; //Maksimum iterasyon says int j; double dfr,dx,dxold,fr,fh,fl; double temp,b,a,r; fl=fx(x1,Bi); fh=fx(x2,Bi); //System.out.println("x1 ="+x1+"fl="+fl+"x2="+x2+"fh="+fh); //Blgemizde kk deeri varm kontrol et if ((fl > 0.0 && fh > 0.0) || (fl < 0.0 && fh < 0.0)) System.out.println("n = "+n+"x1="+x1+"x2="+x2+"verilen snrlar iinde kk deeri yok"); if (fl == 0.0) return x1; if (fh == 0.0) return x2; if (fl < 0.0) { a=x1;b=x2;} else { b=x1;a=x2;} r=0.5*(x1+x2); //orta nokta deeri dxold=Math.abs(x2-x1); dx=dxold; fr=fx(r,Bi); //fonksiyonun r noktasndaki deeri dfr=dfx(r,h,Bi); //fonksiyonun trevinin r noktasndaki deeri for (j=1;j<=MAXIT;j++) { if ((((r-b)*dfr-fr)*((r-a)*dfr-fr) > 0.0) || (Math.abs(2.0*fr) > Math.abs(dxold*dfr))) { //ikiye blme dxold=dx; dx=0.5*(b-a); r=a+dx;

if (a == r) return r; //zm!!!! } else{ //Newton-Raphson dxold=dx; dx=fr/dfr; temp=r; r -= dx; if (temp == r) return r; //zm!!!!! } if (Math.abs(dx) < xacc) return r; //zm!!!!! fr=fx(r,Bi); dfr=dfx(r,h,Bi); if (fr < 0.0) a=r; else b=r; } System.out.println("Maksimum iterasyon says ald"); return 0.0; //program buraya gelmemeli } public static double teta(double Fo,double Bi,double x) {double tt=0;double ln=0; for(int n=1;n<20;n++) {ln=lambda(n,Bi);tt+=Cn(ln,Bi)*Math.exp(-ln*ln*Fo)*Math.cos(ln*x);} return tt; } public static void main (String args[]) { fb f=new fb(); double Bi,Fo,x; Fo=Double.parseDouble(JOptionPane.showInputDialog(" Forier says Fo : ")); Bi=Double.parseDouble(JOptionPane.showInputDialog(" Biot says Bi : ")); x=Double.parseDouble(JOptionPane.showInputDialog(" x* = x/L : ")); JOptionPane.showMessageDialog(null," teta = "+teta(Fo,Bi,x), "duvarda zamana bal s transferi : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

2.8 INVERSE QUADRATIC LAGRANGE INTERPOLATION Lagrange nterpolation will be investigated further in the interpolation section. In here, the Formula for the quadratic inverse lagrange polinomial will be introduced to utilise in finding roots of a function.
x=

[ y f ( x1 )][ y f ( x2 )]x3 [ y f ( x2 )][ y f ( x3 )]x1 [ y f ( x3 )][ y f ( x1 )]x2 + + [ f ( x3 ) f ( x1 )][ f ( x3 ) f ( x2 )] [ f ( x1 ) f ( x2 )][ f ( x1 ) f ( x3 )] [ f ( x2 ) f ( x3 )][ f ( x2 ) f ( x1 )]

In this equation if y=0 is taken, the value x will give us a quadratic approximation to the root of the function
x= f ( x1 ) f ( x2 ) x3 f ( x2 ) f ( x3 ) x1 f ( x3 ) f ( x1 ) x2 + + [ f ( x3 ) f ( x1 )][ f ( x3 ) f ( x2 )] [ f ( x1 ) f ( x2 )][ f ( x1 ) f ( x3 )] [ f ( x2 ) f ( x3 )][ f ( x2 ) f ( x1 )]

If equation is re-arranged

x=

[ f ( x1 ) f ( x2 )] f ( x1 ) f ( x2 ) x3 + [ f ( x3 ) f ( x2 )] f ( x2 ) f ( x3 ) x + [ f ( x3 ) f ( x1 )] f ( x3 ) f ( x1 ) x2 [ f ( x3 ) f ( x1 )][ f ( x3 ) f ( x2 )][ f ( x1 ) f ( x3 )]

In order to make the equation simpler further definations will apply as:

R=

f ( x2 ) f ( x3 ) f ( x2 ) f ( x1 ) f ( x1 ) f ( x3 )

S=

T=

P = S[T(R-T)(x3 x2) (1-R)(x2 x1)] Q = (T - 1)(R 1)(S 1) Now the root can be written as: x = x2 +

P Q

When a new root is found the neigbour region of the root is left for the next iteration and the region further away to the root is eliminated.

Figure 2.8 Quadratic inverse lagrange interpolation region selection Program 2.8-1 NA12 : quadratic inverse lagrange interpolation formula method
public class NA12 { public static double inverseinter2Droot(f_x f,double x1,double x3) {double x2=(x1+x3)/2.0; return inverseinter2Droot(f,x1,x2,x3); } public static double inverseinter2Droot(f_x f,double x1,double x2,double x3) { // Inverse quadratic Lagrange interpolation

double test; double p=0; double es,ea; double f1,f2,f3,fp; int maxit=100,iter=0; double tol=1.0e-10; es=0.00000001; ea=0.001; f1=f.func(x1); f2=f.func(x2); f3=f.func(x3); if(f1==0) return x1; else if(f2==0) return x2; else if(f3==0) return x3; if(f1*f3>0) System.out.println("Root is not existed in the given region"); while((ea>es)&&(iter<maxit)) { p=-(f2*f3*x1*(f2-f3)+f3*f1*x2*(f3-f1)+f1*f2*x3*(f1-f2))/((f1-f2)*(f2-f3)*(f3-f1)); fp=f.func(p); ea=Math.abs(x3-p); if(Math.abs(f3)<tol) return x3; if(p>x3) {x1=x3;f1=f3;x3=p;f3=fp;} else if(p<x3) {x2=x3;f2=f3;x3=p;f3=fp;} iter++; } if(iter>=maxit) JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+ " zm geerli olmyabilir","MAXMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return p; } public static void main (String args[]) { fc f=new fc(); double x1,x2,x3; x1=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x1 : ")); x3=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x2 : ")); double r; r= inverseinter2Droot(f,x1,x3); JOptionPane.showMessageDialog(null," root value : "+r+"\nFunction value : "+f.func(r), "Quadratic inverse Lagrange Interpolation method : ",JOptionPane.PLAIN_MESSAGE); } }

Find the root of f(x)=ln(x*x*x)-3 2.9 BRENT METHOD (INVERSE QUADRATIC LAGRANGE INTERPOLATION BISECTION AND SEKANT COMBINED METHOD)

Being a quadratic method, Inverse quadratic Lagrange interpolation is an efficient method by combining it with bi-section and sekant method method one of the most used root finding method is created. Algorithm of the code is given below. Two versions of the program code is also given below.
Algorithm

input a, b, and a pointer to a subroutine for f calculate f(a) calculate f(b) if f(a) f(b) >= 0 then error-exit end if if |f(a)| < |f(b)| then swap (a,b) end if c := a set mflag repeat until f(b or s) = 0 or |b a| is small enough (convergence) o if f(a) f(c) and f(b) f(c) then

interpolation)

else

o o

(secant rule) end if if (condition 1) s is not between (3a + b)/4 and b or (condition 2) (mflag is set and |sb| |bc| / 2) or (condition 3) (mflag is cleared and |sb| |cd| / 2) or (condition 4) (mflag is set and |bc| < ||) or (condition 5) (mflag is cleared and |cd| < ||) then

(bisection method) set mflag

o o o o o o o

else

clear mflag end if calculate f(s) d := c (d is assigned for the first time here; it won't be used above on the first iteration because mflag is set) c := b if f(a) f(s) < 0 then b := s else a := s end if if |f(a)| < |f(b)| then swap (a,b) end if end repeat output b or s (return the root)

Program 2.9-1 NA13 : BRENT METHOD


/ Numerical Analysis package // Brent root finding method import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function Definition ================ class fc extends f_x { public double func(double x)

{return x*x-2;} } //=========================================================== public class NA13 { public static double brent(f_x f,double a,double b) { // double test; double p=0; double es,ea; double f1,f2,f3,fp; int maxit=500,iter=0; double tol=1.0e-15; es=0.0000001; double x1=a;f1=f.func(x1); double x2=b;f2=f.func(x2); double x3=(x1+x2)/2.0;f3=f.func(x3); if(f1==0) return x1; else if(f2==0) return x2; else if(f3==0) return x3; if(f1*f2>0) System.out.println("No root is existed in the given region"); p=-(f2*f3*x1*(f2-f3)+f3*f1*x2*(f3-f1)+f1*f2*x3*(f1-f2))/((f1-f2)*(f2-f3)*(f3-f1)); fp=f.func(p); ea=Math.abs(x3-p); while((ea>es)&&(iter<maxit)) { if(Math.abs(f3)<tol) return x3; if((p<x1) && (p>x2)) {p=(x1+x2)/2.0; if(p>x3) {x1=x3;f1=f3;x3=p;f3=fp;} else if(p<x3) {x2=x3;f2=f3;x3=p;f3=fp;} } else { if(p>x3) {x1=x3;f1=f3;x3=p;f3=fp;} else if(p<x3) {x2=x3;f2=f3;x3=p;f3=fp;} p=-(f2*f3*x1*(f2-f3)+f3*f1*x2*(f3-f1)+f1*f2*x3*(f1-f2))/((f1-f2)*(f2-f3)*(f3-f1)); fp=f.func(p); ea=Math.abs(x3-p); } iter++; } if(iter>=maxit) JOptionPane.showMessageDialog(null,"Warning : MAximum number of iteration is exceeded \n"+ " result may not be valid","MAXIMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return p; } public static void main (String args[]) { fc f=new fc(); double x1,x2,x3; x1=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x1 : ")); x2=Double.parseDouble(JOptionPane.showInputDialog(" root estimation x2 : ")); double r; r= brent(f,x1,x2);

JOptionPane.showMessageDialog(null," root value : "+r+"\nFunction value : "+f.func(r), "Brent method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Program 2.9-2 NA13A : BRENT METHOD (SECOND VERSION)


// // // // Brent root finding method // // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class fc extends f_x { public double func(double x) {return (x+3)*(x-1)*(x-1);} } public class NA13A { public static double brent(f_x f,double x1,double x2) { // roots of the nonlinear equation: Brent method // referance : Numerical Recipes in C, second edition, William H. Press, // Saul A. Teukolsky, William T. Vetterling, Brian P. Flannery // cambridge university press double ITMAX=100; double EPS=3.0e-8; int iter; double a=x1,b=x2,c,d,e,min1,min2; double tol=1.0e-5; c=0; d=0; e=0; double fa=f.func(a),fb=f.func(b),fc,p,q,r,s,tol1,xm; if (fb*fa > 0.0) JOptionPane.showMessageDialog(null,"Root limit selection is uncorrect \n result may be wrong" ,"ROOT LIMITS WARNING",JOptionPane.WARNING_MESSAGE); fc=fb; for (iter=1;iter<=ITMAX;iter++) { if (fb*fc > 0.0) {

c=a; fc=fa; e=d=b-a; } if (Math.abs(fc) < Math.abs(fb)) { a=b; b=c; c=a; fa=fb; fb=fc; fc=fa; } tol1=2.0*EPS*Math.abs(b)+0.5*tol; xm=0.5*(c-b); if (Math.abs(xm) <= tol1 || fb == 0.0) return b; if (Math.abs(e) >= tol1 && Math.abs(fa) > Math.abs(fb)) { s=fb/fa; if (a == c) { p=2.0*xm*s; q=1.0-s; } else { q=fa/fc; r=fb/fc; p=s*(2.0*xm*q*(q-r)-(b-a)*(r-1.0)); q=(q-1.0)*(r-1.0)*(s-1.0); } if (p > 0.0) q = -q; p=Math.abs(p); min1=3.0*xm*q-Math.abs(tol1*q); min2=Math.abs(e*q); if (2.0*p < (min1 < min2 ? min1 : min2)) { e=d; d=p/q; } else { d=xm; e=d; } } else { d=xm; e=d; } a=b; fa=fb; if (Math.abs(d) > tol1) b += d; else b += (xm > 0.0 ? Math.abs(tol1) : -Math.abs(tol1)); fb=f.func(b); } JOptionPane.showMessageDialog(null,"Maximum number of iterations are exceeded \n"+ " Result mAy not be valid","MAXIMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return b; } public static void main (String args[]) { double a,b;

a=Double.parseDouble(JOptionPane.showInputDialog(" Fonksiyonun alt snrn giriniz a : ")); b=Double.parseDouble(JOptionPane.showInputDialog(" Fonksiyonun st snrn giriniz b : ")); double r; fc f=new fc(); r= brent(f,a,b); JOptionPane.showMessageDialog(null," root value (x) : "+r+"\nfunction value f(x) : "+f.func(r), "Root finding by using brent method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Find the roots of f(x)=tan(x)*2x by using brent method. 2.10 ROOTS OF A COMPLEX NUMBER BY NEWTON_RAPHSON METHOD

Roots of a complex function can be found by Newton Raphson method. Complex variables are not defined in standart java lbraries, therefore should be user defined. A complex class defined by author will be listed here and used for definition of Newton-Raphson method. Program 2.10-1 complex class
import java.io.*; import java.text.*; import java.util.Locale; import java.util.*; import javax.swing.*; // giri k // class complex // complex number abstraction // class complex { // constructors public complex() { nreal=0; nimag=0; } public complex(double nre,double nim) { nreal=nre; nimag=nim; } public complex(int nre,int nim) { nreal=(double)nre; nimag=(double)nim;

} public complex(double numer) { nreal=numer; nimag=0; } public complex(complex value ) { nreal=value.real(); nimag=value.imaginary(); } // accessor functions public double real() { //return real part of the complex number return nreal; } public double imaginary() { //return imaginary part of the complex number return nimag; } public double R() { // Return radius of polar coordinate equivalent of complex number return Math.sqrt(nreal*nreal+nimag*nimag); } public double theta() { // Return angle of polar coordinate equivalent of complex number in radian return Math.atan2(nimag,nreal); } public double dtheta() { // Return angle of polar coordinate equivalent of complex number in degree return Math.atan2(nimag,nreal)*180.0/Math.PI; } // assignments public void assign(complex right) { nreal=right.real(); nimag=right.imaginary(); } public void add(complex right) { nimag = nimag + right.imaginary(); nreal = nreal + right.real(); }

public void add(double nr1) { nreal = nreal+nr1; } public void add(double nr1,double ni1) { nreal = nreal+nr1; nimag = nimag + ni1; } public void substract(complex right) { nimag = nimag - right.imaginary(); nreal = nreal - right.real(); } public void multiply(complex right ) { nreal = nreal*right.real() - nimag*right.imaginary(); nimag = nreal*right.imaginary() + nimag*right.real(); } public void divide(complex right ) { double a=nreal*nreal+nimag*nimag; nreal = ( nreal*right.real() + nimag*right.imaginary())/a; nimag = (-nreal*right.imaginary() + nimag*right.real())/a; } public static complex add(complex left, complex right) { // return sum of two complex numbers double r1=(left.real() + right.real()); double i1=(left.imaginary() + right.imaginary()); complex result; result=new complex(r1,i1); return result; } public static complex add(complex left, double right) { // return sum of two complex numbers double r1=(left.real() + right); double i1=left.imaginary(); complex result; result=new complex(r1,i1); return result; }

public static complex conjugate(complex z) {complex z1=new complex(z.real(),-z.imaginary()); return z1; } public static complex substract(complex left, complex right) { // return substraction of two complex numbers

complex result; result=new complex((left.real() - right.real()), (left.imaginary() - right.imaginary())); return result; } public static complex multiply(complex left, complex right) { // return multiplication of two complex numbers complex result; result=new complex ((left.real()*right.real() - left.imaginary()*right.imaginary()), (left.real()*right.imaginary() + left.imaginary()*right.real())); return result; } public static complex multiply(complex left, double right) {complex result; result=new complex((left.real()*right),(left.imaginary()*right)); return result; } public static complex divide(complex left, complex right) { // return division of two complex numbers double a=right.real()*right.real()+right.imaginary()*right.imaginary(); complex result; result=new complex ((left.real()*right.real() + left.imaginary()*right.imaginary())/a, (-left.real()*right.imaginary() + left.imaginary()*right.real())/a); return result; } public static complex divide(complex left, double right) { // return division of a complex number to a real number complex result; result=new complex((left.real()/right),(left.imaginary()/right)); return result; } public static complex pow(complex left, double right) { // return sum of two complex numbers double Rad,th; Rad=Math.pow(left.R(),right); th=right*left.theta(); complex result; result =new complex((Rad*Math.cos(th) ), (Rad*Math.sin(th) ) ); return result; } public static complex exp(complex left) { // exp(x+i*y) complex result; result =new complex((Math.exp(left.real())*Math.cos(left.imaginary()) ), (Math.exp(left.real())*Math.sin(left.imaginary()) )); return result; } public static complex exp(double left) { // exp(i*left) imaginary exponent complex result;

result =new complex(Math.cos(left), Math.sin(left) ); return result; } public static complex sqrt(complex left) { return pow(left,0.5); } public static double abs(complex left) { return left.R(); } public boolean smaller(complex left,complex right) { // less then comparison of two complex numbers return (left.R() < right.R()); } public boolean smaller_equal(complex left,complex right) { // less then and equal comparison of two complex numbers return (left.R() <= right.R()); } public boolean greater(complex left,complex right) { // greater then comparison of two complex numbers return left.R() > right.R(); } public boolean greater_equal(complex left,complex right) { // greater then and equal comparison of two complex numbers return left.R() >= right.R(); }

public boolean equal(complex left,complex right) { // equal comparison of two complex numbers return left.R() == right.R(); } public boolean not_equal(complex left,complex right) { // not equal comparison of two complex numbers return left.R() != right.R(); } public static String toString(double left, int w, int d) // converts a double to a string with given width and decimals. { NumberFormat df=NumberFormat.getInstance(Locale.US); df.setMaximumFractionDigits(d); df.setMinimumFractionDigits(d);

df.setGroupingUsed(false); String s = df.format(left); while (s.length() < w) s = " " + s; if (s.length() > w) { s = ""; for (int i=0; i<w; i++) s = s + "-"; } return s; } public static String toString(double left) {// converts a double to a string with a constant width and constant decimals. return toString(left,15,10);} public static String toString(complex value) { String b=""; if(Math.abs(value.imaginary())!=1) { if(value.imaginary() >= 0) b=b+"("+toString(value.real())+" + "+toString(value.imaginary())+"i )"; else b=b+"("+toString(value.real())+" - "+toString(-value.imaginary())+"i )"; } else { if(value.imaginary() >= 0) b=b+"("+toString(value.real())+" + i )"; else b=b+"("+toString(value.real())+" - i )"; } return b; } public String toString() { String b=""; if(Math.abs(imaginary())!=1) { if(imaginary() >= 0) b=b+"("+toString(real())+" + "+toString(imaginary())+"i )"; else b=b+"("+toString(real())+" - "+toString(-imaginary())+"i )"; } else { if(imaginary() >= 0) b=b+"("+toString(real())+" + i )"; else b=b+"("+toString(real())+" - i )"; } return b; } public static complex toComplex(String s) {

//bu metod compleks sayy ekrandan okur. //StringTokanizer ktphane snf bir stringi cmlelere ayrr String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } complex b=new complex(a[0],a[1]); return b; } // data areas public double nreal; public double nimag; };

Now a complex definition of Newton-Raphson method can be given. Our example function is f(x)=x3-1
Program 2.10-2 complex Newton-Raphson method
// // // complex number newton-raphson root finding // // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; abstract class cf_cx { abstract complex func(complex x); complex dfunc(complex x) {//complex numerical derivative double hh=0.001; complex h=new complex(hh,hh); complex he=new complex(-hh,-hh); complex h2=new complex(2.0*hh,2.0*hh); complex he2=new complex(-2.0*hh,-2.0*hh); complex xah=complex.add(x,h); complex xa2h=complex.add(x,h2); complex xeh=complex.add(x,he); complex xe2h=complex.add(x,he2); complex fe2h=complex.multiply(func(xe2h),(1.0/12.0)); complex feh=complex.multiply(func(xeh),(-8.0/12.0)); complex fah=complex.multiply(func(xah),(8.0/12.0)); complex fa2h=complex.multiply(func(xa2h),(-1.0/12.0)); complex df=complex.add(fe2h,complex.add(feh,complex.add(fah,fa2h)));

df=complex.divide(df,h); return df; } } class fa extends cf_cx { //one function, one independent variable // example function f=x*x*x-1 complex func(complex x) { complex f=complex.add(complex.multiply(x,complex.multiply(x,x)),-1.0); return f; } }

public class NA5A { public static complex newton(cf_cx f,complex x) { int nmax=50; double tolerance=1.0e-8; complex fx,dfx; for(int i=0;i<nmax;i++) { fx=f.func(x); dfx=f.dfunc(x); //x=complex.add(x,complex.multiply(complex.divide(fx,dfx),-1.0)); x=complex.substract(x,complex.divide(fx,dfx)); if(complex.abs(fx)<tolerance) { return x;} } JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded \n"+ " results may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static double[] enterdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fa f=new fa(); double x0[]=enterdata(" the first guess for the function : "); complex x=new complex(x0[0],x0[1]); complex r= newton(f,x); complex ff=f.func(r); JOptionPane.showMessageDialog(null," root : "+r.toString()+"\nFunction value : "+ff.toString(),

"Newton-Raphson complex root finding method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); }}

2.11 NEWTON-RAPHSON METHOD WITH SECOND DERIVATIVE

Consider the Taylor Formula, but this time assume that two term is remaining f ' ( xn ) f " ( xn ) f ( xn+1 ) = 0 = f ( xn ) + ( xn +! xn ) + ( xn +! xn ) 2 + .. 1! 1! When second term of the Taylor Formula is also taken into acount the Newton-Raphson Formula takes the form ; f ( xn ) * f ' ( xn ) xn+1 = xn f ' ( xn ) * f ' ( xn ) f ( x n ) * f " ( x n )
Program 2.11-1 Newton-Raphson method with second derivative first derivative is given as function, second derivative calculated numerically
// // second derivative corected Newton-Raphson method // function and first derivative should be defined // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= function to be defined ================ class fb extends f_x { public double func (double x) { return x*x*x+3.6*x-36.4;} } class dfb extends f_x { public double func (double x) { return 3.0*x*x+3.6;} } //=========================================================== public class NA5B { public static double newton1(f_x f,f_x df,double x) { int nmax=500; double dx=1.0e-3; double tolerance=1.0e-8;

double fx,dfx,fxm,dfxm,d2fx; for(int i=0;i<nmax;i++) { fx=f.func(x); fxm=f.func(x-dx); dfx=df.func(x); dfxm=df.func(x-dx); d2fx=-6.0/dx/dx*(fx-fxm)+2.0/dx*(2.0*dfx+dfxm); x-=fx*dfx/(dfx*dfx-fx*d2fx); if(Math.abs(fx)<tolerance) { return x;} } JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded \n"+ " results may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static void main (String args[]) { fb f=new fb(); dfb df=new dfb(); double x0; x0=Double.parseDouble(JOptionPane.showInputDialog(" first guess for x : ")); double r= newton1(f,df,x0); JOptionPane.showMessageDialog(null," root : "+r+"\nfunction value : "+f.func(r), "second derivative -Newton Raphson method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Program 2.11-2 Newton-Raphson method with second derivative first and second derivative calculated numerically
// // second derivative corected Newton-Raphson (secant) method // function should be defined // derivatives calculated numerically import java.util.*; import java.awt.*; import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class fb extends f_x { public double func (double x) { return x*x*x+3.6*x-36.4;} }

public class NA5C { public static double newton1(f_x f,double x) { int nmax=500; double h=1.0e-3; double hh=1.0/h; double tolerance=1.0e-8; double fx,dfx,fxm,dfxm,d2fx; for(int i=0;i<nmax;i++) { fx=f.func(x); fxm=f.func(x-h); dfx=(-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/12.0*hh; d2fx=(-f.func(x+2.0*h)+16.0*f.func(x+h)-30.0*f.func(x)+16.0*f.func(x-h)-f.func(x-2.0*h))/(12.0)*hh*hh; x-=fx*dfx/(dfx*dfx-fx*d2fx); if(Math.abs(fx)<tolerance) { return x;} } JOptionPane.showMessageDialog(null,"Maximum number of iterations are exceeded \n"+ " Result mAy not be valid","MAXIMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static void main (String args[]) { fb f=new fb(); double x0; x0=Double.parseDouble(JOptionPane.showInputDialog(" Fonksiyon deeri iin ilk tahmin : ")); double r= newton1(f,x0); JOptionPane.showMessageDialog(null," root : "+r+"\nfunction value : "+f.func(r), "second derivative -Newton Raphson method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

2.12 FINDING THE ROOTS LIMITS

In a one variable function, if a root to be find and if only one root existed in the given region boundaries of the function should cary invers sign. To further process this, if given region is divided up to smaller region and function values at the limits are cheched, the root area can be narrowed down. region can be enlarged to the side that it is estimated to have a root to ensure that at least one root is existed in a given region. Program 2.12-1 Root finding NA13B
// // // root findings in a given region // Dr. M. Turhan oban import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class fx extends f_x { public double func(double x) {return x*x*x-6.0*x*x+11.0*x-6.0;}

} public class NA13B { public static double[] enlarge(f_x f,double x1) {return enlarge(f,x1,(x1+0.1));} public static double[] enlarge(f_x f,double x1,double x2) { //kk olan bir blge bulana kadar verilen snr geniletir int NTRY=50; double a[]=new double[2]; double FACTOR=1.6; int j; double f1,f2; if (x1 == x2) System.out.println("Bad initial range in ZBRAC"); f1=f.func(x1); f2=f.func(x2); for (j=1;j<=NTRY;j++) { if (f1*f2 < 0.0) {a[0]=x1;a[1]=x2;return a;} if (Math.abs(f1) < Math.abs(f2)) f1=f.func(x1 += FACTOR*(x1-x2)); else f2=f.func(x2 += FACTOR*(x2-x1)); } {a[0]=x1;a[1]=x2;return a;} } public static double[][] rootlimits(f_x f,double x1,double x2,int n,int nbb) { // find root limits // n : number of subdivisions // x1,x2 : limit values of the root // nbb = number of roots in the serched region // int nb; int i; double x,fp,fc,dx; double xb[][]=new double[2][nbb]; nb=0; dx=(x2-x1)/n; x=x1; fp=f.func(x1); for (i=1;i<=n;i++) { x+=dx; fc=f.func(x); // eer kk olan blge bulunduysa..... if (fc*fp < 0.0 || fp==0) { xb[0][nb]=x-dx; xb[1][nb]=x; nb++; } fp=fc; if (nbb == nb) return xb; } if( nb == 0) JOptionPane.showMessageDialog(null,"search is completed no root is found"

,"SEARCH RESULT WARNING",JOptionPane.WARNING_MESSAGE); else if(nb<nbb) { JOptionPane.showMessageDialog(null,"search is completed "+nb+" root is found \n"+ "You searched for "+nbb+" root","SEARCH RESULT WARNING",JOptionPane.WARNING_MESSAGE); double xc[][]=new double[2][nb]; for (i=0;i<nb;i++) {xc[0][i]=xb[0][i];xc[1][i]=xb[1][i];} return xc; } return xb; } public static void main (String args[]) { double a[],x0,r; double dx=0.1; fx f=new fx(); a=enlarge(f,Double.parseDouble(JOptionPane.showInputDialog(" first guess x0 : "))); System.out.println(a[0]+" "+a[1]); r= NA13.brent(f,a[0],a[1]); JOptionPane.showMessageDialog(null," root value x : "+r+"\nfunction value f(x) : "+f.func(r)+"\nroot limits: a="+a[0]+" b="+a[1], "root finding with brent method : ",JOptionPane.PLAIN_MESSAGE); double b[][]=rootlimits(f,0.0,10.0,10,4); System.out.println(Matrix.toString(b)); b=rootlimits(f,b[0][0],b[1][0],10,4); System.out.println(Matrix.toString(b)); System.exit(0); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA13B 0.5 1.1760000000000002 1.000000000000000 2.000000000000000 2.000000000000000 3.000000000000000 1.000000000000000 1.100000000000000 > Terminated with exit code 0.

3.000000000000000 4.000000000000000

2.13 RIDDER METHOD

False position (Ragula Falsi) method was previously investigated. Ridder method is a modified form False position method. If a root existed between x1 and x2 Ridder method first evaluate the midpoint x3=(x1+x2)/2 and than solve a special conversion conversion factor eQ. This convertion linearize the equation. IT is basically find the root of the quadratic function f(x1)-2f(x3) eQ + f(x2) e2Q = 0 The root of this function can be calculated as eQ = f ( x3 ) + sign[ f ( x2 )] f ( x3 ) 2 f ( x1 ) f ( x2 ) f ( x2 )

Function sign(x) will be given the sign of x. If x is smaller than 0 it will be -1, if x is bigger than 0 it will be +1. Now the false position method is applied, not to the values f(x1), f(x3), f(x2), but to the values f(x1), f(x3)eQ, f(x2)e2Q, yielding a new guess for the root, x4. The overall formula is:
x4 =x3 + ( x3 x1 ) sign[ f ( x1 ) f ( x2 )] f ( x3 ) f ( x3 ) 2 f ( x1 ) f ( x2 )

Because the equation is quadratic, it supplies a quadratic approximation. In addition the root x4 should be in between x1 and x2 . Program 2.13-1 Root finding NA13C Ridder Method
// Ridder method import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //=============Function to be defined ================ class fb extends f_x { public double func (double x) { return x*x-2;} } public class NA13C { // Ridder method for solving roots of non-linear equations public static double SIGN(double a,double b) { return Math.abs(a)*b/Math.abs(b); } public static double ridder(f_x f, double x1, double x2) {double xacc=1.0e-9; return ridder(f, x1,x2,xacc); } public static double ridder(f_x f, double x1, double x2, double xacc) { int NR;

int MAXITER=200; double ABC=-1.0e30; int j; double answer,f_high,f_low,f_mid,fnew,s,x_high,x_low,x_mid,xnew; f_low=f.func(x1); f_high=f.func(x2); if ((f_low > 0.0 && f_high < 0.0) || (f_low < 0.0 && f_high > 0.0)) { x_low=x1; x_high=x2; answer=ABC; for (j=0;j<MAXITER;j++) { x_mid=0.5*(x_low+x_high); f_mid=f.func(x_mid); s=Math.sqrt(f_mid*f_mid-f_low*f_high); if (s == 0.0) return answer; xnew=x_mid+(x_mid-x_low)*((f_low >= f_high ? 1.0 : -1.0)*f_mid/s); if (Math.abs(xnew-answer) <= xacc) return answer; answer=xnew; fnew=f.func(answer); if (fnew == 0.0) return answer; if (SIGN(f_mid,fnew) != f_mid) { x_low=x_mid; f_low=f_mid; x_high=answer; f_high=fnew; } else if (SIGN(f_low,fnew) != f_low) { x_high=answer; f_high=fnew; } else if (SIGN(f_high,fnew) != f_high) { x_low=answer; f_low=fnew; } else System.out.println("we should not reach to this point"); if (Math.abs(x_high-x_low) <= xacc) return answer; } JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+ " result may not be valid","MAXMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); } else { if (f_low == 0.0) return x1; if (f_high == 0.0) return x2; JOptionPane.showMessageDialog(null,"Warning : No roots existed between the given limits ","NO ROOTS WARNING",JOptionPane.WARNING_MESSAGE); } return 0.0; } public static void main (String args[]) { fb f=new fb(); double a,b; a=Double.parseDouble(JOptionPane.showInputDialog(" Enter lower limit of the function a : ")); b=Double.parseDouble(JOptionPane.showInputDialog(" Enter upper limit of the function b : ")); double r; r= ridder(f,a,b); JOptionPane.showMessageDialog(null," root value(x) : "+r+"\nFunction value (f(x)) : "+f.func(r), "root finding by using Ridder method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

2.14 ROOTS OF THE SECOND AND THIRD DEGREE POLYNOMIALS

The roots of quadratic polynomials are well known. Similar to quadratic polynomials, third degree polynomials can also be solved analytical formulations. The basic difference is that the roots should be calculated as complex variables.
Program 2.14-2 Root of third degree polynomial (analytical formula)
import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.text.*; import java.util.Locale; //import Numeric; class NA13D { public static double[][] cubic_roots(double d[]) { // roots of a degree 3 polynomial // P(x)=a[3]*x^3+a[2]*x^2+a[1]*x+a[0]=0; // assuming all a is all real; // if more than 4 values are entered the remaining ones will be ignored double x[][]=new double[2][3]; double a,b,c; a=d[2]/d[3]; b=d[1]/d[3]; c=d[0]/d[3]; double Q,R,theta; Q=(a*a-3.0*b)/9.0; double x1,x2,x3,x4; x1=2.0*a*a*a; x2=-9*a*b; x3=27*c; x4=x1+x2+x3; R=x4/54.0; //System.out.println("Q="+Q+"R="+R+"x1="+x1+"x2="+x2+"x3="+x3+"x4="+x4); double Q3=Q*Q*Q; double R2=R*R; double qq; //System.out.println("Q3="+Q3+"R2="+R2+(R2<Q3)); double A,B; if(R2<Q3) { qq=-2.0*Math.sqrt(Q); theta=Math.acos(R/Math.sqrt(Q3)); x[0][0]=qq*Math.cos(theta/3.0)-a/3.0; x[0][1]=qq*Math.cos((theta-2.0*Math.PI)/3.0)-a/3.0; x[0][2]=qq*Math.cos((theta+2.0*Math.PI)/3.0)-a/3.0; }

else { A=-Math.pow((R+Math.sqrt(R2-Q3)),(1.0/3.0)); if(A==0) B=0; else B=Q/A; x[0][0]=(A+B)-a/3.0; x[0][1]=-0.5*(A+B)-a/3; x[1][1]=Math.sqrt(3.0)/2.0*(A-B); x[0][2]=-0.5*(A+B)-a/3; x[1][2]=-Math.sqrt(3.0)/2.0*(A-B); } return x; } public static complex[] cubic_rootsC(double c[]) { // roots of a degree 3 polynomial //return 3 complex roots double a[][]=new double[2][3]; a=cubic_roots(c); complex e[]=new complex[3]; for(int i=0;i<3;i++) e[i]=new complex(a[0][i],a[1][i]); return e; } public static double[] enterdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main(String args[]) throws IOException { String s="enter coefficients of the third degree polynomial a[0]..a[3] y=a[0]+a[1]*x+,a[2]*x^2+a[3}*x^3: "; double [] x0=enterdata(s); complex z[]=cubic_rootsC(x0); String s1=""; for(int i=0;i<3;i++) {s1+=complex.toString(z[i])+"\n";} JOptionPane.showMessageDialog(null,s1, "roots of the third degree polynomial : ",JOptionPane.PLAIN_MESSAGE); } }

2.15 COMPLEX ROOTS OF THE POLYNOMIALS BY LE GUERRE METHOD

In this definition, n is the degree of polynomial. The term n f ( x) f " ( x) g ( x) = [ f ' ( x)]2 n 1 n f ( xk ) xk +1 = xk ' f ( xk ) m (n 1) g ( xk )
The term

g ( xk ) in the equation can be complex (root of polynomials can be complex

numbers). Therefore the method will be written as complex variable method. Complex defined previously will be utilised here also. Program 2.15-1 Complex Lequerre method example NA13E
// // // compleks Lequerre method to find roots of the polynomials // // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; public class NA13E { static complex func(double a[],complex x) { // complex polynomial int n=a.length; complex y=new complex(a[n-1]); for(int i=n-2;i>=0;i--) {y=complex.add(complex.multiply(y,x),a[i]);} return y; } static complex dfunc(double a[],complex x) {//complex numeric derivative of a polynomial double hh=0.001; complex h=new complex(hh,0.0); complex he=new complex(-hh,0.0); complex h2=new complex(2.0*hh,0.0); complex he2=new complex(-2.0*hh,0.0); complex xah=complex.add(x,h); complex xa2h=complex.add(x,h2);

complex xeh=complex.add(x,he); complex xe2h=complex.add(x,he2); complex fe2h=complex.multiply(func(a,xe2h),(1.0/12.0)); complex feh=complex.multiply(func(a,xeh),(-8.0/12.0)); complex fah=complex.multiply(func(a,xah),(8.0/12.0)); complex fa2h=complex.multiply(func(a,xa2h),(-1.0/12.0)); complex df=complex.add(fe2h,complex.add(feh,complex.add(fah,fa2h))); df=complex.divide(df,h); return df; } static complex d2func(double a[],complex x) {//complex second numeric derivative of a polynomial double hh=0.001; complex h=new complex(hh,hh); complex hkare=complex.pow(h,2); complex he=new complex(-hh,-hh); complex h2=new complex(2.0*hh,2.0*hh); complex he2=new complex(-2.0*hh,-2.0*hh); complex xah=complex.add(x,h); complex xa2h=complex.add(x,h2); complex xeh=complex.add(x,he); complex xe2h=complex.add(x,he2); complex fe2h=complex.multiply(func(a,xe2h),(-1.0/12.0)); complex feh=complex.multiply(func(a,xeh),(4.0/3.0)); complex ff=complex.multiply(func(a,x),(-5.0/2.0)); complex fah=complex.multiply(func(a,xah),(4.0/3.0)); complex fa2h=complex.multiply(func(a,xa2h),(-1.0/12.0)); complex d2f=complex.add(fe2h,complex.add(feh,complex.add(ff,complex.add(fah,fa2h)))); d2f=complex.divide(d2f,hkare); return d2f; } public static double[] LeguerreD(double a[],complex x) { complex z=LeguerreC(a,x); double b[]=new double[2]; b[0]=z.nreal; b[1]=z.nimag; return b; } public static complex LeguerreC(double a[],complex x) { int n=a.length-1; double eps=1.0e-15; int m=50; int k=0; complex f0=func(a,x); complex f1=new complex(); complex f2=new complex(); complex g=new complex(); complex d1=new complex(); complex d2=new complex(); complex x0=x; complex x1=new complex((-a[0]/a[1]),0.0); do{ f1=dfunc(a,x0);

f2=d2func(a,x0); g=complex.add(complex.multiply(f1,f1),complex.multiply(complex.multiply(f0,f2),(-(double)n/(double)(n-1)))); d1=complex.add(f1,complex.multiply(complex.pow(g,0.5),(double)(n-1))); d2=complex.add(f1,complex.multiply(complex.pow(g,0.5),(double)(1-n))); if(complex.abs(d1)>=complex.abs(d2)) x1=complex.add(x0,complex.multiply(complex.divide(f0,d1),(double)(-n))); else x1=complex.add(x0,complex.multiply(complex.divide(f0,d2),(double)(-n))); x0=complex.add(x1,0.0); f0=func(a,x0); k++; }while(complex.abs(f0)>eps && k<m); if(complex.abs(f0)<eps) { return x1;} JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+ " result may not be valid","MAXIMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE); return x1; }

public static double[] verigir(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { double a[]={-75,-5,4,-4,-1,1}; int n=a.length-1; double x0[]=verigir(" first guess for the root give complex number with a space in between 1.0 0.5 "); complex x=new complex(x0[0],x0[1]); complex r=LeguerreC(a,x); complex ff=func(a,r); String s="root x = "+r.toString()+"\n"+"Function f(x) = "+ff.toString(); JOptionPane.showMessageDialog(null,s, "Leguerre method for complex root of polynomials : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

2.16 PROBLEMS
PROBLEM 1 A spherical tank of radius r is filled with a liquid of height h. The Volume of liquid (V) in the tank is given by he following eqaution V= h2(3r - h) / 3 If r = 1 m and V= 0.5 m3 find the liquid height Note : due to geometry of a sphere h can not be less than 0 and can not be bigger than D=2r. The maximum amount of liquid ths spherical container can be take is 4.18879 m3 (you do not need this data to solve

problem) PROBLEM 2 Find the roots of f(x) = x2 - 0.9x - 8.5 by using bisection method

PROBLEM 3 Calculate
7 by using one of the root finding method

PROBLEM 4 Find the roots of f ( x) = x 2 sin( x) 4 by using one of the root finding method

PROBLEM 5 Find the roots of f ( x) = cos( x) cosh( x) 1 by using one of the root finding method

PROBLEM 6 Find the roots of x0=4 and 7 x 8


f ( x) = x 4 8.6 x 3 35.5 x 2 + 464.4 x 998.46 by using one of the root finding method Try

PROBLEM 7 Function f ( x) = x 2 2e x x + e 2 x has only one real root. Find the root bu using Newton-Raphson method. Use as starting value x0=1

PROBLEM 8 Find all real roots of function f ( x) = x 4 7.223x 3 + 13.447 x 2 0.672 x 10.223

PROBLEM 9 Find roots of function f ( x) = tan( x) 2 x .

PROBLEM 10 For Turbulent flow inside of a pipe (Re>2300) friction coefficient inside of a pipe can be calculated by using Colebrook equation.

/ D 1 2.51 = 2.0 log10 + f 3.7 Re f

2.185 14.5 f = 4.0 1.737 ln 0.269 formundadr. ln(0.269 ) + Re Re D D


In Laminar flow(Re<2300) for round tbe coefficient of friction is f=64/Re. Re is Reynolds number, is surface rougness. Write a program to calulate total pressure drop in the pipe for a given Re number, pipe diameter, pipe length and local pressure drop coefficient. Total pressure drop in the pipe : DP= (K+f*L/D)**U2/2 DP : pressure drop K : local pressure drop coefficient f : friction coefficient L : pipe length D pipe diameter : liquid density U : velocity

PROBLEM 11

In round smooth tubes for turbulent flow region Fanning friction factor is given as:

1/

= 1.737 * ln (Re *
3

f ) 0 .4

This equation is valid for 4 * 10 < Re < 3 * 106 By using one of the root finding methods, calculate friction factor for Re=5000. You can utilize any root finding method you learned so far.
Note : You can assume that f is changing between 0,001 and 0.01 PROBLEM 12 van der Waals equation of state fort he gases :

P=

RT a 2 In this equation vb v

27 R 2Tc2 a= 64 Pc
b= RTc 8 Pc

Redlich ve Kwong equation of states

P=

RT a In this equation v b v(v + b)T 1 / 2

a = 0.42748

R 2Tc5 / 2 Pc
RTc Pc

b = 0.08664

O2, for oxygen Pc=5.04 MPa Tc=154.6 K M=31.999 kg/kmol . N2, for nitrogen Pc=3.39 MPa Tc=126.2 K M=28.013 kg/kmol. R=8314.5 J/kmolK Benedict -Webb-Rubin (BWR) equation of state

c 2 RT RTB0 A0 C 0 / T 2 RTb a a P= + + + + (1 + 2 )e v v6 v3T 2 v v2 v3 v


Gaz O2 N2 A0 1.49880 1.1925 B0 0.046524 0.0458 C0.10-6 0.0038617 0.0058891 A -0.040507 0.01490 B -0.00027963 0.00198154 c.10-6 -0.00020376 0.000548064 .103 0.008641 0.291545 .103 0.359 0.75

Develop a program to calculate P when T and v is given. By using van der Waals equation of state : gas : oxygen and nitrogen Redlich Kwong equation of state : gas : oxygen and nitrogen Benedict-webb rubin denklemi : equation of state : gas : oxygen Benedict-webb rubin denklemi : equation of state : gas : nitrogen RT E) Compare the result with ideal gas equation P = v A) B) C) D)

PROBLEM 13
Find the root of fuction f(x)=x e-x/2 + x3 + 1 with the limits of x1=-1, x2=0, x3=1 ny using muller method.

PROBLEM 14
Find the root of fuction f(x)=x4-20x3-25x2+100x+130 by using Leguerre method.

PROBLEM 15
Find the root of fuction f(x)=x2+1. PROBLEM 16 For a paralel flow hat exchanger Number of transfer unit(NTU) and effectiveness () equation given as follows Paralel akl bir s deitiricinin verimlilik deneyleri sonucu transfer nitesi says NTU=1.989 ve etkinlii = 0.574 olarak bulunmutur. Is deitirici etkinlii ()

= { 1 exp [ NTU (1 + C * )]

(1 + C * )

After the experiments for a specific heat exchanger NTU=1.989 and = 0.574 is found from the experiment a) Determine the range C* . For this start C*=0.1 and increase 0.1 in each step b) Calculate C* by using false position method. PROBLEM 17 Two end of 5 cm long plate with crossection of 0.1cmx20 cm plate is held at 80 C ve 60 C. The environmental temperature is T = 10 C, convective heat transfer coefficient h=15 W/m2 K. Bar cross sectional area A=210-4 m2, and perimeter P=0.402 m.In the experiment mid point(x=0.025 m) temperature is found as T=67.5 C. In this conditions, temperature distribution in the bar is given as

T T = 68.7 e 0.025 m + 11.3 e 0.025 m


In this equation m is defined as: m = ( h P
k A) 0.5

a) Find the range of m, stating from m=5, and increasing 5 at each step. b) Solve m by using Newton-Raphson method c) Determine thermal conductivity coefficient k from this value.

PROBLEM 18 The cost of the heat exchanger(M), is defined as a function of the length of heat excahger, L, diameter of the heat exchanger, D as: M=900 + 1100 D2,5 L + 320 D L In the optimasition study the length of the heat exchangr is found as L=1.3 m and the cost as M=1900 TL. Find the diameter of the heat exchanger. PROBLEM 19 An instrument 20000 TL worth is purcahesed with no first payment and 4000 TL annual payment for 6 years. Calculate the interest rate applied by using secant method.

A = P {i (1 + i ) n / [(1 + i ) n 1]}
In the equation P is the present worth, A is the annual payment, i is the interestrate, n is the number of payment years. PROBLEM 20 In order to heat a substance to a required temperature the energy equation is given as Q=

m * Cp * dT eitlii ile bulunmaktadr.

A substance with an initial temperature of 30 C, with a mass m=0.2 kg, is heated to some temperature and total energy spend is calculated as 3069.7 kJ . The specific heat of the substance is given with the equation Cp = 41.87+ 1.6748* T + 0.007537* T2 (kJ/kg K). Find the final temperature by using bi-section method.
PROBLEM 21 Find the roots of f(x) = x3-6x2+11x-6.1 by using Legurre method

3 LINEER SYSTEM OF EQUATIONS 3.1 LINEER SYSTEM OF EQUATIONS GENERAL INFORMATIONS


In more general form lineer system of equations are in the following form: a11 X1 + a12 X2 + a13 X3 + + a1n X3 = b1 a21 X1 + a22 X2 + a23 X3 + + a2n X3 = b2 a31 X1 + a32 X2 + a33 X3 + + a3n X3 = b3
.

an1 X1 + an2 X2 + an3 X3 + + ann X3 = bn The same set of equation can be given in matrix notation

[A]{X}={B} or

Ax = B
If matrix notation is written in open form

a11 a 21 .... a n1

a12 a 22 .... an2

.... a1n X 1 b1 ..... a 2 n X 2 b2 = .... .... ... .... .... a nn X n bn

In some sources the same system of equation can also be given as:

a11 a12 ..... a1n a21 a22 ..... a2 n .... .... .... .... an1 an 2 ..... ann

b1 a11 a b2 21 veya ..... .... bn an1

b1 b2 ..... ..... ..... ..... an 2 ..... ann bn a12 a22 ..... a1n ..... a2 n

In this set A and B is known values and X is unknown vector. Finding of X values are colled solution of system of equations. 3.2 GAUSS ELIMINATION METHOD Gauss elimination method based on the properties of the lineer system of equations. Changing the matrix rows, adding one row to another, or multiplying one row with a constant will not change the solution values of the system of equations. Therfore a process such as (row i )- constant * (row j ) can be easily applied without changing the solution set of the lineer system of equations. The purpose of this manupulations is to change the A matrix to a upper diagonal matrix. For example if n=5, system of equation will take the following upperdiagonal form

a '11 0 0 0 0

a '12 a ' 22 0 0 0

a '13 a ' 23 a ' 33 0 0

a '14 a ' 24 a ' 34 a ' 44 0

a '15 X 1 b'1 a ' 25 X 2 b' 2 a ' 35 X 3 = b' 3 a ' 45 X 4 b' 4 a ' 55 X 5 b' 5

A systematic elimnation processs is applied fort his purpose. For example in order to change element a21 to 0 in the equation a21 = 0 = a 21 C1 * a 11 a constant with the value C1=(a 21/a 11) is required. Then each member of the second row will be multiply with the same constant and take a new value. a 22 = a 22 C1*a 12 a 23 = a 23 C1*a 13
..

a 25 = a 25 C1*a15 b2=b2 C1*b1 At the end of this process a21 become zero. The same process repeat first repeat the whole rows under element a11 and all first column rows except the first one become zero. Then the same process repeat for the rows under the diagonal element a22 . A for structure doing this process will look like as follows
for(k=0;k<(n-1);k++) { for(i=k+1;i<n;i++) { C[k]=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-= C[k]*a[k][j]; } b[i] =b[i] - C[k]*b[k]; } }

After the lineer system of equation converted into upper diagonal form, unnowns are solved by using backsubstitution method. The set of the back-substitution for the set given above(n=5) will be: X5 = b5 / a55 X4 = (b4 a45*X5)/a44 X3 = (b3 a34*X4 a35*X5)/a33 X2 = (b2 a23*X3 a24*X3 a25*X5)/a22 X1 = (b1 a12*X2 a13*X3 a14*X4 a15*X5)/a11 Java code equivalent for an n by n matrix
x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; }

A matrix output class is also created in here to get easy outputs from the matrices. PROGRAM 3.2-1 matris output program matrixoutput.java to format the system of equation
import java.io.*;

import java.text.*; import java.util.Locale; public class matrixoutput { public static String toString(double left, int w, int d) // convert double into a w digit string with d digits in real part { NumberFormat df=NumberFormat.getInstance(Locale.US); df.setMaximumFractionDigits(d); df.setMinimumFractionDigits(d); df.setGroupingUsed(false); String s = df.format(left); while (s.length() < w) s = " " + s; if (s.length() > w) { s = ""; for (int i=0; i<w; i++) s = s + "-"; } return s; } public static String toString(double left) {return toString(left,25,15);} public static String toString(double[][] left,int w,int d) { //string equivalent of a double matris int n,m; String b; b=""; n=left.length; m=left[0].length; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { b=b+toString(left[i][j],w,d); } b=b+"\n"; } return b; } public static String toString(double[][] a,double b[],int w,int d) {//string equivalent of a lineear system of equation int n,m; String s; s=""; n=a.length; m=a[0].length; for(int i=0;i<n;i++) { s+=" |"; for(int j=0;j<m;j++) { s+=toString(a[i][j],w,d); } s+="| "+toString(b[i],w,d)+"| \n"; } return s; } public static String toString(double[][] a,double b[])

{//string equivalent of a lineear system of equation return toString(a,b,25,15); } public static String toStringT(double[][] left,int w,int d) { return toString(Matrix.Transpose(left),w,d);} public static String toStringT(double[][] left) { return toString(Matrix.Transpose(left),15,10);}

public static String toStringT(double[] left) { // returns a vertical string representation // of a double vector int n,m; String b; b=""; n=left.length; for(int i=0;i<n;i++) { b=b+toString(left[i])+"\n"; } return b; } public static String toString(double[] left) { // returns a horizontal string representation // of a double vector int n,m; String b; b=""; n=left.length; for(int i=0;i<n;i++) { b=b+toString(left[i])+"\t "; } b=b+"\n"; return b; }}

Now A complete Gauss elimination code can be developed PROGRAM 3.2-2 Gauss elimination
import java.io.*; class NA14 { public static double[] gauss(double a[][],double b[]) { int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; //gauss eleme int i,j,k; System.out.println("Original matrix :\n"+Matrix.toString(a)); for(k=0;k<(n-1);k++) { for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++)

{ a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } System.out.println("Eliminated matrix :\n"+Matrix.toString(a)); //back substituting x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; }

public static void main(String args[]) throws IOException { double a[][]={{1,2,5},{2,1,2},{3,1,1}}; double b[]={1,1,2}; double x[]=gauss(a,b); Text.print(x,"Gauss elimination solution matrix"); } }

output 3.2-1 Gauss elimination process


---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA14 Original matrix : 1.000000000000000 2.000000000000000 2.000000000000000 1.000000000000000 3.000000000000000 1.000000000000000 Eliminated matrix : 1.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000

5.000000000000000 2.000000000000000 1.000000000000000

2.000000000000000 -3.000000000000000 0.000000000000000 0.000000000000000

5.000000000000000 -8.000000000000000 -0.666666666666666 -0.666666666666666

> Terminated with exit code 0.

In the program following set of system of equation is taken :

1 2 5 X 0 1 2 1 2 X = 1 1 3 1 1 X 2 2
The solution set of this set of equation is 0,3 ve -1. As it is seen from the results, a small error is remained in the solution set. If the same elinination process is carried out by hand: column 0(column indices starts from 0), row 1(raw indices starts from 0) multiplier C1=a10/a00 = 2/1=2 a10 = a10 - C1 * a00 = a10 - (a10/a00 )* a00 = 2 - 2*1 = 0

a11 = a11 - C1 * a01 = a11 - (a10/a00 )* a01 = 1 - 2*2 = -3 a12 = a12 - C1 * a02 = a12 - (a10/a00 )* a02 = 2 - 2*5 = -8 b2 = b2 - C1 * b1 = b2 - (a10/a00 )* b1 = 1 2*1= -1 column0 raw 1 elimination is completed colun0 raw 2 elimination: C1=a20/a00 = 3/1=3 A20 = a20 C2 * a00 = a20 - (a20/a00 )* a00 = 3 - 3*1 = 0 a21 = a21 C2 * a01 = a21 - (a20/a00 )* a01 = 1 - 3*2 = -5 a22 = a22 C2 * a02 = a22 - (a20/a00 )* a02 = 2 - 3*5 = -14 b2 = b2 - C2 * b1 = b2 - (a10/a00 )* b1 = 1 3*1= -1 column 1, row 2 multiplier C C3=a21/a11 = -5/-3=(5/3) a21 = a21 C3 * a11 = a21 - (a21/a11 )* a11 = -5 (5/3)*(-3) = 0 a22 = a22 - C3 * a12 = a11 - (a21/a11 )* a01 = -14 (5/3)*(-8) = -0.6666666666666 b2 = b2 - C3 * b1 = b2 - (a21/a11 )* b1 = -1 (5/3)*(-1) = 0.6666666666666 system of equation becomes:

5 1 1 2 X 0 0 3 X = 8 1 1 0 0 0.666666666 X 2 0.666666666
Back-substitution process : X2 = b2/a22 = 0.6666666666/(-0.6666666666) = -1 X1 = (b1 - a12*X2)/a11 = (-1 - (-8)*(-1))/(-3) =(-9)/(-3)=3 X0 = (b0 - a01*X1 - a02*X2 )/a00 = (1 2*3-5*(-1))/1=0

X0 0 X = 3 1 X 2 1
Another example for Gauss elimination with detailed steps :

7 1 2 X 0 47 1 4 1 X = 19 1 3 15 20 X 2 87
A 7 1 3 c 0.14286 0.428571 c 3.517241 1 4 15 2 1 20 B 47 19 87

7 1 2 47 0 4.142857 0.71429 25.71429 0 14.57143 19.14286 66.85714 7 1 2 47 0 4.142857 0.71429 25.71429 0 0 21.65517 23.5862

X0 X1 X2

1.08917 6.019108 6.165605

3.3 GAUSS ELIMINATION WITH PARTIAL PIVOTING As it is seen from the previous example some error existed in the solution set. It is called round off error. When gauss elimination process is used as it is defined above a huge error might be created by the process. In order to minimize the round off error, absolute value of the multiplier terms should be as big as possible. In the calculation process of the multiplier if divisor is zero term will go to infinity, if divisior is relatively small it will be a very big number, and this will cause an extra error in digital computers. In order to prevent this in a degree, elimination raw is interchanged with the raw with maximum absolute value fort he multiplier term (in the diagonal). This process is called partial pivoting. Partial pivoting is shown in the example

1 2 5 X 0 1 2 1 2 X = 1 1 3 1 1 X 2 2
Before starting the gauss elimination the column 0 elements are compared and raw with the largest absolute value of this column is replace with elimination raw (raw 0)

3 1 1 X 0 2 2 1 2 X = 1 1 1 2 5 X 2 1
If this process is converted to computer code
double buyuk, dummy; for(k=0;k<(n-1);k++) { //pivotlama p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } }

After pivoting gauss elimination process is carried out as above PROGRAM 3.3-1 Gauss elimination with partial pivoting
import java.io.*; class NA15

{ public static double[] pivotlugauss(double a[][],double b[]) { //gauss elimination with partial pivoting int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss eleme int i,j,k,p,ii,jj; System.out.println("Original matrix :\n"+Matrix.toString(a)); for(k=0;k<(n-1);k++) { //pivoting p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } System.out.println("pivoted matrix : k="+k+"\n"+Matrix.toString(a)); //Solving gauss elimination for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } System.out.println("final upper triangular matrix : \n"+Matrix.toString(a)); //back substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static void main(String args[]) throws IOException { double a[][]={{1,2,5},{2,1,2},{3,1,1}}; double b[]={1,1,2}; //double a[][]={{9.915,2.205},{2.205,0.52}}; //double b[]={1.046,-2.042}; double x[]=pivotlugauss(a,b); Text.print(x,"Pivotlu Gauss eliminasyon metodu zm matrisi"); } }

Output 3.3-1 gauss elimination with partial pivoting


---------- Capture Output -------------------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA15 Original matrix : 1.000000000000000 2.000000000000000 5.000000000000000 2.000000000000000 1.000000000000000 2.000000000000000 3.000000000000000 1.000000000000000 1.000000000000000 pivoted matrix : k=0 3.000000000000000 2.000000000000000 1.000000000000000 pivoted matrix : k=1 3.000000000000000 0.000000000000000 0.000000000000000

1.000000000000000 1.000000000000000 2.000000000000000

1.000000000000000 2.000000000000000 5.000000000000000

1.000000000000000 1.666666666666667 0.333333333333333

1.000000000000000 4.666666666666667 1.333333333333334

final upper triangular matrix : 3.000000000000000 1.000000000000000 0.000000000000000 1.666666666666667 0.000000000000000 0.000000000000000

1.000000000000000 4.666666666666667 0.400000000000000

Sonutan da grld gibi sonu vektrnde bir nce grlen hata burada yok olmutur. Kk matris sistemlerinin zmnde gauss eleme yntemi ideal bir zmdr, ancak matris boyutu bydnde toplam hesap miktar ok fazlalatndan pratik avantajn yitirir. 3.4 GAUSS-JORDAN METHOD Gauss Jordan elimination method is similar to Gauss elimination with exception of a normalisation process before elimination and application of the elimination for all raws (instead of lower lows in gauss elimination) except the diagonal reference raw. Example program is given below PROGRAM 3.4.1 Gauss_Jordan elimination method
import java.io.*; import java.io.*; clasS NA16 { //Gauss-Jordan elimination process public static double[] gaussjordan(double a[][],double b[]) { //NO PVOTNG int n=b.length; int np1=n+1; double C[][]=new double[n][np1]; double x[]=new double[n]; double carpan=0; int i,j,k; for(i=0;i<n;i++) {for(j=0;j<n;j++){C[i][j]=a[i][j];};C[i][n]=b[i];} for(k=0;k<n;k++) { //gauss jordan elimination for(j=k+1;j<np1;j++) { C[k][j]/=C[k][k]; } for(i=0;i<n;i++) { if(i!=k)

{ carpan=C[i][k]; C[i][k]=0; for(j=k+1;j<np1;j++) { C[i][j]-=carpan*C[k][j]; } } } } for(i=0;i<n;i++) {x[i]=C[i][n];} return x; } public static void main(String args[]) throws IOException { double a[][]={{1,2,5},{2,1,2},{3,1,1}}; double b[]={1,1,2}; double x[]=gaussjordan(a,b); Text.print(x,"Gauss Jordan elimination method solution"); } }

output 3.4-1 Gauss_Jordan Elimination solution matrix

To enter a second example into the code :

3 1 1 X 0 2 1 4 1 X = 12 1 2 1 2 X 2 10
Following change can be apply to the program code
double a[][]={{3,1,-1},{1,4,1},{2,1,2}}; double b[]={2,12,10};

solution set will be: output 3.4-2 Gauss_Jordan Elimination solution matrix

Now if the same system of equation is solved by hand:

1 0.3333 0.3333 X 0 0.6667 1 X = 12 4 1 1 2 1 2 X 2 10 1 0.3333 0.3333 X 0 0.6667 0 3.6667 1.3333 X = 11.3333 1 2 1 2 X 2 10 1 0.3333 0.3333 X 0 0.6667 0 3.6667 1.3333 X = 11.3333 1 0 0.3333 2.6667 X 2 8.6667 1 0.3333 0.3333 X 0 0.6667 0 1 0.3636 X 1 = 3.0909 0 0.3333 2.6667 X 2 8.6667 0.4545 X 0 0.3636 0 1 0 1 0.3636 X 1 = 3.0909 0 0.3333 2.6667 X 2 8.6667 1 0 0.4545 X 0 0.3636 0 1 0.3636 X = 3.0909 1 0 0 2.5455 X 2 7.6365 1 0 0.4545 X 0 0.3636 0 1 0.3636 X = 3.0909 1 0 0 X 2 3.0000 1
0 X 0 1.0000 1 0 0 1 0.3636 X = 3.0909 1 0 0 1 X 2 3.0000

1 0 0 X 0 1.0000 0 1 0 X = 2.0001 1 0 0 1 X 2 3.0000


Solution vector

X 0 1.0000 X = 2.0001 1 X 2 3.0000


Exact solution :

X 0 1.0000 X = 2.0000 1 X 2 3.0000

Another example for Gauss-Jordan elimination with detailed steps :

7 1 2 X 0 47 1 4 1 X = 19 1 3 15 20 X 2 87
GaussJordan A 7 1 3 Normalisation 1 1 3 Elimination 1 0 0 Normalisation 1 0 0 Elimination 1 0 0 Normalisation 1 0 0 Elimination 1 0 0 1 4 15 2 1 20 0.142857 0.285714 4 1 15 20 0.142857 0.285714 4.142857 0.71429 14.57143 19.14286 0.142857 0.285714 1 0.17241 14.57143 19.14286 0 0.310345 1 0.17241 0 21.65517 0 0.310345 1 0.17241 0 1 0 1 0 0 0 1 X0 6.165605 X1 6.019108 X2 1.08917 X0 5.827586 X1 6.206897 X2 1.08917 X0 5.827586 X1 6.206897 X2 23.5862 X0 6.714286 X1 6.206897 X2 66.85714 X0 X1 X2 X0 X1 X2 X0 X1 X2 B 47 19 87 6.714286 19 87 6.714286 25.71429 66.85714

3.5 GAUSS-JORDAN METOD WITH PARTIAL PIVOTING Pivoting process can be applied to Gauss-Jordan elimination similar to Gauss elimination PROGRAM 3.5-1 Gauss_Jordan Elimination with partial pivoting
import java.io.*; class NA17 { public static double[][] C(double a[][],double b[]) {int n=a.length;int m=a[0].length;double c[][]=new double[n][m+1]; for(int i=0;i<n;i++){for(int j=0;j<m;j++){c[i][j]=a[i][j];};c[i][m]=b[i];} return c;

} public static double[][] C(double a[][],double b[][]) {int n=a.length;int o=b[0].length;int m=n+o;double c[][]=new double[n][m]; for(int i=0;i<n;i++) {for(int j=0;j<n;j++){c[i][j]=a[i][j];}; for(int j=n;j<m;j++){c[i][j]=b[i][j];} } return c; } public static double[][] gaussjordanwithpivot(double a[][]) { //pivotlu int n=a.length; int m=a[0].length; int o=m-n; double x[][]=new double[n][o]; double carpan=0; double toplam=0; double buyuk; double dummy=0; int i,j,k,p,ii,jj; i=0; for(k=0;k<n;k++) { //pivoting p=k; buyuk=Math.abs(a[k][k]); for(ii=0;ii<n;ii++) { if(i!=k) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } } if(p!=k) { for(jj=k;jj<m;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } } //gauss jordan elimination for(j=k+1;j<m;j++) { a[k][j]/=a[k][k]; } a[k][k]=1.0; for(i=0;i<n;i++) { if(i!=k) { carpan=a[i][k]; a[i][k]=0; for(j=k+1;j<m;j++) {a[i][j]-=carpan*a[k][j]; } } } } for(jj=0;jj<o;jj++) {for(i=0;i<n;i++) {x[i][jj]=a[i][n+jj];}} return x; } public static void main(String args[]) throws IOException { double a[][]={{3,0,2,8},{0,0,2,-7},{6,2,8,26}}; //double a[][]=Text.readDouble_Table(); double x[][]=gaussjordanwithpivot(a); Text.print(x,"Gauss Jordan elimination with partial pivoting");

} }

output 3.5-1Gauss_Jordan Elimination with partial pivoting

3.6 LU-DECOMPOSITION- GAUSS (DOLTTLE) METHOD In Gauss or Gauss-Jordan elimination processes left hand matrix A and right hand vector b is eliminated together. Therefore if a new left hand side vector b1 will be considered to be used in as second set with the same A matrix, Matrix A is reprocessed. This will spend valuable calculation time for a repeated process. In order to avoid this repetation, Matrix A is decomposed into an upper triangle(U) and a lower triangle (L) matrices so that multiplication of this two matricess will give the original A matrix For example for N=5

1 0 l 21 1 [ L] = l31 l32 l41 l42 l51 l52 u11 u12 0 u 22 [U ] = 0 0 0 0 0 0

0 0 1 l43

0 0 0 1

l53 u13 u23 u33 0


0

l54 u14 u 24 u34 u 44


0

0 0 0 0 1

u15 u 25 u35 u 45 u55

To solve system of equation with a B vector following process applies [A]=[L][U] [A]{X}={B} [L][U]{X}={B} [L]{D}={B} [U]{X}={D} In Gauss(Doolittle) decomposition process matrix U is the same as Gauss elimination upper triangualar matrix. L matrix is consist of the multipliers calculated in Gauss elimination process. Due to diagonal element of matrix L is 1, LU matrix can be represented in one matrix if space saving is desired. Exemple code is given below

PROGRAM 3.6-1 Gauss (Dolittle) Elemimination & decomposition


import java.io.*; import javax.swing.*; class NA18 { public static double[][] gaussLU(double ai[][]) { int n=ai.length; double x[]=new double[n]; double a[][]=new double[n][n]; double carpan=0; double toplam=0; //gauss eleme int i,j,k; for(i=0;i<n;i++) for(j=0;j<n;j++){a[i][j]=ai[i][j];}; for(k=0;k<(n-1);k++) { for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=carpan;///L eleman for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } } } return a; }

public static double[] yerine_koyma(double ai[][],double bi[]) { //gauss LU zmyle LU indirgenmesi yaplm Matris kullanarak lineer //denklem sistemi kkleri int n=ai.length; double toplam; double x[]=new double[n]; double a[][]=new double[n][n]; double b[]=new double[n]; int i,j; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};b[i]=bi[i];} System.out.println("a=\n "+Matrix.toString(a)); System.out.println("b=\n "+Matrix.toStringT(b)); for(i=1;i<n;i++) {toplam=b[i];for(j=0;j<i;j++){toplam-=a[i][j]*b[j];};b[i]=toplam;} System.out.println("d=\n "+Matrix.toStringT(b)); x[n-1]=b[n-1]/a[n-1][n-1]; for(i=(n-2);i>=0;i--) {toplam=0; for(j=i+1;j<n;j++) {toplam+=a[i][j]*x[j];}; x[i]=(b[i]-toplam)/a[i][i]; } System.out.println("d=\n "+Matrix.toStringT(x)); return x; } public static double[][] yerine_koyma(double ai[][],double bi[][]) { int n=ai.length; int m=bi[0].length; double toplam; double x[][]=new double[n][m]; double a[][]=new double[n][n]; double b[][]=new double[n][m];

int i,j,k; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};for(k=0;k<m;k++) {b[i][k]=bi[i][k];}} System.out.println("a=\n "+Matrix.toString(a)); System.out.println("b=\n "+Matrix.toStringT(b)); for(k=0;k<m;k++) { for(i=1;i<n;i++) {toplam=b[i][k];for(j=0;j<i;j++){toplam-=a[i][j]*b[j][k];};b[i][k]=toplam;} System.out.println("d=\n "+Matrix.toStringT(b)); x[n-1][k]=b[n-1][k]/a[n-1][n-1]; for(i=(n-2);i>=0;i--) {toplam=0; for(j=i+1;j<n;j++) {toplam+=a[i][j]*x[j][k];}; x[i][k]=(b[i][k]-toplam)/a[i][i]; } }//kin sonu System.out.println("d=\n "+Matrix.toStringT(x)); return x; } public static double [] AXB(double a[][],double b[]) { //denklem sistemi zm b vektr iin int n=a.length; double c[][]=gaussLU(a); Matrix.toString(c); double s[]=yerine_koyma(c,b); return s; } public static double [][] AXB(double a[][],double b[][]) {//denklem sistemi zm b matrisi iin int n=a.length; double c[][]=gaussLU(a); Matrix.toString(c); double s[][]=yerine_koyma(c,b); return s; } public static void main(String args[]) throws IOException { double a[][]={{1,2,5},{2,1,2},{3,1,1}}; double b[]={1,1,2}; JPanel y=new JPanel(); Text.add(y,a,"Matrix A"); Text.addT(y,b,"Matrix b"); double x[]=AXB(a,b); Text.addT(y,x,"Solution Matrix"); Text.print(y,"Solution of Gauss(Doolittle) decomposition and elimination"); } }

PROGRAM 3.6-2 Gauss (Dolittle) elimination with partial pivoting


import java.io.*;

import javax.swing.*; class NA19 { public static void pivot(double a[][],int o[],double s[],int k) {// Gauss(Dolittle) LU decomposition with partial pivoting int n=a.length; int dummy1=0; double buyuk; double dummy=0; //pivotlama int p=k; buyuk=Math.abs(a[o[k]][k]); for(int ii=k+1;ii<n;ii++) { dummy=Math.abs(a[o[ii]][k]/s[o[ii]]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } dummy1=o[p]; o[p]=o[k]; o[k]=dummy1; } public static int[] sirali_indeks(int n) { // sral rakamlar tayan bir referans vektr oluturur int o[]=new int[n]; for(int i=0;i<n;i++) {o[i]=i;} return o; } public static double[][] gaussLU(double ai[][],int o[]) { //ksmi pivotlu gauss eleme yntemi int n=ai.length; double a[][]=new double[n][n]; double s[]=new double[n]; double carpan=0; double toplam=0; double absaij; //gauss eleme int i,j,k,p,ii,jj; //System.out.println("Orijinal Matris :\n"+Matrix.toString(ai)); for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];}} for(i=0;i<n;i++) { o[i]=i;s[i]=Math.abs(a[i][0]); for(j=1;j<n;j++) { absaij=Math.abs(a[i][j]); if(absaij>s[i]) {s[i]=absaij;} } } for(k=0;k<(n-1);k++) { pivot(a,o,s,k); //pivot referansn deitir //System.out.println("pivotlu Matris : k="+k+"\n"+Matrix.toString(a)); //gauss elemeyi zme for(i=k+1;i<n;i++) { carpan=a[o[i]][k]/a[o[k]][k]; a[o[i]][k]=carpan; for(j=k+1;j<n;j++) { a[o[i]][j]-=carpan*a[o[k]][j]; } } } return a; } public static double[] yerine_koyma(double ai[][],double bi[]) { int n=ai.length;

return yerine_koyma(ai,bi,sirali_indeks(n)); } public static double[][] yerine_koyma(double ai[][],double bi[][]) { int n=ai.length; return yerine_koyma(ai,bi,sirali_indeks(n)); } public static double[] yerine_koyma(double ai[][],double bi[],int o[]) { //gauss LU zmyle LU indirgenmesi yaplm Matris kullanarak lineer //denklem sistemi kkleri int n=ai.length; double toplam; double x[]=new double[n]; double a[][]=new double[n][n]; double b[]=new double[n]; int i,j; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};b[i]=bi[i];} //System.out.println("a=\n "+Matrix.toString(a)); //System.out.println("b=\n "+Matrix.toStringT(b)); for(i=1;i<n;i++) {toplam=b[o[i]];for(j=0;j<i;j++){toplam-=a[o[i]][j]*b[o[j]];};b[o[i]]=toplam;} //System.out.println("d=\n "+Matrix.toStringT(b)); x[n-1]=b[o[n-1]]/a[o[n-1]][n-1]; for(i=(n-2);i>=0;i--) {toplam=0; for(j=i+1;j<n;j++) {toplam+=a[o[i]][j]*x[j];}; x[i]=(b[o[i]]-toplam)/a[o[i]][i]; } //System.out.println("d=\n "+Matrix.toStringT(x)); return x; } public static double[][] yerine_koyma(double ai[][],double bi[][],int o[]) { int n=ai.length; int m=bi[0].length; double toplam; double x[][]=new double[n][m]; double a[][]=new double[n][n]; double b[][]=new double[n][m]; int i,j,k; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};for(k=0;k<m;k++) {b[i][k]=bi[i][k];}} //System.out.println("a=\n "+Matrix.toString(a)); //System.out.println("b=\n "+Matrix.toStringT(b)); for(k=0;k<m;k++) { for(i=1;i<n;i++) {toplam=b[o[i]][k];for(j=0;j<i;j++){toplam-=a[o[i]][j]*b[o[j]][k];};b[o[i]][k]=toplam;} //System.out.println("d=\n "+Matrix.toStringT(b)); x[n-1][k]=b[o[n-1]][k]/a[o[n-1]][n-1]; for(i=(n-2);i>=0;i--) {toplam=0; for(j=i+1;j<n;j++) {toplam+=a[o[i]][j]*x[j][k];}; x[i][k]=(b[o[i]][k]-toplam)/a[o[i]][i]; } }//kin sonu //System.out.println("d=\n "+Matrix.toStringT(x)); return x; } public static double [] AXB(double a[][],double b[])

{ //denklem sistemi zm b vektr iin int n=a.length; int q[]=new int[n]; double c[][]=gaussLU(a,q); double s[]=yerine_koyma(c,b,q); return s; } public static double [][] AXB(double a[][],double b[][]) {//denklem sistemi zm b matrisi iin int n=a.length; int q[]=new int[n]; double c[][]=gaussLU(a,q); double s[][]=yerine_koyma(c,b,q); return s; } public static void main(String args[]) throws IOException { double a[][]={{3,-0.1,-0.2},{0.1,7,-0.3},{0.3,-0.2,10}}; JPanel y=new JPanel(); Text.add(y,a,"a matrisi"); double b[]={7.85,-19.3,71.4}; Text.addT(y,b,"b matrisi"); double x[]=AXB(a,b); Text.addT(y,x,"Solution Matrix"); Text.print(y,"Solution of Gauss(Doolittle) decomposition and elimination"); } }

Output 3.6-1 Ksmi Pivotlu LU Gauss (Dolittle) Eleme yntemi

3.7 CRAUT LU DECOMPOSITION METHOD In the Gauss (Dolittle) LU decomposition diagonal of matrix L was setted as 1, In the Craut LU decompositon diagonal of matrix U is given as 1. For example for a 5 by 5 system

l11 l 21 [ L] = l31 l41 l51

0 0

0 0

l22 l32 l42 l52

l33 0 l43 l44 l53 l54

0 0 0 0 l55

1 u12 0 1 [U ] = 0 0 0 0 0 0

u13 u14 u23 u24 1 u34 0 1 0 0

u15 u25 u35 u45 1

Solution process is similar [A]=[L][U] [A]{X}={B} [L][U]{X}={B} [L]{D}={B} [U]{X}={D} Algorithm of Craut is different compare to Dolittle li,1 = ai,1 loop i=1,2,.,n

u1, j =

a1, j l11

loop i=2,3,.,n

loop j=2,3,.,n 1

li , j = ai , j li ,k uk , j loop k=j,j+2,.,n
k =1

j 1

ui , j =
End of loop

a j ,k l j ,i ui ,k
i =1

j 1

l jj

loop k=j+1,j+2,.,n

l n , n = a n ,n l n , k u k , n
k =1

n 1

Algorithm is converted to programming code in the following example PROGRAM 3.7-1 LU Craut Decomposition method
import java.io.*; class NA20 { public static void pivot(double a[][],int o[],double s[],int k) {// Craut LU decomposition int n=a.length; int dummy1=0; double buyuk; double dummy=0; //pivoting int p=k;

buyuk=Math.abs(a[o[k]][k]); for(int ii=k+1;ii<n;ii++) { dummy=Math.abs(a[o[ii]][k]/s[o[ii]]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } dummy1=o[p]; o[p]=o[k]; o[k]=dummy1; } public static int[] index(int n) { int o[]=new int[n]; for(int i=0;i<n;i++) {o[i]=i;} return o; } public static double[] back_substitution(double ai[][],double bi[]) { int n=ai.length; return back_substitution(ai,bi,index(n)); } public static double[][] back_substitution(double ai[][],double bi[][]) { int n=ai.length; return back_substitution(ai,bi,index(n)); } public static double[] back_substitution(double ai[][],double bi[],int o[]) { int n=ai.length; double toplam; double x[]=new double[n]; double a[][]=new double[n][n]; double b[]=new double[n]; int i,j; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};b[i]=bi[i];} for(i=1;i<n;i++) {toplam=b[o[i]];for(j=0;j<i;j++){toplam-=a[o[i]][j]*b[o[j]];};b[o[i]]=toplam;} x[n-1]=b[o[n-1]]/a[o[n-1]][n-1]; for(i=(n-2);i>=0;i--) {toplam=0; for(j=i+1;j<n;j++) {toplam+=a[o[i]][j]*x[j];}; x[i]=(b[o[i]]-toplam)/a[o[i]][i]; } return x; } public static double[][] back_substitution(double ai[][],double bi[][],int o[]) { int n=ai.length; int m=bi[0].length; double toplam; double x[][]=new double[n][m]; double a[][]=new double[n][n]; double b[][]=new double[n][m]; int i,j,k;

for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};for(k=0;k<m;k++) {b[i][k]=bi[i][k];}} for(k=0;k<m;k++) { for(i=1;i<n;i++) {toplam=b[o[i]][k];for(j=0;j<i;j++){toplam-=a[o[i]][j]*b[o[j]][k];};b[o[i]][k]=toplam;} x[n-1][k]=b[o[n-1]][k]/a[o[n-1]][n-1]; for(i=(n-2);i>=0;i--) {toplam=0; for(j=i+1;j<n;j++) {toplam+=a[o[i]][j]*x[j][k];}; x[i][k]=(b[o[i]][k]-toplam)/a[o[i]][i]; } } return x; } public static double[][] crautLU(double c[][]) { //craut LU decomposition without pivoting int n=c.length; double sum; int i,j,k; double d[][]=new double[n][n]; for(i=0;i<n;i++) {for(j=0;j<n;j++) d[i][j]=c[i][j];} for(j=1;j<=n;j++) { for(i=1;i<j;i++) { sum=d[i-1][j-1]; for(k=1;k<i;k++) { sum-=d[i-1][k-1]*d[k-1][j-1];} d[i-1][j-1]=sum; } for(i=j;i<=n;i++) { sum=d[i-1][j-1]; for(k=1;k<j;k++) {sum-=d[i-1][k-1]*d[k-1][j-1];} d[i-1][j-1]=sum; } if(j != n) { for(i=j+1;i<=n;i++) d[i-1][j-1]/=d[j-1][j-1]; } } System.out.println(Matrix.toString(d)); return d; }

public static void main(String args[]) throws IOException { double a[][]={{2,-6,1},{-1,7,-1},{1,-3,2}};

double b[]={12,-8,16}; double c[][]= crautLU(a); double x[]=back_substitution(c,b); Text.print(x,"craut LU decomposition method"); } }

3.8 CHOLESKY SYMMETRIC MATRIX LU DECOMPOSITION METHOD Cholesky method is similar to Dolittle(Gauss) LU method. This method is applied to symmetric matrix. Therefore, instead of LU, it decomposes to [L][L]T form. [A]=[L][L]T For the decomposition process following steps are used
lki = aki lij lkj
j =1 i 1

lii

k = 1,2,......, k 1

and
2 lkk = akk lkj

Backsubstitution is similar to Dolittle method. PROGRAM 3.8-1 Cholesky symmetric matrix decomposition method
import java.io.*; class NA22 { public static double[][] choleskyLU(double c[][]) { //cholesky symmetric matrix LU //symmetric matrices only double factor=0; int i,j,k,p,ii,jj; int n=c.length; double d[][]=new double[n][n]; double dummy; double sum; for(i=0;i<n;i++) {for(j=0;j<n;j++) d[i][j]=c[i][j];} for(k=1;k<=n;k++) { for(i=1;i<=(k-1);i++) {sum=0;; for(j=1;j<=(i-1);j++) { sum+=d[i-1][j-1]*d[k-1][j-1]; } d[k-1][i-1]=(d[k-1][i-1]-sum)/d[i-1][i-1];

} sum=0.0; for(j=1;j<=(k-1);j++) { sum+=d[k-1][j-1]*d[k-1][j-1]; } d[k-1][k-1]=Math.sqrt((d[k-1][k-1]-sum)); for(j=k+1;j<=n;j++) d[k-1][j-1]=0.0; } return d; } public static double[] substitutecholesky(double c[][],double b[]) { //solving of system of equation by using cholesky decomposed matrix int i,j; double U[][]=T(c); double L[][]=c; int n=b.length; double sum=0; double x[]=new double[n]; double d[]=new double[n]; //forward substituting double toplam=0; for(i=0;i<n;i++) {toplam=0; for(j=0;j<i;j++) {toplam+=L[i][j]*d[j];} d[i]=(b[i]-toplam)/L[i][i]; } //backward substitution x[n-1]=d[n-1]/U[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=U[i][j]*x[j];} x[i]=(d[i]-toplam)/U[i][i]; } return x; } public static double[][] T(double [][] left) { //transpose matrix (if A=a(i,j) T(A)=a(j,i) int i,j; int n=left.length; int m=left[0].length; double b[][]; b=new double[m][n]; for(i=0;i<n;i++) { for(j=0;j<m;j++) { b[j][i]=left[i][j]; } } return b; }

public static void main(String args[]) throws IOException { double a[][]={{4,-1,1},{-1,4.25,2.75},{1,2.75,3.5}}; double b[]={1,1,1}; double L[][]=choleskyLU(a); System.out.println("Matris L : \n"+Matrix.toString(L)); double x[]=substitutecholesky(L,b); Text.print(x,"Cholesky LU symmetric matrix solution"); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA22 Matris L : 2.000000000000000 0.000000000000000 -0.500000000000000 2.000000000000000 0.500000000000000 1.500000000000000

0.000000000000000 0.000000000000000 1.000000000000000

Let us solve the same problem step by step by hand:

1 1 4 1 4.25 2.75 1 2.75 3.5


l11 = a11 = 4 = 2

l21 =

a21 1 = = 0.5 l11 2

2 l22 = a22 l21 = 4.25 (0.5) 2 = 2

l31 = l32 =

a31 1 = = 0.5 l11 2 a32 l21 l31 2.75 (0.5) * 0.5 = = 1.5 l22 2

2 2 l33 = a33 l31 l32 = 3.5 0.52 1.52 = 1 so cholesky decomposed L matrix :

0 0 2 0 .5 2 0 [L] = 0 .5 1 .5 1 2 0 .5 0 .5 [U ] = [L] = 0 2 1.5 0 0 1 [L]{D} = {B}


T

0 0 d1 1 2 0.5 2 0 d = 1 2 0.5 1.5 1 d 3 1


d1=1/2=0.5 d2=(1-(-0.5)*0.5)/2=0.625 d3=(1-0.5*0.5-1.5*0.625)/1=-0.1875

2 0.5 0.5 x1 0.5 0 2 1.5 x2 = 0.625 0 0 1 x 3 0.1875


x3=-0.1875 x2=(0.625-1.5*(-0.1875))/2=0.453125 x1=(0.5-(-0.5)*0.453125-0.5*(-0.1875))/2=0.41015625 3.9 JACOBI GAUSS -SEIDEL AND SUCCESSIVE RELAXATION METHODS In this section, iterative solution methods will be described. Iterative methods could be important in solution of big matrices. Gauss elimination requires n3/3, Gauss-Jordan method requires n3/2 process. When n is big, the total number of process will climb up. In this case the total number of processes in iterative methods will be much smaller. The simplest of the iterative methods are Jacobi method. In this method, a set of x solution vector is selected and substituted into the system of equation in the following form :

xik +1 =

1 (bi Aij x k ) j Aij j i

1 i n

When a new set of x values is found, it will be substituted in the the equation as the next iteration step. To ensure the convergence matrix should be diagonally dominant

i =1

Aii Ai , j
i =1 j =1 j i

Diagonal dominancy is the important condition for all iterative formulations. Unfortunately, very big matrices solved in engineering problems are in general fits to this definition. In order to reduce number of iteration steps, every new calculated x value in the x vector set can be substited into the equation as soon as it is calculated and is not waited the complete set to be calculated as in Jacobi method. This form of iterative process is called Gauss-Seidel iteration.

xik +1 =

i 1 n 1 (bi Aij x k +1 Aij x k ), j j Aij j =1 j =i +1

1 i n

Gauss-Seidel and Jacobi methods can be merged to define an intermediate method between two by defining a relaxation coefficient, , in the equation.

xik +1 = (1 ) xik +

Aij

(bi Aij x k +1 Aij x k ), j j


j =1 j =i +1

i 1

1 i n

When is equal to one equation coverged to the Gauss-Seidel method.

PROGRAM 3.9-1 Iterative relaxation method


import java.io.*; class NA21 { public static double [] gauss_seidel(double a[][],double b[]) { double lambda=1.2; int n=a.length; //starting x vector is zero double x[]=new double[n]; return gauss_seidel(a,b,x,lambda); } public static double [] gauss_seidel(double a[][],double b[],double x[]) { double lambda=1.2; int n=a.length; return gauss_seidel(a,b,x,lambda); } public static double [] gauss_seidel(double a[][],double b[],double x[],double lambda) { //lambda is the relaxation coefficient int n=a.length; int imax=500;//maximum number of iterations double es=1.0e-20; double dummy=0; double ea=1e-1; double sum=0; double old=0; int iter=0; int sentinel=0; System.out.print("iter = "+(iter+1)+" "); for(int i=0;i<n;i++) { dummy=a[i][i]; for(int j=0;j<n;j++) {a[i][j]/=dummy;} b[i]/=dummy; } for(int i=0;i<n;i++) { sum=b[i]; for(int j=0;j<n;j++) {if(i!=j)sum-=a[i][j]*x[j];} x[i]=sum; System.out.print(x[i]+ " "); } System.out.println(); iter++; do { sentinel=1; System.out.print("iter = "+(iter+1)+" "); for(int i=0;i<n;i++) { old=x[i]; sum=b[i];

for(int j=0;j<n;j++) {if(i!=j) sum-=a[i][j]*x[j];} x[i]=lambda*sum+(1.0-lambda)*old; if(sentinel==1 && x[i]!=0.0 ) {ea=Math.abs((x[i]-old)/x[i])*100.0;} if(ea<es) sentinel=0; System.out.print(x[i]+ " "); } System.out.println(); iter++; }while(sentinel==1 && iter<imax); if(iter>=imax) System.out.println("Maximum number of iteration is exceed, result might not be valid iter = "+iter); return x; } public static void main(String args[]) throws IOException { double a[][]={{3,-0.1,-0.2},{0.1,7,-0.3},{0.3,-0.2,10}}; double b[]={7.85,-19.3,71.4}; double x[]={0,0,0}; double lambda=0.0; Text.print(gauss_seidel(a,b),"iterative relaxation method"); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA21 iter = 1 2.6166666666666667 -2.7945238095238096 7.005609523809524 iter = 2 3.0653344761904764 -2.4419267678911565 6.997919811665852 iter = 3 2.9890896189795266 -2.5115345924328882 7.000531981165179 iter = 4 3.0017632509999936 -2.49769594964207 6.999885423939555 iter = 5 2.999730345729483 -2.5004620799100574 7.000021532847987 iter = 6 3.00003717028554 -2.4999071138192726 6.999996584568461 iter = 7 2.999996008155598 -2.5000186844552923 7.000000378365779 iter = 8 3.0000000812599312 -2.4999962450431576 7.000000011520452 iter = 9 3.000000134867924 -2.500000752710909 6.999999974775603 iter = 10 2.999999940900027 -2.4999998497419305 7.000000010778672 iter = 11 3.000000018692611 -2.500000029817727 6.999999996455708 iter = 12 2.9999999947852256 -2.4999999941293365 7.0000000010374865 iter = 13 3.00000000136078 -2.500000001144104 6.999999999716058 iter = 14 2.9999999996593645 -2.499999999779943 7.0000000000743325 iter = 15 3.000000000082876 -2.500000000041609 6.999999999981153 iter = 16 2.9999999999802522 -2.4999999999923093 7.000000000004666 iter = 17 3.0000000000046305 -2.500000000001377 6.999999999998867 iter = 18 2.9999999999989284 -2.4999999999997646 7.000000000000272 iter = 19 3.000000000000245 -2.500000000000037 6.999999999999938 iter = 20 2.9999999999999445 -2.4999999999999956 7.000000000000015 iter = 21 3.0000000000000124 -2.5000000000000004 6.999999999999997 iter = 22 2.999999999999997 -2.5 7.000000000000001 iter = 23 3.0000000000000004 -2.5 7.0

3.10 METHODS FOR BAND MATRICES A three-band matrix can be shown as:
f1 e 2 g1 f2 e3 g2 f3 e4 g3 f4 e5 g4 f5 g5 x1 r1 x r 2 2 x3 r3 x4 = r4 x5 r5 . . g n1 xn1 rn1 f n xn rn

.
en1

.
f n1 en

This kind of matrix can be solved by using decomposition back and front substitution processes. Following form of basic solition process is called Thomas algorithm Decomposition step : for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } Forward substitution : for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } Backward substitution: x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} The whole Thomas algorith is defined in the following code Program 3.10-1 Thomas algorithm for band matrices
import java.io.*; class NA23 { public static double [] thomas(double a[][],double r[]) { // int n=a.length; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double x[]=new double[n]; for(int i=0;i<n;i++) {f[i]=a[i][i];}

for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];} for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];} for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; } public static double [] thomas(double f[],double e[],double g[],double r[]) { int n=f.length; double x[]=new double[n]; for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; } public static void main(String args[]) throws IOException { double f[]={2.04,2.04,2.04,2.04}; double g[]={-1,-1,-1,0}; double e[]={0,-1,-1,-1}; double b[]={40.8,0.8,0.8,200.8}; double x[]=thomas(f,e,g,b); Text.print(x,"thomas algorithm for 3 band matrix"); System.out.println("thomas : \n"+Matrix.toStringT(x)); double a[][]={{2.04,-1,0,0},{-1,2.04,-1,0 },{0,-1,2.04,-1},{0,0,-1,2.04}}; double d[]={40.8,0.8,0.8,200.8}; System.out.println("thomas : \n"+Matrix.toStringT(thomas(a,d))); } }

thomas : 65.969834366776620 93.778462108224330 124.538228334001000 159.479523693137740

Another example with detailed calculation steps:

f1 e 2 0

g1 f2 e3

0 X 1 41 g 2 X 2 = 25 f 3 X 3 105

0 X 0 41 0 .8 0 .4 0.4 0.8 0.4 X = 25 1 0 0.4 0.8 X 2 105


Decomposition:

ek =

ek f k 1
k 1

fk = fk ek g
e2 =

e2 0 .4 = = 0 .5 f1 0 .8 0 .4 e3 = = 0 . 6666666666 f2 0 .6
2

f 2 = f 2 e 2 g 1 = 0 . 8 ( 0 . 5 )( 0 . 4 ) = 0 . 6

e3 =

f3 = f3 e3 g
Matrix took the form of

= 0 . 8 ( 0 . 66666666

)( 0 . 4 ) = 0 . 5333333

0 0 .4 0 .8 0 .5 0 .6 0 .4 0 0.66666 0.53333
LU decomposed form of the matrix :

0 0 0 .8 0 .4 0 1 0 0 .5 [A] = [L][U ] = 1 0 0 .6 0 .4 0 0 0.53333 0.66666 1 0


Forward substitution

[L]{D} = {B}

0 0 d1 41 1 0 .5 1 0 d 2 = 25 0 0.66666 1 d 3 105
d1=41 d2=25-(-0.5)*41=45.5 d3=105-(-0.66666)*45.5=135.3333333 Backward substitution

[U ]{X } = {D}

0 X1 41 0 .8 0 .4 X = 45.5 0 0 .6 0 .4 2 X 3 135.333333 0 0 0.53333


X3=135.33333/0.53333=253.75 X2=(45.5-(-0.4)* 253.75)/0.6=245 X1=(41-(-0.4)*245)/0.8=173.75

Five band matrix equation can be written as :


d1 a 1 e1 c1 d2 a2 e2 f1 c2 d3 a3 e3 f2 c3 d4 a4 f3 c4 d5 f4 c5 f5 x1 b1 x b 2 2 x3 b3 x4 = b4 x5 b5 f n2 . . cn1 xn 1 bn1 d n xn bn

.
en 3

.
an2 en 2

.
d n 1 an1

It should be noted that bands are written as vectors, instead of a complete matrix. It will give us storage advantage as well as calculation advantage. A five band algorithm is given in the following program for the system of equation:

Program 3.10-2 five band matrix algorithm


import java.io.*; class NA24 { public static double [] fiveband(double A[][],double b[]) { // int n=A.length; double f[]=new double[n-1]; double c[]=new double[n-1]; double d[]=new double[n]; double a[]=new double[n-1]; double e[]=new double[n-2]; double x[]=new double[n]; for(int i=0;i<n;i++) {d[i]=A[i][i];} for(int i=0;i<(n-1);i++) {c[i]=A[i][i+1];} for(int i=0;i<(n-1);i++) {a[i]=A[i+1][i];} for(int i=0;i<(n-2);i++) {f[i]=A[i][i+2];} for(int i=0;i<(n-2);i++) {e[i]=A[i+2][i];} int i; double xmult; for(i=1;i<(n-1);i++) {xmult=a[i-1]/d[i-1]; d[i]+=-xmult*c[i-1]; c[i]+=-xmult*f[i-1]; b[i]+=-xmult*b[i-1]; xmult=e[i-1]/d[i-1]; a[i]+=-xmult*c[i-1]; d[i+1]+=-xmult*f[i-1]; b[i+1]+=-xmult*b[i-1]; //System.out.println(i+"d="+d[i]+"c="+c[i]+"b="+b[i]); } xmult=a[n-2]/d[n-2]; d[n-1]+=-xmult*c[n-2];

x[n-1]=(b[n-1]-xmult*b[n-2])/d[n-1]; x[n-2]=(b[n-2]-c[n-2]*x[n-1])/d[n-2]; for(i=n-3;i>=0;i--) {x[i]=(b[i]-f[i]*x[i+2]-c[i]*x[i+1])/d[i];} return x; } public static double [] fiveband(double f[],double c[],double d[],double a[],double e[],double b[]) { int n=d.length; double x[]=new double[n]; int i; double xmult; for(i=1;i<(n-1);i++) {xmult=a[i-1]/d[i-1]; d[i]+=-xmult*c[i-1]; c[i]+=-xmult*f[i-1]; b[i]+=-xmult*b[i-1]; xmult=e[i-1]/d[i-1]; a[i]+=-xmult*c[i-1]; d[i+1]+=-xmult*f[i-1]; b[i+1]+=-xmult*b[i-1]; //System.out.println(i+"d="+d[i]+"c="+c[i]+"b="+b[i]); } xmult=a[n-2]/d[n-2]; d[n-1]+=-xmult*c[n-2]; x[n-1]=(b[n-1]-xmult*b[n-2])/d[n-1]; x[n-2]=(b[n-2]-c[n-2]*x[n-1])/d[n-2]; for(i=n-3;i>=0;i--) {x[i]=(b[i]-f[i]*x[i+2]-c[i]*x[i+1])/d[i];} return x; } public static void main(String args[]) throws IOException { double d[]={6,6,6,6,6,6,6,6,6,7}; double c[]={-4,-4,-4,-4,-4,-4,-4,-4,-4}; double f[]={1,1,1,1,1,1,1,1}; double a[]={-4,-4,-4,-4,-4,-4,-4,-4,-4}; double e[]={1,1,1,1,1,1,1,1}; double b[]={3,0,0,0,0,0,0,0,0,4}; double x[]=fiveband(f,c,d,a,e,b); Text.printT(x,"five band matrix"); } }

The solution vector is :

3.11 CONJUGATE GRADIENT METHOD If vector x* is a solution of positive and well defined system of equation Ax = b . This solution is valid when the vector x* is defined as minimum of function g ( x) = x T A x 2b T x . The x vector is defined a different vector x then the solution vector x* . In addtiotion a v vector with values different than 0 and a real number t is defined. g(x+tv)=g(x)+2t [vT (Ax-b)] + t2 [vT(Av)] In this equation, if v and x vector is known, for some value of t function will be minimum (One dimensional minimisation problem). If it is assumed that only independent variable of the function is t h(t)=g(x+tv) can be written. The minimum of the function, can be defined as the root of the derivative of the function with respect to t h(t)= 2[vT (Ax-b)]-2t [vT(Av)] = 0 . From this equation
t= v T ( Ax b) v T Av

To simplify the eqaution a little bit further definition r = b A x can be applied. If this definition is substituted into the previous equation:
t= vT r . v Av
T

If t is looked as a part of iterative process xk=xk-1 + tkvk and for rk = b - xk tk value would be
tk = v T rk k vkT Avk

New xk vektor should be give solution to the

A xk = b equation of state. When a solution is obtained. It is known that eqaution will converged to A xk = A(xk-1 + tkvk ) = b But we stil do not know the search direction vector, vk . As a starting search direction steepest descent optimization direction can be selected. This direction is defined as

g ( x) = 2( Ax b) = 2r In ths case vk+1 can be selected as


v k +1 = g ( x k ) = rk . This direction gives the maximum negative change in g(xk) . An alternative selection to the

direction vector can be carried out such a vector that for this vector kondition viT A v j = 0 i j will be valid for a non-zero v vector. This type of vectors are clled orthogonal vectors. If some orthogonal v vectors can be found, it will simplify our solution. In orthogonal sets terms out of diagonal will be zero so only diagonal terms will remain in the equation. Therefore process of solving matrix system will be converted to simple multiplication and division processes. The orthogonal set suggested by Hesteness and Stiefel has the following form :
sk = rkT rk rkT1 rk 1

vk+1= rk + skvk As it is seen from the equation, values are derived from the previous sets. In summary this methon can be applied as: In the first iteration steepest descent value is taken. r0= b A x0 and v0= r0 (steepest descent value) . An then for the next iteration sets calculates the values as for k=1 to n
tk = rkT1 rk vkT Avk

xk=xk-1 + tkvk rk = b - xk rT r s k = Tk k rk 1 rk 1 vk+1= rk + skvk As it is seen process is quite a complex process. It is nor preffered to Gauss elimination in solution of the smal systems, but when the system becomes larger, it will be prefferable. 3.11-1 Conjugate-garadient method to solve system of equations
import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; import java.io.*; //CONJUGATE GRADIENT METHOD public class NA25 {

public static double VT_V(double [] left) { //multiplies a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; } } return aa; } public static double VT_X(double [] left,double [] right) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double[] multiply(double[] left,double[][] right) { //multiplication of one vector with one matrix int ii,jj,i,j,k; int m2=right[0].length; int n2=right.length; int m1=left.length; double[] b; b=new double[m1]; if(n2 != m1) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n2;ii++) { b[ii]=0; } return b; } for(i=0;i<m2;i++)

{ b[i]=0; for(k=0;k<m1;k++) b[i]+=right[i][k]*left[k]; } return b; //end of multiply of a vector and a matrix } public static double[] multiply(double[][] left,double[] right) { //multiplication of one matrix with one vector int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right.length; double[] b; b=new double[m2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) { b[ii]=0; } return b; } for(i=0;i<m1;i++) { b[i]=0; for(k=0;k<n1;k++) b[i]+=left[i][k]*right[k]; } return b; //end of multiply of a matrix and a vector } public static double[] multiply(double left,double[] right) { //multiplying a vector with a constant int i; int n=right.length; double b[]; b=new double[n]; for(i=0;i<n;i++) { b[i]=left*right[i]; } return b; } public static double[] substract(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2;

double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]-right[i]; } return b; //end of vector substraction method } public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method } public static double[] equals(double[] left) { //addition of two vectors int n1=left.length; double b[]; b=new double[n1]; for(int i=0;i<n1;i++) { b[i]=left[i]; } return b; //end of vector equal method } public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } public static double VT_G_V(double V[],double G[][])

{ double x1[]=multiply(V,G); return VT_X(V,x1); } public static double[] conjugate_gradient(double A[][],double b[]) {int n=A.length; double x[]=new double[n]; return conjugate_gradient(x,A,b); }

public static double[] conjugate_gradient(double x[],double A[][],double b[]) { // iterative system of equation solution double eps=1.0e-15; int maxiter=500; int n=x.length; double r[]=new double[n]; double re[]=new double[n]; double v[]=new double[n]; double t; double s; r=substract(b,multiply(A,x)); v=equals(r); int k=0; do { re=equals(r); t=VT_V(re)/VT_G_V(v,A); x=add(x,multiply(t,v)); r=substract(r,multiply(multiply(t,v),A)); s=VT_V(r)/VT_V(re); v=add(r,multiply(s,v)); k++; }while(norm(r)>eps && k<maxiter); System.out.println("k = "+k+" norm(r)="+norm(r)); return x; } public static void main(String args[]) throws IOException { double A[][]={{0.2,0.1,1,1,0},{0.1,4,-1,1,-1},{1,-1,60,0,-2},{1,1,0,8,4},{0,-1,-2,4,700}}; double b[]={1,2,3,4,5}; double x[]=conjugate_gradient(A,b); System.out.println("conjugate gradient method : \n"+Matrix.toStringT(x)); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA25 k = 9 norm(r)=5.161554370481792E-18 conjugate gradient method : 7.859713075445860 0.422926408295008 -0.073592239024046 -0.540643016894627

0.010626162854036 > Terminated with exit code 0.

We will now extend the conjugate gradient method to include preconditioning. If the matrix A is ill-conditioned, the conjugate gradient method is highly susceptible to rounding errors. So, although the exact answer should be obtained in n steps, this is not usually the case. As a direct method the conjugate gradient method is not as good as Gaussian elimination with pivoting. The main use of the conjugate gradient method is as an iterative method applied to a better-conditioned system. To apply the method to a better-conditioned system, we want to select a nonsingular conditioning matrix C so that system of equation can be converted to a better conditioned system. A*= C-1A C-1 x*= C-1x ve b*= C-1b A* x* = b* As a C matrix, a matrix with the rootsquare values of the diagonal elements and 0 values fort he rest of the elements can be selected. For such a matrix, inverse matrix can be produced just by dividing numbers in the matrix by 1. If the whole orocedure is given as a recipe: Input : Matrix A,b and x , C-1 inverse of positive conditioning matrix Step 1 : r0 = b A x w0 = C-1r0 v0 = C-1 w0

0 = w2 j
j =1

Step 2 : k=1 Step 3 : while(k<=N) do adm 4-5 Step 4 : u=Avk

tk =

k 1
j

v u
j =1

xk=xk-1 + tkvk rk=rk-1 + tkuk wk = C-1rk

k = w2 j
j =1

Step 5 : sk= k/k vk+1 = C-1 wk + skvk k = k k=k+1 Preconditioned conjugate-gradient program using above recipee is given below PROBLEM 3.112 Conditioned conjugate-garadient method to solve system of equations
import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; import java.io.*; //reconditioned Conjugate Gradient

public class NA26 { public static double VT_V(double [] left) { //multiplies a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; } } return aa; } public static double VT_X(double [] left,double [] right) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double[][] multiply(double left,double[][] right) { //multiplying a matrix with a constant int i,j; int n=right.length; int m=right[0].length; double b[][]; b=new double[n][m]; for(i=0;i<n;i++) { for(j=0;j<m;j++) b[i][j]=right[i][j]*left; } return b; //end of multiplying a matrix with a constant double } public static double[] multiply(double[] left,double[][] right) {

//multiplication of one vector with one matrix int ii,jj,i,j,k; int m2=right[0].length; int n2=right.length; int m1=left.length; double[] b; b=new double[m1]; if(n2 != m1) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n2;ii++) { b[ii]=0; } return b; } for(i=0;i<m2;i++) { b[i]=0; for(k=0;k<m1;k++) b[i]+=right[i][k]*left[k]; } return b; //end of multiply of a vector and a matrix } public static double[] multiply(double[][] left,double[] right) { //multiplication of one matrix with one vector int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right.length; double[] b; b=new double[m2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) { b[ii]=0; } return b; } for(i=0;i<m1;i++) { b[i]=0; for(k=0;k<n1;k++) b[i]+=left[i][k]*right[k]; } return b; //end of multiply of a matrix and a vector } public static double[] multiply(double left,double[] right) { //multiplying a vector with a constant int i; int n=right.length;

double b[]; b=new double[n]; for(i=0;i<n;i++) { b[i]=left*right[i]; } return b; } public static double[] substract(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]-right[i]; } return b; //end of vector substraction method } public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method } public static double[] equals(double[] left) { //addition of two vectors int n1=left.length; double b[]; b=new double[n1];

for(int i=0;i<n1;i++) { b[i]=left[i]; } return b; //end of vector equal method } public static double norm(double v[]) {return Math.sqrt(normkare(v));} public static double normkare(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return total; } public static double VT_G_V(double V[],double G[][]) { double x1[]=multiply(V,G); return VT_X(V,x1); } public static double[][] I(int n) { //unit matrix double b[][]; b=new double[n][n]; for(int i=0;i<n;i++) b[i][i]=1.0; return b; } public static double[][] D(double A[][]) { //Diagonal elements of matrix int n=A.length; double b[][]; b=new double[n][n]; for(int i=0;i<n;i++) b[i][i]=A[i][i]; return b; } public static double[][] invsqrtD(double A[][]) { //square root of Diagonal elements of a matrix int n=A.length; double b[][]; b=new double[n][n]; for(int i=0;i<n;i++) b[i][i]=1.0/Math.sqrt(A[i][i]); return b; } public static double[] conjugate_gradient(double x[],double A[][],double b[])

{ int n=A.length; double C[][]=new double[n][n]; C=invsqrtD(A); return conjugate_gradient(x,A,b,C); } public static double[] conjugate_gradient(double A[][],double b[]) { int n=A.length; double C[][]=new double[n][n]; C=invsqrtD(A); double x[]=new double[n]; //balang x deerleri 0 olarak alnd return conjugate_gradient(x,A,b,C); } public static double[] conjugate_gradient(double x[],double A[][],double b[],double C[][]) { // iterative system of equation solution double eps=1.0e-15; int maxiter=500; int n=x.length; double r[]=new double[n]; double re[]=new double[n]; double v[]=new double[n]; double w[]=new double[n]; double u[]=new double[n]; double t; double alfa,beta,s; int k=0; r=substract(b,multiply(A,x)); w=multiply(C,r); v=multiply(C,w); alfa=normkare(w); do { k++; if(norm(v)<eps) return x; re=equals(r); u=multiply(A,v); t=alfa/VT_X(v,u); x=add(x,multiply(t,v)); r=substract(r,multiply(t,u)); w=multiply(C,r); beta=normkare(w); if(beta<eps) {if(norm(r)<eps) return x;} s=beta/alfa; v=add(multiply(C,w),multiply(s,v)); alfa=beta; }while(norm(r)>eps && beta>eps && k<maxiter ); //System.out.println("k="+k); return x; } public static void main(String args[]) throws IOException { double A[][]={{0.2,0.1,1,1,0},{0.1,4,-1,1,-1},{1,-1,60,0,-2},{1,1,0,8,4},{0,-1,-2,4,700}}; double b[]={1,2,3,4,5}; double x[]=conjugate_gradient(A,b); System.out.println("conditioned conjugate gradient method : \n"+Matrix.toStringT(x));

} }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA26 conditioned conjugate gradient method : 7.859713075445862 0.422926408295008 -0.073592239024046 -0.540643016894627 0.010626162854036

> Terminated with exit code 0. 3.12 NORMS Norms are an important tool to evaluate matrices: SEveral form of norms are defined. General definitions of normsa re as follows: Infinity norm : Maximum value of the sum of the columns

A=

max( aij )
i =1 j =1

Norm: Maximum value of the sum of the raws

A =

max( aij )
j =1 i =1

Euclidian norm: square root of the sum of the square of the elements

Ae=

a
i =1 j =1 n n

2 ij

m norm: mth root of the sum of the mth power of the elements
m A m = m aij i =1 j =1

Basic norm definitions are given in the norm class. 3.12-1 Matrix norm definitions : class norm
public class norm { //*************norm methods definition**************** public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } public static double norm(double v[],int p) { //p vector norm if(p==0) p=1; double total=0; double x,y; for(int i=0;i<v.length;i++) {

x=Math.abs(v[i]); total+=Math.pow(x,p); } return Math.pow(total,(1.0/p)); } public static double norminf(double v[]) { //infinite vector norm double x; double max=0; for(int i=0;i<v.length;i++) { x=Math.abs(v[i]); if(x>max) max=x; } return max; } public static double[] vek(double mat[][],int n) { double a[]=new double[mat.length]; for(int i=0;i<mat.length;i++) {a[i]=mat[i][n];} return a; } public static double norminf(double a[][]) { //infinite matrix norm double x; double max = 0; double total; int i,j; int n=a.length; for(i=0;i<n;i++) { total=0; for(j=0;j<n;j++) {total+=Math.abs(a[i][j]);} x=total; if(x>max) max=x; } return max; } public static double norm(double a[][]) { //matrix norm double x; double max = 0; double total; int i,j; int n=a.length; for(j=0;j<n;i++) { total=0; for(i=0;i<n;j++) {total+=Math.abs(a[i][j]);} x=total; if(x>max) max=x; } return max; } public static double normE(double a[][]) { //Euclidian matrix norm

double x; double total; int i,j; total=0; int n=a.length; for(j=0;j<n;i++) {for(i=0;i<n;j++) {total+=a[i][j]*a[i][j];}} return Math.sqrt(total); } }

3.13 UNIT MATRIX, TRANPOSE MATRIX AND MATRIX INVERSION Unit matrix is a matrix with all diagonal elemnts are one and all the remaining elements are zero

aii = 1 1 0 [I ] = 0 0

aij , i j = 0 0 1 0 0 0 0 1 0 0 0 0 1

As an example for n=4

Transpose matrix is the matrix with rows and colums are interchanged. Mathematically, it can be shown with a T sign at the top of the matrix. Transpose of a matrix can be taken as in the following method: public static double[][] Transpose(double [][] left) { //transpose matrix (if A=a(i,j) Transpose(A)=a(j,i) int i,j; int n=left.length; int m=left[0].length; double b[][]; b=new double[m][n]; for(i=0;i<n;i++) { for(j=0;j<m;j++) { b[j][i]=left[i][j]; } } return b; }

Inverse matrix is the matrix when multiplied with the original matrix will give the unit matrix [A][A]-1.= [I] Inverse matrix can be obtained by using any of the previous system of equation solution methods. 3.13-1 Inverse matrix program with partially pivoted Gauss LU method
import java.io.*; import java.text.*; import java.util.Locale; class NA28

{ public static void pivot(double a[][],int o[],double s[],int k) {// GaussLU pivoting process int n=a.length; int dummy1=0; double buyuk; double dummy=0; //pivotlama int p=k; buyuk=Math.abs(a[o[k]][k]); for(int ii=k+1;ii<n;ii++) { dummy=Math.abs(a[o[ii]][k]/s[o[ii]]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } dummy1=o[p]; o[p]=o[k]; o[k]=dummy1; } public static int[] pivot_index(int n) { // indexed the pivoted matrix int o[]=new int[n]; for(int i=0;i<n;i++) {o[i]=i;} return o; } public static double[][] gaussLU(double ai[][],int o[]) { //Gauss LU with pivoting int n=ai.length; double a[][]=new double[n][n]; double s[]=new double[n]; double carpan=0; double sum=0; double absaij; //gauss elimination int i,j,k,p,ii,jj; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];}} for(i=0;i<n;i++) { o[i]=i;s[i]=Math.abs(a[i][0]); for(j=1;j<n;j++) { absaij=Math.abs(a[i][j]); if(absaij>s[i]) {s[i]=absaij;} } } for(k=0;k<(n-1);k++) { pivot(a,o,s,k); //change pivort reference //solve gauss elimination for(i=k+1;i<n;i++) { carpan=a[o[i]][k]/a[o[k]][k]; a[o[i]][k]=carpan; for(j=k+1;j<n;j++) { a[o[i]][j]-=carpan*a[o[k]][j]; } } } return a; } public static double[] back_substiution(double ai[][],double bi[]) { int n=ai.length; return back_substiution(ai,bi,pivot_index(n)); } public static double[][] back_substiution(double ai[][],double bi[][])

{ int n=ai.length; return back_substiution(ai,bi,pivot_index(n)); } public static double[] back_substiution(double ai[][],double bi[],int o[]) { int n=ai.length; double sum; double x[]=new double[n]; double a[][]=new double[n][n]; double b[]=new double[n]; int i,j; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};b[i]=bi[i];} for(i=1;i<n;i++) {sum=b[o[i]];for(j=0;j<i;j++){sum-=a[o[i]][j]*b[o[j]];};b[o[i]]=sum;} x[n-1]=b[o[n-1]]/a[o[n-1]][n-1]; for(i=(n-2);i>=0;i--) {sum=0; for(j=i+1;j<n;j++) {sum+=a[o[i]][j]*x[j];}; x[i]=(b[o[i]]-sum)/a[o[i]][i]; } return x; } public static double[][] back_substiution(double ai[][],double bi[][],int o[]) { int n=ai.length; int m=bi[0].length; double sum; double x[][]=new double[n][m]; double a[][]=new double[n][n]; double b[][]=new double[n][m]; int i,j,k; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};for(k=0;k<m;k++) {b[i][k]=bi[i][k];}} for(k=0;k<m;k++) { for(i=1;i<n;i++) {sum=b[o[i]][k];for(j=0;j<i;j++){sum-=a[o[i]][j]*b[o[j]][k];};b[o[i]][k]=sum;} x[n-1][k]=b[o[n-1]][k]/a[o[n-1]][n-1]; for(i=(n-2);i>=0;i--) {sum=0; for(j=i+1;j<n;j++) {sum+=a[o[i]][j]*x[j][k];}; x[i][k]=(b[o[i]][k]-sum)/a[o[i]][i]; } } return x; } public static double [] AXB(double a[][],double b[]) { //solution of the system of equation int n=a.length; int q[]=new int[n]; double c[][]=gaussLU(a,q); double s[]=back_substiution(c,b,q); return s; } public static double [][] AXB(double a[][],double b[][]) {//solution of the set of the system of equations int n=a.length; int q[]=new int[n];

double c[][]=gaussLU(a,q); double s[][]=back_substiution(c,b,q); return s; } public static double[][] I(int n) { //Inverse matrix double b[][]; b=new double[n][n]; for(int i=0;i<n;i++) b[i][i]=1.0; return b; } public static double[][] inverse(double a[][]) { //Inverse matrix int n=a.length; int q[]=new int[n]; double c[][]=gaussLU(a,q); double s[][]=back_substiution(c,I(n),q); return s; } // Matrix output classes public static String toString(double[][] left) { //return a string representation of a matrix return toString(left,25,15); } public static String toString(double[][] left,int w,int d) { //return a string representation of a matrix int n,m; String b; b=""; n=left.length; m=left[0].length; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { b=b+toString(left[i][j],w,d); } b=b+"\n"; } return b; } public static String toString(double left, int w, int d) // converts a double to a string with given width and decimals. { Locale us=new Locale("US"); String s="%"+w+"."+d+"f"; String s1=String.format(us,s,left); return s1; } public static void main(String args[]) throws IOException { double a[][]={{3.556,-1.778,0},{-1.778,3.556,-1.778},{0,-1.778,3.556}}; System.out.println("Matrix a : \n"+toString(a)); System.out.println("Inverse Matrix of a : \n"+toString(inverse(a))); } }

Inverse matrix class NA28 output


---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA28 Matrix a : 3.556000000000000 -1.778000000000000 -1.778000000000000 3.556000000000000 0.000000000000000 -1.778000000000000 Inverse Matrix of a : 0.421822272215973 0.281214848143982 0.140607424071991

0.000000000000000 -1.778000000000000 3.556000000000000

0.281214848143982 0.562429696287964 0.281214848143982

0.140607424071991 0.281214848143982 0.421822272215973

> Terminated with exit code 0.

3.14 PROBLEMS PROBLEM 1 Solve system of equation by using Gauss elimination method.

7 1 2 X 0 47 1 4 1 X = 19 1 3 15 20 X 2 87
PROBLEM 2 Solve system of equation by using a) By using Gauss elimination b) By using Gauss-Jordan elimination

1.1348 0.5301 3.4129 1.2371

3.4017 X 0 9.5342 1.7875 2.5330 1.5435 X 1 6.3941 = 4.9317 8.7643 1.3142 X 2 18.4231 4.9998 10.6721 0.0147 X 3 16.9237 3.8326 1.1651

X 0 1 X 1 1 Exact solution: = X 2 1 X 3 1
PROBLEM 3 Solve system of equation by using a) By using Gauss elimination b) By using Gauss-Jordan elimination

4 3 1 X 0 6 7 2 3 X = 9 1 5 18 13 X 2 3

PROBLEM 4 Solve system of equation by using a) By using Gauss elimination b) By using Gauss-Jordan elimination c) LU decomposition (Dolittle) d) LU Craut Decomposition

1 1 / 2 1 / 3 1 / 4 1 / 5

1 / 2 1 / 3 1 / 4 1 / 5 X 0 137 / 60 1 / 3 1 / 4 1 / 5 1 / 6 X 1 87 / 60 1 / 4 1 / 5 1 / 6 1 / 7 X 2 = 459 / 60 1 / 4 1 / 6 1 / 7 1 / 8 X 3 743 / 840 1 / 6 1 / 7 1 / 8 1 / 9 X 4 1879 / 2520

PROBLEM 5 Solve system of equation by using a) By using Gauss elimination b) Gauss-Seidel iteration c) Conjugate gradient method

8 2 3 X 0 30 1 9 2 X = 1 1 2 3 6 X 2 31
PROBLEM 6 Solve system of equation by using a) By using Gauss elimination b) Gauss-Seidel with relaxation c) Conjugate gradient method

20 X 0 18 3 5 47 11 16 17 10 X 1 26 = 56 22 11 18 X 2 34 17 66 12 7 X 3 82
PROBLEM 7 Solve system of equation by using Thomas algorithm (3 band matrix method)

X 0 0 . 5 2 1 X 1.5 1 2 1 1 X 2 1.5 1 2 1 ... = ... X 7 1.5 1 2 1 1 2 1 X 8 1.5 1 2 X 9 0.5


PROBLEM 8

Solve system of equation by using Thomass algorithm (3 band matrix method)

4 1 X 0 27 1 4 1 X 15 1 X 2 15 1 4 1 ... = ... X 7 15 1 4 1 1 4 1 X 8 15 1 4 X 9 15
PROBLEM 9 Solve the following system of equation

4 2 3 8 X 0 47 3 5 6 1 1 9 15 1 9 2 X 1 17 2 1 7 5 1 6 11 X 2 24 3 2 7 1 2 X 3 = 8 1 1 4 3 1 7 2 1 1 X 4 13 9 8 11 1 4 1 X 5 10 2 7 2 1 2 7 1 9 X 6 34
PROBLEM 10 Solve the following system of equation

5 7 8 X 0 12 1 1 2 3 9 1 1 8 1 X1 8 1 1 9 9 2 3 X 2 22 = 7 2 3 1 4 X 3 41 1 7 1 2 4 1 1 X 4 15 3 9 12 2 7 X 5 50 2
PROBLEM 11 Solve the following system of equation

PROBLEM 12

Load Matrix of the figure can be given with the following system of eqautions

Where c=cos(),s=sin() . For =60o find the load distribution profile PROBLEM 13

Fort he given electrical circuit current density-Voltage eqautions can be given as:

Calculate current densities for R=10 . PROBLEM 14

-x1 + 3x2 + 5x3 + x4 = 8 x1 +9x2 + 3x3 + 4x4 = 10 x2 +x4 = 2 3x1 + x2 +x3 - x4 = -4

Solve the given system of equations. Determine the best method to solve it. PROBLEM 15 Solve the following system of equation by using Doolittle LU method

3 5 1 2 1 3 0 0 2 2 6 3
PROBLEM 16

7 x1 1 5 x 2 2 = 5 x3 3 1 x 4 4

Solve system of equation by using a) By using Gauss elimination b) By using Gauss-Jordan elimination c) LU decomposition (Dolittle) d) LU Craut Decomposition

3x1 + 5x2 + 4x3 + 2x4 = 11 6x1 +14x2 + 11x3 + 6x4 = 26 9x1 + 11x2 +16x3 + 5x4 = 68 3x1 + 13x2 +17x3 + 12x4 = 43 PROBLEM 17 Solve system of equation by using a) By using Gauss elimination b) By using Gauss-Jordan elimination c) LU decomposition (Dolittle) d) LU Craut Decomposition e) Gauss Seidel iteration f) Conjugate-Gradian method

x1 + 3 x2 + x3 + 5 x4 = 4 2 x1 + x2 + 3 x4 = 5 4 x1 + 2 x2 + 2 x3 + x4 = 11 3x1 + x2 + 3 x3 + 2 x4 = 3

PROBLEM 18 Solve system of equation by using a) By using Gauss elimination b) By using Gauss-Jordan elimination c) LU decomposition (Dolittle) d) LU Craut Decomposition

x1 + x2 x3 = -3 6x1 +2x2+2x3=2 -3x1+4x2+x3=1


denklem sistemini ksmi pivotlamal gauss eleme yntemi ile zn. PROBLEM 19 Find infinite norm two norm and m=3 norm of the matrix
7 1 2 1 4 1 3 15 20

PROBLEM 20 Find the inverse matrix


1.1348 0.5301 3.4129 1.2371 3.8326 1.1651 1.7875 2.5330 4.9317 8.7643 4.9998 10.6721 3.4017 1.5435 1.3142 0.0147

PROBLEM 21 Find the inverse matrix


4 3 1 7 2 3 5 18 13

PROBLEM 22 Find two norm and inverse matrix


1 1 / 2 1 / 3 1 / 4 1 / 5 1 / 2 1 / 3 1 / 4 1 / 5 1 / 3 1 / 4 1 / 5 1 / 6 1/ 4 1 / 5 1 / 6 1 / 7 1/ 4 1/ 6 1/ 7 1 / 8 1 / 6 1 / 7 1/ 8 1 / 9

PROBLEM 23

Find two norm, infinite norm and and inverse matrix

8 2 3 1 9 2 2 3 6

PROBLEM 24 Find two norm, infinite norm and and inverse matrix
20 3 5 47 11 16 17 10 56 22 11 18 17 66 12 7

PROBLEM 25 Find two norm, infinite norm and and inverse matrix
2 1 1 2 1 1 2 1 ... 1 2 1 1 2 1 1 2

4 NONLINEAR SYSTEM OF EQUATIONS (ROOTS OF SYSTEM OF EQUATIONS) 4.1 NEWTON-RAPHSON METHOD


One dimensional Newton-Raphson Formula was:

xn+1 = xn - f(xn) /f(xn)


If the equation format changed a little, it can be witten in the form of xn+1 = xn+1 - xn = - f(xn) /f(xn)

f(xn)*xn+1 = - f(xn)
Now the same equation can be considered for a system of non-lineer equation

f1 x 1 f 2 x1 [f ] = f 3 x1 ... f n x 1

f1 x2 f 2 x2 f 3 x2 ... f n x2

f1 x3 f 2 x3 f 3 x3 ... f n x3

... ... ... ... ...

f1 xn f 2 xn f 3 xn ... f n xn

x1 f1 x f 2 2 {x} = x3 { f } = f 3 ... ... xn fn

So multidimensional Newton-Raphson equation becomes

[f ] {x} = - { f } eklini alr.


The equation can be solved by using tecqniques such as gauss elimination. An initial estimate for all the x values are required to start iterative solution First example function f1(x0,x1)=x02+ x0 x1-10 f1(x0,x1)=x1+3 x0 x12-57 PROGRAM 4.1-1 NA29 : Newton Raphson method both function and derivatives are defined
// NA29 Newton Raphson method // to solve non-lineer system of equation import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Tanm gereken fonksiyon ================ abstract class fi_xi { // multifunction multi independent variable // vector of dependent variables are returned // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x) returns the value of f[0] and f[1] // as a two dimensional vector

abstract double[] func(double x[]); } abstract class fij_xi { // multifunction multi independent variable // for n independent variable n*n matrix of // dependent variables are returned // example f[0][0]=x[0]+Math.sin(x[1]) f[0][1]=x[0]-x[1] // f[1][0]=x[0]*x[0]-x[1] f[1][1]=Math.exp(x[0]+x[1]*x[1] // func(x) returns the value of f[0][0], f[0][1] // f[1][0], f[1][1] // as a two dimensional matrix abstract double[][] func(double x[]); } class fa extends fi_xi { public double[] func(double x[]) { //system of non-lineer equation double ff[]=new double[2]; ff[0]=x[0]*x[0]+x[0]*x[1]-10.0; ff[1]=x[1]+3.0*x[0]*x[1]*x[1]-57.0; return ff; } } class fb extends fij_xi { //derivative of system of non-lineer equation public double[][] func(double x[]) { double a[][]=new double[2][2]; a[0][0]=2.0*x[0]+x[1]; a[0][1]=x[0]; a[1][0]=3.0*x[1]*x[1]; a[1][1]=1.0+6.0*x[0]*x[1]; return a; } } public class NA29 { // Roots of system of non-lineer equations // By using Newton-Raphson Method public static double[] newton(fi_xi f,fij_xi df,double x[]) { //x vector of independent variables //y vector of dependent variable //dy derivative of vector of dependent variable double ti=1.0; int i; int nmax=400; double tolerance=1.0e-20; int n=x.length;

double b[]; b=new double[n]; for(i=0;i<n;i++) { b[i]=1.0; } i=0; while( i++ < nmax && Matrix.abs(b) > tolerance ) { b=Matrix.multiply(Matrix.divide(f.func(x),df.func(x)),-1.0); x=Matrix.add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"Maximum number of iterations are exceeded \n"+ " Results might not be valid","MAXIMUM NUMBER OF ITERATIONS ERROR",JOptionPane.WARNING_MESSAGE); System.out.println(); return x; } public static double[] verigir() { String s=JOptionPane.showInputDialog("ENTER FIRST ROOT ESTIMATES WTH ONE EMPTY SPPACE IN BETWEEN: "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fa f =new fa(); fb df=new fb(); double [] x0=verigir(); double [] r= newton(f,df,x0); String s=" Root values : \n"+Matrix.toStringT(r); String s1="Finding roots of the multivariable non-lineer system of equations : "; JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

If the same problem is solved by hand starting from the initial guess x={1,2} NewtonRaphsonNonlinearequationsolution Initialguess x0 1 x1 2 4 12 3 4 0 1 13 1 10 dx0 7 dx1 43 dx0 7 dx1 22

dx0 1.2 dx1 2.2 Firstiterationresult x0 2.2 x1 4.2 8.6 52.92 6.153488 8.6 0 dx0 0.24475 dx1 0.8978 Seconditerationresults x0 1.955252 x1 3.302199 7.212702 1.955252 dx0 0.27964 32.71355 39.73977 dx1 10.2654 4.535547 7.212702 1.955252 dx0 0.27964 0 27.70816 dx1 8.54467 dx0 0.044827 dx1 0.30838 Thirditerationresults x0 2.000079 x1 2.993818 2.2 56.44 dx0 4.08 dx1 63.624

2.2 dx0 4.08 42.90233 dx1 38.5178

6.993975 2.000079 dx0 0.011814 26.88883 36.92723 dx1 0.2264 3.844571 6.993975 2.000079 dx0 0.011814 0 29.23778 dx1 0.180979 dx0 dx1 x0 x1 8.1E05 0.00619 1.999998 3.000008 7.000003 1.999998 dx0 4.94E07 27.00014 37.00005 dx1 0.00022 3.857161 7.000003 1.999998 dx0 4.94E07 0 29.28574 dx1 0.00022 dx0 2.24E06 dx1 7.6E06 Fourthiterationresults x0 2 x1 3 7 27 3.857143 7 0 dx0 2.56E12 dx1 2.9E12 Fifthiterationresults x0 2 x1 3
PROGRAM 4.1-2 : Newton Raphson method only function are defined, derivatives are taken numerically
// NA30 Newton Raphson method // to solve non-lineer system of equation import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Tanm gereken fonksiyon ================

2 37

dx0 1.2E11 dx1 4E11

2 dx0 1.2E11 29.28571 dx1 8.6E11

abstract class fi_xi { // multifunction multi independent variable // vector of dependent variables are returned // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x) returns the value of f[0] and f[1] // as a two dimensional vector abstract double[] func(double x[]); } class fa extends fi_xi { public double[] func(double x[]) { //system of non-lineer equation double ff[]=new double[2]; ff[0]=x[0]*x[0]+x[0]*x[1]-10.0; ff[1]=x[1]+3.0*x[0]*x[1]*x[1]-57.0; return ff; } } public class NA30 { // Roots of system of non-lineer equations // By using Newton-Raphson Method

public static double[] newtond( fi_xi f,double x[]) { //x vector of independent variables //y vector of dependent variable //dy derivative of vector of dependent variable // derivative vector calculated by numerical derivatives double ti=1.0; int i,ii,jj; int nmax=500; double tolerance=1.0e-15; int n=x.length; double b[]; b=new double[n]; double dy[][]; dy=new double[n][n]; i=0; for(i=0;i<n;i++) { b[i]=1.0; } while( i++ < nmax && Matrix.abs(b) > tolerance ) { for(ii=0;ii<n;ii++) { for(jj=0;jj<n;jj++) { dy[ii][jj]=derivative(f,x,ii,jj); }

} b=Matrix.multiply(Matrix.divide(f.func(x),dy),-ti); x=Matrix.add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"Maximum number of iterations are exceeded \n"+ " Results might not be valid","MAXIMUM NUMBER OF ITERATIONS ERROR",JOptionPane.WARNING_MESSAGE); return x; } public static double derivative(fi_xi f,double x[],int denklem_ref,int x_ref) { double h0=0.0256; int i,m; int n=7; double f1[]; f1=new double[x.length]; double f2[]; f2=new double[x.length]; double x1[]; x1=new double[x.length]; double x2[]; x2=new double[x.length]; for(i=0;i<x.length;i++) { x1[i]=x[i]; x2[i]=x[i]; } //derivative of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; //vector<double> h(n,0); for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]; f1=f.func(x1); f2=f.func(x2); T[i][0]=( f1[denklem_ref] - f2[denklem_ref])/(2.0*h[i]); x1[x_ref]=x[x_ref]; x2[x_ref]=x[x_ref]; } for(m=1;m<n;m++)

{ for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] verigir() { String s=JOptionPane.showInputDialog("ENTER FIRST ROOT ESTIMATES WTH ONE EMPTY SPPACE IN BETWEEN: "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fa f =new fa(); double [] x0=verigir(); String s2="Newton raphson method withy the numerically calculated derivatives : "; double [] r1= newtond(f,x0); String s=" root vector : \n"+Matrix.toStringT(r1); JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

The problem steps including numerical derivative is evaluated step by step as :


NewtonRaphsonNonlinearequationsolution

Initialguess x0 x1 1 2

dx0 x0+dx0

0.01 1.01 f1(x0+dx0,x1) 6.9599 f2(x0+dx0,x1) 42.88

x0+2dx0 1.02 x0dx0 0.99

f1(x0+2dx0,x1) 6.9196 f1(x0dx0,x1) 7.0399

f2(x0+2dx0,x1) 42.76 f2(x0dx0,x1) 43.12

4.0000 12.0000

1.0000 13.0000

dx0 7.0000 dx1 43.0000

x02dx0 0.98 x1+dx1 2.01

f1(x02dx0,x1) 7.0796 f1(x0,x1+dx1) 6.99

f2(x02dx0,x1) 43.24 f2(x0,x1+dx1) 42.8697

x1+2dx1 2.02 3 4 0 1 10 dx0 7 dx1 22 x1dx1 1.99

f1(x0,x1+2dx1) 6.98 f1(x0,x1dx1) 7.01

f2(x0,x1+2dx1) 42.7388 f2(x0,x1dx1) 43.1297

x12dx1 1.98

f1(x0,x12dx1) 7.02

f2(x0,x12dx1) 43.2588

dx0 dx1

1.2 2.2 dx0 x0+dx0 0.01 2.21 f1(x0+dx0,x1) 4.1661 f2(x0+dx0,x1) 64.1532

Firstiterationresult x0 x1 2.2 4.2

x0+2dx0 2.22 x0dx0 2.19

f1(x0+2dx0,x1) 4.2524 f1(x0dx0,x1) 3.9941

f2(x0+2dx0,x1) 64.6824 f2(x0dx0,x1) 63.0948

8.6000 52.9200

2.2000 56.4400

dx0 4.0800 dx1 63.6240

x02dx0 2.18 x1+dx1 4.21

f1(x02dx0,x1) 3.9084 f1(x0,x1+dx1) 4.102

f2(x02dx0,x1) 62.5656 f2(x0,x1+dx1) 64.18906

x1+2dx1 4.22 6.153488 8.6000 0 2.2000 42.90233 dx0 4.0800 x1dx1 4.19

f1(x0,x1+2dx1) 4.124 f1(x0,x1dx1) 4.058

f2(x0,x1+2dx1) 64.75544 f2(x0,x1dx1) 63.06026

dx1 38.51777 x12dx1 4.18

f1(x0,x12dx1) 4.036

f2(x0,x12dx1) 62.49784

dx0 dx1

0.244748 0.897801 dx0 x0+dx0 0.01 1.9653 f1(x0+dx0,x1) 0.351864 f2(x0+dx0,x1) 10.59255

Seconditerationresults x0 x1 1.955252 3.302199

x0+2dx0 1.9753 x0dx0 1.9453

f1(x0+2dx0,x1) 0.424291 f1(x0dx0,x1) 0.20761

f2(x0+2dx0,x1) 10.91968 f2(x0dx0,x1) 9.938276

7.2127 32.7135

1.9553 39.7398

dx0 0.279637 x02dx0 1.9353 dx1 10.26541 x1+dx1 3.3122

f1(x02dx0,x1) 0.135783 f1(x0,x1+dx1) 0.29919

f2(x02dx0,x1) 9.61114 f2(x0,x1+dx1) 10.6634

x1+2dx1 3.3222 4.535547 7.212702 0 1.955252 27.70816 dx0 0.279637 x1dx1 3.2922

f1(x0,x1+2dx1) 0.318742 f1(x0,x1dx1) 0.260085

f2(x0,x1+2dx1) 11.06255 f2(x0,x1dx1) 9.8686

dx1 8.544666 x12dx1 3.2822

f1(x0,x12dx1) 0.240532

f2(x0,x12dx1) 9.472962

dx0 dx1

0.044827 0.308381 dx0 x0+dx0 0.01 2.0101 f1(x0+dx0,x1) 0.058225 f2(x0+dx0,x1) 0.042488

Thirditerationresults x0 x1 2.000079 2.993818

x0+2dx0 2.0201 x0dx0 1.9901

f1(x0+2dx0,x1) 0.128465 f1(x0dx0,x1)

f2(x0+2dx0,x1) 0.311377 0.495288

0.081654 f2(x0dx0,x1)

6.9940 26.8888

2.0001 36.9272

dx0 0.011814 dx1 0.2264

x02dx0 1.9801 x1+dx1 3.0038

f1(x02dx0,x1) 0.151294 f2(x02dx0,x1) 0.764177 f1(x0,x1+dx1) 0.008186 f2(x0,x1+dx1) 0.143472

x1+2dx1 3.0138 3.844571 6.993975 0 2.000079 29.23778 dx0 0.011814 dx1 0.180979 x1dx1 2.9838

f1(x0,x1+2dx1) 0.028187 f1(x0,x1dx1)

f2(x0,x1+2dx1) 0.514545 0.595072

0.031815 f2(x0,x1dx1)

x12dx1 2.9738

f1(x0,x12dx1) 0.051816 f2(x0,x12dx1) 0.962544

dx0 dx1

8.09E05 0.00619

x0 x1

1.999998 3.000008

4.2 BROYDEN SECANT METHOD WITH SHARMAN-MORRISON FORMULA

We would like to solve f(x)=0 linear system of equation. In these equations x={x1, x1,, xn1, xn} is a multidimensional independent variable set. As a second way of solving his system of equations secant method can be used instead of Newton-Raphson method. In NewtonRaphson approach was defined as : f1 f1 f1 f1 x x x ... x n 2 3 1 x1 f1 f 2 f 2 f 2 f 2 x f ... x1 x2 x3 xn 2 2 f {x} = x3 { f } = f 3 [f ] = f 3 f 3 f 3 ... 3 ... ... xn x1 x2 x3 ... ... ... ... ... xn fn f n f n f n f n ... x x x xn 2 3 1 equation is used. This equation can be change to a new format: [f ] {x} = { f } In this set derivative term [f ] be replaced by difference (numerical derivative)
f j xk (x )
(i )

f j ( x ( i ) + ek h) f j ( x ( i ) ) h

h in the equation is finite difference parameter (x) and ek is unit vektor (kth componet is 1 the other components are 0). i is the iteration number. Note that secant method is always approach to the roots less quickly compare to Newton-Raphson method. In order to improve number of iterations for the solutions, a modified version of secant method, Broyden method will be used. In Broyden method when x(0) is a given value, first iteration, x(1), will be calculated by using Newton-Raphson method. For the following steps [f ( x ( 0 ) )] {x (1) x ( 0) } = - { f ( x ( 0) } form of the equation can be changed to be
[ A1 ] {x (1) x ( 0) } = { f ( x (1) f ( x ( 0 ) } In order to have this equation valid for every vector z, vector should satisfy following equations [ A1 ] {z} = [f ( x ( 0) )]z {x (1) x ( 0 ) }t z = 0
By using this defination Formula to calculate [ A1 ] can be derived.

[ A1 ] = f ( x ) +
( 0)

[{ f ( x (1) ) f ( x ( 0 ) )} f ( x ( 0) )( x (1) x ( 0 ) )] * ( x (1) x ( 0 ) )t x (1) x ( 0 )


2 2 2 2

Where the term

x (1) x ( 0 )

is a two norm. Norms was defined in previous chapter. After finding [ A1 ] new

value for vector x can be constructed.

x ( 2) = x (1) A1 f ( x (1) )

When x ( 2 ) is found, this value can be used to calculate x ( 3) .General Formula will become:

[ Ai ] = [ Ai 1 ] +

[{ f ( x (1) ) f ( x ( 0 ) )} [ Ai 1 ]( x (1) x ( 0) )] * ( x (1) x ( 0 ) )t x (1) x ( 0)


1

2 2

x ( i +1) = x ( i ) Ai f ( x ( i ) )

One difficulty in this equation is the requirement of solving inverse of the matrix each time which requires solving of system of equation by a method such as Gauss elimination. But Gauss method is expensive in calculation time. A beter alternative could be using ShermanMorrison approach. This approach calculates inverse directly as an iterative process. Matrix Ai-1 can directly be calculated from value Ai-1-1. Equations of Sherman-Morrison approach is as follows si = {x (1) x (i 1) } yi = { f ( x ( i ) ) f ( x ( i 1) )}
( A + xy t ) 1 = A1 A1 xy t A1 1 + y t A1 x
1

1 yi Ai 1si t 1 Ai = A + si 2 si 2 yi Ai 1si t 1 Ai1 si Ai 1 2 si 2 1 Ai = A1 + sit y Ai 1si t si 1 + sit Ai11 i 2 si 2


1

Ai = A1 +

( si Ai11 yi ) sit Ai11 sit Ai11 yi

Due to the fact that this equation only involves multiplication processes, it is easier to use compare to Gauss elimination method. Algorithm of the method is given below.
Program 4.2-1 Broyden secant method with sherman-Morrison Formula.
// NA30A Broyden secant method with Sherman-Morrison formula to solve inverse matrix // Solution of non-linear system of equations. // Referans : Numerical Analysis, 8th edition Richard L. Burden, J. Douglas Faires 3 // page 617 import java.util.*;

import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Tanm gereken fonksiyon ================ abstract class fi_xi { // multifunction multi independent variable // vector of dependent variables are returned // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x) returns the value of f[0] and f[1] // as a two dimensional vector abstract double[] func(double x[]); } abstract class fij_xi { // multifunction multi independent variable // for n independent variable n*n matrix of // dependent variables are returned // example f[0][0]=x[0]+Math.sin(x[1]) f[0][1]=x[0]-x[1] // f[1][0]=x[0]*x[0]-x[1] f[1][1]=Math.exp(x[0]+x[1]*x[1] // func(x) returns the value of f[0][0], f[0][1] // f[1][0], f[1][1] // as a two dimensional matrix abstract double[][] func(double x[]); } class fa extends fi_xi { public double[] func(double x[]) { //function to be solved. double ff[]=new double[3]; ff[0]=3.0*x[0]-Math.cos(x[1]*x[2])-0.5; ff[1]=x[0]*x[0]-81.0*(x[1]+0.1)*(x[1]+0.1)+Math.sin(x[2])+1.06; ff[2]=Math.exp(-x[0]*x[1])+20.0*x[2]+(10.0*Math.PI-3.0)/3.0; return ff; } } class fb extends fij_xi { fi_xi f; public fb(fi_xi fi) {f=fi;} public double[][] func(double x[]) { int n=x.length; double a[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { a[i][j]=derivative(x,i,j); } } return a; } public double derivative(double x[],int denklem_ref,int x_ref) { // df(x)/dx[x_ref] x_ref=0...n

double h0=0.0256; int i,m; int n=7; double f1[]; f1=new double[x.length]; double f2[]; f2=new double[x.length]; double x1[]; x1=new double[x.length]; double x2[]; x2=new double[x.length]; for(i=0;i<x.length;i++) { x1[i]=x[i]; x2[i]=x[i]; } //derivative of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; //vector<double> h(n,0); for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]; f1=f.func(x1); f2=f.func(x2); T[i][0]=( f1[denklem_ref] - f2[denklem_ref])/(2.0*h[i]); x1[x_ref]=x[x_ref]; x2[x_ref]=x[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } } public class NA30A { // SCO5E Sherman-Morrison forml destekli broyden sekant metoduyla //denklem sistemlerinin kklerini (zmn) bulmak public static double[] multiply(double left,double[] right) { //multiplying a vector with a constant

int i; int n=right.length; double b[]; b=new double[n]; for(i=0;i<n;i++) {b[i]=left*right[i];} return b; } public static double[] multiply(double[][] left,double[] right) { //multiplication of one matrix with one vector int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right.length; double[] b; b=new double[m2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) { b[ii]=0; } return b; } for(i=0;i<m1;i++) { b[i]=0; for(k=0;k<n1;k++) b[i]+=left[i][k]*right[k]; } return b; //end of multiply of a matrix and a vector } public static double[] multiply(double[] left,double[][] right) { //multiplication of one vector with one matrix int ii,jj,i,j,k; int m2=right[0].length; int n2=right.length; int m1=left.length; double[] b; b=new double[m1]; if(n2 != m1) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n2;ii++) { b[ii]=0; } return b; } for(i=0;i<m2;i++) { b[i]=0; for(k=0;k<m1;k++) b[i]+=right[i][k]*left[k]; } return b; //end of multiply of a vector and a matrix } public static double VT_X(double [] left,double [] right) {

//multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method } public static double[][] multiply(double left,double[][] right) { //multiplying a matrix with a constant int i,j; int n=right.length; int m=right[0].length; double b[][]; b=new double[n][m]; for(i=0;i<n;i++) { for(j=0;j<m;j++) b[i][j]=right[i][j]*left; } return b; //end of multiplying a matrix with a constant double } public static double[][] add(double[][] left,double[][] right) { //addition of two matrices int n1=left.length; int m1=left[0].length; int n2=right.length; int m2=right[0].length; int nMax,mMax; int i,j; if(m1>=m2) mMax=m1; else mMax=m2; if(n1>=n2) nMax=n1; else nMax=n2; double b[][]; b=new double[nMax][mMax]; for(i=0;i<n1;i++) {

for(j=0;j<m1;j++) { b[i][j]=b[i][j]+left[i][j]; } } for(i=0;i<n2;i++) { for(j=0;j<m2;j++) { b[i][j]=b[i][j]+right[i][j]; } } return b; //end of matrix addition method } public static double[] substract(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]-right[i]; } return b; //end of vector addition method } public static double[][] inv(double[][] a) { // INVERSION OF A MATRIX // inversion by using gaussian elimination // with full pivoting int n=a.length; int m=a[0].length; double b[][]; b=new double[n][n]; int indxc[]; int indxr[]; double ipiv[]; indxc=new int[n]; indxr=new int[n]; ipiv=new double[n]; int i,j,k,l,ll,ii,jj; int icol=0; int irow=0; double big,dum,pivinv,temp; if(n!=m) { System.out.println("Matrix must be square "); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } for(i=0;i<n;i++)

for(j=0;j<n;j++) b[i][j]=a[i][j]; for(i=0;i<n;i++) { big=0.0; for(j=0;j<n;j++) { if(ipiv[j] != 1) for(k=0;k<n;k++) { if(ipiv[k] == 0) { if(Math.abs(b[j][k]) >= big) { big=Math.abs(b[j][k]); irow=j; icol=k; } } else if(ipiv[k] > 1 ) { System.out.println("error : inverse of the matrix : singular matrix-1"); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } } } ++ ipiv[icol]; if(irow != icol) for(l=0;l<n;l++) { temp=b[irow][l]; b[irow][l]=b[icol][l]; b[icol][l]=temp; } indxr[i]=irow; indxc[i]=icol; if(b[icol][icol] == 0.0) { System.out.println("error : inverse of the matrix : singular matrix-2"); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } pivinv=1.0/b[icol][icol]; b[icol][icol]=1.0; for(l=0;l<n;l++) b[icol][l] *=pivinv; for(ll=0;ll<n;ll++) if(ll != icol) { dum=b[ll][icol]; b[ll][icol]=0.0; for(l=0;l<n;l++) b[ll][l]-= b[icol][l]*dum; } } for(l=n-1;l>=0;l--) { if(indxr[l] != indxc[l]) for(k=0;k<n;k++) { temp=b[k][indxc[l]]; b[k][indxc[l]]=b[k][indxr[l]]; b[k][indxr[l]]=temp; }

} return b; } public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } public static double[][] multiply(double[] left,double[] right) { //multiplication of one vector with one matrix int ii,jj,i,j,k; int m2=left.length; int n2=right.length; int m1=left.length; double[][] b; b=new double[m2][n2]; for(i=0;i<m2;i++) { for(k=0;k<n2;k++) b[i][k]=left[i]*right[k]; } return b; //end of multiply of a vector and a matrix } public static double[] broyden(fi_xi f,double x[]) { //==================================================== // Roots of non-linear system of equations // Broyden secant method fb J=new fb(f); int i; int nmax=400; double tolerance=1.0e-15; int n=x.length; double b[]=new double[n]; double v[]=new double[n]; double s[]=new double[n]; double w[]=new double[n]; double y[]=new double[n]; double z[]=new double[n]; double p; double ut[]=new double[n]; double x1[]=new double[n]; double A[][]=new double[n][n]; x1=x; A=J.func(x1); v=f.func(x1); A=inv(A); s=multiply(-1.0,multiply(A,v)); x=add(x,s); int k=1; while(k<nmax) { w=v; v=f.func(x); y=substract(v,w); z=multiply(-1.0,multiply(A,y)); p=-VT_X(s,z); ut=multiply(s,A); //********* A=add(A,multiply((1.0/p),multiply(add(s,z),ut))); s=multiply(-1.0,multiply(A,v));

x=add(x,s); if(norm(s)<tolerance) break; k++; } return x; } public static double[] enterdata() { String s=JOptionPane.showInputDialog("enter first gueses for roots with a sapce in between: "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fa f =new fa(); double [] x0=enterdata(); double [] r= broyden(f,x0); String s=" roots : \n"+Matrix.toStringT(r); String s1="Broyden secant method with Sherman-Morrison iterative inverse matrix formula : "; JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

4.3 HOMOTOPY OR CONTINUATION METHODS


When a problem of system of nonlinear equations of the form F(x)=0 desired to be solved, another interesting solution method is Homotopy or Continuation method. Assume that solution set to be found is x*. Consider a parametric function G(,x) in the form of G(,x) = F(x) + (1-)[ F(x) - F(x(0)) ] Where =0 coresponds to initial guess of the solution , x(0) ,and where =1 value correspods the actual solution set x(1)= x* It is desired to be found G(,x) = 0 therefore for =0 equation becomes G(,x) =G(0,x) = F(x) - F(x(0)) and for =1 0=G(1,x) = F(x) Therefore x(1)=x* solution set will be obtained. If a function G(,x) satisfies the above equation can be found, it will also five us the solution. Function G is called a homotopy between the function G(0,x) and G(1,x)=F(x) In order to find such a function, it is assumed to have a function G(,x)=0 is existed and partial derivative of this function with respect to and x will also be zero

0=

G ( , x) G ( , x) ' + x ( ) x
1

if x() is isolated form this equation, it becomes:

G ( , x( ) G ( , x( )) ' x ( ) = x ( ) x
'

If G(,x) = F(x) + (1-)[ F(x) - F(x(0)) ] equation is substitutd ito the differential equation

f1 ( x( )) x1 G ( , x( ) f 2 ( x( )) = x x 1 f ( x( )) 3 x1
Forms a Jacobian matrix. and

f1 ( x( )) x 2 f 2 ( x( )) x 2 f 3 ( x( )) x 2

f 1 ( x( )) x3 f 2 ( x( )) = J ( x ( ) x3 f 3 ( x( )) x3

G ( , x( ) = F ( x(0)) x

Differential equation becomes

x ' ( ) =

dx( ) 1 = [J ( x( ))] F ( x(0)) d

0 1

It is possible to solve such a differential equation by using initial value problem approaches, solution at x(1) will be given us the roots of the system of equation. Solutions of initial value problems will be given latter chapters in details, but A fourth order Runge-Kutta solution will be defined here to solve our homotopy problem. If equation dx( ) = f ( , x( )) is given the 4th order Runge-Kutta method to numerically solve this d differential equation is defined as:
xi+1 = xi + (1/6)*( k1 + 2k2 +2k3+k4)h k1=f(i,xi) k2=f(i+0.5h,xi+0.5k1h) k3=f(i+0.5h,xi+0.5k2h) k4=f(i+h,xi +k3h)

In these equations h is finite difference step size. Solution starts by using the initial value =0 , x0(0) and adds h into in each iteration step. The example program uses 4th degree and 6th degree RungeKutta method to solve homotopy(Continuation problem). It should be note that Homotophy method is less dependent to initial value compare to methods such as Newton-Raphson therefore one possibility is to approachsolution with a relatively rough estimate with homotophy following with a NewtonRaphson type of method. In the following code methods of Homotophy with 4th and 6th order RungeKutta methods and combination of Homotophy with 4th order Runge-Kuttaand Newton-Raphson method is given. Program 4.3-1 Homotophy (Contiuation) method
// Continuation-homotopy // solution of system of nonlinar equations (roots of equations) // Referans : Numerical Analysis, 8th edition Richard L. Burden, J. Douglas Faires 3 // page 637 // All the required derivatives calculated numercally import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Functions to be defined ================

abstract class fi_xi { // multifunction multi independent variable // vector of dependent variables are returned // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x) returns the value of f[0] and f[1] // as a two dimensional vector abstract double[] func(double x[]); } abstract class fij_xi { // multifunction multi independent variable // for n independent variable n*n matrix of // dependent variables are returned // example f[0][0]=x[0]+Math.sin(x[1]) f[0][1]=x[0]-x[1] // f[1][0]=x[0]*x[0]-x[1] f[1][1]=Math.exp(x[0]+x[1]*x[1] // func(x) returns the value of f[0][0], f[0][1] // f[1][0], f[1][1] // as a two dimensional matrix abstract double[][] func(double x[]); } class fa extends fi_xi { public double[] func(double x[]) { //zm istenen fonksiyon double ff[]=new double[3]; ff[0]=3.0*x[0]-Math.cos(x[1]*x[2])-0.5; ff[1]=x[0]*x[0]-81.0*(x[1]+0.1)*(x[1]+0.1)+Math.sin(x[2])+1.06; ff[2]=Math.exp(-x[0]*x[1])+20.0*x[2]+(10.0*Math.PI-3.0)/3.0; return ff; }} class fb extends fij_xi { fi_xi f; public fb(fi_xi fi) {f=fi;} public double[][] func(double x[]) { int n=x.length; double a[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { a[i][j]=derivative(x,i,j); } } return a; } public double derivative(double x[],int denklem_ref,int x_ref) { double h0=0.0256; int i,m; int n=7; double f1[]; f1=new double[x.length]; double f2[]; f2=new double[x.length]; double x1[];

x1=new double[x.length]; double x2[]; x2=new double[x.length]; for(i=0;i<x.length;i++) { x1[i]=x[i]; x2[i]=x[i]; } //derivative of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; //vector<double> h(n,0); for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]; f1=f.func(x1); f2=f.func(x2); T[i][0]=( f1[denklem_ref] - f2[denklem_ref])/(2.0*h[i]); x1[x_ref]=x[x_ref]; x2[x_ref]=x[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } } public class NA30B { // (Continuity-homotopy) method to solve a system of nonlinear eqations // public static double[] multiply(double left,double[] right) { //multiplying a vector with a constant int i; int n=right.length; double b[]; b=new double[n]; for(i=0;i<n;i++) {b[i]=left*right[i];} return b;

} public static double[] multiply(double[][] left,double[] right) { //multiplication of one matrix with one vector int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right.length; double[] b; b=new double[m2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) { b[ii]=0; } return b; } for(i=0;i<m1;i++) { b[i]=0; for(k=0;k<n1;k++) b[i]+=left[i][k]*right[k]; } return b; //end of multiply of a matrix and a vector } public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method } public static double[][] inv(double[][] a) { // INVERSION OF A MATRIX // inversion by using gaussian elimination // with full pivoting int n=a.length; int m=a[0].length; double b[][]; b=new double[n][n]; int indxc[]; int indxr[]; double ipiv[]; indxc=new int[n]; indxr=new int[n];

ipiv=new double[n]; int i,j,k,l,ll,ii,jj; int icol=0; int irow=0; double big,dum,pivinv,temp; if(n!=m) { System.out.println("Matrix must be square "); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } for(i=0;i<n;i++) for(j=0;j<n;j++) b[i][j]=a[i][j]; for(i=0;i<n;i++) { big=0.0; for(j=0;j<n;j++) { if(ipiv[j] != 1) for(k=0;k<n;k++) { if(ipiv[k] == 0) { if(Math.abs(b[j][k]) >= big) { big=Math.abs(b[j][k]); irow=j; icol=k; } } else if(ipiv[k] > 1 ) { System.out.println("error : inverse of the matrix : singular matrix-1"); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } } } ++ ipiv[icol]; if(irow != icol) for(l=0;l<n;l++) { temp=b[irow][l]; b[irow][l]=b[icol][l]; b[icol][l]=temp; } indxr[i]=irow; indxc[i]=icol; if(b[icol][icol] == 0.0) { System.out.println("error : inverse of the matrix : singular matrix-2"); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } pivinv=1.0/b[icol][icol]; b[icol][icol]=1.0; for(l=0;l<n;l++) b[icol][l] *=pivinv; for(ll=0;ll<n;ll++) if(ll != icol) {

dum=b[ll][icol]; b[ll][icol]=0.0; for(l=0;l<n;l++) b[ll][l]-= b[icol][l]*dum; } } for(l=n-1;l>=0;l--) { if(indxr[l] != indxc[l]) for(k=0;k<n;k++) { temp=b[k][indxc[l]]; b[k][indxc[l]]=b[k][indxr[l]]; b[k][indxr[l]]=temp; } } return b; } public static double[] continuationRK4(fi_xi f,fij_xi J,double x[],int N) { //==================================================== // Roots of nonlinear system of equations Homotophy RK6 // 4.th order Runge-Kutta // yi+1 = yi + (1/6)*( k1 + 2k2 +2k3+k4)h // k1=f(xi,yi) // k2=f(xi+0.5h,yi+0.5k1h) // k3=f(xi+0.5h,yi+0.5k2h) // k4=f(xi+h,yi +k3h) //=================================================== //x vector of independent variables //y vector of dependent variables //dy derivative vector of dependent variables int i; int nmax=400; double tolerance=1.0e-20; int n=x.length; double h=1.0/(double)N; double b[]=new double[n]; double x1[]=new double[n]; double k[][]=new double[4][n]; double A[][]=new double[n][n]; b=multiply(-h,f.func(x)); for(i=0;i<N;i++) { x1=x; A=J.func(x1); k[0]=multiply(inv(A),b); x1=add(x,multiply(0.5,k[0])); A=J.func(x1); k[1]=multiply(inv(A),b); x1=add(x,multiply(0.5,k[1])); A=J.func(x1); k[2]=multiply(inv(A),b); x1=add(x,k[2]); A=J.func(x1); k[3]=multiply(inv(A),b); for(int j=0;j<n;j++) {x[j]=x[j]+1.0/6.0*(k[0][j]+2.0*k[1][j]+2.0*k[2][j]+k[3][j]);} } return x; } public static double[] continuationRK6(fi_xi f,fij_xi J,double x[],int N) { //==================================================== // Roots of nonlinear system of equations Homotophy RK6 // yi+1 = yi + (1/90)*( 7k1 + 32k3 +12k4+32k5+7k6)h // k1=f(xi,yi)

// k2=f(xi+0.25h , yi+0.25k1h) // k3=f(xi+0.25h , yi+0.125k1h+0.125k2h) // k4=f(xi+0.5h , yi - 0.5k2h+k3h) // k5=f(xi+0.75h , yi + (3/16)k1h+(9/16)k4h) // k6=f(xi+h , yi - (3/7)k1h+(2/7)k2h+(12/7)k3h - (12/7)k4h+(8/7)k5h) //=================================================== //x vector of independent variables //y vector of dependent variables //dy derivative vector of dependent variables int i; int nmax=400; double tolerance=1.0e-20; int n=x.length; double h=1.0/(double)N; double b[]=new double[n]; double x1[]=new double[n]; double k[][]=new double[6][n]; double A[][]=new double[n][n]; b=multiply(-h,f.func(x)); for(i=0;i<N;i++) { A=J.func(x); // k1=f(xi,yi) k[0]=multiply(inv(A),b); x1=add(x,multiply(0.25,k[0])); A=J.func(x1); // k2=f(xi+0.25h , yi+0.25k1h) k[1]=multiply(inv(A),b); x1=add(x,add(multiply(0.125,k[0]),multiply(0.125,k[1]))); A=J.func(x1); // k3=f(xi+0.25h , yi+0.125k1h+0.125k2h) k[2]=multiply(inv(A),b); x1=add(x,add(multiply(-0.5,k[1]),k[2])); A=J.func(x1); // k4=f(xi+0.5h , yi - 0.5k2h+k3h) k[3]=multiply(inv(A),b); x1=add(x,add(multiply((3.0/16.0),k[0]),multiply((9.0/16.0),k[3]))); A=J.func(x1); // k5=f(xi+0.75h , yi + (3/16)k1h+(9/16)k4h) k[4]=multiply(inv(A),b); x1=add(x, add(multiply((-3.0/7.0),k[0]),add(multiply((2.0/7.0),k[1]), add(multiply((12.0/7.0),k[2]), add(multiply((-12.0/7.0),k[3]),multiply((8.0/7.0),k[4])))))); A=J.func(x1); // k6=f(xi+h , yi - (3/7)k1h+(2/7)k2h+(12/7)k3h - (12/7)k4h+(8/7)k5h) k[5]=multiply(inv(A),b); // yi+1 = yi + (1/90)*( 7k1 + 32k3 +12k4+32k5+7k6)h for(int j=0;j<n;j++) {x[j]=x[j]+1.0/90.0*(7.0*k[0][j]+32.0*k[2][j]+12.0*k[3][j]+32.0*k[4][j]+7.0*k[5][j]);} } return x; } public static double[] newton_continuationRK4(fi_xi f,fij_xi df,double x[]) { // Roots of nonlinear system of equations Homotophy RK4 plus Newton-Raphson //ti : weight function //x vector of independent variables //y vector of dependent variables //dy derivative vector of dependent variables x=continuationRK4(f,df,x,4); double ti=1.0; int i; int nmax=400; double tolerance=1.0e-10; int n=x.length; double b[]; b=new double[n];

for(i=0;i<n;i++) { b[i]=1.0; } i=0; while( i++ < nmax && Matrix.abs(b) > tolerance ) { b=Matrix.multiply(Matrix.divide(f.func(x),df.func(x)),-ti); x=Matrix.add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"Warning : Maximum number of iterations are exceeded \n"+ " Results may not be valid","MAXIMUM ITERATIONS WARNING",JOptionPane.WARNING_MESSAGE); System.out.println(); return x; } public static double[] enterdata() { String s=JOptionPane.showInputDialog("enter initial root estimations with a space in between "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fa f =new fa(); fb df=new fb(f); double [] x0=enterdata(); double [] r= continuationRK4(f,df,x0,4); double [] r1= continuationRK6(f,df,x0,4); double [] r2= newton_continuationRK4(f,df,x0); String s=" Root values with homotophy RK4 : \n"+Matrix.toStringT(r); s+=" Root values with homotophy RK6 : \n"+Matrix.toStringT(r1); s+=" Root values with homotophy RK4 plus Newton-Raphson : \n"+Matrix.toStringT(r2); String s1="Finding multiple roots with homotophy method : "; JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

4.4 PROBLEMS PROBLEM 1 Solve the following system of non-linear equations 4x12-20x1+0.25x22+8=0 0.5x1x22+2x1+8=0 By using a) Newton raphson method b) Broyden sekant method c) Continuity(Homotopy) method . PROBLEM 2 Solve the following system of non-linear equations 3x12-cos(x2x2) - 0.5=0 4x12-6.25x22+2x3-1=0 ex1 x2 +20x3+(10-3)/3=0 By using a) Newton raphson method b) Broyden sekant method c) Continuity(Homotopy) method PROBLEM 3 Solve te following system of non-linear equations x1(1- x1) + 4x3 12 =0 (x1-2)2+(2x2-3)2-25 =0 By using a) Newton raphson method b) Broyden sekant method c) Continuity(Homotopy) method PROBLEM 4 Solve the following system of non-linear equations sin(4x1x2)-2x2-x1=0

4 1 2 x1 2 (e e) + 4ex2 2ex1 = 0 4
By using a) Newton raphson method b) Broyden sekant method c) Continuity(Homotopy) method PROBLEM 5 Solve the following system of non-linear equations 10x1-2x22+x2-2x3-5=0 8x22+4x32-9=0 8x2x3+4=0

By using a) Newton raphson method b) Broyden sekant method c) Continuity(Homotopy) method

5 OPTIMIZATION 51 DEFINITION OF OPTIMIZATION AND GRAPHIC METHODS Optimization is the name given to maximum or minimum finding process. When it is considered in mathematically, the root of the derivative of the function defines the optimum point.
df(x)/dx = 0

As the mathematical process, maximum and minimum is similar processes because maximum (f(x)) = minimum(-f(x))

As a first example to one dimensional non-linear optimization, Plotting will be utilised. Mathematically plotting means to subdivide the region into n equal parts and investigate the region of the minimum.
PROGRAM 5.2-1 PlotT02 class as using plots in optimization example
import java.io.*; class fa1 extends f_x { double func (double c) { return -(2.0*c/(4.0+0.8*c+c*c+0.2*c*c*c)); } } class PlotT02 { public static void main (String args[]) throws IOException { fa1 ff1=new fa1(); Plot pp=new Plot(ff1,0.0,10.0,400,0); pp.plot(); } }

As it is seen from the plot windows, a guess of 1.5634 can be obtained for the optimum point. 5.2 ONE DIMENSIONAL NON-LINEAR FIBONNACHI SEARCH (GOLDEN SEARCH) METHOD Series 0,1,1,2,3,5,8,13,21,34, is called fibonnachi series. General definition is in the form of

F(n)=F(n-1)+F(n-2) F(0)=0, F(1)=1 For the big fibonnaci terms the ration of two following fibonnachi number becomes constant. This constant is known as golden ratio, R= lim

F (n 1) 5 1 = = 0.618033989 n F ( n) 2

If a minimum being searched in a given region (a,c). In order to determine if a minimum is existed, region should be divided up into three sub-regions. This requires avaluation of two intermediate data points. Neigbors of the minimum point remains and the third region is eliminated. In the new avaluation a new two points will be required. Golden ratio is given us a fine propery so that one of the selected points will always be one of the new search point. So that only one new point will be evaluated in each step of the evaluation.

Figure 5.3-1 Golden ratio serach If minimumin in region [a,c] is required The length of the regon will be d0=(c a) . Then b1 is taken as b1=a+d0*R And b2 is taken as b2 = c d0*R = a + d0*R2 And if it is assumed that minimum is in point b2, region (b1-c) will be thrown. Our new region will be d1=d0*R olacandan new b1 is taken as b1=a+d1*R=a+d0*R2 which is the same as previous b2. New b2 is taken n as b2 = c d1*R = a + d1*R2 = a + d0*R3 . Since one of the point is the same, evaluation of only one new point is required for each iteration step. The Algorithm of Golden search is given in Program 5.3-1 PROGRAM 5.2-1 Golden Ratio or Fibonnachi search / OPTIMIZATION Golden search or fibonnachi method import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class fb extends f_x {

public double func (double c) { return -(2.0*c/(4.0+0.8*c+c*c+0.2*c*c*c));} } public class NA31 { public static double golden(f_x f,double a,double b,double epsilon) {return golden(f,a,b,epsilon,1.0e-10,0);} public static double golden(f_x f,double a,double b) {return golden(f,a,b,1.0e-10,1.0e-10,0);} public static double golden(f_x f,double a,double b,double epsilon,double delta,int print) { // find the minimum of the function // note maximum f(x) = minimum (-f(x)) double r1 = (Math.sqrt(5.0)-1.0)/2.0; // golden ratio double r2 = r1*r1; double h = b - a; double ya = f.func(a); double yb = f.func(b); double c = a + r2*h; double d = a + r1*h; double yc = f.func(c); double yd = f.func(d); int k = 1; double dp,dy,p,yp; while ((Math.abs(yb-ya)>epsilon) || (h>delta)) { k++; if (yc<yd) { b = d; yb = yd; d = c; yd = yc; h = b - a; c = a + r2 * h; yc = f.func(c); } else { a = c; ya = yc; c = d; yc = yd; h = b - a; d = a + r1 * h; yd = f.func(d); }//end of if }//end of while dp = Math.abs(b-a); dy = Math.abs(yb-ya);

p = a; yp = ya; if (yb<ya) { p = b; yp = yb; } if(print==1) {System.out.println("x min = "+p+"ymin = "+yp+"error of x ="+dp+"error of y"+dy); } return p; } public static void main (String args[]) { fb f=new fb(); double a,b; a=Double.parseDouble(JOptionPane.showInputDialog(" enter lower limit of the function a : ")); b=Double.parseDouble(JOptionPane.showInputDialog(" enter upper limit of the function b : ")); double r; System.out.println("x=0.5"+f.func(0.5)); System.out.println("x=1.5"+f.func(1.5)); r= golden(f,a,b,1.0e-10,1.0e-5,0); JOptionPane.showMessageDialog(null," optimization value : "+r+"\nFunction value : "+f.func(r), "Fibonacchi (Golden search) optimization : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Aramaya step step bakacak olursak : a 1 1.381966011 1.381966011 1.381966011 1.472135955 1.527864045 1.527864045 1.549150281 1.562305899 1.562305899 b 2 2 1.763932 1.618034 1.618034 1.618034 1.583592 1.583592 1.583592 1.575462 h 1 0.618034 0.381966 0.236068 0.145898 0.09017 0.055728 0.034442 0.021286 0.013156 c 1.381966 1.618034 1.527864 1.472136 1.527864 1.562306 1.54915 1.562306 1.570437 1.567331 d 1.618034 1.763932 1.618034 1.527864 1.562306 1.583592 1.562306 1.570437 1.575462 1.570437 yc 0.366410476 0.369427868 0.36949646 0.368819066 0.36949646 0.369632208 0.36960486 0.369632208 0.369634304 0.369634826 yd 0.369427868 0.366711114 0.369427868 0.36949646 0.369632208 0.369614176 0.369632208 0.369634304 0.369630023 0.369634304

1.562305899 1.565411519 1.565411519 1.56659776 1.567330897 1.567330897

1.570437 1.570437 1.568517 1.568517 1.568517 1.568064

0.008131 0.005025 0.003106 0.001919 0.001186 0.000733

1.565412 1.567331 1.566598 1.567331 1.567784 1.567611

1.567331 1.568517 1.567331 1.567784 1.568064 1.567784

0.369634332 0.369634826 0.369634711 0.369634826 0.369634851 0.369634846

0.369634826 0.369634819 0.369634826 0.369634851 0.36963485 0.369634851

Fonksiyonun grafik kts

5.3 ONE DIMENSIONAL NON-LINEAR QUADRATIC POLYNOMIAL METHOD Golden search is a linear search method, it will always Works if a minimum(Maximum) is existed in the given region. But convergence speed is relatively slow. A quadratic search should theoretiacally be given values closer to the actual optimum value.

Minimum value of a quadratic equation is given with the Formula

d = b + 0.5(

(c b) 2 [ f (a ) f (b)] (a b) 2 [ f (c) f (a )] (c b) [ f (a ) f (b)] (a b) [ f (c) f (a )]

After finding point d, point b is replaced with this point and iteration continues.
Program 5.3-1 Quadratic polynomial search
// OPTIMIZATION // 2nd degree polynomial (Quadratic polynomial) optimization // of function f(x)

import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class fb extends f_x { public double func (double c) { return -(2.0*c/(4.0+0.8*c+c*c+0.2*c*c*c));} } public class NA32 { public static double polinom2derece(f_x f,double x0,double x2) {double x1=(x0+x2)/2.0; return polinom2derece(f,x0,x1,x2); } public static double polinom2derece(f_x f,double x0,double x1,double x2) { double epsilon=1.0e-10; double delta=1.0e-5; int print=0; return polinom2derece(f,x0,x1,x2,epsilon,delta,print); } public static double polinom2derece(f_x f,double x0,double x1,double x2,double epsilon,double delta,int print) { int maxit=100; double f0 = f.func(x0); double f1 = f.func(x1); double f2 = f.func(x2); double f3 = f2; double x3 = 0; double h = x1 - x0; double k=1; double dd=Math.abs(f1-f0); while ((dd >epsilon) || (h>delta)) { k++; x3=(f0*(x1*x1-x2*x2)+f1*(x2*x2-x0*x0)+f2*(x0*x0-x1*x1))/ (2*f0*(x1-x2)+2.0*f1*(x2-x0)+2.0*f2*(x0-x1)); f3=f.func(x3); if(x3 >= x0 && x3<x1) {x2=x1;f2=f1;x1=x3;f1=f3;} else if(x3 >= x1 && x3 <x2) {x0=x1;f0=f1;x1=x3;f1=f3; } else if(x3 >x2) {x0=x1;f0=f1;x1=x2;f1=f2;x2=x3;f2=f3; } else if(x3 < x0) {x0=x3;f0=f3;x1=x0;f1=f0;x2=x1;f2=f1; } dd=Math.abs(f1-f0); h=Math.abs(x1-x0); if(k>maxit) break; }//end of while if(print==1) {System.out.println("x = "+x3+"f = "+f3+"hata x ="+h+"hata f(x)=y "+dd); } return x3; }

public static void main (String args[]) { fb f=new fb(); double x0,x1,x2; x0=Double.parseDouble(JOptionPane.showInputDialog(" enter lower limit of the function x0 : ")); x1=Double.parseDouble(JOptionPane.showInputDialog(" enter middle value of the function x1 : ")); x2=Double.parseDouble(JOptionPane.showInputDialog(" enter upper limit of the function x2 : ")); double r; r= polinom2derece(f,x0,x1,x2,1.0e-10,1.0e-5,1); JOptionPane.showMessageDialog(null," optimization value : "+r+"\nFunction value : "+f.func(r), "Quadratic polynomial optimization : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.4 ONE DIMENSIONAL NON-LINEAR CUBIC POLYNOMIAL METHOD If it is assumed that our polynomial is a cubic polynomial, a method similar to the quadratic polynomial method can be obtained. Hermite cubic polinomial is used for this purpose.

P(x) = a(x-x0)3 + b(x-x0)2 + c(x-x0) + d optimum(minimum , maximum or extremum) point of this polynomial in the [x0,x1] region wil be calculated from formula

x x0 =

b b 2 4ac 3a b + b 2 4ac 3a

x* = x0 +

a b c d coefficients of this equation is unknown. This coefficients can be calculated from: p(x0)=d=f(x0) p(x0)=c=f(x0) a=(G-2H)/h h =x1 x0 b=3H-G F=[f(x1) - f(x0)]/h G==[f(x1) f(x0)]/h H=[F f(x0)]/h
PROGRAM 5.4-1 Cubic polynomial search
// ======================= //

// Cubic polynomial optimization // // ======================= // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Tanm gereken fonksiyon ================ class fb extends f_x { public double func (double x) { return x+25.0/(x*x);} } //=========================================================== public class NA33 { public static double df(f_x f,double x,double h) { //numerical derivative double hh=1.0/h; return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/12.0*hh; } public static double cubic_search_opt(f_x f,double x0,double x1) { int nmax=500; double tolerance=1.0e-15; double f0,f1,df0,df1,fxs,dfxs; double h=0.00001; double xs; int k=0; double X,F,G,H,a,b,c; f0=f.func(x0); f1=f.func(x1); df0=df(f,x0,h); df1=df(f,x1,h); while(Math.abs(x0-x1)>tolerance && k<nmax) { k++; X=1.0/(x1-x0); F=X*(f1-f0); G=X*(df1-df0); H=X*(F-df0); a=X*(G-2.0*H); b=3.0*H-G; c=df0; xs=x0+(Math.sqrt(b*b-3.0*a*c)-b)/(3.0*a); fxs=f.func(xs); dfxs=df(f,xs,h); if(dfxs > 0 ) {x1=xs;f1=fxs;df1=dfxs;} else x0=xs;f0=fxs;df0=dfxs; } if(k>=nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); return (x0+x1)/2.0; }

public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fb f=new fb(); double x[]=inputdata("enter search area limits : xo and x1"); double r= cubic_search_opt(f,x[0],x[1]); JOptionPane.showMessageDialog(null," minimum value : "+r+"\nFunction value : "+f.func(r), "minimisation by using cubic search method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.5 ONE DIMENSIONAL NON-LINEAR NEWTON-RAPHSON METHOD Newton- Raphson is investigated as one of the root finding methods. Due to the fact that roots of the derivatives of a function is also given the optimum point makes Newton method a cendidate to find optimum through the roots of the derivative of the function. Minimum of f(x) Roots of

g (x) =

df ( x ) = 0 dx

Newton raphson formula

df ( x) f ' ( x) g ( x) = xn 2dx = xn xn+1 = xn dg ( x) d f ( x) f " ( x) 2 dx dx


Program 5.6-1 Newton-Raphson method
/// OPTIMIZATION // Newton-Raphson method import java.util.*;

import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class fc { public double func(double x) { return -(2.0*Math.sin(x)-x*x/10.0);} } class fb extends f_x { public double func(double x) { return -(2.0*Math.cos(x)-x/5.0);} } public class NA34 { public static double derivative(f_x df,double x) { // derivative sub-program // calculates derivative of function df double h0=0.256; int i,m; int n=7; //derivative of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; //vector<double> h(n,0); for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { T[i][0]=( df.func(x + h[i]) - df.func( x - h[i]))/(2.0*h[i]); } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/ (h[i]*h[i]- h[i+m]*h[i+m]); } }

double xx=T[0][n-1]; return xx; } public static double newton(f_x df,double x) { // root finding newton-raphson program int nmax=500; double tolerance=1.0e-15; double fx,dfx; for(int i=0;i<nmax;i++) { fx=df.func(x); dfx=derivative(df,x); x-=fx/dfx; if(Math.abs(fx)<tolerance) { return x; } } return x; } public static double newton_2nd_derivative(f_x df,double x) { // Newton-Raphson with second derivative int nmax=500; double dx=1e-3; double x1m; double tolerance=1.0e-15; double fx,fxm,dfx,dfxm,d2fx; for(int i=0;i<nmax;i++) { fx=df.func(x); fxm=df.func(x-dx); dfx=derivative(df,x); dfxm=derivative(df,(x-dx)); d2fx=-6.0/dx/dx*(fx-fxm)+2.0/dx*(2.0*dfx+dfxm); x-=(fx/dfx+.5*fx*fx/(dfx*dfx*dfx)*d2fx); if(Math.abs(fx)<tolerance) { return x; } } return x; } public static void main (String args[]) { fc f=new fc(); fb df=new fb(); double x0; x0=Double.parseDouble(JOptionPane.showInputDialog(" enter guess value x0 : ")); double r; r= newton(df,x0); JOptionPane.showMessageDialog(null," minimum value : "+r+"\nFunction value : "+f.func(r), "Newton optimization method : ",JOptionPane.PLAIN_MESSAGE); r= newton_2nd_derivative(df,x0); JOptionPane.showMessageDialog(null," minimum value : "+r+"\nFunction value : "+f.func(r),

"Newton with second derivative optimization method : ",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.6 ONE DIMENSIONAL NON-LINEAR BRENT METHOD It is seen that quadratic formula can used to find with formula

x = b + 0.5(

(b a ) 2 [ f (b) f (a )] (b c) 2 [ f (b) f (a )] (b a ) [ f (b) f (a )] (b c) [ f (b) f (c)]

But this Formula can be reached to maximum instead of minimum as well. One more secure approximation could be to use Golden search to approach to the minimum point and then utilise the quadratic polynomial method. This combined method is called Brent method.
Program 5.6-1 Brent method
/// OPTIMIZATION // one dimensional non-lineer functions // Brent method // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function to be defined ================ class fb extends f_x { public double func (double x) { return -(2.0*Math.sin(x)-x*x/10.0);} //- is to obtain maximum value } //===========================================================

public class NA35 { public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);} public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { // ax,bx,cx is three initial guesses (bx should be located between ax and cx) // f : function (defined with f_x abstract class) // tol tolerance int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //golden ratio double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw);

p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("maximum number of iterations are exceeded in BRENT optimization"); xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } public static void main (String args[]) { String s="enter three initial guess values with a space in between xo x1 x2: "; double [] x0=inputdata(s); fb f=new fb(); double r1= brent(f,x0[0],x0[1],x0[2],1.0e-8); s=" zm seti : "+r1+"\n fonksiyon deeri : "+(-f.func(r1)); //fonksiyonun gerek deerini elde etmek iin -1 ile arplmtr String s2="Optimization by Brent method:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); Plot pp=new Plot(f,0,2,100); pp.plot(); } }

A second version of Brent method can also consider derivatives of the function to determine movement of the function better is given as the next program.
Program 5.6-2 dbrent method (brent method with derivative)
//============================== // OPTIMIZATION // One dimensional optimization // brent method with derivative //============================== import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function to be defined ================ class fb extends f_x { public double func (double x) { return -(2.0*Math.sin(x)-x*x/10.0);} } //=========================================================== public class NA35A { public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);} public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static double df(f_x f,double x) { // trev alt program // df fonksiyonunun trevini hesaplar double h0=0.0256; int i,m;

int n=7; //turev of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; //vector<double> h(n,0); for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { T[i][0]=( f.func(x + h[i]) - f.func( x - h[i]))/(2.0*h[i]); } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/ (h[i]*h[i]- h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double dbrent(f_x f,double ax,double bx,double cx,double tol) { // ax,bx,cx is three initial guesses (bx should be located between ax and cx) // f : function (defined with f_x abstract class) // tol tolerance int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-10; int iter; boolean ok1,ok2; double a,b,d,d1,d2,du,dv,dw,dx,e=0.0; d=0.0; double fu,fv,fw,fx,olde,tol1,tol2,u,u1,u2,v,w,x,xm; double xmin=bx; a=(ax < cx ? ax : cx); b=(ax > cx ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); dw=dv=dx=df(f,x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol1=tol*Math.abs(x)+ZEPS;

tol2=2.0*tol1; if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; return xmin; } if (Math.abs(e) > tol1) { d1=2.0*(b-a); d2=d1; if (dw != dx) d1=(w-x)*dx/(dx-dw); if (dv != dx) d2=(v-x)*dx/(dx-dv); u1=x+d1; u2=x+d2; ok1 = (a-u1)*(u1-b) > 0.0 && dx*d1 <= 0.0; ok2 = (a-u2)*(u2-b) > 0.0 && dx*d2 <= 0.0; olde=e; e=d; if (ok1 || ok2) { if (ok1 && ok2) d=(Math.abs(d1) < Math.abs(d2) ? d1 : d2); else if (ok1) d=d1; else d=d2; if (Math.abs(d) <= Math.abs(0.5*olde)) { u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } else { d=0.5*(e=(dx >= 0.0 ? a-x : b-x)); } } else { d=0.5*(e=(dx >= 0.0 ? a-x : b-x)); } } else { d=0.5*(e=(dx >= 0.0 ? a-x : b-x)); } if (Math.abs(d) >= tol1) { u=x+d; fu=f.func(u); } else { u=x+SIGN(tol1,d); fu=f.func(u); if (fu > fx) { xmin=x; return xmin; } } du=df(f,u); if (fu <= fx) { if (u >= x) a=x; else b=x; //MOV3(v,fv,dv, w,fw,dw) {v=w;fv=fw;dv=dw;} //MOV3(w,fw,dw, x,fx,dx) {w=x;fw=fx;dw=dx;} //MOV3(x,fx,dx, u,fu,du) {x=u;fx=fu;dx=du;} } else { if (u < x) a=u; else b=u;

if (fu <= fw || w == x) { //MOV3(v,fv,dv, w,fw,dw) {v=w;fv=fw;dv=dw;} //MOV3(w,fw,dw, u,fu,du) {w=u;fw=fu;dw=du;} } else if (fu < fv || v == x || v == w) { //MOV3(v,fv,dv, u,fu,du) {v=u;fv=fu;dv=du;} } } } System.out.println("maximum number of iterations are exceeded in DBRENT optimization"); return xmin; } public static void main (String args[]) { String s="enter three initial guess values with a space in between xo x1 x2: "; double [] x0=inputdata(s); fb f=new fb(); double r1= dbrent(f,x0[0],x0[1],x0[2],1.0e-8); s=" optimum value : "+r1; String s2="Optimization by Brent method with derivatives:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); } }

5.7 IMPROVING ONE DIMENSIONAL OPTIMIZATION BRACKET VALUES Before starting the search for the optimal value search range can be improved, This also can ensure us to have an optimum point in the given range. Method mnbrak takes 3 initial gues points and enlarge the the given zone untill a minimum is existed. It uses golden search n this process.
Program 5.7-1 mnbrak method (barketing the minimum value)

// OPTIMIZATION // one dimensional non-lineer functions // mnbark bracket method // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function to be defined ================ class fb extends f_x { public double func (double x) { return -(2.0*Math.sin(x)-x*x/10.0);} } //=========================================================== public class NA35B

{ public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);}

public static double[] mnbrak(f_x f,double ax,double dx) { return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax) { return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20; double fa,fb,fc; fa=0; fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx);

if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax; aa[1]=bx; aa[2]=cx; } return aa; } //________________________________________________________________ public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m];

int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { String s="enter one or three initial guess value with a space in between: "; double [] x0=inputdata(s); fb f=new fb(); double r1[]; if(x0.length==3) {r1= mnbrak(f,x0[0],x0[1],x0[2]);} else if(x0.length==1) {r1= mnbrak(f,x0[0]);} else{System.out.println("Wrong number of first initial guesses are given only the first value will be used"); r1= mnbrak(f,x0[0]);} s=" new bracket values : \n"+Matrix.toString(r1); //fonksiyonun gerek deerini elde etmek iin -1 ile arplmtr String s2="bracket values with an ensured optimum value in the given range :"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); } }

5.8 NEWTON-RAPHSON METHOD FOR NONLINEAR MULTIVARIABLE OPTIMIZATION Root finding of multivariable functions by using Newton-Raphson method was prevously investigated. By remembering that optimum point is the root of the derivative of the function, the method can be utilise also to find optimum point. If function f(x1,x2,x3,,xn) function can be written as: First and second derivative and independent difference vector of the

2 f f x 2 x 21 1 ( x1m+1 x1m ) f f m+1 m ( x2 x 2 ) x x x2 1 2 m+1 m+1 m f = f and = ( x3 x3 ) and [ H ] = 2 f x x1 x3 ... 3 ... m+1 m ... ( xn xn ) 2 f f xn x1 xn

2 f x1 x2 2 f 2 x2 2 f x1 x2 ... 2 f x2 xn

2 f x1 x3 2 f x21x3 2 f 2 x3 ... 2 f x3 xn

... ... ... ... ...

2 f x1 xn 2 f x2 xn 2 f x1 xn ... 2 f 2 xn

Newton-Raphson iteration for (m+1) th iteration

[H ]{
m

m +1

} = {f m }

As it is seen from the equation a lineer system of equation is formed. The equation can be solved by using methods such as gauss elimination. An initial estimate for all the x values are required to start iterative solution. Second derivartive matrix is also called a Hessian matrix. First and second derivatives can also be calculated numerically, but that will increase the errors orthe number of iteration steps. The system of equation can also be written as:

{ m +1} = H m [{f m }] . In this format H m


in open form, it becomes

[ ]

[ ]

is the inverse Hessian matrix. If vector is written

{x m +1} = {x m } + H m [{f m }] . As a more general definition of this equation a step function can
be added into the term. It becomes

[ ]

{x m +1} = {x m } + m H m [{f m }] . In Newton-Raphson method step function is equal to unity.


In some other methods different utilisation of this general description will be invetigated. Program 5.8-1 Multivariable non-linear Newton-Raphson method (First and second derivatives of the function is given as seperate functions)
// NA38 Newton-Raphson method for optimization problems // multifunctional non-lineer import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Tanm gereken fonksiyon ================ abstract class fij_xi extends Mathd { // multifunction multi independent variable // vector of dependent variables are returned // example f[0][0]=x[0]+sin(x[1]) f[0][1]=x[0]*x[1] // f[1][0]=x[0]*x[0]-x[1] f[1][1]=x[0]+x[1]*x[0] // func(x) returns the value of f[0][0] and f[0][1] // f[0][1] and f[1][1] // as a two dimensional vector // abstract double[][] func(double x[]); } abstract class fi_xi extends Mathd { // multifunction multi independent variable // vector of dependent variables are returned // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x) returns the value of f[0][0] // f[0][1] // as a two dimensional vector

[ ]

// abstract double[] func(double x[]); } abstract class f_xj extends Mathd { // single function multi independent variable // a single value is returned indiced to equation_ref // example f=x[0]+sin(x[1]) // // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[]); } class fxa extends f_xj { public double func(double x[]) { //optimization function double ff; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return ff; //minimum deil maxsimum istendiinden - ile arptk } } class fxb extends fi_xi { public double[] func(double x[]) { //first derivative of optimization function double ff[]=new double[2]; ff[0]=6.0*x[0]-4.0*x[1]-1.0; ff[1]=-4.0*x[0]+4.0*x[1]-1.0; return ff; } } class fxc extends fij_xi { public double[][] func(double x[]) { //second derivative of optimization function double ff[][]=new double[2][2]; ff[0][0]=6.0;ff[0][1]=-4.0; ff[1][0]=-4.0;ff[1][1]=4.0; return ff; } } public class NA38 { // Optimization by using Newton-Raphson non-lineer multidimensional // root finding method

public static double[] newtond( fi_xi f,fij_xi f1,double x[])

{ double ti=1.0; int i,ii,jj; int nmax=500; double tolerance=1.0e-15; int n=x.length; double b[]; b=new double[n]; double dy[][]; dy=new double[n][n]; i=0; for(i=0;i<n;i++) { b[i]=1.0; } while( i++ < nmax && Matrix.abs(b) > tolerance ) { for(ii=0;ii<n;ii++) { for(jj=0;jj<n;jj++) { dy[ii][jj]=dfx(f,x,ii,jj); } } b=multiply(divide(dy(f,x),H(f1,x)),-ti); x=add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"WArn,ng : Maximum number of iteration exceeded \n"+ " result may not be valis","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static double[][]H(fij_xi f,double x[]) { //Hessian matriX int n=x.length; double dy[][]=f.func(x); System.out.println(Matrix.toString(dy)); return dy; } public static double[] dy(fi_xi f,double x[]) { //FIRST DERVATVE int n=x.length; double deltay[]=f.func(x); System.out.println(Matrix.toStringT(deltay)); return deltay; }

public static double dfx(fi_xi f,double x[],int denklem_ref,int x_ref) { // Trev hesab double h=0.001; double hh=1.0/h; int i,m; int n=x.length;

double f1[]; f1=new double[n]; double f2[]; f2=new double[n]; double f3[]; f3=new double[n]; double f4[]; f4=new double[n]; double x1[]; x1=new double[n]; double x2[]; x2=new double[n]; double x3[]; x3=new double[n]; double x4[]; x4=new double[n]; for(i=0;i<n;i++) { x1[i]=x[i]; x2[i]=x[i]; x3[i]=x[i]; x4[i]=x[i]; } x1[x_ref]+=2.0*h; x2[x_ref]+=h; x3[x_ref]-=h; x4[x_ref]-=2.0*h; f1=f.func(x1); f2=f.func(x2); f3=f.func(x3); f4=f.func(x4); double fx=(f1[denklem_ref]+8.0*f2[denklem_ref]-8.0*f3[denklem_ref]+f4[denklem_ref])/12.0*hh; //System.out.println("f("+x_ref+","+denklem_ref+") = "+fx); return fx; } public static double[] multiply(double[] left,double right) { //multiplying a vector with a constant int i; int n=left.length; double b[]; b=new double[n]; for(i=0;i<n;i++) { b[i]=right*left[i]; } return b; } public static double abs(double[] left) { // absolute value of a vector int i; int n=left.length; double b=0;

for(i=0;i<n;i++) { b=b+Math.abs(left[i]); } return b; } public static double[] divide(double[] left,double[][] right) { return pivotlugauss(right,left); } //en kucuk kareler metodu public static double[] pivotlugauss(double a[][],double b[]) { //ksmi pivotlu gauss eleme yntemi int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss eleme int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //pivotlama p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } //solve gauss elimination for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //backward substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; }

return x; } public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method } public static double[] verigir() { String s=JOptionPane.showInputDialog("enter optimum point first estimation values with one empty space in between: "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fxa f1 =new fxa(); fxb f2 =new fxb(); fxc f3 =new fxc(); double [] x0=verigir(); String s2="Newton-Raphson optimisaton : "; double [] r1= newtond(f2,f3,x0); String s=" optimum values : \n"+Matrix.toStringT(r1); JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Program 5.8-2 Multivariable non-linear Newton-Raphson method (Second derivatives-Hessian of the function is calculated numerically)
// NA39 Newton-Raphson method for optimization problems // multifunctional non-lineer // Hessian matrix calculation is done numerically import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Tanm gereken fonksiyon ================ class fxa extends f_xj { public double func(double x[]) { //optimization function double ff; ff=3.0*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return ff; } } class fxb extends fi_xi { public double[] func(double x[]) { //first derivative of optimization function double ff[]=new double[2]; ff[0]=6.0*x[0]-4.0*x[1]-1.0; ff[1]=-4.0*x[0]+4.0*x[1]-1.0;

return ff; } } public class NA39 { // Optimization by using Newton-Raphson non-lineer multidimensional // root finding method

public static double[] newtond( fi_xi f,double x[]) { //Newton-Raphson method double ti=1.0; int i,ii,jj; int nmax=500; double tolerance=1.0e-10; int n=x.length; double b[]; b=new double[n]; double dy[][]; dy=new double[n][n]; i=0; for(i=0;i<n;i++) { b[i]=1.0; } while( i++ < nmax && Matrix.abs(b) > tolerance ) { b=multiply(divide(dy(f,x),H(f,x)),-ti); x=add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"WArn,ng : Maximum number of iteration exceeded \n"+ " result may not be valis","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static double dfx(fi_xi f,double x[],int denklem_ref,int x_ref) { // derivative calculations double h=0.001; double hh=1.0/h; int i,m; int n=x.length; double f1[]; f1=new double[n]; double f2[]; f2=new double[n]; double f3[]; f3=new double[n]; double f4[]; f4=new double[n]; double x1[]; x1=new double[n]; double x2[]; x2=new double[n]; double x3[]; x3=new double[n];

double x4[]; x4=new double[n]; for(i=0;i<n;i++) { x1[i]=x[i]; x2[i]=x[i]; x3[i]=x[i]; x4[i]=x[i]; } x1[x_ref]+=2.0*h; x2[x_ref]+=h; x3[x_ref]-=h; x4[x_ref]-=2.0*h; f1=f.func(x1); f2=f.func(x2); f3=f.func(x3); f4=f.func(x4); double fx=(-f1[denklem_ref]+8.0*f2[denklem_ref]-8.0*f3[denklem_ref]+f4[denklem_ref])/12.0*hh; return fx; } public static double[][] H(fi_xi f,double x[]) { int n=x.length; double dy[][]=new double[n][n];; for(int ii=0;ii<n;ii++) { for(int jj=0;jj<n;jj++) { //Hessian matrisi dy[ii][jj]=dfx(f,x,ii,jj); } } System.out.println(Matrix.toString(dy)); return dy; } public static double[] dy(fi_xi f,double x[]) { //birinci trev int n=x.length; double deltay[]=f.func(x); System.out.println(Matrix.toStringT(deltay)); return deltay; } public static double[] multiply(double[] left,double right) { //multiplying a vector with a constant int i; int n=left.length; double b[]; b=new double[n]; for(i=0;i<n;i++) { b[i]=right*left[i]; } return b;

public static double[] divide(double[] left,double[][] right) { return pivotlugauss(right,left); } //en kucuk kareler metodu public static double[] pivotlugauss(double a[][],double b[]) { //ksmi pivotlu gauss eleme yntemi int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss eleme int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //pivotlama p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } //dolving gauss elimination for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //backward substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static double[] add(double[] left,double[] right)

{ //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method } public static double[] verigir() { String s=JOptionPane.showInputDialog("enter optimum point first estimation values with one empty space in between: "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fxa f1 =new fxa(); fxb f2 =new fxb(); double [] x0=verigir(); String s2="Newton-Raphson optimization: "; double [] r1= newtond(f2,x0); String s=" optimum values : \n"+Matrix.toStringT(r1); JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Program 5.9-3 Multivariable non-linear Newton-Raphson method (First and second derivatives are calculated numerically)

// NA40 Newton-Raphson method for optimization problems // multifunctional non-lineer // first derivative and Hessian matrix calculation is done numerically import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Tanm gereken fonksiyon ================ abstract class fij_xi extends Mathd { // multifunction multi independent variable // vector of dependent variables are returned // example f[0][0]=x[0]+sin(x[1]) f[0][1]=x[0]*x[1] // f[1][0]=x[0]*x[0]-x[1] f[1][1]=x[0]+x[1]*x[0] // func(x) returns the value of f[0][0] and f[0][1] // f[0][1] and f[1][1] // as a two dimensional vector // abstract double[][] func(double x[]); } abstract class fi_xi extends Mathd { // multifunction multi independent variable // vector of dependent variables are returned // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x) returns the value of f[0][0] // f[0][1] // as a two dimensional vector // abstract double[] func(double x[]); } abstract class f_xj extends Mathd { // single function multi independent variable // a single value is returned indiced to equation_ref // example f=x[0]+sin(x[1]) // // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[]); } class fxa extends f_xj { public double func(double x[]) { //optimization function double ff; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return -ff; //minimum deil maxsimum istendiinden - ile arptk } }

class fxb extends fi_xi { f_xj fxx; fxb(f_xj fxi) {fxx=fxi;} public double[] func(double x[]) { // numerical derivative double h=0.0013421432165; double hh=1.0/h; int i,m; int n=x.length; double f1; double f2; double f3; double f4; double x1[]; x1=new double[n]; double x2[]; x2=new double[n]; double x3[]; x3=new double[n]; double x4[]; x4=new double[n]; double fx[]=new double[n]; for(int j=0;j<n;j++) { for(i=0;i<n;i++) { x1[i]=x[i]; x2[i]=x[i]; x3[i]=x[i]; x4[i]=x[i]; } x1[j]+=2.0*h; x2[j]+=h; x3[j]-=h; x4[j]-=2.0*h; f1=fxx.func(x1); f2=fxx.func(x2); f3=fxx.func(x3); f4=fxx.func(x4); fx[j]=(-f1+8.0*f2-8.0*f3+f4)/12.0*hh; } return fx; } } public class NA40 { // Optimization by using Newton-Raphson non-lineer multidimensional // root finding method

public static double[] newtond( fi_xi f,double x[]) { //Newton-Raphson method double ti=1.0; int i,ii,jj; int nmax=500; double tolerance=1.0e-15; int n=x.length; double b[]; b=new double[n]; double dy[][]; dy=new double[n][n]; i=0; for(i=0;i<n;i++) { b[i]=1.0; } while( i++ < nmax && Matrix.abs(b) > tolerance ) { for(ii=0;ii<n;ii++) { for(jj=0;jj<n;jj++) { dy[ii][jj]=dfx(f,x,ii,jj); } } b=multiply(divide(dy(f,x),H(f,x)),-ti); x=add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"WArn,ng : Maximum number of iteration exceeded \n"+ " result may not be valis","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static double dfx(fi_xi f,double x[],int denklem_ref,int x_ref) { // derivative calculations double h=0.00014354523465; double hh=1.0/h; int i,m; int n=x.length; double f1[]; f1=new double[n]; double f2[]; f2=new double[n]; double f3[]; f3=new double[n]; double f4[]; f4=new double[n]; double x1[]; x1=new double[n]; double x2[]; x2=new double[n]; double x3[]; x3=new double[n]; double x4[];

x4=new double[n]; for(i=0;i<n;i++) { x1[i]=x[i]; x2[i]=x[i]; x3[i]=x[i]; x4[i]=x[i]; } x1[x_ref]+=2.0*h; x2[x_ref]+=h; x3[x_ref]-=h; x4[x_ref]-=2.0*h; f1=f.func(x1); f2=f.func(x2); f3=f.func(x3); f4=f.func(x4); double fx=(-f1[denklem_ref]+8.0*f2[denklem_ref]-8.0*f3[denklem_ref]+f4[denklem_ref])/12.0*hh; //System.out.println("f("+x_ref+","+denklem_ref+") = "+fx); return fx; } public static double[][] H(fi_xi f,double x[]) { //Hessian matrix - Second derivative calculations int n=x.length; double dy[][]=new double[n][n];; for(int ii=0;ii<n;ii++) { for(int jj=0;jj<n;jj++) { //Hessian matrix dy[ii][jj]=dfx(f,x,ii,jj); } } System.out.println(Matrix.toString(dy)); return dy; } public static double[] dy(fi_xi f,double x[]) { //first derivative int n=x.length; double deltay[]=f.func(x); System.out.println(Matrix.toStringT(deltay)); return deltay; } public static double[] multiply(double[] left,double right) { //multiplying a vector with a constant int i; int n=left.length; double b[]; b=new double[n]; for(i=0;i<n;i++) { b[i]=right*left[i]; }

return b; }

public static double[] divide(double[] left,double[][] right) { return pivotlugauss(right,left); } //en kucuk kareler metodu public static double[] pivotlugauss(double a[][],double b[]) { //ksmi pivotlu gauss eleme yntemi int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss eleme int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //pivotlama p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } //gauss elimination for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //back substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; }

public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method } public static double[] verigir() { String s=JOptionPane.showInputDialog("enter optimum point first estimation values with one empty space in between: "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fxa f1 =new fxa(); fxb f2 =new fxb(f1); double [] x0=verigir(); String s2="Newton-Raphson optimization : "; double [] r1= newtond(f2,x0); String s=" optimum value : \n"+Matrix.toStringT(r1); JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); }}

5.9 NELDER MEAD SIMPLEX NONLINEAR MULTIVARIABLE OPTIMIZATION METHOD The simplex method developed by Nelder and Mead method is a multidimensional search method . Simplex is an n dimensional geometric entity. It contains (n+1) points in n dimensional space. The method uses the concept of a simplex, which is a special polytop of N + 1 vertices in N dimensions.

Examples of simplices include a line segment on a line, a triangle on a plane, a tetrahedron in threedimensional space and so forth. Required processes for the simplex amobeas movements are shown in the figure

Process can be given as follows

1. Order according to the values at the vertices:

2. Calculate xo, the center of gravity of all points except xn + 1. 3. Reflection Compute reflected point If the reflected point is better than the second worst, but not better than the best, i.e.: , then obtain a new simplex by replacing the worst point xn + 1 with the reflected point xr, and go to step 1.

4. Expansion If the reflected point is the best point so far, then compute the expanded point If the expanded point is better than the reflected point, then obtain a new simplex by replacing the worst point xn + 1 with the expanded point xe, and go to step 1. Else obtain a new simplex by replacing the worst point xn + 1 with the reflected point xr, and go to step 1. Else (i.e. reflected point is worse than second worst) continue at step 5.

5. Contraction

Here, it is certain that Compute contracted point If the contracted point is better than the worst point, i.e. then obtain a new simplex by replacing the worst point xn + 1 with the contracted point xc, and go to step 1. Else go to step 6.

6. Reduction For all but the best point, replace the point with . go to step 1.

Note: and are respectively the reflection, the expansion, the contraction and the shrink coefficient. Standard values are = 1, = 2, = 1 / 2 and = 1 / 2. For the reflection, since xn + 1 is the vertex with the higher associated value among the vertices, we can expect to find a lower value at the reflection of xn + 1 in the opposite face formed by all vertices point xi except xn + 1. For the expansion, if the reflection point xr is the new minimum along the vertices we can expect to find interesting values along the direction from xo to xr. Concerning the contraction: If f(xr) > f(xn) we can expect that a better value will be inside the simplex formed by all the vertices xi. The initial simplex is important, indeed, a too small initial simplex can lead to a local search, consequently the NM can get more easily stuck. So this simplex should depend on the nature of the problem.
Instead of given n+1 point same process can be done by giving one point and a change vector by defining

xi1=Pi+dxjwhere if i=j =1 else if i j=0.For example:


a da if P = b and dx = db c dc a x0 = b c a + da a a x1 = b x 2 = b + db x3 = b c c c + dc

Figure 6.2-1 Movements of the nelder-Mead simplex element to find the optimum point

Program 5.9-1 Nelder and Mead simplex method


// Nelder-Mead Simplex optimization method// Nelder-Mead Simplex optimization method // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*;

class f1 extends f_xj { public double func(double x[]) { //optimization function double ff;
ff=(x[0]-2.0)*(x[0]-2.0)*(x[0]-2.0)*(x[0]-2.0)+(x[1]-1.0)*(x[1]-1.0)+ 3.0*(x[2]-1.0)*(x[2]-1.0)*(x[2]-1.0)*(x[2]-1.0)*(x[2]-1.0)*(x[2]-1.0);

return ff; } }

public class NA41 { //6-2. Nelder and Mead Simplex multivariable nonlinear optimization method // Nelder & Mead 1965 Computer J, v.7, 308-313. //________________________________________________________________ public static double[] nelder(f_xj fnelder,double a[],double da[],int maxiteration,double tolerance,int printlist) { int i,j; double x[][]=new double[a.length+1][a.length]; double p[][]=new double[a.length+1][a.length+1]; for(i=0;i<x.length;i++) {for(j=0;j<x[0].length;j++) {if(i==j){x[i][j]=a[j]+da[j];p[i][j]=x[i][j];} else {x[i][j]=a[j];p[i][j]=x[i][j]; } } p[i][j] = fnelder.func(p[i]); }

// Inlet variable definitions // fnelder : abstract multivariable function f(x) // x : independent variable set of n+1 simplex elements // maxiteration : maximum iteration number // tolerance : int NDIMS = x.length-1; int NPTS = x.length; int FUNC = NDIMS; int ncalls = 0; ////// construct the starting simplex ////////////////// //double p[][]=new double[NPTS][NPTS]; // [row][col] = [whichvx][coord,FUNC] double z[]=new double[NDIMS]; double best = 1E99; //////////////// calculate the first function values for the simplex //////////////// int iter=0; for (iter=1; iter<maxiteration; iter++) { /////////// define lo, nhi, hi (low high next_to_high ////////////// int ilo=0, ihi=0, inhi = -1; // -1 means missing double flo = p[0][FUNC]; double fhi = flo; double pavg,sterr; for (i=1; i<NPTS; i++) { if (p[i][FUNC] < flo) {flo=p[i][FUNC]; ilo=i;} if (p[i][FUNC] > fhi) {fhi=p[i][FUNC]; ihi=i;} } double fnhi = flo; inhi = ilo; for (i=0; i<NPTS; i++) if ((i != ihi) && (p[i][FUNC] > fnhi)) {fnhi=p[i][FUNC]; inhi=i;} ////////// exit criteria ////////////// if ((iter % 4*NDIMS) == 0) { // calculate the avarage (including maximum value) pavg=0; for(i=0;i<NPTS;i++) pavg+=p[i][FUNC]; pavg/=NPTS; double tot=0; if(printlist!=0) { System.out.print(iter); for (j=0; j<=NDIMS; j++) { System.out.print(p[ilo][j]+" ");} System.out.println(""); } for(i=0;i<NPTS;i++) { tot=(p[i][FUNC]-pavg)*(p[i][FUNC]-pavg);} sterr=Math.sqrt(tot/NPTS); //if(sterr < tolerance) { for (j=0; j<NDIMS; j++) { z[j]=p[ilo][j];}

//break; } best = p[ilo][FUNC]; } ///// calculate avarage without maximum value ////// double ave[] = new double[NDIMS]; for (j=0; j<NDIMS; j++) ave[j] = 0; for (i=0; i<NPTS; i++) if (i != ihi) for (j=0; j<NDIMS; j++) ave[j] += p[i][j]; for (j=0; j<NDIMS; j++) ave[j] /= (NPTS-1);

///////// reflect //////////////// double r[] = new double[NDIMS]; for (j=0; j<NDIMS; j++) r[j] = 2*ave[j] - p[ihi][j]; double fr = fnelder.func(r); if ((flo <= fr) && (fr < fnhi)) // in zone: accept { for (j=0; j<NDIMS; j++) p[ihi][j] = r[j]; p[ihi][FUNC] = fr; continue; } if (fr < flo) //// expand { double e[] = new double[NDIMS]; for (j=0; j<NDIMS; j++) e[j] = 3*ave[j] - 2*p[ihi][j]; double fe = fnelder.func(e); if (fe < fr) { for (j=0; j<NDIMS; j++) p[ihi][j] = e[j]; p[ihi][FUNC] = fe; continue; } else { for (j=0; j<NDIMS; j++) p[ihi][j] = r[j]; p[ihi][FUNC] = fr; continue; } } ///////////// shrink: if (fr < fhi)

{ double c[] = new double[NDIMS]; for (j=0; j<NDIMS; j++) c[j] = 1.5*ave[j] - 0.5*p[ihi][j]; double fc = fnelder.func(c); if (fc <= fr) { for (j=0; j<NDIMS; j++) p[ihi][j] = c[j]; p[ihi][FUNC] = fc; continue; } else /////// daralt { for (i=0; i<NPTS; i++) if (i != ilo) { for (j=0; j<NDIMS; j++) p[i][j] = 0.5*p[ilo][j] + 0.5*p[i][j]; p[i][FUNC] = fnelder.func(p[i]); } continue; } } if (fr >= fhi) /// { double cc[] = new double[NDIMS]; for (j=0; j<NDIMS; j++) cc[j] = 0.5*ave[j] + 0.5*p[ihi][j]; double fcc = fnelder.func(cc); if (fcc < fhi) { for (j=0; j<NDIMS; j++) p[ihi][j] = cc[j]; p[ihi][FUNC] = fcc; continue; } else ///////// { for (i=0; i<NPTS; i++) if (i != ilo) { for (j=0; j<NDIMS; j++) p[i][j] = 0.5*p[ilo][j] + 0.5*p[i][j]; p[i][FUNC] = fnelder.func(p[i]); } } } } return z; } public static double[] nelder(f_xj fnelder,double a[],double da[],double tolerance) {return nelder(fnelder,a,da,500,tolerance,0);} public static double[] nelder(f_xj fnelder,double a[],double da[]) {return nelder(fnelder,a,da,500,1.0e-10,0);}

public static double[] nelder(f_xj fnelder,double a[]) { double [] da=new double[a.length]; for(int i=0;i<a.length;i++) da[i]=0.1*a[i]; return nelder(fnelder,a,da); } // input data //________________________________________________________________ public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { double [] x0=inputdata("enter optimum point first estimation values with one empty space in between: "); double [] dx0=new double[x0.length]; for(int i=0;i<x0.length;i++) dx0[i]=0.2*x0[i]; f1 f=new f1(); double [] r1= nelder(f,x0,dx0); String s=" solution set : \n"+Matrix.toStringT(r1); String s2="Nelder-Mead simplex optimization method "; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

EXAMPLE PROBLEM It is desired to find the minimum of function f(x0,x1)=3x02-4x0x1+2x12-x0-x1 with the initial value of {x}=[-2,2] Function written in java : class f1 extends f_xj { public double func(double x[])

{ //optimization function double ff; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return ff; } } The results of first 50 iteration is recorded and results are plotted for first 17 iterations Output 5.9-1 Sample Nelder-Mead output fort he example program
iter=1p= -2.400000000000000 -2.000000000000000 -2.000000000000000 2.000000000000000 2.400000000000000 2.000000000000000 44.879999999999995 42.320000000000000 36.000000000000000

maximum y i=44.879999999999995 0 second maximum y i=42.32 1 minimum y i=36.0 2 iter=2p= -1.200000000000000 2.600000000000000 28.920000000000005 -2.000000000000000 2.400000000000000 42.320000000000000 -2.000000000000000 2.000000000000000 36.000000000000000 maximum y i=42.32 1 second maximum y i=36.0 2 minimum y i=28.920000000000005 0 iter=3p= -1.200000000000000 2.600000000000000 28.920000000000005 -0.800000000000001 2.100000000000000 16.160000000000014 -2.000000000000000 2.000000000000000 36.000000000000000 maximum y i=36.0 2 second maximum y i=28.920000000000005 0 minimum y i=16.160000000000014 1 iter=4p= -1.200000000000000 2.600000000000000 28.920000000000005 -0.800000000000001 2.100000000000000 16.160000000000014 0.999999999999999 3.050000000000002 5.355000000000019 maximum y i=28.920000000000005 0 second maximum y i=16.160000000000014 1 minimum y i=5.355000000000019 2 iter=5p= 1.399999999999998 2.550000000000002 0.655000000000011 -0.800000000000001 2.100000000000000 16.160000000000014 0.999999999999999 3.050000000000002 5.355000000000019 maximum y i=16.160000000000014 1 second maximum y i=5.355000000000019 2 minimum y i=0.6550000000000114 0 iter=6p= 1.399999999999998 2.550000000000002 0.655000000000011 3.199999999999998 3.500000000000003 3.719999999999994 0.999999999999999 3.050000000000002 5.355000000000019 maximum y i=5.355000000000019 2 second maximum y i=3.7199999999999944 1 minimum y i=0.6550000000000114 0 iter=7p= 1.399999999999998 2.550000000000002 0.655000000000011 3.199999999999998 3.500000000000003 3.719999999999994 1.649999999999998 3.037500000000002 1.885312500000012 maximum y i=3.7199999999999944 1 second maximum y i=1.8853125000000115 2 minimum y i=0.6550000000000114 0 iter=8p= 1.399999999999998 2.550000000000002 0.655000000000011 2.362499999999998 3.146875000000002 1.302519531250004 1.649999999999998 3.037500000000002 1.885312500000012 maximum y i=1.8853125000000115 2 second maximum y i=1.3025195312500042 1 minimum y i=0.6550000000000114 0 iter=9p= 1.399999999999998 2.550000000000002 0.655000000000011 2.362499999999998 3.146875000000002 1.302519531250004 2.112499999999998 2.659375000000002 0.288925781250000

maximum y i=1.3025195312500042 1 second maximum y i=0.6550000000000114 0 minimum y i=0.2889257812500001 2 iter=10p= 1.399999999999998 2.550000000000002 0.655000000000011 1.149999999999998 2.062500000000001 -0.224687499999993 2.112499999999998 2.659375000000002 0.288925781250000 maximum y i=0.6550000000000114 0 second maximum y i=0.2889257812500001 2 minimum y i=-0.22468749999999327 1 iter=11p= 1.862499999999998 2.171875000000001 -0.374042968750002 1.149999999999998 2.062500000000001 -0.224687499999993 2.112499999999998 2.659375000000002 0.288925781250000 maximum y i=0.2889257812500001 2 second maximum y i=-0.22468749999999327 1 minimum y i=-0.3740429687500022 0 iter=12p= 1.862499999999998 2.171875000000001 -0.374042968750002 1.149999999999998 2.062500000000001 -0.224687499999993 0.899999999999998 1.575000000000000 -0.753749999999998 maximum y i=-0.22468749999999327 1 second maximum y i=-0.3740429687500022 0 minimum y i=-0.7537499999999975 2 iter=13p= 1.862499999999998 2.171875000000001 -0.374042968750002 1.612499999999998 1.684375000000000 -0.686386718750004 0.899999999999998 1.575000000000000 -0.753749999999998 maximum y i=-0.3740429687500022 0 second maximum y i=-0.6863867187500037 1 minimum y i=-0.7537499999999975 2 iter=14p= 0.649999999999998 1.087499999999999 -0.932187499999998 1.612499999999998 1.684375000000000 -0.686386718750004 0.899999999999998 1.575000000000000 -0.753749999999998 maximum y i=-0.6863867187500037 1 second maximum y i=-0.7537499999999975 2 minimum y i=-0.9321874999999984 0 iter=15p= 0.649999999999998 1.087499999999999 -0.932187499999998 1.193749999999998 1.507812500000000 -1.079252929687500 0.899999999999998 1.575000000000000 -0.753749999999998 maximum y i=-0.7537499999999975 2 second maximum y i=-0.9321874999999984 0 minimum y i=-1.0792529296874995 1 iter=16p= 0.649999999999998 1.087499999999999 -0.932187499999998 1.193749999999998 1.507812500000000 -1.079252929687500 0.943749999999998 1.020312499999998 -1.061674804687500 maximum y i=-0.9321874999999984 0 second maximum y i=-1.0616748046875 2 minimum y i=-1.0792529296874995 1 iter=17p= 0.859374999999998 1.175781249999999 -1.096405029296875 1.193749999999998 1.507812500000000 -1.079252929687500 0.943749999999998 1.020312499999998 -1.061674804687500 .. iter=49p= 1.000000969021338 1.250005597923464 -1.124999999956208 0.999993292813279 1.249984113824493 -1.124999999786506 1.000019269216091 1.250017992060081 -1.124999999625434 maximum y i=-1.1249999996254345 2 second maximum y i=-1.124999999786506 1 minimum y i=-1.1249999999562075 0

If we would like to see amip movement graphically : Program 5.9-2 Graphic program to see amobe movement in the example
import java.io.*; class PlotT01 { public static void main (String args[]) throws java.io.IOException

{ double x1[]={-2.4,-2,-2,-2.4}; double y1[]={2,2.4,2,2}; double x2[]={-1.2,-2,-2,-1.2}; double y2[]={2.6,2.4,2,2.6}; double x3[]={-1.2,-0.8,-2,-1.2}; double y3[]={2.6,2.1,2,2.6}; double x4[]={-1.2,-0.8,1,-1.2}; double y4[]={2.6,2.1,3.05,2.6}; double x5[]={1.4,-0.8,1,1.4}; double y5[]={2.55,2.1,3.05,2.55}; double x6[]={1.4,3.2,1,1.4}; double y6[]={2.55,3.5,3.05,2.55}; double x7[]={1.4,3.2,1.65,1.4}; double y7[]={2.55,3.5,3.0375,2.55}; double x8[]={1.4,2.3625,1.65,1.4}; double y8[]={2.55,3.146875,3.0375,2.55}; double x9[]={1.4,2.3625,2.1125,1.4}; double y9[]={2.55,3.146875,2.659375,2.55}; double x10[]={1.4,1.15,2.1125,1.4}; double y10[]={2.55,2.0625,2.659375,2.55}; double x11[]={1.8625,1.15,2.1125,1.8625}; double y11[]={2.171875,2.0625,2.659375,2.171875}; double x12[]={1.8625,1.15,0.9,1.8625}; double y12[]={2.171875,2.0625,1.575,2.171875}; double x13[]={1.8625,1.6125,0.9,1.8625}; double y13[]={2.171875,1.684375,1.575,2.171875}; double x14[]={0.65,1.6125,0.9,0.65}; double y14[]={1.0874,1.684375,1.575,1.0874}; double x15[]={0.65,1.19375,0.9,0.65}; double y15[]={1.0874,1.5078125,1.575,1.0874}; double x16[]={0.65,1.19375,0.94375,0.65}; double y16[]={1.0874,1.5078125,1.0203125,1.0874}; double x17[]={0.859375,1.19375,0.94375,0.859375}; double y17[]={1.17578125,1.5078125,1.0203125,1.17578125}; Plot pp=new Plot(x1,y1); pp.addData(x2,y2); pp.addData(x3,y3); pp.addData(x4,y4); pp.addData(x5,y5); pp.addData(x6,y6); pp.addData(x7,y7); pp.addData(x8,y8); pp.addData(x9,y9); pp.addData(x10,y10); pp.addData(x11,y11); pp.addData(x12,y12); pp.addData(x13,y13); pp.addData(x14,y14); pp.addData(x15,y15); pp.addData(x16,y16); pp.addData(x17,y17); pp.plot(); } }

Nelder-Mead actual amip movement for the example function

result :

EXAMPLE PROBLEM As a second example factory space selection problem is taken. In this problem, every city has a number of potential customer. In order to minimize transportation cost of the product new factory should be located in a place minimizing the following function

f = N i [( x xi ) 2 + ( y yi ) 2 ]
n i =1

Where Ni population of the city i and , xi and yi are location of the city. x,y are the location of the factory. Following city location function is given as a test function in file city.txt Population 10 10 10 10 X coordinate of the city 0 0 1 1 Y coordinate of the city 0 1 0 1

It is clear that optimum point for such a data will be located at [0.5,0.5]. In the example program nelder mead method is taken from class NA41 Program 5.9-3 factory placement optimization import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*;

class ff1 extends f_xj { double a[][]; public ff1(String s) {a=Text.readDoubleT(s);} public double func(double x[]) { //optimization function double ff=0; int n=a[0].length; for(int i=0;i<n;i++) ff+=a[0][i]*((x[0]-a[1][i])*(x[0]-a[1][i])+(x[1]-a[2][i])*(x[1]-a[2][i])); return ff; } } public class NA42 { public static void main (String args[]) { String s="enter optimum point first estimation values with one empty space in between: "; double [] x0=NA41.inputdata(s); double [] dx0=new double[x0.length]; for(int i=0;i<x0.length;i++) dx0[i]=0.2*x0[i]; ff1 f=new ff1("city.txt"); double [] r1= NA41.nelder(f,x0); s=" solution set : \n"+Matrix.toStringT(r1); String s2="Nelder-Mead simplex optimization method "; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

---------- Capture Output ---------> "C:\co\java\bin\java.exe" NA41 iter=1p= 0.120000000000000 0.100000000000000 0.100000000000000 0.120000000000000 0.100000000000000 0.100000000000000

32.176000000000000 32.176000000000000 32.800000000000004

maximum y i=32.800000000000004 2 second maximum y i=32.176 0 minimum y i=32.176 0 iter=2p= 0.120000000000000 0.100000000000000 32.176000000000000 0.100000000000000 0.120000000000000 32.176000000000000 0.130000000000000 0.130000000000000 30.951999999999990 maximum y i=32.176 0 second maximum y i=32.176 1 minimum y i=30.95199999999999 2 iter=3p= 0.110000000000000 0.150000000000000 30.983999999999995 0.100000000000000 0.120000000000000 32.176000000000000 0.130000000000000 0.130000000000000 30.951999999999990 maximum y i=32.176 1 second maximum y i=30.983999999999995 0 minimum y i=30.95199999999999 2 iter=4p= 0.110000000000000 0.150000000000000 30.983999999999995 0.160000000000000 0.180000000000000 28.719999999999995 0.130000000000000 0.130000000000000 30.951999999999990 maximum y i=30.983999999999995 0 second maximum y i=30.95199999999999 2 minimum y i=28.719999999999995 1 iter=5p= 0.215000000000000 0.165000000000000 27.737999999999985 0.160000000000000 0.180000000000000 28.719999999999995 0.130000000000000 0.130000000000000 30.951999999999990 maximum y i=30.95199999999999 2 second maximum y i=28.719999999999995 1 minimum y i=27.737999999999985 0 iter=6p= 0.215000000000000 0.165000000000000 27.737999999999985 0.160000000000000 0.180000000000000 28.719999999999995 0.302500000000000 0.257500000000001 23.912499999999984 maximum y i=28.719999999999995 1 second maximum y i=27.737999999999985 0 minimum y i=23.912499999999984 2 iter=7p= 0.215000000000000 0.165000000000000 27.737999999999985 0.456250000000000 0.273750000000001 22.124124999999978 0.302500000000000 0.257500000000001 23.912499999999984 maximum y i=27.737999999999985 0 second maximum y i=23.912499999999984 2 minimum y i=22.124124999999978 1 iter=8p= 0.543750000000001 0.366250000000001 20.792124999999988 0.456250000000000 0.273750000000001 22.124124999999978 0.302500000000000 0.257500000000001 23.912499999999984 maximum y i=23.912499999999984 2 second maximum y i=22.124124999999978 1 minimum y i=20.792124999999988 0 iter=9p= 0.543750000000001 0.366250000000001 20.792124999999988

0.456250000000000 0.697500000000001

0.273750000000001 0.382500000000002

22.124124999999978 22.112499999999997

maximum y i=22.124124999999978 1 second maximum y i=22.112499999999997 2 minimum y i=20.792124999999988 0 iter=10p= 0.543750000000001 0.366250000000001 20.792124999999988 0.538437500000001 0.324062500000001 21.297257812499986 0.697500000000001 0.382500000000002 22.112499999999997 maximum y i=22.112499999999997 2 second maximum y i=21.297257812499986 1 minimum y i=20.792124999999988 0 iter=11p= 0.543750000000001 0.366250000000001 20.792124999999988 0.538437500000001 0.324062500000001 21.297257812499986 0.462890625000000 0.326484375000001 21.259391113281236 maximum y i=21.297257812499986 1 second maximum y i=21.259391113281236 2 minimum y i=20.792124999999988 0 iter=12p= 0.543750000000001 0.366250000000001 20.792124999999988 0.433085937500000 0.390976562500001 20.654544067382805 0.462890625000000 0.326484375000001 21.259391113281236 maximum y i=21.259391113281236 2 second maximum y i=20.792124999999988 0 minimum y i=20.654544067382805 1 iter=13p= 0.543750000000001 0.366250000000001 20.792124999999988 0.433085937500000 0.390976562500001 20.654544067382805 0.539472656250001 0.482871093750001 20.074059600830080 maximum y i=20.792124999999988 0 second maximum y i=20.654544067382805 1 minimum y i=20.07405960083008 2 iter=14p= 0.428808593750000 0.507597656250001 20.205037628173827 0.433085937500000 0.390976562500001 20.654544067382805 0.539472656250001 0.482871093750001 20.074059600830080 maximum y i=20.654544067382805 1 second maximum y i=20.205037628173827 0 minimum y i=20.07405960083008 2 iter=15p= 0.428808593750000 0.507597656250001 20.205037628173827 0.509667968750000 0.547363281250001 20.093470001220710 0.539472656250001 0.482871093750001 20.074059600830080 maximum y i=20.205037628173827 0 second maximum y i=20.09347000122071 1 minimum y i=20.07405960083008 2 iter=16p= 0.476689453125000 0.511357421875001 20.026894905090334 0.509667968750000 0.547363281250001 20.093470001220710 0.539472656250001 0.482871093750001 20.074059600830080 maximum y i=20.09347000122071 1 second maximum y i=20.07405960083008 2 minimum y i=20.026894905090334 0 iter=17p= 0.476689453125000 0.511357421875001 20.026894905090334 0.508874511718750 0.522238769531251 20.022932793140413 0.539472656250001 0.482871093750001 20.074059600830080 maximum y i=20.07405960083008 2 second maximum y i=20.026894905090334 0 minimum y i=20.022932793140413 1 iter=18p= 0.476689453125000 0.511357421875001 20.026894905090334 0.508874511718750 0.522238769531251 20.022932793140413 0.516127319335938 0.499834594726564 20.010404711514713 maximum y i=20.07405960083008 2 second maximum y i=20.026894905090334 0 minimum y i=20.022932793140413 1 iter=52p= 0.500002657994614 0.499999541116936 20.000000000291020 0.499999313009312 0.500000148257349 20.000000000019753 0.499999853685161 0.499998268210034 20.000000000120820 maximum y i=20.00000000029102 0 second maximum y i=20.00000000012082 2 minimum y i=20.000000000019753 1 > Terminated with exit code 0.

5.10 NON-LINEAR MULTIDIMENSIONAL STEEPEST DESCENT METHOD For the n dimensional non lineear f(x0,x1,x2,,xn) function , we would like to find minimum value of the function. The General definition of the Newton Raphson approach can be written as:

{x k +1} = {x k } + k H k
Where

[ ]

[{f k }]

f ( x1 , x2 , x3 ,..., xn ) x1 f ( x1 , x2 , x3 ,..., xn ) x2 f ( x1 , x2 , x3 ,..., xn ) = f ( x1 , x2 , x3 ,..., xn ) is derivative function and x3 ... f ( x1 , x2 , x3 ,..., xn ) xn 2 f x 2 21 f x x 1 2 [H ] = 2 f x1 x3 ... 2 f x1 xn 2 f x1 x2 2 f 2 x2 2 f x1 x2 ... 2 f x2 xn 2 f x1 x3 2 f x21x3 2 f 2 x3 ... 2 f x3 xn
... ... ... ... ...

2 f x1 xn 2 f x2 xn 2 f x1 xn ... 2 f 2 xn

is Hessian Matrix, is a step function, its value was taken equal to unity for standart Newton-Raphson model. If Inverse Hessian in the equation is assumed to be a unit vector, the definition becomes

{x k +1} = {x k } + k [{f k }] = {x k } + k {d k } It is called the Steepest descent(or ascent) method.


In this method, As a first action a starting point for the iteration will be selected. If the starting function is P0
0 0 0 P 0 = f ( x10 , x2 , x3 ,..., xn )

In each iteration step values of the fuction and derivatives are calculated.
k k k d k = f ( x1k , x2 , x3 ,..., xn ) k = 0....n

Then for each x value is increased by dk with an unknown multiplier . In the new equation remains as only onkown (one dimensional equation)
f ( k ) = f ( x k + k d k ) = f ([ x k k 1 f ( x , x , x ,..., x ) f ( x , x , x ,..., x ) f ( x , x , x ,..., x ) 1 2 3 1 2 3 n ],...,[ x k k 1 2 3 n ]) n ],[ x k k 2 n x x x 2 1 n

value minimizing function f ( ) is found by using one of the one dimensional minimisation
methods. The new x k +1 value calculated by using
x k +1 = x k + k d k

Process continues untill the error is small enough. EXAMPLE PROBLEM

Find minimum value of function f(x0,x1)=3x02-4x0x1+2x12-x0-x1 with the initial value of {x}=[-2,2] by using steepest descent method.

6 x 4 x1 1 21 f = 0 = 4 x1 4 x0 1 15
0 x0 2 0 = x1 2
1 0 6 x 0 4 x10 1 x0 x0 = 0 + 00 1 4 x0 4 x10 1 x1 x1 1 x0 2 21 1 = + 15 x1 2

x1 =221 0
1 x1 =2+15

If these values are substituted into the function f(x0,x1)=3x02-4x0x1+2x12-x0-x1 it becomes f()=3(221 )24(221)(2+15)+2(2+15)2(221)(2+15) f()==36+666+3033 2 In order to find minimum of this function, analitical solution will be used in this example f()== 666+6066 = 0 =-0.109792284
1 If this value is substituted into x1 and x1 0 1 x0 0.3056379 For =-0.109792284 1 = x1 0.35311572 2 x0 0.9544105 For =-1.121212 2 = x1 1.2613973

2=-0.109792284 iin

3 x0 0.9894 = 3 x1 1.2363

For 3=-1.121212

4 x0 0.9993 = 4 x1 1.250173

The same problem is also solved in the algorithm Program 5.10-1 steepest descent algorithm
// Multidimensional non-Linear optimization // Steepest descent method // import javax.swing.*; import java.util.*; import java.awt.*;

import java.applet.Applet; import java.awt.event.*; import java.io.*; class f1 extends f_xj { public double func(double x[]) { //optimization function double ff; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return ff; } } class f1dim extends f_x { // pseudofunction to convert multidimensional function to // one dimensional function f_xj ff1; double pc[]; double xc[]; double xt[]; int n; public f1dim(f_xj ff,double pi[],double xi[]) { n=pi.length; ff1=ff; pc=new double[n]; xc=new double[n]; xt=new double[n]; for(int i=0;i<n;i++) {pc[i]=pi[i];xc[i]=xi[i];} } public double func(double x) { for (int j=0;j<n;j++) xt[j]=pc[j]+x*xc[j]; return ff1.func(xt); } }

public class NA43 { public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);} // 3. brent one dimensional optimization method public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0;

d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("maximum iteration limit in BRENT method is exceeded"); xmin=x; aa[0]=xmin; aa[1]=fx; return aa;

} public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } // 3. Steepest descent //________________________________________________________________ public static double[] steepestdescent( f_xj f,double x[]) { // steepestdescent method // uses linmin and brent methods double ti=1.0; int i,ii,jj; int nmax=500; double tolerance=1.0e-10; int n=x.length; double b[]; b=new double[n]; double lambda; double dy[]=new double[n]; i=0; for(i=0;i<n;i++) { b[i]=1.0; } i=0; while( i++ < nmax && Matrix.abs(b) > tolerance ) { dy=derivative(f,x); lambda=linmin(f,x,dy); b=Matrix.multiply(dy,lambda); x=Matrix.add(x,b); System.out.println("i= "+i+"alfa="+lambda+"\n"+Matrix.toString(x)); } if(i >= nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); return x; } public static double[] mnbrak(f_x f,double ax,double dx) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seilen uzaklk return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seiln uzaklk return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { // ax,bx,cx adet tahmin noktas verildiinde iinde minimum bulunan ax,bx,cx adetnoktay bize arama

// sonucu bulur // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20; double fa,fb,fc; fa=0; fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim;

fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax; aa[1]=bx; aa[2]=cx; } return aa; } public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-4; int n=p.length; double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0]; xx=aa[1]; bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[] derivative(f_xj f_deriv,double x[]) { // df/dxj j=0...x.length // This method calculates derivative of a function with more than one independent variable. // Accuracy of method can be adjusted by changing variables h0 and n // function input should be in the form given in abstract class // f_xj,j=0...x.length = df/dx(x_ref) double a[]=new double[x.length]; for(int x_ref=0;x_ref<x.length;x_ref++) { a[x_ref]=derivative(f_deriv,x,x_ref); } return a; } public static double derivative(f_xj f,double xxi[],int x_ref) { // df/dxj double h0=0.256808; int i,m; int n=7; double x1[]; x1=new double[xxi.length]; double x2[];

x2=new double[xxi.length]; for(i=0;i<xxi.length;i++) { x1[i]=xxi[i]; x2[i]=xxi[i]; } //derivative of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { //h[i]=h0*Math.pow(r,i); h0/=2.0; h[i]=h0; } //first derivative (difference formula) for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]; T[i][0]=(f.func(x1)-f.func(x2))/(2.0*h[i]); x1[x_ref]=xxi[x_ref]; x2[x_ref]=xxi[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } //input data method //________________________________________________________________ public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens())

{ Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { String s="enter optimum point first estimation values with one empty space in between: "; double [] x0=inputdata(s); f1 f_x=new f1(); double [] r1= steepestdescent(f_x,x0); s=" zm seti : \n"+Matrix.toStringT(r1); String s2="Steepest Descent multidimensional non-linear optimization method:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); }}

EXAMPLE PROBLEM Find the minimum of the function f(x0,x1)=sin(x0)cos(x1) with the initial starting point {x}=[1,0.5] Sample function used in the problem
class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=Math.sin(x[0])*Math.cos(x[1]); return ff; }}

5.11 MODIFIED NEWTON METHOD(NEWTON RAPHSON STEEPEST DESCENT COMBINED METHOD)

If the initial guess is close to the actual value Newton-Raphson method performs well. But when it is far from the actual solution it is not behave so well. Modified Newton method combines NewtonRaphson method with steepest descent method to get a better convergence performance. Remembering general definition of optimizing equation

{x k +1} = {x k } + k H k

[ ]

[{f k }]

Now both and Hessian matrix can be taken into the calculation steps to get a combined method. Program 5.11-1 Modified Newton Method(Combined Newton-Raphson and Steepest descent method)
// Modified Newton method // uses Newton-Raphson together with Steepest descent import java.util.*;

import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class f1 extends f_xj { public double func(double x[]) {//function double ff; ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1]; return -ff; } } class f2 extends fi_xi {//first derivative of the function public double[] func(double x[]) { double ff[]={-(2.0*x[1]+2.0-2.0*x[0]),-(2.0*x[0]-4.0*x[1])}; return ff; } } class f3 extends fij_xi { public double[][] func(double x[]) {//second derivative of the function double ff[][]={{2,-2},{-2,-4}}; return ff; } } public class NA38A { public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);} public static double[] df(f_xj f,double xxi[]) { double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=dfdx(f,xxi,i); } return a; }

public static double dfdx(f_xj f,double xxi[],int xref) { int i; double h=1.0e-3; double hh=1.0/h; double xp2h[]=new double[xxi.length]; double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh; for(i=0;i<xxi.length;i++)

{ if(i!=xref) {xp2h[i]=xxi[i];xph[i]=xxi[i];xm2h[i]=xxi[i];xmh[i]=xxi[i];} else {xp2h[i]=xxi[i]+2.0*h;xph[i]=xxi[i]+h;xm2h[i]=xxi[i]-2.0*h;xmh[i]=xxi[i]-h;} } fp2h=f.func(xp2h); fph=f.func(xph); fm2h=f.func(xm2h); fmh=f.func(xmh); return (-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; } public static double d2fdx2(f_xj f,double xxi[],int xiref,int xjref) { //second derivative int i; double h=1.0e-4; double hh=1.0/h; int n=xxi.length; double ff; double xp2h[]=new double[xxi.length]; double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh; for(int j=0;j<n;j++) {if(j!=xjref) {xp2h[j]=xxi[j];xph[j]=xxi[j];xm2h[j]=xxi[j];xmh[j]=xxi[j];} else {xp2h[j]=xxi[j]+2.0*h;xph[j]=xxi[j]+h;xm2h[j]=xxi[j]-2.0*h;xmh[j]=xxi[j]-h;} } fp2h=dfdx(f,xp2h,xiref); fph=dfdx(f,xph,xiref); fm2h=dfdx(f,xm2h,xiref); fmh=dfdx(f,xmh,xiref); ff=(-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; return ff; } public static double[][]H(f_xj f,double x[]) { //Hessian matrix int n=x.length; double dy[][]=new double[n][n];; for(int ii=0;ii<n;ii++) { for(int jj=0;jj<n;jj++) { //Hessian matrix dy[ii][jj]=d2fdx2(f,x,ii,jj); } } return dy; } public static double[] dampednewton(f_xj f,fi_xi df,fij_xi d2f,double x[]) { // Modified Newton method // uses Newton-Raphson together with Steepest descent // Steepest descent also uses linmin and brent methods of one dimensionaloptimization double ti=1.0; int i,ii,jj; int nmax=800; double tolerance=1.0e-14;

int n=x.length; double b[]; b=new double[n]; double lambda; double g[]=new double[n]; double G[][]=d2f.func(x);//Hessian matrix double g1,g2,g3,g4,g5; //dy=new double[n][n]; i=0; for(i=0;i<n;i++) { b[i]=1.0; } while( i++ < nmax && Matrix.abs(b) > tolerance ) { g=df.func(x); g1=Matrix.VT_V(g); g2=Matrix.VT_G_V(g,G); g1*=g1; g3=g1/g2; g4=Matrix.VT_G_V(g,Matrix.inverse(G)); lambda=linmin(f,x,g); if(g4>Math.max(g3,0)) b=Matrix.multiply(Matrix.divide(g,G),lambda); else b=Matrix.multiply(g,lambda); x=Matrix.add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"WArn,ng : Maximum number of iteration exceeded \n"+ " result may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static double[] mnbrak(f_x f,double ax,double dx) { return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seiln uzaklk return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { // ax,bx,cx adet tahmin noktas verildiinde iinde minimum bulunan ax,bx,cx adetnoktay bize arama // sonucu bulur // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20; double fa,fb,fc; fa=0;

fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax;

aa[1]=bx; aa[2]=cx; } return aa; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { // ax,bx,cx adet tahmin noktas (bxax ile cx arasnda yer almaldr) // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; // SHFT(double a,double b,double c,d) (a)=(b);(b)=(c);(c)=(d); int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;}

{fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("Maximum iteration is exceeded in berent optimization method"); xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-4; int n=p.length; double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0]; xx=aa[1]; bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[][] linminiter(f_xj f,double p[],double xi[]) { // gives new iteration point int n=p.length; double xmin=linmin(f,p,xi); double aa[][]=new double[2][n]; for (int j=0;j<n;j++) { xi[j] *= xmin; p[j] += xi[j]; aa[0][j]=p[j]; aa[1][j]=xi[j]; } return aa; }

// veri giri k metodu //________________________________________________________________ public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { String s="enter optimum point first estimation values with one empty space in between: "; double [] x0=inputdata(s); f1 f=new f1(); f2 df=new f2(); f3 d2f=new f3(); double r1[]=dampednewton(f,df,d2f,x0); s=" zm seti : \n"+Matrix.toStringT(r1); String s2="Nonlinear multvariable optimization with Newton+Steepest descent:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.12 NON-LINEAR MULTIDIMENSIONAL FLETCHER-REEVES CONJUGATE GRADIENT METHOD Fletcher Reeves Method is a method between Modified Newton-Raphson and Conjugate-Gradient. Remembering the general equation Newton-Raphson type solution with a step function

{x k +1} = {x k } + k H k [{f k }] = {x k } + k H k { g k } = {x k } + k {d k }
where
g k = f ( x1k , x2k , x3k ,..., xnk ) , k = 0,..., n and

[ ]

[ ]

dk = Hk

[ ]

[{g k }]

{x k +1} = {x k } + k {d k }
Remembering that in this definition in pure Newton Raphson step function was taken as unitiy, In the other hand in the steepest descent inverse hessian was taken as unity. In the Fletcher-Reeves method nether is unity, but multiplied total term is calculating by assuming conjugate gradient vectors. Fletcher Reeves calculation starts from a Steepest descent step (assumed invesre hessian equals to unity in the first step)

d0 = g0
k k k g k = f ( x1k , x2 , x3 ,..., xn ) , k = 0,..., n

For k=0 x k +1 = x k k d k For the next steps k = d


k

1 k n

( g k )T ( g k ) ( g k 1 ) T ( g k 1 )

= g k + k d k 1

x k +1 = x k + k d k EXAMPLE PROBLEM General equation :

{x k + 1 } = {x k } + k H k

[ ]

[{f k }] = {x k } + k H k

[ ]

{ g k } = {x k } k {d k }

Steepest descent formula : {x

k +1

} = {x k } + k [{f k }] = {x k } + k {g k } = {x k } + k {d k }

Our problem is finding minimum of function f(x0,x1)=3x02-4x0x1+2x12-x0-x1 starting from {x}=[-2,2]

6 x 4 x1 1 21 f = 0 = 4 x1 4 x0 1 15
0 x0 2 0 = x1 2 1 0 0 6 x0 4 x10 1 x0 x0 1 = 0 + 0 0 x1 x1 4 x0 4 x1 1

1 x0 2 0 21 1 = + 15 x1 2

x1 =221 0
1 x1 =2+15

Substituting this values into the equation

f(x0,x1)=3x02-4x0x1+2x12-x0-x1 y=3(221 )24(221)(2+15)+2(2+15)2(221)(2+15) y = 36 +666+3033 2


Minimum of this one dimensional function is needed. In the example programs brent method is used, in the example just analytical derivative method will be use to solve the minimum

y=666+6066 = 0

=0.109792284
1 1

substituting back into the x0 and x1 gives


1 x0 0.305637982195846 =0.109792284iin 1 = x1 0.353115727002967

So far, what is applied was steepest descent method. Fort he next iteration Flether-Reeves step can be taken

6 x 4 x1 1 -0.578635014836797 g = f = 0 = 4 x1 4 x0 1 -0.810089020771512
k =

0.9910627019697276 ( g k )T ( g k ) = =0.001488082135089681 k 1 T k 1 (g ) (g ) 666.0


0.609884739673680 0.787767788745167
0.3056379 0.609884739673680 + 0.35311572 0.787767788745167

d k = g k + k d k 1 =

x k +1 = x k + k d k =

1=1.138513513513513
2 x0 1.000000000000001 2 = x1 1.249999999999998

Since the function taken is almost linear the result is almost solution values. 5.12-1 Fletcher-Reeves Conjugate Gradient method (NA43A.java)
// Modified Newton method

// uses Newton-Raphson together with Steepest descent import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class f1 extends f_xj { public double func(double x[]) {//function double ff; ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1]; return -ff; } } class f2 extends fi_xi {//first derivative of the function public double[] func(double x[]) { double ff[]={-(2.0*x[1]+2.0-2.0*x[0]),-(2.0*x[0]-4.0*x[1])}; return ff; } } class f3 extends fij_xi { public double[][] func(double x[]) {//second derivative of the function double ff[][]={{2,-2},{-2,-4}}; return ff; } } public class NA38A { public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);} public static double[] df(f_xj f,double xxi[]) { double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=dfdx(f,xxi,i); } return a; }

public static double dfdx(f_xj f,double xxi[],int xref) { int i; double h=1.0e-3; double hh=1.0/h; double xp2h[]=new double[xxi.length]; double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length];

double fp2h,fph,fm2h,fmh; for(i=0;i<xxi.length;i++) { if(i!=xref) {xp2h[i]=xxi[i];xph[i]=xxi[i];xm2h[i]=xxi[i];xmh[i]=xxi[i];} else {xp2h[i]=xxi[i]+2.0*h;xph[i]=xxi[i]+h;xm2h[i]=xxi[i]-2.0*h;xmh[i]=xxi[i]-h;} } fp2h=f.func(xp2h); fph=f.func(xph); fm2h=f.func(xm2h); fmh=f.func(xmh); return (-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; } public static double d2fdx2(f_xj f,double xxi[],int xiref,int xjref) { //second derivative int i; double h=1.0e-4; double hh=1.0/h; int n=xxi.length; double ff; double xp2h[]=new double[xxi.length]; double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh; for(int j=0;j<n;j++) {if(j!=xjref) {xp2h[j]=xxi[j];xph[j]=xxi[j];xm2h[j]=xxi[j];xmh[j]=xxi[j];} else {xp2h[j]=xxi[j]+2.0*h;xph[j]=xxi[j]+h;xm2h[j]=xxi[j]-2.0*h;xmh[j]=xxi[j]-h;} } fp2h=dfdx(f,xp2h,xiref); fph=dfdx(f,xph,xiref); fm2h=dfdx(f,xm2h,xiref); fmh=dfdx(f,xmh,xiref); ff=(-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; return ff; } public static double[][]H(f_xj f,double x[]) { //Hessian matrix int n=x.length; double dy[][]=new double[n][n];; for(int ii=0;ii<n;ii++) { for(int jj=0;jj<n;jj++) { //Hessian matrix dy[ii][jj]=d2fdx2(f,x,ii,jj); } } return dy; } public static double[] dampednewton(f_xj f,fi_xi df,fij_xi d2f,double x[]) { // Modified Newton method // uses Newton-Raphson together with Steepest descent // Steepest descent also uses linmin and brent methods of one dimensionaloptimization double ti=1.0; int i,ii,jj;

int nmax=800; double tolerance=1.0e-14; int n=x.length; double b[]; b=new double[n]; double lambda; double g[]=new double[n]; double G[][]=d2f.func(x);//Hessian matrix double g1,g2,g3,g4,g5; //dy=new double[n][n]; i=0; for(i=0;i<n;i++) { b[i]=1.0; } while( i++ < nmax && Matrix.abs(b) > tolerance ) { g=df.func(x); g1=Matrix.VT_V(g); g2=Matrix.VT_G_V(g,G); g1*=g1; g3=g1/g2; g4=Matrix.VT_G_V(g,Matrix.inverse(G)); lambda=linmin(f,x,g); if(g4>Math.max(g3,0)) b=Matrix.multiply(Matrix.divide(g,G),lambda); else b=Matrix.multiply(g,lambda); x=Matrix.add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"WArn,ng : Maximum number of iteration exceeded \n"+ " result may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static double[] mnbrak(f_x f,double ax,double dx) { return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seiln uzaklk return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { // ax,bx,cx adet tahmin noktas verildiinde iinde minimum bulunan ax,bx,cx adetnoktay bize arama // sonucu bulur // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20;

double fa,fb,fc; fa=0; fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu)

{fa=fb;fb=fc;fc=fu;} aa[0]=ax; aa[1]=bx; aa[2]=cx; } return aa; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { // ax,bx,cx adet tahmin noktas (bxax ile cx arasnda yer almaldr) // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; // SHFT(double a,double b,double c,d) (a)=(b);(b)=(c);(c)=(d); int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) {

if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("Maximum iteration is exceeded in berent optimization method"); xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-4; int n=p.length; double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0]; xx=aa[1]; bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[][] linminiter(f_xj f,double p[],double xi[]) { // gives new iteration point int n=p.length; double xmin=linmin(f,p,xi); double aa[][]=new double[2][n]; for (int j=0;j<n;j++) { xi[j] *= xmin; p[j] += xi[j]; aa[0][j]=p[j]; aa[1][j]=xi[j]; }

return aa; } // veri giri k metodu //________________________________________________________________ public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { String s="enter optimum point first estimation values with one empty space in between: "; double [] x0=inputdata(s); f1 f=new f1(); f2 df=new f2(); f3 d2f=new f3(); double r1[]=dampednewton(f,df,d2f,x0); s=" minimum values : \n"+Matrix.toStringT(r1); String s2="Nonlinear multvariable optimization with Newton+Steepest descent:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

The same algorithm is also prepared as a second version where no derivative of the function is required. All the derivatives are calculated numerically. 5.12-2 Fletcher-Reeves Conjugate Gradient method with numerical derivatives (NA43B.java)
import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class f1 extends f_xj { public double func(double x[]) { // minimisation function double ff; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return ff; } } public class NA43B {

public static double VT_V(double [] left) { int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; } } return aa; } public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } //=================== public static double[] df(f_xj f,double xxi[]) { //G radian vector // derivative of f(x0,x1,x2,...xn) // [df/dx0, df/dx1,df/dx2,....,df/dxn] // df/dxj j=0...x.length // double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=dfdx(f,xxi,i); } return a; }

public static double dfdx(f_xj f,double xxi[],int xref) { int i; double h=1.0e-3; double hh=1.0/h; double xp2h[]=new double[xxi.length];

double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh; for(i=0;i<xxi.length;i++) { if(i!=xref) {xp2h[i]=xxi[i];xph[i]=xxi[i];xm2h[i]=xxi[i];xmh[i]=xxi[i];} else {xp2h[i]=xxi[i]+2.0*h;xph[i]=xxi[i]+h;xm2h[i]=xxi[i]-2.0*h;xmh[i]=xxi[i]-h;} } fp2h=f.func(xp2h); fph=f.func(xph); fm2h=f.func(xm2h); fmh=f.func(xmh); return (-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; } public static double d2fdx2(f_xj f,double xxi[],int xiref,int xjref) { //second derivative int i; double h=1.0e-4; double hh=1.0/h; int n=xxi.length; double ff; double xp2h[]=new double[xxi.length]; double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh; for(int j=0;j<n;j++) {if(j!=xjref) {xp2h[j]=xxi[j];xph[j]=xxi[j];xm2h[j]=xxi[j];xmh[j]=xxi[j];} else {xp2h[j]=xxi[j]+2.0*h;xph[j]=xxi[j]+h;xm2h[j]=xxi[j]-2.0*h;xmh[j]=xxi[j]-h;} } fp2h=dfdx(f,xp2h,xiref); fph=dfdx(f,xph,xiref); fm2h=dfdx(f,xm2h,xiref); fmh=dfdx(f,xmh,xiref); ff=(-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; return ff; } //=================== public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);}

public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-4; int n=p.length; double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0];

xx=aa[1]; bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[][] linminiter(f_xj f,double p[],double xi[]) { int n=p.length; double xmin=linmin(f,p,xi); double aa[][]=new double[2][n]; for (int j=0;j<n;j++) { xi[j] *= xmin; p[j] += xi[j]; aa[0][j]=p[j]; aa[1][j]=xi[j]; } return aa; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d;

if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("Maximum iteration limit is exceeded in brent method"); xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } public static double[] mnbrak(f_x f,double ax,double dx) { return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seiln uzaklk return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20; double fa,fb,fc; fa=0; fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3];

fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax; aa[1]=bx; aa[2]=cx; } return aa;

} public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } // 4. fletcher Reeves method //________________________________________________________________ public static double[] fletcher_reeves( f_xj f,double x[]) { int i,j,ii,jj,k; int nmax=500; double gamma=1.0; double tolerance=1.0e-11; int n=x.length; double alpha; double g[]=new double[n]; double ge[]=new double[n]; double d[]=new double[n]; double beta; double alpha_d; double Q[]=new double[n]; int nn=15; i=0; j=0;k=0; while( j < nmax && gamma > tolerance ) { g=df(f,x); gamma=norm(g); if(k<1) {for(i=0;i<n;i++){d[i]=-g[i];};ge=g;} else {beta=VT_V(g)/VT_V(ge); for(i=0;i<n;i++){Q[i]=beta*d[i];}; } for(i=0;i<n;i++){d[i]=-g[i]+Q[i];} alpha=linmin(f,x,d); for(i=0;i<n;i++){x[i]+=alpha*d[i];} ge=g; j++;k++; if(k>=nn) k=0; //tekrar bir en dik yaa metodu uygulamas } if(i >= nmax) JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration exceeded \n"+ " result may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); }

return a; } public static void main (String args[]) { String s="enter optimum point first estimation values with one empty space in between: "; double [] x0=inputdata(s); f1 f_x=new f1(); double [] r1= fletcher_reeves(f_x,x0); s=" minimum values : \n"+Matrix.toStringT(r1); String s2="Fletcher-Reeves multivariable optimization:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.13 NON-LINEAR MULTIDIMENSIONAL POLAK-RIBIERE CONJUGATE GRADIENT METHOD


A modified form of Fletcher Reeves method is called Polak-Ribiere method. Fletcher Reeves definition k =

( g k )T ( g k ) ( g k 1 ) T ( g k 1 )

With a small change of definition it becomes in Polack-Ribiere method

k =

( g k 1 g k )T ( g k ) ( g k 1 )T ( g k 1 )

5.13-1 Plok-Ribiere Conjugate Gradient method with numerical derivatives (NA43C.java


import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class f1 extends f_xj { public double func(double x[]) { //function to minimize double ff; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return ff; } } public class NA43C { public static double VT_V(double [] left) { //multiplies a vector transpose with a vector int n=left.length;

double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; } } return aa; } public static double VT_X(double [] left,double [] right) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double[] substract(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]-right[i]; } return b; //end of vector substraction method }

public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } //=================== public static double[] df(f_xj f,double xxi[]) { // f(x0,x1,x2,...xn) derivative of the function // [df/dx0, df/dx1,df/dx2,....,df/dxn] // df/dxj j=0...x.length // double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=dfdx(f,xxi,i); } return a; } public static double dfdx(f_xj f,double xxi[],int xref) { int i; double h=1.0e-3; double hh=1.0/h; double xp2h[]=new double[xxi.length]; double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh; for(i=0;i<xxi.length;i++) { if(i!=xref) {xp2h[i]=xxi[i];xph[i]=xxi[i];xm2h[i]=xxi[i];xmh[i]=xxi[i];} else {xp2h[i]=xxi[i]+2.0*h;xph[i]=xxi[i]+h;xm2h[i]=xxi[i]-2.0*h;xmh[i]=xxi[i]-h;} } fp2h=f.func(xp2h); fph=f.func(xph); fm2h=f.func(xm2h); fmh=f.func(xmh); return (-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; } public static double d2fdx2(f_xj f,double xxi[],int xiref,int xjref) { //second derivative int i; double h=1.0e-4; double hh=1.0/h; int n=xxi.length; double ff; double xp2h[]=new double[xxi.length]; double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh;

for(int j=0;j<n;j++) {if(j!=xjref) {xp2h[j]=xxi[j];xph[j]=xxi[j];xm2h[j]=xxi[j];xmh[j]=xxi[j];} else {xp2h[j]=xxi[j]+2.0*h;xph[j]=xxi[j]+h;xm2h[j]=xxi[j]-2.0*h;xmh[j]=xxi[j]-h;} } fp2h=dfdx(f,xp2h,xiref); fph=dfdx(f,xph,xiref); fm2h=dfdx(f,xm2h,xiref); fmh=dfdx(f,xmh,xiref); ff=(-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; return ff; } //=================== public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);}

public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-4; int n=p.length; double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0]; xx=aa[1]; bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[][] linminiter(f_xj f,double p[],double xi[]) { // yeni iterasyon noktasn verir int n=p.length; double xmin=linmin(f,p,xi); double aa[][]=new double[2][n]; for (int j=0;j<n;j++) { xi[j] *= xmin; p[j] += xi[j]; aa[0][j]=p[j]; aa[1][j]=xi[j]; } return aa; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; // SHFT(double a,double b,double c,d) (a)=(b);(b)=(c);(c)=(d); int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm;

double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("Maximum iteration in BRENT is exceeded"); xmin=x; //minumum deer a[0] ile fonksiyon deeri a[1] ile geridndrlmektedir aa[0]=xmin;

aa[1]=fx; return aa; } public static double[] mnbrak(f_x f,double ax,double dx) {return mnbrak(f,ax,(ax-dx),(ax+dx));} public static double[] mnbrak(f_x f,double ax) {return mnbrak(f,ax,(ax-0.1),(ax+0.1));} public static double[] mnbrak(f_x f,double ax,double bx,double cx) { int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20; double fa,fb,fc; fa=0; fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u);

if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax; aa[1]=bx; aa[2]=cx; } return aa; } public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } // 4. Polak Ribiere Method //________________________________________________________________ public static double[] polak_ribiere( f_xj f,double x[]) { int i,j,ii,jj,k; int nmax=500; double gamma=1.0; double tolerance=1.0e-7; int n=x.length; double alpha; double g[]=new double[n]; double ge[]=new double[n]; double d[]=new double[n]; double beta; double alpha_d; double Q[]=new double[n]; int nn=15; i=0; j=0;k=0; while( j < nmax && gamma > tolerance ) { g=df(f,x); gamma=norm(g); if(k<1) {for(i=0;i<n;i++){d[i]=-g[i];};ge=g;} else { // Polack-Ribiere conjugate gradient definition beta=VT_X(substract(ge,g),g)/VT_V(ge); for(i=0;i<n;i++){Q[i]=beta*d[i];}; } for(i=0;i<n;i++){d[i]=-g[i]+Q[i];}

alpha=linmin(f,x,d); for(i=0;i<n;i++){x[i]+=alpha*d[i];} ge=g; j++;k++; if(k>=nn) k=0; //re-apply a step of steepest descent } if(i >= nmax) JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration exceeded \n"+ " result may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } public static double[] verigir(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { String s="enter optimum point first estimation values with one empty space in between: "; double [] x0=verigir(s); f1 f_x=new f1(); double [] r1= polak_ribiere(f_x,x0); s=" minimum values : \n"+Matrix.toStringT(r1); String s2="Polak-Ribiere multivariable non linear optimization:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.14 MULTIDIMENSIONAL NONLINEAR DAVDON-FLETCHER-POWELL METHOD The general equation was in the form of

{x k +1} = {x k } + k H k [{f k }] = {x k } + k H k { g k } = {x k } k {d k }
where
d k = [H k ] g k
1

[ ]

[ ]

g k = f ( x1k , x2k , x3k ,..., xnk ) , k = 0,..., n and

x k +1 = x k k d k

In this method, instead of Hessian an approximation to Hessian will be constructed. Let us define
Qk = [H k ]
1

,k 0

d k = Qk g k

x k +1 = x k k Qk g k = x k k d k
To calculate Q in Davidon Fletcher Powell metod
g k = g k +1 g k

r k = Qk g k
x k = { k +1} = x k +1 x k olarak yazarsak
Qk +1 = Qk r k (r k )T x k (x k )T + , 0k <n k T k (r ) g (x k )T g k

Formulas are used. Program definition of these is given in Program 5.14-1. A second version of the formulas are also existed as follows r k (r k )T x k (x k )T Qk +1 = k [Qk k T k ] + , 0k <n (r ) g (x k )T g k

k =

(x k )T x k , 0k <n (r k )T g k

In this iteratif solution method, iteration starts as Q equals to unit matrix. Q0=I

x k +1 = x k k Qk g k = x k k d k
k again is used by using one of the one variable optimization method. In our program Brent method
is used. First step is always a steepest descent method also in this approach. After in every n step a steepest descent step is used EXAMPLE PROBLEM where
d k = [H k ] g k
1

General equation : {x k +1} = {x k } + k H k


g k = f ( x1k , x2k , x3k ,..., xnk ) , k = 0,..., n and

[ ] [{f
1

}] = {x k } + k H k { g k } = {x k } k {d k }

[ ]

x k +1 = x k k d k

Problem is to find minimum of f(x0,x1)=3x02-4x0x1+2x12-x0-x1 with initial guess {x}=[-2,2]

6 x 4 x1 1 21 f = 0 = 4 x1 4 x0 1 15
0 x0 2 0 = x1 2

1 0 0 6 x0 4 x10 1 x0 x0 1 = 0 + 0 0 x1 x1 4 x0 4 x1 1

1 x0 2 0 21 1 = + 15 x1 2

x1 = -2 -21 0
1 x1 = 2 +15

If these values are substituted into the function f(x0,x1)=3x02-4x0x1+2x12-x0-x1 y=3(221 )2-4(-2 -21)(2 +15)+2(2 +15)2-(-2 -21)-(2 +15) The function becomes; y = 36 +666+3033 2 Minimum of the function ca be find with analytical method as: y = 666+6066 = 0 =-0.109792284
1 1 If this value is substituted in x0 and x1 1 x0 0.305637982195846 = 1 x1 0.353115727002967

For 0=-0.109792284

This first step was steepest descent, as a second step Davidon-Fletcher-Powell can be applied

6 x 4 x1 1 -0.578635014836797 g = f = 0 = 4 x1 4 x0 1 -0.810089020771512
20.4213649 85163200 g k = g k +1 g k = = - 15.810089020771512 1 0 Q = 0 1

20.421364985163200 r k = Qk g k = - 15.810089020771512 2.305637982195846 x k = { k +1} = x k +1 x k = - 1.646884272997033


Qk +1 = Qk r k (r k )T x k (x k )T + (r k )T g k (x k )T g k

0.447456314953212 0.432131073481232 = 0.432131073481232 0.662335969913257

0.608978529603123 d k = Qk g k = 0.786597267404034

x k +1 = x k + k d k

0.305637982195846 0.608978529603123 = + 0.353115727002967 0.786597267404034

If these values are back substituted into the function f(x0,x1)=3x02-4x0x1+2x12-x0-x1 and minimum is solved again it is found that 1=1.1402077151335304:
x02 1.000000000000000 2 = x1 1.250000000000000

Problem 5.14-1 Davidon_Fletcher-Powell method (NA43D.java)


// Davidon-Fletcher-Powell optimization method // Metod Referans : Numerical Analysis, 8th edition Richard L. Burden, J. Douglas Faires 3 // sayfa 617 import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; //ff=7.9+0.13*x[0]+0.21*x[1]-0.05*x[0]*x[0]-0.016*x[1]*x[1]-0.007*x[0]*x[1]; //ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1]; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return ff; //minimum deil maxsimum istendiinden - ile arptk } } public class NA43D { public static double VT_V(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; }

} return aa; } public static double VT_X(double [] left,double [] right) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } public static double turev(f_xj f,double x[],int denklem_ref,int x_ref) { double h0=0.256808; int i,m; int n=7; double f1[]; f1=new double[x.length]; double f2[]; f2=new double[x.length]; double x1[]; x1=new double[x.length]; double x2[]; x2=new double[x.length]; for(i=0;i<x.length;i++) { x1[i]=x[i]; x2[i]=x[i]; } //turev of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; //vector<double> h(n,0); for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++)

{ h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]*1.0094847; f1=df(f,x1); f2=df(f,x2); T[i][0]=( f1[denklem_ref] - f2[denklem_ref])/(2.0*h[i]); x1[x_ref]=x[x_ref]; x2[x_ref]=x[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] df(f_xj f,double xxi[]) { //Gredyen vektr // f(x0,x1,x2,...xn) founksiyonunun trev vektr // [df/dx0, df/dx1,df/dx2,....,df/dxn] // df/dxj j=0...x.length // // bu fonksiyon bilgisayar tarafndan hesaplanmaktadr // kullanc tarafndan tanmlanan f(x) fonksiyonunun // saysal olarak alnan trevidir. // double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=turev(f,xxi,i); } return a; } public static double turev(f_xj f,double xxi[],int x_ref) { // df/dxj double h0=0.256808; int i,m; int n=7; double x1[]; x1=new double[xxi.length]; double x2[]; x2=new double[xxi.length]; for(i=0;i<xxi.length;i++) { x1[i]=xxi[i]; x2[i]=xxi[i];

} //turev of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { //h[i]=h0*Math.pow(r,i); h0/=2.0; h[i]=h0; } //first turev (difference formula) for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]; T[i][0]=(f.func(x1)-f.func(x2))/(2.0*h[i]); x1[x_ref]=xxi[x_ref]; x2[x_ref]=xxi[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] turev(f_xj f_deriv,double x[]) { // df/dxj j=0...x.length // This method calculates turev of a function with more than one independent variable. // Accuracy of method can be adjusted by changing variables h0 and n // function input should be in the form given in abstract class // f_xj,j=0...x.length = df/dx(x_ref) double a[]=new double[x.length]; for(int x_ref=0;x_ref<x.length;x_ref++) { a[x_ref]=turev(f_deriv,x,x_ref); } return a; } public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);}

public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-4; int n=p.length; double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0]; xx=aa[1]; bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[][] linminiter(f_xj f,double p[],double xi[]) { // yeni iterasyon noktasn verir int n=p.length; double xmin=linmin(f,p,xi); double aa[][]=new double[2][n]; for (int j=0;j<n;j++) { xi[j] *= xmin; p[j] += xi[j]; aa[0][j]=p[j]; aa[1][j]=xi[j]; } return aa; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { // ax,bx,cx adet tahmin noktas (bxax ile cx arasnda yer almaldr) // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; // SHFT(double a,double b,double c,d) (a)=(b);(b)=(c);(c)=(d); int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x;

aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("BRENT metodunda maksimum iterasyon says ald"); xmin=x; //minumum deer a[0] ile fonksiyon deeri a[1] ile geridndrlmektedir aa[0]=xmin; aa[1]=fx; return aa; } public static double[] mnbrak(f_x f,double ax,double dx) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seilen uzaklk return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax)

{ // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seiln uzaklk return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { // ax,bx,cx adet tahmin noktas verildiinde iinde minimum bulunan ax,bx,cx adetnoktay bize arama // sonucu bulur // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20; double fa,fb,fc; fa=0; fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) {

fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax; aa[1]=bx; aa[2]=cx; } return aa; } public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } public static double[][] I(int n) { //unit matrix double b[][]; b=new double[n][n]; for(int i=0;i<n;i++) b[i][i]=1.0; return b; } // Davidon - Fletcher-Powell metodu //________________________________________________________________ public static double[] davidon_fletcher_powell( f_xj f,double x[]) { int i,j,k; int nmax=500; double gamma=1.0; double tolerance=1.0e-12 ; int n=x.length; double alpha; double dx[]=new double[n]; double g[] =new double[n]; double ge[]=new double[n]; double dg[]=new double[n]; double d[] =new double[n]; double r[] =new double[n]; double rdg,dxdg; double beta; double alpha_d;

double Q[][]=I(n); double Q1[][]=new double[n][n]; double Q2[][]=new double[n][n]; int nn=15; i=0; j=0;k=0; g=turev(f,x); while( j++ < nmax && gamma > tolerance ) { for(int ii=0;ii<n;ii++) { d[ii]=0.0; for(int kk=0;kk<n;kk++) {d[ii]=d[ii]-Q[ii][kk]*g[kk];} } System.out.println("j="+j+"d="+Matrix.toString(d)); alpha=linmin(f,x,d); System.out.println("alfa="+alpha); if(alpha==0) break; for(i=0;i<n;i++){dx[i]=alpha*d[i];x[i]+=dx[i];ge[i]=g[i];} System.out.println("j="+j+"dx=\n"+Matrix.toString(dx)); System.out.println("j="+j+"x=\n"+Matrix.toString(x)); g=turev(f,x); System.out.println("j="+j+"g=\n"+Matrix.toString(g)); for(i=0;i<n;i++){dg[i]=g[i]-ge[i];} gamma=norm(g); for(int ii=0;ii<n;ii++) {r[ii]=0.0; for(int kk=0;kk<n;kk++) {r[ii]+=Q[ii][kk]*dg[kk];} } rdg=VT_X(r,dg); dxdg=VT_X(dx,dg); Q1=V_VT(r); Q2=V_VT(dx); for(int ii=0;ii<n;ii++) for(int jj=0;jj<n;jj++) Q[ii][jj]=Q[ii][jj]-Q1[ii][jj]/rdg+Q2[ii][jj]/dxdg; gamma=norm(dx); System.out.println("j="+j+"Q=\n"+Matrix.toString(Q)); j++;k++; if(k>=nn) {k=0;Q=I(n);} } if(j >= nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); return x; } public static double[] davidon_fletcher_powell_V1( f_xj f,double x[]) { int i,j,k; int nmax=500; double gamma=1.0; double tolerance=1.0e-12 ; int n=x.length; double alpha; double dx[]=new double[n]; double g[] =new double[n]; double ge[]=new double[n];

double dg[]=new double[n]; double d[] =new double[n]; double r[] =new double[n]; double rdg,dxdg; double beta; double alpha_d; double Q[][]=I(n); double Q1[][]=new double[n][n]; double Q2[][]=new double[n][n]; int nn=15; i=0; j=0;k=0; g=turev(f,x); while( j++ < nmax && gamma > tolerance ) { for(int ii=0;ii<n;ii++) { d[ii]=0.0; for(int kk=0;kk<n;kk++) {d[ii]=d[ii]-Q[ii][kk]*g[kk];} } alpha=linmin(f,x,d); if(alpha==0) break; for(i=0;i<n;i++){dx[i]=alpha*d[i];x[i]+=dx[i];ge[i]=g[i];} g=turev(f,x); for(i=0;i<n;i++){dg[i]=g[i]-ge[i];} gamma=norm(g); for(int ii=0;ii<n;ii++) {for(int kk=0;kk<n;kk++) {r[ii]=Q[ii][kk]*dg[kk];} } rdg=VT_X(r,dg); dxdg=VT_X(dx,dg); beta=dxdg/rdg; Q1=V_VT(r); Q2=V_VT(dx); for(int ii=0;ii<n;ii++) for(int jj=0;jj<n;jj++) Q[ii][jj]=beta*(Q[ii][jj]-Q1[ii][jj]/rdg)+Q2[ii][jj]/dxdg; gamma=norm(dx); j++;k++; if(k>=nn) {k=0;Q=I(n);} } if(j >= nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); return x; } public static double[] inputdata(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) {

Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { String s="enter optimum point first estimation values with one empty space in between: "; double [] x0=inputdata(s); f1 f_x=new f1(); double [] r1= davidon_fletcher_powell(f_x,x0); double [] r2= davidon_fletcher_powell_V1(f_x,x0); s=" solution DFP : \n"+Matrix.toStringT(r1); s+=" solution second DFP : \n"+Matrix.toStringT(r2); String s2="Davidon - Fletcher - Powell multivariable nonlinear function optimization:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.15 MULTIVARIABLE NON-LINEAR BROYDEN-FLETCHER-GOLDBERG-SHANNO (BFGS) METHOD Conjugate gradient methods, Fletcher-Reeves, Polak-Ribiere and Davidon-Fletcher-Powell were investigated inprevious subchapters. These methods were constructing a matrix to represent inverse Hessian matrix. Broyden-Fletcher-Goldberg-Shano methods are also similar, but instead of constructing an inverse hessianrepresentation, it construct directly a hessian matrix representation. Then inverse is calculated by using some form of equation solving method. Algorithm of sucha meth 1. Give an initial estimate vector, x(0) . As the first guess of the Hessianunit matrix take unit matrix H(0) = I. Define conversion factor and maximum number of iterations, nmax, start to iteration with the iteration counter k=0 Calculate the gradian vector c
(0)

= f ( x ( 0) )

2. Calculate the norm of gradian vector,

c (k ) . If norm is smaller than conversion factor stop iteration


(k ) (k ) (k )

c ( k ) < Resultisobtained.
3. 4. 5. 6. 7. Solve vector d from linear system of equation H d = c (k) Find parameter in d direction from minimizing of one independent variable function f(x(k)+ (k)d(k)) It should be noted in here that the first iteration is a steepest descent iteration. Calculate vector x ( k +1) = x ( k ) + ( k ) d ( k ) Calculate new Hessian Matrix as:

H ( k +1) = H ( k ) + D ( k ) + E ( k )

D (k ) =

y ( k ) y ( k )T c ( k ) c ( k )T E ( k ) = ( k ) ( k ) ( y ( k ) .s ( k ) ) (c .d )

s ( k ) = ( k ) d ( k ) y ( k ) = c ( k +1) c ( k )

c ( k +1) = f ( x ( k +1) )
8. Increase iteration parameter k and continiue to the next iteration. In this algorith derivatives can be calculated numericlly if so desired. As the example problem the same problem previously invesigeted will be looked at: function f(x0,x1)=3x02-4x0x1+2x12-x0-x1 with initial conditions {x}=[-2,2] Program 5.15-1 Broyden - Fletcher-Goldfarb-Shanno (BFGS) method
// Broyden - Fletcher-Goldfarb-Shanno (BFGS) method // Method Reference : Intruduction to Optimum Design, Second Edition // Jasbir S. Arora, Elsevier, ISBN: 0-12-064155-0 import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; //ff=5.0*x[0]*x[0]+2.0*x[0]*x[1]+x[1]*x[1]+7.0; return ff; } } class df1 extends fi_xi { public double[] func(double x[]) { //function to be minimize double ff[]=new double[2]; //ff[0]=10.0*x[0]+2.0*x[1]; //ff[1]=2.0*x[0]+2.0*x[1]; ff[0]=6*x[0]-4.0*x[1]-1; ff[1]=-4.0*x[0]+4.0*x[1]-1; return ff; } }

public class NA43E { public static double VT_V(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; } } return aa; } public static double[][] V_VT_y_s(double [] left,double [] y,double s[]) { //multiplies a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; double ss=y_s(y,s); double x1=y_s(y,s); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) {aa[i][j]=left[i]*left[j]/ss;} } return aa; } public static double y_s(double [] y,double [] s) { //scalar multiply two vectors int n=y.length; double aa=0; for(int i=0;i<n;i++) {aa+=y[i]*s[i];} return aa; } public static double VT_X(double [] left,double [] right) {

//multiplies a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } public static double derivative(f_xj f,double x[],int denklem_ref,int x_ref) { double h0=0.256808; int i,m; int n=7; double f1[]; f1=new double[x.length]; double f2[]; f2=new double[x.length]; double x1[]; x1=new double[x.length]; double x2[]; x2=new double[x.length]; for(i=0;i<x.length;i++) { x1[i]=x[i]; x2[i]=x[i]; } //derivative of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; //vector<double> h(n,0); for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++)

{ x1[x_ref]+=h[i]; x2[x_ref]-=h[i]*1.0094847; f1=df(f,x1); f2=df(f,x2); T[i][0]=( f1[denklem_ref] - f2[denklem_ref])/(2.0*h[i]); x1[x_ref]=x[x_ref]; x2[x_ref]=x[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] df(f_xj f,double xxi[]) { //Gradien vektor // f(x0,x1,x2,...xn) founksiyonunun trev vektr // [df/dx0, df/dx1,df/dx2,....,df/dxn] // df/dxj j=0...x.length

// double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=derivative(f,xxi,i); } return a; } public static double derivative(f_xj f,double xxi[],int x_ref) { // df/dxj double h0=0.256808; int i,m; int n=7; double x1[]; x1=new double[xxi.length]; double x2[]; x2=new double[xxi.length]; for(i=0;i<xxi.length;i++) { x1[i]=xxi[i]; x2[i]=xxi[i]; } //derivative of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; for(i=0;i<n;i++)

{ h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { //h[i]=h0*Math.pow(r,i); h0/=2.0; h[i]=h0; } //first derivative (difference formula) for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]; T[i][0]=(f.func(x1)-f.func(x2))/(2.0*h[i]); x1[x_ref]=xxi[x_ref]; x2[x_ref]=xxi[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] derivative(f_xj f_deriv,double x[]) { // df/dxj j=0...x.length // This method calculates derivative of a function with more than one independent variable. // Accuracy of method can be adjusted by changing variables h0 and n // function input should be in the form given in abstract class // f_xj,j=0...x.length = df/dx(x_ref) double a[]=new double[x.length]; for(int x_ref=0;x_ref<x.length;x_ref++) { a[x_ref]=derivative(f_deriv,x,x_ref); } return a; } public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);}

public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-10; int n=p.length;

double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0]; xx=aa[1]; bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[][] linminiter(f_xj f,double p[],double xi[]) { int n=p.length; double xmin=linmin(f,p,xi); double aa[][]=new double[2][n]; for (int j=0;j<n;j++) { xi[j] *= xmin; p[j] += xi[j]; aa[0][j]=p[j]; aa[1][j]=xi[j]; } return aa; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-12; double xmin; double aa[]=new double[2]; int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p;

q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("maximum iteration is exceeded in BRENT"); xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } public static double[] mnbrak(f_x f,double ax,double dx) { return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax) { return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20; double fa,fb,fc; fa=0;

fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax;

aa[1]=bx; aa[2]=cx; } return aa; } public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } public static double[][] I(int n) { //unit matrix double b[][]; b=new double[n][n]; for(int i=0;i<n;i++) b[i][i]=1.0; return b; } // Broyden - Fletcher-Goldfarb-Shanno (BFGS) method // // only function is given derivatives are calculated numerically //________________________________________________________________ public static double[] BFGS( f_xj f,double x[]) { int i,j,k; int nmax=100; double gamma=1.0; double tolerance=1.0e-6 ; int n=x.length; double alpha; double s[]=new double[n]; double c[] =new double[n]; double c1[] =new double[n]; double d[] =new double[n]; double y[] =new double[n]; double alpha_d; double H[][]=I(n); double H1[][]=I(n); //double H[][]={{1,0},{0,0}}; double D[][]=new double[n][n]; double E[][]=new double[n][n]; int nn=15; j=0;k=0; c=derivative(f,x); for(i=0;i<c.length;i++) {d[i]=-c[i];} //first iteration : steepest descent gamma=norm(c); while( k++ < nmax && gamma > tolerance ) { alpha=linmin(f,x,d); for(i=0;i<n;i++) {s[i]=alpha*d[i]; x[i]+=s[i]; }

c1=derivative(f,x); for(i=0;i<n;i++) {y[i]=c1[i]-c[i];} D=V_VT_y_s(y,y,s); E=V_VT_y_s(c,c,d); for(i=0;i<n;i++) {for(j=0;j<n;j++) {H[i][j]=H[i][j]+D[i][j]+E[i][j]; H1[i][j]=H[i][j]; } } for(i=0;i<n;i++) {c[i]=c1[i]; c1[i]=-c1[i]; } d=pivotlugauss(H1,c1); gamma=norm(c); } if(k >= nmax) JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration exceeded \n"+ " result may not be valid","MAXIMUM ITERATION WARNING",JOptionPane.WARNING_MESSAGE); return x; } // fonksiyon ve trev fonksiyonu verilmi //_______________________________________________________________ public static double[] BFGS( f_xj f,fi_xi df,double x[]) { int i,j,k; int nmax=100; double gamma=1.0; double tolerance=1.0e-6 ; int n=x.length; double alpha; double s[]=new double[n]; double c[] =new double[n]; double c1[] =new double[n]; double d[] =new double[n]; double y[] =new double[n]; double alpha_d; double H[][]=I(n); double H1[][]=I(n); //double H[][]={{1,0},{0,0}}; double D[][]=new double[n][n]; double E[][]=new double[n][n]; int nn=15; j=0;k=0; c=df.func(x); for(i=0;i<c.length;i++) {d[i]=-c[i];} //first iteration : steepest descent gamma=norm(c); while( k++ < nmax && gamma > tolerance ) { alpha=linmin(f,x,d); for(i=0;i<n;i++) {s[i]=alpha*d[i]; x[i]+=s[i]; }

c1=df.func(x); for(i=0;i<n;i++) {y[i]=c1[i]-c[i];} D=V_VT_y_s(y,y,s); E=V_VT_y_s(c,c,d); for(i=0;i<n;i++) {for(j=0;j<n;j++) {H[i][j]=H[i][j]+D[i][j]+E[i][j]; H1[i][j]=H[i][j]; } } for(i=0;i<n;i++) {c[i]=c1[i]; c1[i]=-c1[i]; } d=pivotlugauss(H1,c1); gamma=norm(c); } if(k >= nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); return x; }

public static double[] multiply(double[] left,double right) { //multiplying a vector with a constant int i; int n=left.length; double b[]; b=new double[n]; for(i=0;i<n;i++) { b[i]=right*left[i]; } return b; }

public static double[] divide(double[] left,double[][] right) { return pivotlugauss(right,left); } //en kucuk kareler metodu public static double[] pivotlugauss(double a[][],double b[]) { //ksmi pivotlu gauss eleme yntemi int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss eleme int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //pivotlama p=k;

buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } //gauss elemeyi zme for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //geriye doru yerine koyma x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method }

public static double[][] add(double[][] left,double[][] right) { //addition of two matrices int n1=left.length; int m1=left[0].length; int n2=right.length; int m2=right[0].length; int nMax,mMax; int i,j; if(m1>=m2) mMax=m1; else mMax=m2; if(n1>=n2) nMax=n1; else nMax=n2; double b[][]; b=new double[nMax][mMax]; for(i=0;i<n1;i++) { for(j=0;j<m1;j++) { b[i][j]=left[i][j]+right[i][j]; } } return b; //end of matrix addition method } public static double[] verigir(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { String s="enter optimum point first estimation values with one empty space in between: "; double [] x0=verigir(s); f1 f_x=new f1(); double [] r1= BFGS(f_x,x0); s=" solution set BFGS: \n"+Matrix.toStringT(r1); String s2="Broyden - Fletcher-Goldfarb-Shanno (BFGS) multivariable optimization:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.16 HOMOTOPY OR CONTINUATION METHODS Continuation method is defined at chaer 4.3 to solve non-linear system of equations. As we can remember from Newton-Raphson method optimization problem can be defined as solution of system of nonlinear equation for the derivative of the given function. System of equations were defined as:

f x 1 ( x1m+1 x1m ) f m+1 m x2 ( x2 x2 ) m m+1 m f(x1,x2,x3,,xn f = f and = ( x3 +1 x3 ) and x ... 3 m m ( xn +1 xn ) ... f xn 2 f x 2 21 f x x 1 2 [H ] = 2 f x1 x3 ... 2 f x1 xn 2 f x1 x2 2 f 2 x2 2 f x1 x2 ... 2 f x2 xn 2 f x1 x3 2 f x21x3 2 f 2 x3 ... 2 f x3 xn ... ... ... ... ... 2 f x1 xn 2 f x2 xn 2 f x1 xn ... 2 f 2 xn

for the (k+1)th iteration

[H ]{
m

m +1

} = [{f m }]

And fort h inverse of the Hessian matrix, it can be written as

{ m +1} = H m

[ ]

[{f m }] . In this equation the term H m

[ ]

is inverse Hessian matrix .

In order to solve this equation Homotopy (Continuation ) method given in chapter 4.3 will be used. Please refer to this chapter for the details of the method. 5.16-1 Continuation method for optimization (NA34F.java)
// NA34F Continuation-homotopy optimization // Method Referance : Numerical Analysis, 8th edition Richard L. Burden, J. Douglas Faires 3

// page 637 import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============ Tanm gereken fonksiyon ================ abstract class fi_xi { // multifunction multi independent variable // vector of dependent variables are returned // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x) returns the value of f[0] and f[1] // as a two dimensional vector abstract double[] func(double x[]); } abstract class fij_xi { // multifunction multi independent variable // for n independent variable n*n matrix of // dependent variables are returned // example f[0][0]=x[0]+Math.sin(x[1]) f[0][1]=x[0]-x[1] // f[1][0]=x[0]*x[0]-x[1] f[1][1]=Math.exp(x[0]+x[1]*x[1] // func(x) returns the value of f[0][0], f[0][1] // f[1][0], f[1][1] // as a two dimensional matrix abstract double[][] func(double x[]); } //========================= class f1 extends f_xj { public double func(double x[]) {//zm istenen fonksiyon double ff; ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1]; return -ff; //minimum deil maxsimum istendiinden - ile arptk } } //========================= public class NA34F { // Continuity-homotopy method for optimization // (Solution of root of derivatives of function) public static double[] multiply(double left,double[] right) { //multiplying a vector with a constant int i; int n=right.length; double b[]; b=new double[n]; for(i=0;i<n;i++) {b[i]=left*right[i];} return b; }

public static double[] multiply(double[][] left,double[] right) { //multiplication of one matrix with one vector int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right.length; double[] b; b=new double[m2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) { b[ii]=0; } return b; } for(i=0;i<m1;i++) { b[i]=0; for(k=0;k<n1;k++) b[i]+=left[i][k]*right[k]; } return b; //end of multiply of a matrix and a vector } public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method } public static double[][] inv(double[][] a) { // INVERSION OF A MATRIX // inversion by using gaussian elimination // with full pivoting int n=a.length;

int m=a[0].length; double b[][]; b=new double[n][n]; int indxc[]; int indxr[]; double ipiv[]; indxc=new int[n]; indxr=new int[n]; ipiv=new double[n]; int i,j,k,l,ll,ii,jj; int icol=0; int irow=0; double big,dum,pivinv,temp; if(n!=m) { System.out.println("Matrix must be square "); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } for(i=0;i<n;i++) for(j=0;j<n;j++) b[i][j]=a[i][j]; for(i=0;i<n;i++) { big=0.0; for(j=0;j<n;j++) { if(ipiv[j] != 1) for(k=0;k<n;k++) { if(ipiv[k] == 0) { if(Math.abs(b[j][k]) >= big) { big=Math.abs(b[j][k]); irow=j; icol=k; } } else if(ipiv[k] > 1 ) { System.out.println("error : inverse of the matrix : singular matrix-1"); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } } } ++ ipiv[icol]; if(irow != icol) for(l=0;l<n;l++) { temp=b[irow][l]; b[irow][l]=b[icol][l]; b[icol][l]=temp;

} indxr[i]=irow; indxc[i]=icol; if(b[icol][icol] == 0.0) { System.out.println("error : inverse of the matrix : singular matrix-2"); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } pivinv=1.0/b[icol][icol]; b[icol][icol]=1.0; for(l=0;l<n;l++) b[icol][l] *=pivinv; for(ll=0;ll<n;ll++) if(ll != icol) { dum=b[ll][icol]; b[ll][icol]=0.0; for(l=0;l<n;l++) b[ll][l]-= b[icol][l]*dum; } } for(l=n-1;l>=0;l--) { if(indxr[l] != indxc[l]) for(k=0;k<n;k++) { temp=b[k][indxc[l]]; b[k][indxc[l]]=b[k][indxr[l]]; b[k][indxr[l]]=temp; } } return b; } public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);} public static double[] df(f_xj f,double xxi[]) { //Gredien vector // f(x0,x1,x2,...xn) founksiyonunun trev vektr // [df/dx0, df/dx1,df/dx2,....,df/dxn] // df/dxj j=0...x.length double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=dfdx(f,xxi,i); } return a; }

public static double dfdx(f_xj f,double xxi[],int xref) { int i; double h=1.0e-3; double hh=1.0/h; double xp2h[]=new double[xxi.length];

double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh; for(i=0;i<xxi.length;i++) { if(i!=xref) {xp2h[i]=xxi[i];xph[i]=xxi[i];xm2h[i]=xxi[i];xmh[i]=xxi[i];} else {xp2h[i]=xxi[i]+2.0*h;xph[i]=xxi[i]+h;xm2h[i]=xxi[i]-2.0*h;xmh[i]=xxi[i]-h;} } fp2h=f.func(xp2h); fph=f.func(xph); fm2h=f.func(xm2h); fmh=f.func(xmh); return (-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; } public static double d2fdx2(f_xj f,double xxi[],int xiref,int xjref) { //ikincitrev matrisi int i; double h=1.0e-4; double hh=1.0/h; int n=xxi.length; double ff; double xp2h[]=new double[xxi.length]; double xph[]=new double[xxi.length]; double xm2h[]=new double[xxi.length]; double xmh[]=new double[xxi.length]; double fp2h,fph,fm2h,fmh; for(int j=0;j<n;j++) {if(j!=xjref) {xp2h[j]=xxi[j];xph[j]=xxi[j];xm2h[j]=xxi[j];xmh[j]=xxi[j];} else {xp2h[j]=xxi[j]+2.0*h;xph[j]=xxi[j]+h;xm2h[j]=xxi[j]-2.0*h;xmh[j]=xxi[j]-h;} } fp2h=dfdx(f,xp2h,xiref); fph=dfdx(f,xph,xiref); fm2h=dfdx(f,xm2h,xiref); fmh=dfdx(f,xmh,xiref); ff=(-fp2h+8.0*fph-8.0*fmh+fm2h)/12.0*hh; return ff; } public static double[][]H(f_xj f,double x[]) {//Hessian Matrix int n=x.length; double dy[][]=new double[n][n];; for(int ii=0;ii<n;ii++) { for(int jj=0;jj<n;jj++) { //Hessian matrx dy[ii][jj]=d2fdx2(f,x,ii,jj); } } return dy; } public static double[] continuationRK4(f_xj f,double x[],int N) { //==================================================== // Roots of nonlinear system of equation with Homotophy-Continuaton method // 4.th degree Runge-Kutta equations

// yi+1 = yi + (1/6)*( k1 + 2k2 +2k3+k4)h // k1=f(xi,yi) // k2=f(xi+0.5h,yi+0.5k1h) // k3=f(xi+0.5h,yi+0.5k2h) // k4=f(xi+h,yi +k3h) //=================================================== // x : independent variable vector // y : dependent variable vetor // dy : derivativeof the dependent vector // N : number of points in Runge-Kutta differential equation solution int i; int nmax=400; double tolerance=1.0e-20; int n=x.length; double h=1.0/(double)N; double b[]=new double[n]; double x1[]=new double[n]; double k[][]=new double[4][n]; double A[][]=new double[n][n]; b=multiply(-h,df(f,x)); for(i=0;i<N;i++) { x1=x; A=H(f,x1); k[0]=multiply(inv(A),b); x1=add(x,multiply(0.5,k[0])); A=H(f,x1); k[1]=multiply(inv(A),b); x1=add(x,multiply(0.5,k[1])); A=H(f,x1); k[2]=multiply(inv(A),b); x1=add(x,k[2]); A=H(f,x1); k[3]=multiply(inv(A),b); for(int j=0;j<n;j++) {x[j]=x[j]+1.0/6.0*(k[0][j]+2.0*k[1][j]+2.0*k[2][j]+k[3][j]);} } return x; } public static double[] continuationRK6(f_xj f,double x[],int N) { //==================================================== // Roots of nonlinear system of equation with Homotophy-Continuaton method // 6.th degree Runge-Kutta equations // yi+1 = yi + (1/90)*( 7k1 + 32k3 +12k4+32k5+7k6)h // k1=f(xi,yi) // k2=f(xi+0.25h , yi+0.25k1h) // k3=f(xi+0.25h , yi+0.125k1h+0.125k2h) // k4=f(xi+0.5h , yi - 0.5k2h+k3h) // k5=f(xi+0.75h , yi + (3/16)k1h+(9/16)k4h) // k6=f(xi+h , yi - (3/7)k1h+(2/7)k2h+(12/7)k3h - (12/7)k4h+(8/7)k5h) //=================================================== // x : independent variable vector // y : dependent variable vetor // dy : derivativeof the dependent vector // N : number of points in Runge-Kutta differential equation solution

int i; int nmax=400; double tolerance=1.0e-20; int n=x.length; double h=1.0/(double)N; double b[]=new double[n]; double x1[]=new double[n]; double k[][]=new double[6][n]; double A[][]=new double[n][n]; b=multiply(-h,df(f,x)); for(i=0;i<N;i++) { x1=x; A=H(f,x1); // k1=f(xi,yi) k[0]=multiply(inv(A),b); x1=add(x,multiply(0.25,k[0])); A=H(f,x1); // k2=f(xi+0.25h , yi+0.25k1h) k[1]=multiply(inv(A),b); x1=add(x,add(multiply(0.125,k[0]),multiply(0.125,k[1]))); A=H(f,x1); // k3=f(xi+0.25h , yi+0.125k1h+0.125k2h) k[2]=multiply(inv(A),b); x1=add(x,add(multiply(-0.5,k[1]),k[2])); A=H(f,x1); // k4=f(xi+0.5h , yi - 0.5k2h+k3h) k[3]=multiply(inv(A),b); x1=add(x,add(multiply((3.0/16.0),k[0]),multiply((9.0/16.0),k[3]))); A=H(f,x1); // k5=f(xi+0.75h , yi + (3/16)k1h+(9/16)k4h) k[4]=multiply(inv(A),b); x1=add(x, add(multiply((-3.0/7.0),k[0]),add(multiply((2.0/7.0),k[1]), add(multiply((12.0/7.0),k[2]), add(multiply((-12.0/7.0),k[3]),multiply((8.0/7.0),k[4])))))); A=H(f,x1); // k6=f(xi+h , yi - (3/7)k1h+(2/7)k2h+(12/7)k3h - (12/7)k4h+(8/7)k5h) k[5]=multiply(inv(A),b); // yi+1 = yi + (1/90)*( 7k1 + 32k3 +12k4+32k5+7k6)h for(int j=0;j<n;j++) {x[j]=x[j]+1.0/90.0*(7.0*k[0][j]+32.0*k[2][j]+12.0*k[3][j]+32.0*k[4][j]+7.0*k[5][j]);} } return x; } public static double[] newton_continuationRK4(f_xj f,double x[],int N) { // Newton-Raphson combined with Continuation (RK4) method x=continuationRK4(f,x,4); double ti=1.0; int i; int nmax=400; double tolerance=1.0e-10; int n=x.length; double b[]; b=new double[n]; for(i=0;i<n;i++)

{ b[i]=1.0; } i=0; while( i++ < nmax && Matrix.abs(b) > tolerance ) { b=Matrix.multiply(Matrix.divide(df(f,x),H(f,x)),-ti); x=Matrix.add(x,b); } if(i >= nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); System.out.println(); return x; } public static double[] inputdata() { String s=JOptionPane.showInputDialog("kk tahmin deerlerini arada bir boluk brakarak giriniz: "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) {f1 ff=new f1(); double [] x0=inputdata(); double [] r= continuationRK4(ff,x0,4); double [] r1= continuationRK6(ff,x0,4); double [] r2=newton_continuationRK4(ff,x0,4); String s=" optimum continuity method with RK4 : \n"+Matrix.toStringT(r); s+=" optimum continuity method with RK6 : \n"+Matrix.toStringT(r1); s+=" optimum continuity method with Newton-Raphson plus RK4 : \n"+Matrix.toStringT(r2); String s1="multivariable continuity method for optimization : "; JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.17 MULTIVARIABLE NONLINEAR BROYDEN SECANT METHOD WITH SHERMAN MORRISON FORMULA Broyden secant method wasdefined in chapter 4.2. It is basically secant root finding method for multivariable functions.

As we can remember from Newton-Raphson method optimization problem can be defined as solution of system of nonlinear equation for the derivative of the given function. System of equations were defined as:

f x 1 ( x1m+1 x1m ) f m+1 m x2 ( x2 x2 ) m m+1 m f(x1,x2,x3,,xn f = f and = ( x3 +1 x3 ) and x ... 3 m m ( xn +1 xn ) ... f xn 2 f x 2 21 f x x 1 2 [H ] = 2 f x1 x3 ... 2 f x1 xn 2 f x1 x2 2 f 2 x2 2 f x1 x2 ... 2 f x2 xn 2 f x1 x3 2 f x21x3 2 f 2 x3 ... 2 f x3 xn ... ... ... ... ... 2 f x1 xn 2 f x2 xn 2 f x1 xn ... 2 f 2 xn

for the (k+1)th iteration

[H ]{
m

m +1

} = [{f m }]
1

And fort h inverse of the Hessian matrix, it can be written as

{ m +1} = H m

[ ]

[{f m }] . In this equation the term H m

[ ]

is inverse Hessian matrix .

Hesian matrix can be replaced by numerical differencedefinition of the essian [ H ] as

g j xk

(x )
(i )

g j ( x (i ) + ek h) g j ( x (i ) ) h

h in the equation is finite difference parameter (x) and ek is unit vektor (kth componet is 1 the other components are 0). i is the iteration number. Note that secant method is always approach to the roots less quickly compare to Newton-Raphson method. In order to improve number of iterations for the solutions, a modified version of secant method, Broyden method will be used. In Broyden method when x(0) is a given value, first iteration, x(1), will be calculated by using Newton-Raphson method. For the following steps [ H ( x (0) )] {x (1) x ( 0 ) } = {g ( x (0) } form of the equation can be changed to be
[ A1 ] {x (1) x ( 0) } = {g ( x (1) g ( x (0) } In order to have this equation valid for every vector z, vector should satisfy following equations [ A1 ] {z} = [g ( x (0) )]z {x (1) x ( 0 ) }t z = 0 t
By using this defination Formula to calculate [ A1 ] can be derived.

[ A1 ] = H ( x ) +
(0)

[{g ( x (1) ) g ( x (0) )} H ( x (0) )( x (1) x (0) )]*( x (1) x (0) )t

x (1) x (0)
2 2

2 2

Where the term

x (1) x ( 0 )

is a two norm. Norms was defined in previous chapter. After finding [ A1 ] new

value for vector x can be constructed.

x (2) = x (1) A11 g ( x (1) )


When x ( 2 ) is found, this value can be used to calculate x ( 3) .General Formula will become:

[ Ai ] = [ Ai 1 ] +

[{g ( x (1) ) g ( x (0) )} [ Ai 1 ]( x (1) x (0) )]*( x (1) x (0) )t x (1) x (0)
2 2

x (i +1) = x (i ) Ai 1 g ( x (i ) ) One difficulty in this equation is the requirement of solving inverse of the matrix each time which requires solving of system of equation by a method such as Gauss elimination. But Gauss method is expensive in calculation time. A beter alternative could be using ShermanMorrison approach. This approach calculates inverse directly as an iterative process. Matrix Ai-1 can directly be calculated from value Ai-1-1. Equations of Sherman-Morrison approach is as follows si = {x (1) x ( i 1) } yi = { f ( x ( i ) ) f ( x ( i 1) )}
( A + xy t ) 1 = A1 A1 xy t A1 1 + y t A1 x
1

1 yi Ai 1si t 1 Ai = A + si 2 si 2

yi Ai 1si t 1 Ai1 si Ai 1 2 si 2 1 1 Ai = A + sit t 1 yi Ai 1si t si 1 + si Ai 1 2 si 2


1

Ai = A1 +

( si Ai11 yi ) sit Ai11 sit Ai11 yi

// NA34G Broyden secant method with Sherman-Morrison formula // optimizasyon // Reference : Numerical Analysis, 8th edition Richard L. Burden, J. Douglas Faires 3 // page 617 import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; //============= Function to be defined ================ class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; //ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1]; ff=3.0*x[0]*x[0]-4.0*x[0]*x[1]+2.0*x[1]*x[1]-x[0]-x[1]; return ff; } } class fxb extends fi_xi { public double[] func(double x[]) { double ff[]=new double[2]; ff[0]=6.0*x[0]-4.0*x[1]-1.0; ff[1]=-4.0*x[0]+4.0*x[1]-1.0; return ff; } } class fb extends fij_xi { fi_xi f; public fb(fi_xi fi) {f=fi;} public double[][] func(double x[]) { int n=x.length;

double a[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { a[i][j]=turev(x,i,j); } } return a; } public double turev(double x[],int denklem_ref,int x_ref) { double h0=0.0256; int i,m; int n=7; double f1[]; f1=new double[x.length]; double f2[]; f2=new double[x.length]; double x1[]; x1=new double[x.length]; double x2[]; x2=new double[x.length]; for(i=0;i<x.length;i++) { x1[i]=x[i]; x2[i]=x[i]; } double T[][]; T=new double[n][n]; double h[]; h=new double[n]; for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]; f1=f.func(x1); f2=f.func(x2); T[i][0]=( f1[denklem_ref] - f2[denklem_ref])/(2.0*h[i]); x1[x_ref]=x[x_ref]; x2[x_ref]=x[x_ref]; } for(m=1;m<n;m++) {

for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } } public class NA34G { //Broyden secant method with Sherman-Morrison formula public static double[] multiply(double left,double[] right) { //multiplying a vector with a constant int i; int n=right.length; double b[]; b=new double[n]; for(i=0;i<n;i++) {b[i]=left*right[i];} return b; } public static double[] multiply(double[][] left,double[] right) { //multiplication of one matrix with one vector int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right.length; double[] b; b=new double[m2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) { b[ii]=0; } return b; } for(i=0;i<m1;i++) { b[i]=0; for(k=0;k<n1;k++) b[i]+=left[i][k]*right[k]; } return b; //end of multiply of a matrix and a vector } public static double[] multiply(double[] left,double[][] right) { //multiplication of one vector with one matrix

int ii,jj,i,j,k; int m2=right[0].length; int n2=right.length; int m1=left.length; double[] b; b=new double[m1]; if(n2 != m1) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n2;ii++) { b[ii]=0; } return b; } for(i=0;i<m2;i++) { b[i]=0; for(k=0;k<m1;k++) b[i]+=right[i][k]*left[k]; } return b; //end of multiply of a vector and a matrix } public static double VT_X(double [] left,double [] right) { //multiplies a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double[] add(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax; int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]+right[i]; } return b; //end of vector addition method

} public static double[][] multiply(double left,double[][] right) { //multiplying a matrix with a constant int i,j; int n=right.length; int m=right[0].length; double b[][]; b=new double[n][m]; for(i=0;i<n;i++) { for(j=0;j<m;j++) b[i][j]=right[i][j]*left; } return b; //end of multiplying a matrix with a constant double } public static double[][] add(double[][] left,double[][] right) { //addition of two matrices int n1=left.length; int m1=left[0].length; int n2=right.length; int m2=right[0].length; int nMax,mMax; int i,j; if(m1>=m2) mMax=m1; else mMax=m2; if(n1>=n2) nMax=n1; else nMax=n2; double b[][]; b=new double[nMax][mMax]; for(i=0;i<n1;i++) { for(j=0;j<m1;j++) { b[i][j]=b[i][j]+left[i][j]; } } for(i=0;i<n2;i++) { for(j=0;j<m2;j++) { b[i][j]=b[i][j]+right[i][j]; } } return b; //end of matrix addition method } public static double[] substract(double[] left,double[] right) { //addition of two vectors int n1=left.length; int n2=right.length; int nMax;

int i; if(n1>=n2) nMax=n1; else nMax=n2; double b[]; b=new double[nMax]; for(i=0;i<n1;i++) { b[i]=b[i]+left[i]; } for(i=0;i<n2;i++) { b[i]=b[i]-right[i]; } return b; //end of vector addition method } public static double[][] inv(double[][] a) { // INVERSION OF A MATRIX // inversion by using gaussian elimination // with full pivoting int n=a.length; int m=a[0].length; double b[][]; b=new double[n][n]; int indxc[]; int indxr[]; double ipiv[]; indxc=new int[n]; indxr=new int[n]; ipiv=new double[n]; int i,j,k,l,ll,ii,jj; int icol=0; int irow=0; double big,dum,pivinv,temp; if(n!=m) { System.out.println("Matrix must be square "); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } for(i=0;i<n;i++) for(j=0;j<n;j++) b[i][j]=a[i][j]; for(i=0;i<n;i++) { big=0.0; for(j=0;j<n;j++) { if(ipiv[j] != 1) for(k=0;k<n;k++) { if(ipiv[k] == 0) { if(Math.abs(b[j][k]) >= big)

{ big=Math.abs(b[j][k]); irow=j; icol=k; } } else if(ipiv[k] > 1 ) { System.out.println("error : inverse of the matrix : singular matrix-1"); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } } } ++ ipiv[icol]; if(irow != icol) for(l=0;l<n;l++) { temp=b[irow][l]; b[irow][l]=b[icol][l]; b[icol][l]=temp; } indxr[i]=irow; indxc[i]=icol; if(b[icol][icol] == 0.0) { System.out.println("error : inverse of the matrix : singular matrix-2"); for(ii=0;ii<n;ii++) for(jj=0;jj<n;jj++) b[ii][jj]=0.0; return b; } pivinv=1.0/b[icol][icol]; b[icol][icol]=1.0; for(l=0;l<n;l++) b[icol][l] *=pivinv; for(ll=0;ll<n;ll++) if(ll != icol) { dum=b[ll][icol]; b[ll][icol]=0.0; for(l=0;l<n;l++) b[ll][l]-= b[icol][l]*dum; } } for(l=n-1;l>=0;l--) { if(indxr[l] != indxc[l]) for(k=0;k<n;k++) { temp=b[k][indxc[l]]; b[k][indxc[l]]=b[k][indxr[l]]; b[k][indxr[l]]=temp; } } return b; } public static double norm(double v[])

{ // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } public static double[][] multiply(double[] left,double[] right) { //multiplication of one vector with one matrix int ii,jj,i,j,k; int m2=left.length; int n2=right.length; int m1=left.length; double[][] b; b=new double[m2][n2]; for(i=0;i<m2;i++) { for(k=0;k<n2;k++) b[i][k]=left[i]*right[k]; } return b; //end of multiply of a vector and a matrix } public static double[] broyden(fi_xi f,double x[]) { //==================================================== // roots of non-linear system of equation fb J=new fb(f); int i; int nmax=400; double tolerance=1.0e-15; int n=x.length; double b[]=new double[n]; double v[]=new double[n]; double s[]=new double[n]; double w[]=new double[n]; double y[]=new double[n]; double z[]=new double[n]; double p; double ut[]=new double[n]; double x1[]=new double[n]; double A[][]=new double[n][n]; x1=x; A=J.func(x1); v=f.func(x1); A=inv(A); s=multiply(-1.0,multiply(A,v)); x=add(x,s); int k=1; while(k<nmax) { w=v; v=f.func(x); y=substract(v,w); z=multiply(-1.0,multiply(A,y));

p=-VT_X(s,z); ut=multiply(s,A); //********* A=add(A,multiply((1.0/p),multiply(add(s,z),ut))); s=multiply(-1.0,multiply(A,v)); x=add(x,s); if(norm(s)<tolerance) break; k++; } return x; } public static double[] inputdata() { String s=JOptionPane.showInputDialog("enter optimum point first estimation values with one empty space in between: "); StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { fa f =new fa(); fxb f1=new fxb(); double [] x0=inputdata(); double [] r= broyden(f1,x0); String s=" optimization with broyden secant method : \n"+Matrix.toStringT(r); String s1="optimization with broyden secant method : "; JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.18

SCHOLASTIC METHODS : GENETIC ALGORITHMS

5.18.1 NATURAL SELECTION AND SYSTEM OF GENES IN NATURE

Genetic algorithms are a widely use modern programming method. It specially has interseting applications in artificial intelligence area. The method is based on evolution mechanism observed in the nature. Natural evolution is based on survival of the fittest in nature. Father and Mother passed charecteristics to their child. Sometimes additional characteristics can also be added by mutation. If these characteristics improve the chance of survival the new child has a good chance to pass this characteristic to his/her child. If the new characteristics decrease the chance of survival child will die early and the chance of passing these characteristics will be less. So in avarage all the characteristics that will improve survival will pass to the next generations. Characteristic carrying mechanisms in the biological world is called gens. Genes correspond to regions within deoxyribonucleic acid DNA, a molecule composed of a chain of four different types of nucleotides, adenine, cytosine, guanine, and thymine.the sequence of these nucleotides is the genetic information organisms inherit. Each cell contains one or more chromosomes. Chromosomes are basically long DNA sequences. Chromosoms has DNA blocks called genes, genes determine characteristics in the organism, such as eye color. Chromosome section called gene can carry different nucleatides combinasions. Different combinations of the nucleotides in gene structure can carry out different characteristic such as different eye color. It is called a genotype. When a new child is created, Double spiral molecule of DNA split into two single spiral an done spiral from mother combines with the other spiral from mother to create a new child. The new DNA structure carry out genotypes both from mother and father. Sometimes errors can happen in recombination and copying processes, this is called a mutation. Natural selection process is based on survival of the fittest. I new child posses some genotypes that makes it easier to survive,It will pass this properties to his/her child, if not it will die or eliminate from the different gene combination pool evailable. If mutation was not existed, the most sucsessfull genes will fill out all the gene pool after several generations, but mutation might create sometimes a beter survivor, and this new gene will started to exist in the gene pool. This mechanism ensures that population will not g oto a uniform structure.

5.18.2 GENETIC ALGORITHMS, GENERAL DEFINITIONS Genetic calculations are first refered 1960s in Evolutionsstrategie in original by I Rechenberg. With the meaning we are using today, it is developed and explained by John Holland and his students in "Adaption in Natural and Artificial Systems Evrimsel hesaplama ilk defa 1960l yllarda I Rechenbergin Evolutionsstrategie in original evrim statejileri almasnda yer almtr. Fikir daha sonra baka aratrclarca gelitirilmitir. Genetik algoritmalar John Holland ve rencileri tarafndan ilk defa bu gnk anlamyla oluturulmutur. 1975 ylnda baslan "Adaption in Natural and Artificial Systems kitabnda yazar bu tekniin nasl kullanlacan aklamtr Genetic algorithms basically mimic the natures system of survival to solve optimization problems. In general an algorithm of optimization by genetic algorithms can be summerized as: [START] Set up a population of m member (one chromosome for each member, x gene-vector of independent variables) [START] for each chromosome (vector of independent variables) calculate f(x) fitting value, repeat this for each individual in population [NEW GENERATION] Generate new generations according to the following steps [SELECTION] Select parents from population by considering their fitting values. Better fits should have a better chance to be selected but no one is eliminated completely from the selection process. [PRODUCTION] Produce a new individual(s) from the parents. Replace parent generation with the new generation. Some of the successful parents can retain in the new generation [MUTATION] mutate new generations with a ceratain (given) mutation rate [ACCEPT] Add new individuals to the population. [REPEAT] use the new generations to create new populations by using the same rule [TEST] when the required conditions are occured or enough generations are generated stop the simulation [LOOP] If the required conditions are not occured continue the process Gens an choromosomes are the basic calculating units of the genetic calculations. Mathematically all the independent variales are an individual gene. . Chromsome is the vector of genes(independent varables). SAmple of some chromosome types utilised fort he computer simulation are given as the following table
chromosome A chromosome B chromosome C 5.3243 0.4556 2.3293 2.4545 ABDJEIFJDHDIERJFDLDFLFEGT (back), (foward), (right), (left), (turn)

chromosome D

(+ x (/ 5 y))

chromosome E ( do_until step wall )

In the natural genes the fitting function is the survival in the nature, in mathmatical genes optimization (Maximisation) of the fitting function will be the base. Fitting functions usually added up and converted into the percantage form

This percentage is called converted fitting function. Percentage in the total pipe is biger for the individuals with a beter fitting functions. Parents selected by using converted fitting functions and random number, so that individuals with a beter fitting has a beter chance to be selected. From the selected population of parent candidates actual parents are also selected by random selection. Then from parents gene the gene of th new population is created. Mathematically this process is called crossing. In crossing process genes of each parents are randomly selected to create the new child. For example if the gene is binary numbers and if single crossing is used, both parents gene is divided from a randomly selected point the first part of first parent is combined with the second part of second paren tor the second part of the first parent combined with the first part of the second parent to crate a child. For example for a binary group of 8 bit: If 8*random(0-1) = 5 and if the first 5 bit is taken from the first parent and the remaining bits from the second parent : The new child: 11001011 + 11011111 = 11001111

Chromosome 1 (First Parent) Chromosome 2 (Second parent)

11011 | 00100110110 11011 | 11000011110 11011 | 11000011110 11011 | 00100110110

1. Child 2. Child

If double crossing applied, two random numbers are generated and genes are divided into three parts. The first part is taken from the first parent, second part is taken from the second parent and the third part is taken from the first parent(or vice versa) For example for a binary group of 8 bit i: Random1=8*random(0-1) = 2 Random2=8*random(0-1) = 6 First 2 bit is taken from the first parent, from third to 6th bit is taken from the second parent and 7th and 8th bit is taken from the first parent. 11001011 + 11011111 = 11011111

As a third method every gene is selected by a random number decision process. If generated random number is smaller than 0.5 the first parents binary digit is selected, if not second parents binary digit is selected. 11001011 + 11011111 = 11001011

5.18.3 GENETIC ALGORITHMS, OPTIMIZATION In this section actual optimization process with genetic algorithms will be given in details. AS it is seen earlier in this chapter optimization process is minimisation or maximisation of a multivariable function f(x1,x2,x3,..xi) It should again be rminded tt maximisation and minimisation is the same process with a sign change max [ f(x) ] = min [- f(x) ] = min [ g(x) ] Another mathematical fact that will be used in genetic algorithms is that addition of a constant to the function will not change the optimum point max [ f(x) ] = max [ f(x) + C ] = max [ g() ] In genetic algorithm applications the nature of the algorithm is to find the maximum, in order to get correct percentages constant numbers are added into the function to ensure the resulting value is always positive. In engineering optimization we usually face the real numbers. IF a real number is to be represented as a binary sequence, a conversion process between binary digits and real numbers are needed. If n+1 independent variable is existed as (x0,x1,x2,..xi..xn) In order to apply genetic algorithms lower and upper bounds of each variable should be known. xo : xo_minimum xo_maximum x1 : x1_minimum x1_maximum . xi : xi_minimum xi_maximum

xn : xn_minimum xn_maximum definitins should be given. Ifbinary digit system is used number of representation digits also should be known. If the independent variable is defined as xi_min <= xi <= xi_max N bit binary Binary equivalent of N digits can be calculated as follows: Binary group with n digit first converted into a 10 base integer number as

(b n , b N -1 , b N -2 ,.b1 , b 0 ) 2 = ( b i 2i )10 = x'


Then the integer number is converted into a binary digit by using the following conversion process
x_max - x_min x = x_min + x ' 2N 1
i =0

N 1

Inverse process can also be apply to convert digits back to the binary digits. Each independent variables can be given with one gene, and the set of independent variables can be given b a a vector of genes. Evaluation function is assumd to be in the following form class fa extends f_xj { double func (double x[]) { return (21.5+x[0]*Math.sin(4.0*Math.PI*x[0])+x[1]*Math.sin(20.0*Math.PI*x[1])); } } The value sets of independent variables as whole together with the evaluated function value will be called as genotype (as borrowed from natures genetic system). Number of chromosomes in each individual can be one or more. Number of individual will be set by the user of the program. For each individual starting variable values set by using random selection. Function values are calculated for each genotype. It is the fitting value of the genome. All fitting values are adde and then normalise to get percantage base fitting values. Percantage base fitting values are adde by starting from zero to 1and created cumulative fitting scale for all population. Cumulative fitting values are based on the scale of the fitting function. If a random number between 0 to 1 is selected, the chance of this number is fall in a region with a bigger fitting function is grater than that of a small one. After an individual in the population is selected as a parent cendidate, it can be removed from the selection list or it can remain in the list(a second selection can be allowed). From the selected parent candidates two parents are selected randomly to create a new child.In the new child population a selected percentage can be allowed (selected again randomly) to mutate. Mutation in a binary code can be achived to flip an indivudual binary digit(from 0 to 1 or from 1 to 0). Process continues for a defined number of generations. 5.18.4 GENETIC ALGORITHMS, CLASS GENE Genetic algorithms mimic the natural selection process of genes. The first structure used here to mimic natural genes are used binary numbers as defined in previous subchapter. Constructor of gene class can accept either double or binary numbers. Total bit length is a variable but it is limited to 64 bits (063). Gene class consist of gene crossing and mutation processes as well. Program 5.18.4-1 Gene class

import java.util.*; import java.io.*; public class Gene extends Object { //Gene class generates digital(binary) array to represent a double number //actual binary defination changes depends on number of bits, xmin and xmax //binary data stored in b BitSet array int N; //number of binary digits(number of bits in a byte) //N should be less than 64 double xN;//Limit value for integer(2^N-1) double xmin,xmax;//maximum and minimum limits of double number BitSet b; //binary array that strore the integer public Gene(double ixmin,double ixmax,int iN) {//randomly generate the gene N=iN; if(N>63) N=63; b=new BitSet(N); xN=Math.pow(2.0,N)-1; xmin=ixmin; xmax=ixmax; //generate random numbers to fill binary array setbits(); } public Gene(String s,double ixmin,double ixmax) { //generate gene equal to a given string N=s.length(); b=new BitSet(N); xN=Math.pow(2.0,N)-1; xmin=ixmin; xmax=ixmax; for(int i=N-1;i>=0;i--) if(s.charAt(i)=='1') b.set(N-1-i); } public Gene(double number,double ixmin,double ixmax,int iN) { //Generate a gene from a given double number N=iN;//number of binary digits(number of bits in a byte) if(N>63) N=63; b=new BitSet(N); xN=StrictMath.pow(2.0,N)-1; xmin=ixmin; xmax=ixmax; //z : integer equivalent of the double number long z=(long)((number-xmin)/(xmax-xmin)*xN); //System.out.println("inside double"+z); long y=(long)xN; double r=z; for(int i=0;i<N;i++) { if((z-(z/2)*2)!=0) {b.set(i);} z/=2; } }

public Gene() { //empty Gene N=1; b=new BitSet(N); xN=StrictMath.pow(2.0,N)-1; xmin=0; xmax=1.0; b.set(0); } public Gene(Gene g) { // copy a Gene from another one copyGene(g); } public void copyGene(Gene g) { N=g.N; b=(BitSet)g.b.clone(); xN=Math.pow(2.0,N)-1; xmin=g.xmin; xmax=g.xmax; } public Gene copyGene() { Gene g=new Gene(this); return g; } public Gene(Gene Father,Gene Mother) { //crosover type will be selected randomly double ix=Math.random(); if(ix<0.1) setGene(Father,Mother,"regular"); else if(ix>0.9) setGene(Father,Mother,"single"); else setGene(Father,Mother,"double"); }

public void setGene(Gene Father,Gene Mother,String crossovertype) { xmin=Father.xmin; xmax=Father.xmax; N=Father.N; xN=Father.xN; b=new BitSet(N); int ir1,ir2,ir3; if(crossovertype.equals("regular")) { for(int i=0;i<N;i++) {if(boolean_random()) { if(Father.b.get(i)) b.set(i);} else { if(Mother.b.get(i)) b.set(i);} }

} else if(crossovertype.equals("single")) { ir1=int_random(); if(boolean_random()) { for(int i=0;i<ir1;i++) { if(Father.b.get(i)) b.set(i);} for(int i=ir1;i<N;i++) { if(Mother.b.get(i)) b.set(i);} } else { for(int i=0;i<ir1;i++) { if(Mother.b.get(i)) b.set(i);} for(int i=ir1;i<N;i++) { if(Father.b.get(i)) b.set(i);} } } else if(crossovertype.equals("double")) { ir1=int_random(); ir2=int_random(); int ix; if(ir1>ir2) {ix=ir2;ir2=ir1;ir1=ix;} if(boolean_random()) { for(int i=0;i<ir1;i++) { if(Father.b.get(i)) b.set(i);} for(int i=ir1;i<ir2;i++) { if(Mother.b.get(i)) b.set(i);} for(int i=ir2;i<N;i++) { if(Father.b.get(i)) b.set(i);} } else { for(int i=0;i<ir1;i++) { if(Mother.b.get(i)) b.set(i);} for(int i=ir1;i<ir2;i++) { if(Father.b.get(i)) b.set(i);} for(int i=ir2;i<N;i++) { if(Mother.b.get(i)) b.set(i);} } } }

public void cross1(double crossratio,Gene Father,Gene Mother) {// create gene from two parent genes // crossover process // Genes from Father and Mother should have the same limit values xmin=Father.xmin; xmax=Father.xmax; N=Father.N; xN=Father.xN; b=new BitSet(N); int ir1=(int)(crossratio*N+0.5); for(int i=0;i<ir1;i++) { if(Father.b.get(i)) b.set(i);} for(int i=ir1;i<N;i++) { if(Mother.b.get(i)) b.set(i);} }

public void cross2(double crossratio,Gene Father,Gene Mother) {// create gene from two parent genes // crossover process // Genes from Father and Mother should have the same limit values xmin=Father.xmin; xmax=Father.xmax; N=Father.N; xN=Father.xN; b=new BitSet(N); int ir1=(int)(crossratio*N+0.5); for(int i=0;i<ir1;i++) { if(Mother.b.get(i)) b.set(i);} for(int i=ir1;i<N;i++) { if(Father.b.get(i)) b.set(i);} } public long getXP() { //returns intermediate integer of double number equivalent long y=1; double r=xN; long z=0; for(int i=0;i<N;i++) { if(b.get(i)) {z+=y;} y*=2; } return z; } public double getX() {//return double number equivalent of bit set number return (xmin+(xmax-xmin)/xN*getXP()); } public void clear() {//fill the entire population with zero b.clear(0,N);} public void fill() {//fill the entire population with ones b.clear(0,N);b.flip(0,N);} public String toBitSetString() {//return bit set array values as defined in class BitSet return b.toString(); } public String toString() { // return bit set array values as a binary number definition // plus double value of array String s="("; for(int i=N-1;i>=0;i--) if(b.get(i)) s+="1"; else s+="0"; s+=") "+getX();

return s; } public boolean boolean_random() { if(Math.random()<=0.5) return false; else return true; } public int int_random() { int x1=(int)(Math.random()*N); return x1; } public double double_random() { return (xmin+Math.random()*(xmax-xmin)); } public void setbits() { b.clear(); for(int i=0;i<N;i++) { if(boolean_random()) b.set(i); } } public void flip(int i) { b.flip(i); } public void mutate() { //fip one bit of data int i1=int_random(); if(i1<0) i1=0; b.flip(i1); } }

Few examples are given to explain gene processes. In the first example 5 63 bit gene is created and the results are listed Program 5.18.4-2 GeneTest class creating random genes 63 bits between 0 to 10 limit values
public class geneTest { public static void main(String arg[]) { Gene x1=new Gene(0,10,63); for(int i=0;i<5;i++) { x1.setbits();System.out.println(x1.toString());} } }

---------- Capture Output ---------> "D:\java\bin\javaw.exe" geneTest (101000000101110101010001111001110000000000010110110101010110101) 6.264239491429992 (110010010000011011101100011100011111001101001111110111010000110) 7.852618959616957 (001110000001100001011101010101101010000100101111011101001010011) 2.191217743394967 (101101000111010001101010011110100111110110110000100001011010010) 7.049013661432487 (100001011001100110111001101111111110011001001010001101001110000) 5.218769162659464 > Terminated with exit code 0.

Program 5.18.4-3 GeneTest1 class creating 63 bits between 0 to 10 limit values, value of 3.2
public class geneTest1 { public static void main(String arg[]) { Gene x1=new Gene(3.2,0,10,63);System.out.println(x1.toString());} }

---------- Capture Output ---------> "D:\java\bin\javaw.exe" geneTest1 (010100011110101110000101000111101011100001010001111011000000000) 3.2 > Terminated with exit code 0.

Program 5.18.4-3 GeneTest1 class creating 63 bits between 0 to 10 limit values, value of 3.2
public class geneTest2 { public static void main(String arg[]) {Gene x1=new Gene("010100011110101110000101000111101011100001010001111011000000000",0,10); System.out.println(x1.toString());} }

---------- Capture Output ---------> "D:\java\bin\javaw.exe" geneTest2 (010100011110101110000101000111101011100001010001111011000000000) 3.2 > Terminated with exit code 0.

5.18.5 GENETIC ALGORITHMS, CLASSES GENOTYPE1 AND GENETIC1 Genotype1 can be considered as vector of Gene class variables. In means of Genetics, it represents a chromosome. In mathematically, it is a set of independent variables (x0,x1,x2,..xi..xn) The main class of genetic algorithms is class Genetic1. Genetic optimization process with all the stages defined above calculated in this class. Some of the variables of Genetic1 should be defined by user. These variables are: int POPSIZE; //number of individuals in population int MAXGENS; // maximum number of generations int NVARS; // number of variables in chromosome (number of independent variables) int N; //bit size of the binary genes

double PXOVER; //probability of cross-over; double PMUTATION; //probability of mutation method evaluate is the main method to do genetic algorithm calculations. It should be emhised again that all the function values in Genetic algorithms should be positive, if it is not the case, please add up a constant to convert the values into positive values. This proses does not change the optimum. Maximum of function f(x1,x2,x3,,xn) is same as the maximum of g(x1,x2,x3,,xn) = f(x1,x2,x3,,xn) +C. This process help to avoid errors during the calculations of cumulative percentage fitting function. Program 5.18.5-1 Genotype1 and Genetic1 classes
import java.io.*; import java.text.*; import java.util.Locale; abstract class f_xj { // single function multi independent variable // a single value is returned indiced to equation_ref // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[]); } class genotype1 { // each genotype1 is member of the population // Genotype is like a chromosone in real life genetic systems // it can be thought as an evaluation point of a function with n variable // f(xi)=f(x1,x2,x3,..,x_nvars) public Gene G[] ; // string of variables makes a genotype1 public double Fitness; // genotype1's fitness f(xi) public double Upper[]; // gene's upper bound; xi_lower < xi < xi_upper public double Lower[]; // Gene's lower bound; xi_lower < xi < xi_upper public double RFitness; // the relative fitness public double CFitness; // cumulative fitness public int NVARS; // Number of variables(Genes) public int N; // Number of bits defining each gene public boolean Breeder; public genotype1(int iNVARS,double low[],double high[],int iN,f_xj fi) {setgenotype1(iNVARS,low,high,iN,fi);} public void setgenotype1(int iNVARS,double low[],double high[],int iN,f_xj fi) { setN(iN); setNVARS(iNVARS); G=new Gene[NVARS]; Upper=new double[NVARS]; Lower=new double[NVARS]; setLower(low); setUpper(high); setGene(); setFitness(fi);

} public genotype1(genotype1 gi,f_xj fi) {setgenotype1(gi,fi);} public void setgenotype1(genotype1 gi,f_xj fi) { setN(gi.N); setNVARS(gi.NVARS); G=new Gene[NVARS]; Upper=new double[NVARS]; Lower=new double[NVARS]; setLower(gi.Upper); setUpper(gi.Lower); for(int i=0;i<NVARS;i++) G[i]=gi.G[i].copyGene(); setFitness(fi); } public genotype1 copygenotype(f_xj fi) { genotype1 g1=new genotype1(this,fi); return g1; } public void setN(int iN) {N=iN;} public void setNVARS(int iNVARS) {NVARS=iNVARS;} public void setLower(double lbound[]) {for(int i=0;i<NVARS;i++) Lower[i]=lbound[i];} public void setUpper(double ubound[]) {for(int i=0;i<NVARS;i++) Upper[i]=ubound[i];} public void setFitness(f_xj fi) { Fitness=fi.func(getGene());} public void setRFitness(double isum) {RFitness=Fitness/isum;} public void setCFitness(double ix) {CFitness=ix;} public double getLower(int j) {return Lower[j];} public double getUpper(int j) {return Upper[j];} public double getFitness() {return Fitness;} public double getRFitness(){return RFitness;} public double getCFitness(){return CFitness;} public void breed() {Breeder=true;} public void not_breed() {Breeder=false;} public boolean isBreed() {return Breeder;}

public void setGene(int val) { G[val]=new Gene(Lower[val],Upper[val],N);} public void setGene() { for(int j=0;j<NVARS;j++) setGene(j); } public double getGene(int val) { return G[val].getX();} public double[] getGene() { double x[]=new double[NVARS]; for(int j=0;j<NVARS;j++) x[j]=getGene(j);

return x; } public String toString() { //x values, Fitness,relative fitness and cumulative fitness String s=""; for(int j=0;j<NVARS;j++) s+=G[j].toString()+" F:"; s+=getFitness(); s+="RF:"+getRFitness(); s+="CF:"+getCFitness(); return s; } } //end of class genotype1 public class Genetic1 { // Maximizing a function int POPSIZE; //population size int MAXGENS; //maximum number of generations; int NVARS; //number of independent variables of function int N; //number of genes in each variable double PXOVER; //probability of crossover; double PMUTATION; //proportion of mutated variables int Generation; //Current generation number int NBreeder; //Number of survivors in a generation int Best; //The best genotype1 in the population genotype1 Population[]; //f_xj f; // function to be avaluated by genetic algorithm public Genetic1(int iPOPSIZE,int iMAXGENS,int iNVARS,int iN, double iPXOVER,double iPMUTATION) { POPSIZE=iPOPSIZE; MAXGENS=iMAXGENS; NVARS=iNVARS; N=iN; PXOVER=iPXOVER; PMUTATION=iPMUTATION; NBreeder=0; Population=new genotype1[POPSIZE+1]; } public void setPopulation(double low[],double up[],f_xj fi) { for(int i=0;i<POPSIZE+1;i++) { Population[i]=new genotype1(NVARS,low,up,N,fi);} } public genotype1[] copyPopulation(f_xj fi) { genotype1 Po[]=new genotype1[POPSIZE+1]; for(int i=0;i<POPSIZE+1;i++) { Po[i]=new genotype1(Population[i],fi); Po[i].Fitness=Population[i].Fitness; Po[i].RFitness=Population[i].RFitness; Po[i].CFitness=Population[i].CFitness; }

return Po; } public void evaluate(f_xj fi) { int mem; int j; double x[]=new double[NVARS]; for(mem=0;mem<POPSIZE;mem++) { //System.out.println(Population[mem].toString()); Population[mem].setFitness(fi); if(Population[mem].getFitness() > Population[POPSIZE].getFitness()) { Best=mem; Population[POPSIZE]=Population[mem]; for(j=0;j<NVARS;j++) { Population[POPSIZE].G[j].copyGene(Population[mem].G[j]); }//end of for(j=0.. }//end of if }//end of for(mem=0;.. }

public void setRFCF(genotype1 Pi[]) { //calculates relative and cumulative fitness functions int mem; int PN=Pi.length; double sum=0.0; //total fitness of population for(mem=0;mem<PN;mem++) {sum+=Pi[mem].Fitness;} //System.out.println("sum="+sum); //calculate relative fitness of each genotype1 for(mem=0;mem<PN;mem++) {Pi[mem].setRFitness(sum);} //calculate cumulative fitness Pi[0].setCFitness(Pi[0].getRFitness()); for(mem=1;mem<PN;mem++) {Pi[mem].setCFitness(Pi[mem-1].getCFitness()+Pi[mem].getRFitness()); } } public void toString(genotype1 Pi[]) { int mem; int PN=Pi.length; for(mem=0;mem<PN;mem++) { //list them System.out.println("Population["+mem+"]="+Pi[mem].toString()); } } public void select(f_xj fi) {

//select the new generation members of population double r; int mem; setRFCF(Population); //create a new population; genotype1 Po[]=copyPopulation(fi); setRFCF(Po); for(int i=0;i<POPSIZE;i++) { mem=0; r=Math.random(); double gf1=Population[i].getCFitness(); double gf2; if (gf1 > r) Po[i]=Population[mem].copygenotype(fi); for(mem=1;mem<POPSIZE;mem++) { gf2=Population[mem].getCFitness(); if( gf2>=r && gf1< r ) {Po[i]=Population[mem].copygenotype(fi);break;} } } setRFCF(Po); Population=Po; evaluate(fi); //toString(Population); } public void crossover(f_xj fi) { int i,count; int mem; int POne,a; int PTwo,b; int point; //select members for breeding int iselect[]=new int[POPSIZE]; int counter=0; double r; for(mem=0;mem<POPSIZE;mem++) { r=Math.random(); if(r < PXOVER) { iselect[counter++]=mem; Population[mem].breed(); NBreeder++; } else Population[mem].not_breed(); } //System.out.println("iselect="+Matrix.toString(iselect)); //let also best of the population to breed Population[Best].breed(); //loop through the population select breeding pairs for(mem=0;mem<POPSIZE;mem++) { //select two in popolation in random a=(int)(Math.random()*NBreeder)+1; b=(int)(Math.random()*NBreeder)+1;

count=0; POne=0; PTwo=0; //select two individuals for breeding for(i=0;i<POPSIZE;i++) { if(Population[i].isBreed()) { count++; if(count==a) POne=count; if(count==b) PTwo=count; } } //perform a crossover; genotype1 Po[]=new genotype1[2]; Po[0]=Population[POne].copygenotype(fi); Po[1]=Population[PTwo].copygenotype(fi); for(i=0;i<NVARS;i++) { Population[POne].G[i].cross1(Math.random(),Po[0].G[i],Po[1].G[i]); Population[PTwo].G[i].cross1(Math.random(),Po[0].G[i],Po[1].G[i]); } Population[POne].setFitness(fi); Population[PTwo].setFitness(fi); } } public void mutate(f_xj fi) { int i; double lbound,hbound; int nmutations; int member; int var; nmutations=(int)((double)(POPSIZE*NVARS*PMUTATION*N)); for(i=0;i<nmutations;i++) { member=(int)(Math.random()*POPSIZE); var=(int)(Math.random()*NVARS); //replace the old value with a new mutated one Population[member].G[var].mutate(); //after mutation recalculate the function value Population[member].setFitness(fi); } } public String report() { String s; int i; double best_value; double avg; double stdev; double sum_square; double square_sum; double sum; double xx; sum=0;

sum_square=0.0; for(i=0;i<POPSIZE;i++) { xx= Population[i].getFitness(); sum+=xx; sum_square=xx*xx; } avg=sum/(double)POPSIZE; square_sum=sum*sum/(double)POPSIZE; stdev=Math.sqrt(square_sum); best_value=Population[POPSIZE].getFitness(); double aa[]=new double[NVARS+1]; for(i=0;i < NVARS;i++) { aa[i]=Population[POPSIZE].getGene(i); } aa[NVARS]=Population[POPSIZE].getFitness(); //s="Generation = "+Generation+"best value = "+Matrix.toString(aa)+"Average = "+avg+"Standart Deviation = "+stdev; s="Generation = "+Generation+"best value = "+Matrix.toString(aa); return s; } public double[] getBest() { double aa[]=new double[NVARS+1]; for(int i=0;i < NVARS;i++) { aa[i]=Population[POPSIZE].getGene(i); } aa[NVARS]=Population[POPSIZE].getFitness(); return aa; } public double[] calculate(f_xj fi,boolean report) { evaluate(fi); Generation=0; while(Generation<MAXGENS) { Generation++; NBreeder=(int)(0.8*POPSIZE); select(fi); crossover(fi); mutate(fi); evaluate(fi); //if(report) //System.out.println(report()); } return getBest(); } }

Now that a Genetic algorithm is available to us, optimization of actual functions can be carry out. The first example problem will be f(x0,x1)=2x0 x1+2x0-x02-2x12+10 . This function was used in previous sub-sections of optimization, but an additional constant value of 10 is added to ensure

positive cumulative numbers. Please remember that we do not have any dificulty solving this problem with previously given geometrical based optimization algorithms. Note that the result is probabilistic so that it will never be exactly the same like geometric methods. Program 5.18.5-2 Genetic1 class test program NA104
import java.io.*; class fa extends f_xj { public double func(double x[]) { //function required to be solved double ff; ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1]+10.0; return ff; } } class NA104 { public static void main(String args[]) throws IOException { int N=18; //number of figits in binary genes int POPSIZE=200; //population size int MAXGENS=200; //maximum number of generations int NVARS=2; //number of variables (nember of genes in genome double PXOVER=0.3; //probability of crossover double PMUTATION=0.02; //probability of mutation fa fx=new fa(); Genetic1 xx=new Genetic1(POPSIZE,MAXGENS,NVARS,N,PXOVER,PMUTATION); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-1.0;high[0]=5.0; low[1]=-1.0;high[1]=5.0; xx.setPopulation(low,high,fx); System.out.println("best=\n"+Matrix.toStringT(xx.calculate(fx,false))); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA104 best= 1.992183655485746 0.992172211350293 11.999938725593887 > Terminated with exit code 0. As a second example a one dimensional function f(x0)=x0 sin(10x0)+1 function will be nvestigated . The
pplot of the function shows the difficulty of this function: it has many local maximum. It is very hard for geometric methods to get a solution of such function unless some jump mechanisms built into the solution.

Program 5.18.5-3 Genetic1 class test program NA105


import java.io.*; class fa extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=x[0]*Math.sin(10.0*Math.PI*x[0])+3.0; return ff; //maksimum testi } }

class NA105 { public static void main(String args[]) throws IOException { int N=18; int POPSIZE=200; int MAXGENS=200; int NVARS=1; double PXOVER=0.3; double PMUTATION=0.02; fa fx=new fa(); Genetic1 xx=new Genetic1(POPSIZE,MAXGENS,NVARS,N,PXOVER,PMUTATION); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-1.0;high[0]=2.0; xx.setPopulation(low,high,fx); System.out.println("best=\n"+Matrix.toStringT(xx.calculate(fx,false))); }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA105 best= 1.850608255799315 4.850270390533590 > Terminated with exit code 0.

As a last example of class Genetic1 a two variable nonlimear function is defined as f(x,y)=21.5+x*sin(4.0**y)+y*Math.sin(20.0* *y) The 3D plot of the function

It is clear from the plot that this problem also have the same kind of difficulty as the previous problem. Program 5.18.5-4 Genetic1 class test program NA106
import java.io.*; class fa extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=21.5+x[0]*Math.sin(4.0*Math.PI*x[0])+x[1]*Math.sin(20.0*Math.PI*x[1]); return ff; //maksimum testi } } class NA106 { public static void main(String args[]) throws IOException { int N=18; int POPSIZE=200; int MAXGENS=200; int NVARS=2;

double PXOVER=0.3; double PMUTATION=0.02; fa fx=new fa(); Genetic1 xx=new Genetic1(POPSIZE,MAXGENS,NVARS,N,PXOVER,PMUTATION); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-3.0;high[0]=12.1; low[1]=4.1;high[1]=5.8; xx.setPopulation(low,high,fx); System.out.println("best=\n"+Matrix.toStringT(xx.calculate(fx,false))); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA106 best= 11.625646307549696 5.725091648451418 38.850259608040204 > Terminated with exit code 0.

To see the statistical effect on the result if the same problem re-run with exactly the same parameters:
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA106 best= 11.628180802081305 5.325199986267037 38.443672502487340 > Terminated with exit code 0.

Another interesting equation to optimise z=f(x,y)= 1.0+cos(*(x-3.0))*cos(2.0* *(y-2.0))/(1+(x-3.0)*(x-3.0)+(y-2.0)*(y-2.0));

Program 5.18.5-4 Genetic1 class test program NA107


import java.io.*; import javax.swing.*;

class fa extends f_xj { //zm istenen fonksiyon double func(double x[]) { double ff= 1.0+Math.cos(Math.PI*(x[0]-3.0))*Math.cos(2.0*Math.PI*(x[1]-2.0))/ (1+(x[0]-3.0)*(x[0]-3.0)+(x[1]-2.0)*(x[1]-2.0)); return ff; //maksimum testi; } } class fb extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; fa fx=new fa(); return -fx.func(x); //maksimum testi } } class NA107 { public static void main(String args[]) throws IOException { int N=63; int POPSIZE=200; int MAXGENS=200; int NVARS=2; double PXOVER=0.3; double PMUTATION=0.02; fa fax=new fa(); fb fbx=new fb(); Genetic1 xx=new Genetic1(POPSIZE,MAXGENS,NVARS,N,PXOVER,PMUTATION); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-10.0;high[0]=10.0; low[1]=-10.0;high[1]=10.0; xx.setPopulation(low,high,fax); double a[]=xx.calculate(fax,false); double x0[]=new double[NVARS]; for(int i=0;i<NVARS;i++) x0[i]=a[i]; String s="results of genetic algorithms =\n"+Matrix.toStringT(a); String s2="Genetic algorithm multi-variable optimization:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); } }

5.18.6 GENETC ALGORITHMS BY USING GAUSS RANDOM NUMBERS In the previous model, binary numbers are used as the base of constructing the genetic structure. In this second model Gauss random numbers will be used as the basis of the model. Gauss random numbers construct a bell shape distribution around the given point. We will start to create a Gauss random number algorithm.

Program 5.18.6-1Gausrandom method example


import java.io.*; //java girdi cikti sinifini cagir class randeneme { public static double gaussrandom(double xort,double r) { int iset=0; double gset=0; double fac,rsq,v1,v2; double aret=0; if(iset==0) { do{ v1=2.0*Math.random()-1.0; v2=2.0*Math.random()-1.0; rsq=v1*v1+v2*v2; }while(rsq>=1.0 || rsq==0.0); fac=Math.sqrt(-2.0*Math.log(rsq)/rsq); gset=v1*fac; iset=1; aret=v2*fac; } else { iset=0; aret=gset; } return aret/8.0*r+xort; } public static void main(String args[]) { double xort,r; double gr=0; double x[]=new double[201]; double y[]=new double[201]; xort=0.0; r=2.0; for(int i=0;i<=200;i++) {x[i]=xort-r/2+r*(double)i/200.0;} for(int i=0;i<=5000000;i++) {gr=gaussrandom(xort,r);for(int j=0;j<200;j++){if(gr>=x[j] && gr< x[j+1]){y[j]+=1.0;}}}

Plot pp=new Plot(x,y); pp.setColor(0,0,51,204); pp.setPlotType(0,41); pp.plot(); } }

In the example program 5000000 Gauss random points are generated and plotted. Now genetic classes can be generated by using this Gauss Random numbers concept. This set of program is very similar exceppt definition of the genes. With this type of gene definitions when cross-over of the genes are calculated border values should be considered carefully and not to exceed.

Program 5.18.6-2Gausrandom Gene class Gen.java


// Dr. M. Turhan OBAN // EGE University School of Engineering, Mechanical Engineering Department // package : Numerical analysis package // subject : optimization // group : Genetic Algorithms gauss random metodu kullanarak. // Gen Class // Gen class generates a double number represent a gene import java.util.*; import java.io.*; public class Gen extends Object { public double xmin,xmax;//maximum and minimum limits of double number public double x; public double gaussrandom(double ixmin,double ixmax,double ix) { double d1=ixmax-ix; double d2=ix-ixmin; double d=Math.abs(Math.min(d1,d2)); ix=gaussrandom(ix,d); return ix;

} public void gaussrandom() { double d1=xmax-x; double d2=x-xmin; double d=Math.abs(Math.min(d1,d2)); x=gaussrandom(x,d); } public double gaussrandom(double xort,double r) { //r yaylma blgesi //xort ortalama deer int iset=0; double gset=0; double fac,rsq,v1,v2; double aret=0; if(iset==0) { do{ v1=2.0*Math.random()-1.0; v2=2.0*Math.random()-1.0; rsq=v1*v1+v2*v2; }while(rsq>=1.0 || rsq==0.0); fac=Math.sqrt(-2.0*Math.log(rsq)/rsq); gset=v1*fac; iset=1; aret=v2*fac; } else { iset=0; aret=gset; } return aret/4.0*r+xort; } public Gen(double ixmin,double ixmax) {//randomly generate the gene xmin=ixmin; xmax=ixmax; x=double_random(); } public Gen(double s,double ixmin,double ixmax) { xmin=ixmin; xmax=ixmax; //generate gene equal to a given string if((s>=xmin)&& (s<=xmax)) x=s; else if(s<xmin) x=xmin; else if(s>xmax) x=xmax;

public Gen() { xmin=0; xmax=1.0; x=xmin+Math.random(); } public Gen(Gen g) { xmin=g.xmin; xmax=g.xmax; x=g.x; } public void copyGen(Gen g) { xmin=g.xmin; xmax=g.xmax; x=g.x; } public Gen copyGen() { Gen g=new Gen(this); return g; } public Gen(Gen Father,Gen Mother) {setGen(Father,Mother);} public void setGen(Gen Father,Gen Mother) { xmin=Father.xmin; xmax=Father.xmax; double r1=xmax-xmin; double r=Math.random(); double d1=xmax-Father.x; double d2=Father.x-xmin; double d=Math.abs(Math.min(d1,d2)); double r2=gaussrandom(Father.x,d); d1=xmax-Mother.x; d2=Mother.x-xmin; d=Math.abs(Math.min(d1,d2)); double r3=gaussrandom(Mother.x,d); x=r2*r+r3*(1-r); System.out.println("r2="+r2+"r3="+r3+"r="+r); if(x<xmin) x=xmin;

else if(x>xmax) x=xmax; } public void cross1(double crossratio,Gen Father,Gen Mother) {xmin=Father.xmin; xmax=Father.xmax; double r1=xmax-xmin; double d1=xmax-Father.x; double d2=Father.x-xmin; double d=Math.abs(Math.min(d1,d2)); double gr1=gaussrandom(Father.x,d); d1=xmax-Mother.x; d2=Mother.x-xmin; d=Math.abs(Math.min(d1,d2)); double gr2=gaussrandom(Mother.x,d); x=gr1*crossratio+gr2*(1-crossratio); if(x<xmin) x=xmin; else if(x>xmax) x=xmax; } public void cross2(double crossratio,Gen Father,Gen Mother) {xmin=Father.xmin; xmax=Father.xmax; double r1=xmax-xmin; double d1=xmax-Father.x; double d2=Father.x-xmin; double d=Math.abs(Math.min(d1,d2)); double gr1=gaussrandom(Father.x,d); d1=xmax-Mother.x; d2=Mother.x-xmin; d=Math.abs(Math.min(d1,d2)); double gr2=gaussrandom(Mother.x,d); x=gr1*(1.0-crossratio)+gr2*crossratio; if(x<xmin) x=xmin; else if(x>xmax) x=xmax; } public double getX() {//return double number equivalent of bit set number return x; } public boolean boolean_random() { if(Math.random()<=0.5) return false; else return true; } public double double_random() { return (xmin+Math.random()*(xmax-xmin));} public void mutate() {

double r=(xmax-xmin); double d1=xmax-x; double d2=x-xmin; double d=Math.abs(Math.min(d1,d2)); x=gaussrandom(x,d); }}

Program 5.18.6-3 Gausrandom Genome (vector of gene) class class gentype.java


import java.io.*; import java.text.*; import java.util.Locale; abstract class f_xj { // single function multi independent variable // a single value is returned indiced to equation_ref // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[]); } class gentype { // each genotype1 is member of the population // Genotype is like a chromosone in real life genetic systems // it can be thought as an evaluation point of a function with n variable // f(xi)=f(x1,x2,x3,..,x_nvars) public Gen G[] ; // string of variables makes a genotype1 public double Fitness; // genotype1's fitness f(xi) public double Upper[]; // gene's upper bound; xi_lower < xi < xi_upper public double Lower[]; // Gene's lower bound; xi_lower < xi < xi_upper public double RFitness; // the relative fitness public double CFitness; // cumulative fitness public int NVARS; // Number of variables(Genes) public int N; // Number of bits defining each gene public boolean Breeder; public gentype(int iNVARS,double low[],double high[],int iN,f_xj fi) {setgentype(iNVARS,low,high,iN,fi);} public void setgentype(int iNVARS,double low[],double high[],int iN,f_xj fi) { setN(iN); setNVARS(iNVARS); G=new Gen[NVARS]; Upper=new double[NVARS]; Lower=new double[NVARS]; setLower(low);

setUpper(high); setGen(); setFitness(fi); } public gentype(gentype gi,f_xj fi) {setgentype(gi,fi);} public void setgentype(gentype gi,f_xj fi) { setN(gi.N); setNVARS(gi.NVARS); G=new Gen[NVARS]; Upper=new double[NVARS]; Lower=new double[NVARS]; setLower(gi.Upper); setUpper(gi.Lower); for(int i=0;i<NVARS;i++) G[i]=gi.G[i].copyGen(); setFitness(fi); } public gentype copygentype(f_xj fi) { gentype g1=new gentype(this,fi); return g1; } public void setN(int iN) {N=iN;} public void setNVARS(int iNVARS) {NVARS=iNVARS;} public void setLower(double lbound[]) {for(int i=0;i<NVARS;i++) Lower[i]=lbound[i];} public void setUpper(double ubound[]) {for(int i=0;i<NVARS;i++) Upper[i]=ubound[i];} public void setFitness(f_xj fi) { Fitness=fi.func(getGen());} public void setRFitness(double isum) {RFitness=Fitness/isum;} public void setCFitness(double ix) {CFitness=ix;} public double getLower(int j) {return Lower[j];} public double getUpper(int j) {return Upper[j];} public double getFitness() {return Fitness;} public double getRFitness(){return RFitness;} public double getCFitness(){return CFitness;} public void breed() {Breeder=true;} public void not_breed() {Breeder=false;} public boolean isBreed() {return Breeder;}

public void setGen(int val) { G[val]=new Gen(Lower[val],Upper[val]);} public void setGen() { for(int j=0;j<NVARS;j++) setGen(j);

} public double getGen(int val) { return G[val].getX();} public double[] getGen() { double x[]=new double[NVARS]; for(int j=0;j<NVARS;j++) x[j]=getGen(j); return x; } public String toString() { //x values, Fitness,relative fitness and cumulative fitness String s=""; for(int j=0;j<NVARS;j++) s+=G[j].toString()+" F:"; s+=getFitness(); s+="RF:"+getRFitness(); s+="CF:"+getCFitness(); return s; } } //end of class gentype

Program 5.18.6-4 Gausrandom Genetic algorithm Genetic2.java


public class Genetic2 { // Maximizing a function int POPSIZE; //population size int MAXGENS; //maximum number of generations; int NVARS; //number of independent variables of function int N; //number of genes in each variable double PXOVER; //probability of crossover; double PMUTATION; //proportion of mutated variables int Generation; //Current generation number int NBreeder; //Number of survivors in a generation int Best; //The best gentype in the population gentype Population[]; //f_xj f; // function to be avaluated by genetic algorithm public Genetic2(int iPOPSIZE,int iMAXGENS,int iNVARS,int iN, double iPXOVER,double iPMUTATION) { POPSIZE=iPOPSIZE; MAXGENS=iMAXGENS; NVARS=iNVARS; N=iN; PXOVER=iPXOVER; PMUTATION=iPMUTATION; NBreeder=0;

Population=new gentype[POPSIZE+1]; } public void setPopulation(double low[],double up[],f_xj fi) { for(int i=0;i<POPSIZE+1;i++) { Population[i]=new gentype(NVARS,low,up,N,fi);} } public gentype[] copyPopulation(f_xj fi) { gentype Po[]=new gentype[POPSIZE+1]; for(int i=0;i<POPSIZE+1;i++) { Po[i]=new gentype(Population[i],fi); Po[i].Fitness=Population[i].Fitness; Po[i].RFitness=Population[i].RFitness; Po[i].CFitness=Population[i].CFitness; } return Po; } public void evaluate(f_xj fi) { int mem; int j; double x[]=new double[NVARS]; for(mem=0;mem<POPSIZE;mem++) { //System.out.println(Population[mem].toString()); Population[mem].setFitness(fi); if(Population[mem].getFitness() > Population[POPSIZE].getFitness()) { Best=mem; Population[POPSIZE]=Population[mem]; for(j=0;j<NVARS;j++) { Population[POPSIZE].G[j].copyGen(Population[mem].G[j]); }//end of for(j=0.. }//end of if }//end of for(mem=0;.. }

public void setRFCF(gentype Pi[]) { //calculates relative and cumulative fitness functions int mem; int PN=Pi.length; double sum=0.0; //total fitness of population for(mem=0;mem<PN;mem++) {sum+=Pi[mem].Fitness;} //System.out.println("sum="+sum);

//calculate relative fitness of each gentype for(mem=0;mem<PN;mem++) {Pi[mem].setRFitness(sum);} //calculate cumulative fitness Pi[0].setCFitness(Pi[0].getRFitness()); for(mem=1;mem<PN;mem++) {Pi[mem].setCFitness(Pi[mem-1].getCFitness()+Pi[mem].getRFitness()); } } public void toString(gentype Pi[]) { int mem; int PN=Pi.length; for(mem=0;mem<PN;mem++) { //list them System.out.println("Population["+mem+"]="+Pi[mem].toString()); } } public void select(f_xj fi) { //select the new generation members of population double r; int mem; setRFCF(Population); //create a new population; gentype Po[]=copyPopulation(fi); setRFCF(Po); for(int i=0;i<POPSIZE;i++) { mem=0; r=Math.random(); double gf1=Population[i].getCFitness(); double gf2; if (gf1 > r) Po[i]=Population[mem].copygentype(fi); for(mem=1;mem<POPSIZE;mem++) { gf2=Population[mem].getCFitness(); if( gf2>=r && gf1< r ) {Po[i]=Population[mem].copygentype(fi);break;} } } setRFCF(Po); Population=Po; evaluate(fi); //toString(Population); } public void crossover(f_xj fi) { int i,count; int mem;

int POne,a; int PTwo,b; int point; //select members for breeding int iselect[]=new int[POPSIZE]; int counter=0; double r; for(mem=0;mem<POPSIZE;mem++) { r=Math.random(); if(r < PXOVER) { iselect[counter++]=mem; Population[mem].breed(); NBreeder++; } else Population[mem].not_breed(); } //System.out.println("iselect="+Matrix.toString(iselect)); //let also best of the population to breed Population[Best].breed(); //loop through the population select breeding pairs for(mem=0;mem<POPSIZE;mem++) { //select two in popolation in random a=(int)(Math.random()*NBreeder)+1; b=(int)(Math.random()*NBreeder)+1; count=0; POne=0; PTwo=0; //select two individuals for breeding for(i=0;i<POPSIZE;i++) { if(Population[i].isBreed()) { count++; if(count==a) POne=count; if(count==b) PTwo=count; } } //perform a crossover; gentype Po[]=new gentype[2]; Po[0]=Population[POne].copygentype(fi); Po[1]=Population[PTwo].copygentype(fi); for(i=0;i<NVARS;i++) { Population[POne].G[i].cross1(Math.random(),Po[0].G[i],Po[1].G[i]); Population[PTwo].G[i].cross1(Math.random(),Po[0].G[i],Po[1].G[i]); } Population[POne].setFitness(fi);

Population[PTwo].setFitness(fi); } } public void mutate(f_xj fi) { int i; double lbound,hbound; int nmutations; int member; int var; nmutations=(int)((double)(POPSIZE*NVARS*PMUTATION*N)); for(i=0;i<nmutations;i++) { member=(int)(Math.random()*POPSIZE); var=(int)(Math.random()*NVARS); //replace the old value with a new mutated one Population[member].G[var].mutate(); //after mutation recalculate the function value Population[member].setFitness(fi); } } public String report() { String s; int i; double best_value; double avg; double stdev; double sum_square; double square_sum; double sum; double xx; sum=0; sum_square=0.0; for(i=0;i<POPSIZE;i++) { xx= Population[i].getFitness(); sum+=xx; sum_square=xx*xx; } avg=sum/(double)POPSIZE; square_sum=sum*sum/(double)POPSIZE; stdev=Math.sqrt(square_sum); best_value=Population[POPSIZE].getFitness(); double aa[]=new double[NVARS+1]; for(i=0;i < NVARS;i++) { aa[i]=Population[POPSIZE].getGen(i); } aa[NVARS]=Population[POPSIZE].getFitness();

//s="Generation = "+Generation+"best value = "+Matrix.toString(aa)+"Average = "+avg+"Standart Deviation = "+stdev; s="Generation = "+Generation+"best value = "+Matrix.toString(aa); return s; } public double[] getBest() { double aa[]=new double[NVARS+1]; for(int i=0;i < NVARS;i++) { aa[i]=Population[POPSIZE].getGen(i); } aa[NVARS]=Population[POPSIZE].getFitness(); return aa; } public double[] calculate(f_xj fi,boolean report) { evaluate(fi); Generation=0; while(Generation<MAXGENS) { Generation++; NBreeder=(int)(0.8*POPSIZE); select(fi); crossover(fi); mutate(fi); evaluate(fi); //if(report) //System.out.println(report()); } return getBest(); } }

Program 5.18.5-5 Gausrandom Genetic algorithm test program class NA108.java


import java.io.*; class fa extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1]+10.0; return ff; //maksimum testi } }

class NA108 { public static void main(String args[]) throws IOException { int N=25; int POPSIZE=400; int MAXGENS=400; int NVARS=2; double PXOVER=0.3; double PMUTATION=0.02; fa fx=new fa(); Genetic2 xx=new Genetic2(POPSIZE,MAXGENS,NVARS,N,PXOVER,PMUTATION); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-1.0;high[0]=5.0; low[1]=-1.0;high[1]=5.0; xx.setPopulation(low,high,fx); System.out.println("best=\n"+Matrix.toStringT(xx.calculate(fx,false))); } }

The output of the test program: ---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA108 best= 2.013695935224975 1.013745823716572 11.999811049841490

> Terminated with exit code 0.

5.18.7 GENETC ALGORITHMS BY USING A LONG TYPE VARIABLE AS A GENE In a gene structure number of digits in the gene should be constant. It is similar in the natura system as well. In order to produce a child gene structure of both parents should be similar in structure . A long integer with a setted number of digits will be used be used as a gene structure in the next genetic algorithm code. In this two variable systems long type integer value bits can take all the possible values 0 to 9 in each digit, while double value can only corresponds the working space.

Program 5.18.7-1 Gene constructed from long type integer numbers of 9 digits.
// Dr. M. Turhan OBAN // EGE University School of Engineering, Mechanical Engineering Department // package : Numerical analysis package // subject : optimization // group : Genetic Algorithms // Gene3 Class // Gene3 class generates integer array to represent a double number

// actual binary defination changes depends on number of bits, xmin and xmax // binary data stored in b BitSet array import java.util.*; import java.io.*; public class Gene3 extends Object { //long imin=0; int N=9; public long imax=999999999; public long ix; public double x;//Limit value for integer(2^N-1) public double xmin,xmax;//maximum and minimum limits of double number public Gene3(double ixmin,double ixmax) {//randomly generate the gene xmin=ixmin; xmax=ixmax; ix=(long)(imax*Math.random()); x=xmin+(xmax-xmin)*(double)ix/(double)imax; }

public Gene3(long ixi,double ixmin,double ixmax) {//randomly generate the gene xmin=ixmin; xmax=ixmax; ix=ixi; x=xmin+(double)ix*(xmax-xmin)/(double)imax; } public Gene3(Gene3 g) { // copy a Gene from another one copyGene(g); } public void copyGene(Gene3 g) { ix=g.ix; x=g.x; xmin=g.xmin; xmax=g.xmax; } public Gene3 copyGene() {Gene3 g=new Gene3(x,xmin,xmax); return g; } public Gene3(Gene3 Father,Gene3 Mother)

{cross(Math.random(),Father,Mother);} public Gene3(double xi,double maxi,double mini) {setGene(xi,maxi,mini);}

public Gene3(double crossratio,Gene3 Father,Gene3 Mother) { if(boolean_random()) cross(crossratio,Father,Mother); else cross(crossratio,Mother,Father); } public void setGene(long iix,double ixmin,double ixmax) { xmin=ixmin; xmax=ixmax; ix=iix; x=xmin+(xmax-xmin)*(double)ix/(double)imax; } public void setGene(double idx,double ixmin,double ixmax) { xmin=ixmin; xmax=ixmax; if(idx>=xmin && idx<=xmax) x=idx; else if(idx<xmin) x=xmin; else x=xmax; ix=(long)((x-xmin)/(xmax-xmin)*imax+0.99); } public void setGene(double idx) { if(idx>=xmin && idx<=xmax) x=idx; else if(idx<xmin) x=xmin; else x=xmax; ix=(long)((x-xmin)/(xmax-xmin)*imax); } public void cross(Gene3 Father,Gene3 Mother) {double crossratio=Math.random(); cross(crossratio,Father,Mother); } public double getX() {return x;} public void cross(double crossratio,Gene3 Father,Gene3 Mother) { Long IntFx=new Long(Father.ix); String SFx=IntFx.toString(); int nSFx=SFx.length(); Long IntMx=new Long(Mother.ix);

String SMx=IntMx.toString(); int nSMx=SMx.length(); if(nSFx<N) {String ss="";for(int i=0;i<(N-nSFx);i++){ss+="0";};SFx=ss+SFx;} if(nSMx<N) {String ss="";for(int i=0;i<(N-nSMx);i++){ss+="0";};SMx=ss+SMx;} int N2 = (int)(crossratio*N+0.02)/2; int N1=2*N2; int N3=N1+(N-N1)/2; StringBuffer dest = new StringBuffer(N); int i; for (i = 0; i <N2; i++) { dest.append(SFx.charAt(i)); dest.append(SMx.charAt(i)); } for (i = N1; i <N3; i++) { dest.append(SFx.charAt(i));} for (i = N3; i <N; i++) { dest.append(SMx.charAt(i));} String s=dest.toString(); long iix=Long.parseLong(s); ix=iix; xmin=Father.xmin; xmax=Father.xmax; x=xmin+(xmax-xmin)*(double)ix/(double)imax; }

public boolean boolean_random() { if(Math.random()<=0.5) return false; else return true; } public void mutate() { int N1 = (int)(N*Math.random()); Long IntFx=new Long(ix); String SFx=IntFx.toString(); int nSFx=SFx.length(); if(nSFx<N) {String ss="";for(int i=0;i<(N-nSFx);i++){ss+="0";};SFx=ss+SFx;} StringBuffer dest = new StringBuffer(N); int i; for (i = 0; i <N; i++) { if(i==N1) { int i1=(int)(9.0*Math.random()); String s2=Integer.toString(i1); dest.append(s2.charAt(0)); } else {dest.append(SFx.charAt(i));} }

String s=dest.toString(); long iix=Long.parseLong(s); ix=iix; x=xmin+(xmax-xmin)*(double)ix/(double)imax; //================== } public String toString() {String s; s="x="+x+"ix="+ix+"xmin="+xmin+"xmax="+xmax; return s; } }

Program 5.18.7-2 Gene constructed from long type integer numbers of 9 digits.
import java.io.*; import java.text.*; import java.util.Locale; abstract class f_xj extends Mathd { // single function multi independent variable // a single value is returned indiced to equation_ref // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[]); } class genotype3 { // each genotype3 is member of the population // Genotype is like a chromosone in real life genetic systems // it can be thought as an evaluation point of a function with n variable // f(xi)=f(x1,x2,x3,..,x_nvars) public Gene3 G[] ; // string of variables makes a genotype3 public double Fitness; // genotype3's fitness f(xi) public double Upper[]; // gene's upper bound; xi_lower < xi < xi_upper public double Lower[]; // Gene's lower bound; xi_lower < xi < xi_upper public double RFitness; // the relative fitness public double CFitness; // cumulative fitness public int NVARS; // Number of variables(Genes) public int N; // Number of bits defining each gene public boolean Breeder; public genotype3(int iNVARS,double low[],double high[],int iN,f_xj fi) {setgenotype(iNVARS,low,high,iN,fi);} public void setgenotype(int iNVARS,double low[],double high[],int iN,f_xj fi)

{ setN(iN); setNVARS(iNVARS); G=new Gene3[NVARS]; Upper=new double[NVARS]; Lower=new double[NVARS]; setLower(low); setUpper(high); setGene(); setFitness(fi); } public genotype3(genotype3 gi,f_xj fi) {setgenotype(gi,fi);} public void setgenotype(genotype3 gi,f_xj fi) { setN(gi.N); setNVARS(gi.NVARS); G=new Gene3[NVARS]; Upper=new double[NVARS]; Lower=new double[NVARS]; setLower(gi.Upper); setUpper(gi.Lower); for(int i=0;i<NVARS;i++) G[i]=gi.G[i].copyGene(); setFitness(fi); } public genotype3 copygenotype(f_xj fi) { genotype3 g1=new genotype3(this,fi); return g1; } public void setN(int iN) {N=iN;} public void setNVARS(int iNVARS) {NVARS=iNVARS;} public void setLower(double lbound[]) {for(int i=0;i<NVARS;i++) Lower[i]=lbound[i];} public void setUpper(double ubound[]) {for(int i=0;i<NVARS;i++) Upper[i]=ubound[i];} public void setFitness(f_xj fi) { Fitness=fi.func(getGene());} public void setRFitness(double isum) {RFitness=Fitness/isum;} public void setCFitness(double ix) {CFitness=ix;} public double getLower(int j) {return Lower[j];} public double getUpper(int j) {return Upper[j];} public double getFitness() {return Fitness;} public double getRFitness(){return RFitness;} public double getCFitness(){return CFitness;} public void breed() {Breeder=true;} public void not_breed() {Breeder=false;} public boolean isBreed() {return Breeder;}

public void setGene(int val) { G[val]=new Gene3(Lower[val],Upper[val]);} public void setGene() { for(int j=0;j<NVARS;j++) setGene(j); } public double getGene(int val) { return G[val].getX();} public double[] getGene() { double x[]=new double[NVARS]; for(int j=0;j<NVARS;j++) x[j]=getGene(j); return x; } public String toString() { //x values, Fitness,relative fitness and cumulative fitness String s=""; for(int j=0;j<NVARS;j++) s+=G[j].toString()+" F:"; s+=getFitness(); s+="RF:"+getRFitness(); s+="CF:"+getCFitness(); return s; } } //end of class genotype3 public class Genetic3 { // Maximizing a function int POPSIZE; //population size int MAXGENS; //maximum number of generations; int NVARS; //number of independent variables of function int N; //number of genes in each variable double PXOVER; //probability of crossover; double PMUTATION; //proportion of mutated variables int Generation; //Current generation number int NBreeder; //Number of survivors in a generation int Best; //The best genotype3 in the population genotype3 Population[]; //f_xj f; // function to be avaluated by genetic algorithm public Genetic3(int iPOPSIZE,int iMAXGENS,int iNVARS,int iN, double iPXOVER,double iPMUTATION) { POPSIZE=iPOPSIZE; MAXGENS=iMAXGENS;

NVARS=iNVARS; N=iN; PXOVER=iPXOVER; PMUTATION=iPMUTATION; NBreeder=0; Population=new genotype3[POPSIZE+1]; } public void setPopulation(double low[],double up[],f_xj fi) { for(int i=0;i<POPSIZE+1;i++) { Population[i]=new genotype3(NVARS,low,up,N,fi);} } public genotype3[] copyPopulation(f_xj fi) { genotype3 Po[]=new genotype3[POPSIZE+1]; for(int i=0;i<POPSIZE+1;i++) { Po[i]=new genotype3(Population[i],fi); Po[i].Fitness=Population[i].Fitness; Po[i].RFitness=Population[i].RFitness; Po[i].CFitness=Population[i].CFitness; } return Po; } public void evaluate(f_xj fi) { int mem; int j; double x[]=new double[NVARS]; for(mem=0;mem<POPSIZE;mem++) { //System.out.println(Population[mem].toString()); Population[mem].setFitness(fi); if(Population[mem].getFitness() > Population[POPSIZE].getFitness()) { Best=mem; Population[POPSIZE]=Population[mem]; for(j=0;j<NVARS;j++) { Population[POPSIZE].G[j].copyGene(Population[mem].G[j]); }//end of for(j=0.. }//end of if }//end of for(mem=0;.. }

public void setRFCF(genotype3 Pi[]) { //calculates relative and cumulative fitness functions int mem; int PN=Pi.length;

double sum=0.0; //total fitness of population for(mem=0;mem<PN;mem++) {sum+=Pi[mem].Fitness;} //System.out.println("sum="+sum); //calculate relative fitness of each genotype3 for(mem=0;mem<PN;mem++) {Pi[mem].setRFitness(sum);} //calculate cumulative fitness Pi[0].setCFitness(Pi[0].getRFitness()); for(mem=1;mem<PN;mem++) {Pi[mem].setCFitness(Pi[mem-1].getCFitness()+Pi[mem].getRFitness()); } } public void toString(genotype3 Pi[]) { int mem; int PN=Pi.length; for(mem=0;mem<PN;mem++) { //list them System.out.println("Population["+mem+"]="+Pi[mem].toString()); } } public void select(f_xj fi) { //select the new generation members of population double r; int mem; setRFCF(Population); //create a new population; genotype3 Po[]=copyPopulation(fi); setRFCF(Po); for(int i=0;i<POPSIZE;i++) { mem=0; r=Math.random(); double gf1=Population[i].getCFitness(); double gf2; if (gf1 > r) Po[i]=Population[mem].copygenotype(fi); for(mem=1;mem<POPSIZE;mem++) { gf2=Population[mem].getCFitness(); if( gf2>=r && gf1< r ) {Po[i]=Population[mem].copygenotype(fi);break;} } } setRFCF(Po); Population=Po; evaluate(fi); //toString(Population); }

public void crossover(f_xj fi) { int i,count; int mem; int POne,a; int PTwo,b; int point; //select members for breeding int iselect[]=new int[POPSIZE]; int counter=0; double r; for(mem=0;mem<POPSIZE;mem++) { r=Math.random(); if(r < PXOVER) { iselect[counter++]=mem; Population[mem].breed(); NBreeder++; } else Population[mem].not_breed(); } //System.out.println("iselect="+Matrix.toString(iselect)); //let also best of the population to breed Population[Best].breed(); //loop through the population select breeding pairs for(mem=0;mem<POPSIZE;mem++) { //select two in popolation in random a=(int)(Math.random()*NBreeder)+1; b=(int)(Math.random()*NBreeder)+1; count=0; POne=0; PTwo=0; //select two individuals for breeding for(i=0;i<POPSIZE;i++) { if(Population[i].isBreed()) { count++; if(count==a) POne=count; if(count==b) PTwo=count; } } //perform a crossover; genotype3 Po[]=new genotype3[2]; Po[0]=Population[POne].copygenotype(fi); Po[1]=Population[PTwo].copygenotype(fi); for(i=0;i<NVARS;i++)

{ Population[POne].G[i].cross(Math.random(),Po[0].G[i],Po[1].G[i]); Population[PTwo].G[i].cross(Math.random(),Po[0].G[i],Po[1].G[i]); } Population[POne].setFitness(fi); Population[PTwo].setFitness(fi); } } public void mutate(f_xj fi) { int i; double lbound,hbound; int nmutations; int member; int var; nmutations=(int)((double)(POPSIZE*NVARS*PMUTATION*N)); for(i=0;i<nmutations;i++) { member=(int)(Math.random()*POPSIZE); var=(int)(Math.random()*NVARS); //replace the old value with a new mutated one Population[member].G[var].mutate(); //after mutation recalculate the function value Population[member].setFitness(fi); } } public String report() { String s; int i; double best_value; double avg; double stdev; double sum_square; double square_sum; double sum; double xx; sum=0; sum_square=0.0; for(i=0;i<POPSIZE;i++) { xx= Population[i].getFitness(); sum+=xx; sum_square=xx*xx; } avg=sum/(double)POPSIZE; square_sum=sum*sum/(double)POPSIZE; stdev=Math.sqrt(square_sum); best_value=Population[POPSIZE].getFitness(); double aa[]=new double[NVARS+1];

for(i=0;i < NVARS;i++) { aa[i]=Population[POPSIZE].getGene(i); } aa[NVARS]=Population[POPSIZE].getFitness(); //s="Generation = "+Generation+"best value = "+Matrix.toString(aa)+"Average = "+avg+"Standart Deviation = "+stdev; s="Generation = "+Generation+"best value = "+Matrix.toString(aa); return s; } public double[] getBest() { double aa[]=new double[NVARS+1]; for(int i=0;i < NVARS;i++) { aa[i]=Population[POPSIZE].getGene(i); } aa[NVARS]=Population[POPSIZE].getFitness(); return aa; } public double[] calculate(f_xj fi,boolean report) { evaluate(fi); Generation=0; while(Generation<MAXGENS) { Generation++; NBreeder=(int)(0.8*POPSIZE); select(fi); crossover(fi); mutate(fi); evaluate(fi); //if(report) //System.out.println(report()); } return getBest(); } }

Program 5.18.7-3 Test program NA109 for Gene constructed from long type integer numbers of 9 digits.
import java.io.*; import javax.swing.*;

class fa extends f_xj { //zm istenen fonksiyon double func(double x[])

{ double ff= 1.0+Math.cos(Math.PI*(x[0]-3.0))*Math.cos(2.0*Math.PI*(x[1]-2.0))/ (1+(x[0]-3.0)*(x[0]-3.0)+(x[1]-2.0)*(x[1]-2.0)); return ff; //maksimum testi; } } class fb extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; fa fx=new fa(); return -fx.func(x); //maksimum testi } } class NA109 { public static void main(String args[]) throws IOException { int N=9; int POPSIZE=200; int MAXGENS=200; int NVARS=2; double PXOVER=0.3; double PMUTATION=0.03; fa fax=new fa(); fb fbx=new fb(); Genetic3 xx=new Genetic3(POPSIZE,MAXGENS,NVARS,N,PXOVER,PMUTATION); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-10.0;high[0]=10.0; low[1]=-10.0;high[1]=10.0; xx.setPopulation(low,high,fax); double a[]=xx.calculate(fax,false); double x0[]=new double[NVARS]; for(int i=0;i<NVARS;i++) x0[i]=a[i]; String s="results of genetic algorithms =\n"+Matrix.toStringT(a); String s2="Genetic algorithm multi-variable optimization:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); } }

5.19

STOCHASTIC METHODS: MONTE-CARLO METHOD

Monte carlo optimization as a method is very simple. Points between the given limits are selected randomly and evaluated. When number of points increase to very high numbers, statistically there will be a homogeneous coverege of all the search region. Record the best optimum found so far.What is achived is similar to plotting data, but in monte carlo method taken constant steps and walk through the steps are not needed because randomness takes care of uniformity of search when the total number is big enough. Program 5.19-1 Monte carlo stochastic optimization method
/ Dr. M. Turhan OBAN // // EGE University School of Engineering, Mechanical Engineering Department // package : Numerical analysis package // subject : optimization // group : Monte-Carlo optimization(maximum) Algorithms // monte_carlo_opt Class // class selects random points at the given area and selects the best // results found // import java.util.*; import java.io.*; abstract class f_xj extends Mathd { // single function multi independent variable // a single value is returned indiced to equation_ref // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[]); } public class monte_carlo_opt extends Object { int N; public double max[]; public double min[]; public double x; public monte_carlo_opt(int Ni,double imin[],double imax[]) {

max=new double[imin.length]; min=new double[imin.length]; N=Ni; int n=imin.length; for(int i=0;i<n;i++) {min[i]=imin[i];max[i]=imax[i];} } public double[] monte_carlo_max(f_xj fi) { int n=min.length; double x[]=new double[n]; double xmax[]=new double[n]; double max_number=-1e99; double f; for(int k=0;k<N;k++) { for(int i=0;i<n;i++) {x[i]=min[i]+(max[i]-min[i])*Math.random();} f=fi.func(x); if(f>max_number) { max_number=f; for(int i=0;i<n;i++) {xmax[i]=x[i];} } } return xmax; } }

Program 5.19-2 Monte carlo stochastic optimization method example


import java.io.*; class fa extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=x[0]*Math.sin(10.0*Math.PI*x[0])+3.0; return ff; //maksimum testi } } class NA110 { public static void main(String args[]) throws IOException { fa fx=new fa(); double low[]=new double[1]; double high[]=new double[1]; low[0]=-1.0;high[0]=2.0; monte_carlo_opt mco=new monte_carlo_opt(100000,low,high); double xmax[]=mco.monte_carlo_max(fx); for(int i=0;i<xmax.length;i++) {System.out.println(xmax[i]);} } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA110 1.8505636350371275 > Terminated with exit code 0.

Program 5.19-3 Monte carlo stochastic optimization method example


import java.io.*; class fa extends f_xj { double func(double x[]) { double ff= 1.0+Math.cos(Math.PI*(x[0]-3.0))*Math.cos(2.0*Math.PI*(x[1]-2.0))/ (1+(x[0]-3.0)*(x[0]-3.0)+(x[1]-2.0)*(x[1]-2.0)); return ff; } } class fb extends f_xj { public double func(double x[]) { double ff; fa fx=new fa(); return -fx.func(x); } } class NA111 { public static void main(String args[]) throws IOException { fb fx=new fb(); double low[]=new double[2]; double high[]=new double[2]; low[0]=-10.0;high[0]=10.0; low[1]=-10.0;high[1]=10.0; //10 million iteration monte_carlo_opt mco=new monte_carlo_opt(10000000,low,high); double xmax[]=mco.monte_carlo_max(fx); for(int i=0;i<xmax.length;i++) {System.out.println(xmax[i]);} } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA111 2.9991287583134874 1.5201159960932067 > Terminated with exit code 0.

5.20 STOCHASTIC METHODS: ITERATIVE HILL CLIMBING Iterative Hill climbing method, a global maximum findng method similar to genetic algorithms and monte carlo. It is a simple method, yet it is more complicated than the Monte-Carlo method. As a basic strategy a point is selected and evaluated. Furthermore more point (but not excessively more) in the neigborhood of the point is evaluated then jumped to a new point and repeat the same process. If the
method is given as an algorithm:
Iterative hill climbing algorithm start t=0 while local=false select a random point vc calculate the function for vc while select approximately 30 points in neighbourhood of vc and evaluate the function values In between these points select the maximum one as the solution vector if f(vc) < f(vn) then vc = vn else local=true until local t=t+1 until t=MAXIMUM finish

The basic problem in this code is the definition of neighbourhood region. In reality Genetic engineering Gene concept is just establishing this, a child gene is always in the neighbourhood of the parent genes. By using this property the method can be establish. A few example of describing the gene was given in genetic engineering method, the same methods can be utilised for iterative hill climbing as well. In our first class vector(point) vc is made by using binary gene Program 5.20-1 Iterative hill climbing with binary gene
import java.io.*; import java.text.*; import java.util.Locale; public class iteratedhillclimber { // Maximizing a function //iterated hill climber // a version steepest ascent with statistical approach to variable (endikyama) genotype1 Vc; genotype1 Vn[]; genotype1 best; int MAX; double t; boolean local; int POPSIZE; int NVARS; int N;//number of bit representation for each variable //f_xj f; // function to be avaluated by genetic algorithm public iteratedhillclimber(int iPOPSIZE,int iNVARS,int iN) { POPSIZE=iPOPSIZE;//Vn size number of neigbour variable f.e. 30 NVARS=iNVARS; // number of variables equation to solve

MAX=20; N=iN; // number of bits represent each variable for example 32 Vn=new genotype1[POPSIZE+1]; t=0; local=false; } public void setVc(double low[],double up[],f_xj fi) { //select originalgene andomly Vc=new genotype1(NVARS,low,up,N,fi); } public void setBest(genotype1 gi,f_xj fi) { //select originalgene andomly best=new genotype1(gi,fi); } public void setVc(genotype1 gi,f_xj fi) { //select original gene from other gene Vc=new genotype1(gi,fi); } public void setVn(f_xj fi) {//select neighbouring genes similar to the original one for(int i=0;i<POPSIZE+1;i++) { Vn[i]=new genotype1(Vc,fi);} mutate(fi); }

public void evaluate(f_xj fi) { int mem; int j; double x[]=new double[NVARS]; for(mem=0;mem<POPSIZE;mem++) { //System.out.println(Vn[mem].toString()); Vn[mem].setFitness(fi); if(Vn[mem].getFitness() > Vn[POPSIZE].getFitness()) { Vn[POPSIZE]=Vn[mem]; for(j=0;j<NVARS;j++) { Vn[POPSIZE].G[j].copyGene(Vn[mem].G[j]); }//end of for(j=0.. }//end of if }//end of for(mem=0;.. } public void toString(genotype1 Pi[]) { int mem; int PN=Pi.length; for(mem=0;mem<PN;mem++) { //list them System.out.println("Vn["+mem+"]="+Pi[mem].toString());

} }

public void mutate(f_xj fi) { int i; double lbound,hbound; int nmutations; int member; int var; nmutations=(int)((double)(POPSIZE*NVARS*N)); for(i=0;i<nmutations;i++) { member=(int)(Math.random()*POPSIZE); var=(int)(Math.random()*NVARS); //replace the old value with a new mutated one Vn[member].G[var].mutate(); //after mutation recalculate the function value Vn[member].setFitness(fi); } }

public double[] getBest(genotype1 b) { double aa[]=new double[NVARS+1]; for(int i=0;i < NVARS;i++) { aa[i]=b.getGene(i); } aa[NVARS]=b.getFitness(); return aa; } public double[] calculate(double low[],double up[],f_xj fi,boolean report) { setVc(low,up,fi);// new set of random selection in each time setBest(Vc,fi); do { local=true; do { setVn(fi); evaluate(fi); if(Vn[POPSIZE].getFitness() > Vc.getFitness()) { setVc(Vn[POPSIZE], fi); if(Vn[POPSIZE].getFitness() > best.getFitness()) setBest(Vn[POPSIZE],fi); } else local=false; } while(local); t++; setVc(low,up,fi); //new random value if(report) System.out.println(Matrix.toString(getBest(best))); } while(t<MAX);

return getBest(best); } }

Program 5.20-2 Iterative hill climbing with binary gene example program
//iterated hillclimber //iterasyonlu en dik yama trmanc import java.io.*; import javax.swing.JOptionPane;

class fa extends f_xj { //zm istenen fonksiyon double func(double x[]) { double ff= 21.5+x[0]*Math.sin(4.0*Math.PI*x[0])+x[1]*Math.sin(20.0*Math.PI*x[1]); return ff; } } class fb extends f_xj { public double func(double x[]) { double ff; fa fx=new fa(); return -fx.func(x); } } class NA112 { public static void main(String args[]) throws IOException { int N=18; int POPSIZE=40; int MAXGENS=400; int NVARS=2;

fa fax=new fa(); fb fbx=new fb(); iteratedhillclimber xx=new iteratedhillclimber(POPSIZE,NVARS,N); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-3.0;high[0]=12.1; low[1]=4.1;high[1]=5.8; double a[]=xx.calculate(low,high,fax,false); double x0[]=new double[NVARS]; for(int i=0;i<NVARS;i++) x0[i]=a[i]; String s="Iterative Hill climber result =\n"+Matrix.toStringT(a); String s2="Iterative hill climber stcochastic optimization:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); } }

Program 5.20-3 Iterative hill climbing with Gauss random gene


import java.io.*; import java.text.*; import java.util.Locale;

public class IHC { // Maximizing a function // iterated Hill climber // a stochastic approach double T; double Vc[]; double Vn[][]; double best[]; int MAX;//number int GLIMIT;//arka arkaya baarl steplerin says boolean local; int NVARS;//numberof variables int N;//number of bit representation for each variable double T0; double k,k0;//time index int POPSIZE; double low[]; double high[]; int t; //f_xj f; // function to be avaluated by genetic algorithm public IHC(int iPOPSIZE,int iNVARS,int iN,double Ti)

{ T=Ti; T0=T; POPSIZE=iPOPSIZE; NVARS=iNVARS; // number of variables equation to solve MAX=100; N=iN; // number of bits represent each variable for example 32 local=false; t=0; Vc=new double[NVARS]; Vn=new double[POPSIZE+1][NVARS]; best=new double[NVARS]; low=new double[NVARS]; high=new double[NVARS]; local=true; } public static double gaussrandom(double xort,double r) { int iset=0; double gset=0; double fac,rsq,v1,v2; double aret=0; if(iset==0) { do{ v1=2.0*Math.random()-1.0; v2=2.0*Math.random()-1.0; rsq=v1*v1+v2*v2; }while(rsq>=1.0 || rsq==0.0); fac=Math.sqrt(-2.0*Math.log(rsq)/rsq); gset=v1*fac; iset=1; aret=v2*fac; } else { iset=0; aret=gset; } return aret/4.0*r+xort; } public void setVc(double ilow[],double ihigh[],f_xj fi) { //select original gene randomly for(int i=0;i<NVARS;i++) {low[i]=ilow[i];high[i]=ihigh[i];Vc[i]=(high[i]-low[i])*Math.random();} } public void setBest(double gi[],f_xj fi) { //select originalgene andomly for(int i=0;i<NVARS;i++) {best[i]=gi[i];} } public void setVc(double gi[],f_xj fi)

{ //select original gene from other gene for(int i=0;i<NVARS;i++) {Vc[i]=gi[i];} } public void setVn(f_xj fi) {//select neighbouring genes similar to the original one for(int i=0;i<POPSIZE;i++) { for(int j=0;j<NVARS;j++) {Vn[i][j]=Vc[j];}} mutate(fi); } public void mutate(f_xj fi) { int i; double lbound,hbound; int nmutations; int member; int var; nmutations=(int)((double)(POPSIZE*NVARS*N)); for(i=0;i<nmutations;i++) { member=(int)(Math.random()*POPSIZE); var=(int)(Math.random()*NVARS); //replace the old value with a new mutated one //neigboring factor double d1=high[var]-Vn[member][var]; double d2=Vn[member][var]-low[var]; double d=Math.abs(Math.min(d1,d2)); double R=gaussrandom(Vn[member][var],d); Vn[member][var] = R; //System.out.println("R="+R+"d1="+d1+"d2="+d2+"d="+d+"var = "+var+" "+Vn[member][var]); if(Vn[member][var]>high[var]) Vn[member][var]=high[var]-d*Math.random()*0.122561462; else if (Vn[member][var]<low[var]) Vn[member][var]=low[var]+d*Math.random()*0.122561462;; //after mutation recalculate the function value } }

public void evaluate(f_xj fi) { int mem; int j; double x[]=new double[NVARS]; for(mem=0;mem<POPSIZE;mem++) { //System.out.println(Vn[mem].toString()); if(getFitness(Vn[mem],fi) > getFitness(Vn[POPSIZE],fi)) { //Vn[POPSIZE]=Vn[mem]; for(j=0;j<NVARS;j++) { Vn[POPSIZE][j]=Vn[mem][j]; }//end of for(j=0.. }//end of if

}//end of for(mem=0;.. }

public void toString(genotype Pi[]) { int mem; int PN=Pi.length; for(mem=0;mem<PN;mem++) { //list them System.out.println("Vn["+mem+"]="+Pi[mem].toString()); } } public double[] getBest(double[] b,f_xj fi) { double aa[]=new double[NVARS+1]; for(int i=0;i < NVARS;i++) { aa[i]=b[i]; } aa[NVARS]=getFitness(b,fi); return aa; } public double getFitness(double []xi,f_xj fi) { return fi.func(xi); } public double[] calculate(double low[],double up[],f_xj fi,boolean report) { local=true; setVc(low,up,fi); //new random value t=0; do { do { setVn(fi); evaluate(fi); if(getFitness(Vn[POPSIZE],fi) > getFitness(Vc,fi)) { setVc(Vn[POPSIZE], fi); if(getFitness(Vn[POPSIZE],fi) > getFitness(best,fi)) setBest(Vn[POPSIZE],fi); } else local=false; } while(local); if(report) System.out.println(Matrix.toString(getBest(best,fi))); t++; } while(t<MAX); return getBest(best,fi); } }

Program 5.20-4 Iterative hill climbing with Gauss random gene example problem
// iterated hill climbing

// import java.io.*; import javax.swing.JOptionPane;

class fa extends f_xj { //zm istenen fonksiyon double func(double x[]) { double ff= 21.5+x[0]*Math.sin(4.0*Math.PI*x[0])+x[1]*Math.sin(20.0*Math.PI*x[1]); return ff; //maksimum testi } } class fb extends f_xj { public double func(double x[]) { //zm istenen fonksiyon // newton metodu minimumaalyor bu yzden //- iaretitanmladk. double ff; fa fx=new fa(); return -fx.func(x); //maksimum testi } } class NA113 { public static void main(String args[]) throws IOException { int N=18; int POPSIZE=30; int MAXGENS=400; int NVARS=2; fa fax=new fa(); fb fbx=new fb(); IHC xx=new IHC(POPSIZE,NVARS,N,20); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-3.0;high[0]=12.1; low[1]=4.1;high[1]=5.8; double a[]=xx.calculate(low,high,fax,false); double x0[]=new double[NVARS]; for(int i=0;i<NVARS;i++) x0[i]=a[i]; String s="Iterative Hill climber result =\n"+Matrix.toStringT(a); String s2="Iterative hill climber stcochastic optimization"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); } }

5.21 STOCHASTIC METHODS: SIMULATED ANNEALING An alternative approach obtained from the thermodynamic analogy is the simulated annealing. If a metal is heated obove the melting point, atoms in the structure started randomly move. When the temperature drops movemets slow down, and slide to a lower energy level. At the end atoms self organise themselves in cristal structures with minimum energy levels. This process is known as annealing. The energy state of an annealed metal can be expressed by using Boltzman statistics.

p (e) = exp(

e ) kT

In this equation e>=0 is the energy level, T is the temperature, =1/(kT), k is boltzman constant p(e) is the probability distribution function. Annealing can be taken as a natural optimization process. This phenemonen can be mimic ny computer codes. Consider minimisation of function f(x1,x2,x3,,xn) fonksiyonunu minimize etme problemini gz nne alalm. In order to establish the model an artificial Temperature value will be assumed. Optimization should be started with a big value of T. (In the starting of the annealing process the temperature of the metal is high) . As a starting point take an initial guess vector x0=(x1,x2,x3,,xn)0 vektrn alalm. If we change the initial vector a small amount x, distribution in x should follow the Gauss distribution. So in the neighbourhood of first point a second point can be obtain as x1=x0+ x . The difference in the function value will be f=f(x1)-f(x0)). Gaussian distribution function is:
p ( x j ) = x j exp 2T 2 T 2 1 , 1 j n

If f<0 we can accept the new point as a better approach to the minimum. But if f>=0 we do not automatically reject the second point. A Boltzmann distribution function is created and investigated.
p (f ) = 1 f exp kT kT

In order to decide accepting or rejecting the result arandom number z is created in the range of [0,1/(kT)]. If p(f) > z accept x1 as a new iteration value, If not reject it. Then a new x for a new vector is calculated. In time T temperature factore decreased as a some defined function of time. In the beginning of the iteration due to high temperatures the chance of accepting a new x value is high. In time when the T is getting smaller, the system became more conservative on accepting f>=0 cases. If this processis written as an algorithm Simulated annealing algorithm start t=0 set a T valu. Select a vc value as random Calculate the function value for vc

While while select a new point vn in the neighbourhood of vc if f(vc) < f(vn) then vc = vn (accepth the new point) else if tesadfi say(0-1) < exp[(f(vn)-f(vc))/T] then vc = vn even if f(vc) >= f(vn) accept the new point until (exit condition) t=t+1 until (exit condition) stop Function T vcan be evaluated with different formulas, some examples are given below.
T= T0 1 + log(1 + t ) log(t 0 ) t log(t )

T =T

T=

T0 1+ t

As an actual program two version is developed. In the first version Gene1 and genotpe1 classes of genetic algorithms (binary genes) are used t oto define the neighbourhood area (only one bit is replaced in random-gene is mutated) Program 5.21-1 Simulated annealing with binary gene
import java.io.*; import java.text.*; import java.util.Locale; public class simulatedannealing { // Maximizing a function //simulated annealing // a version of simulated annealing statistical approach double T; //Temperature genotype1 Vc; genotype1 Vn[]; genotype1 best; genotype1 oldbest; int MAX;//number int GLIMIT;//arka arkaya baarl steplerin says boolean local; int NVARS;//numberof variables int N;//number of bit representation for each variable double T0; double k,k0;//time index int POPSIZE; int BESTLIMIT; int bestcounter; //f_xj f; // function to be avaluated by genetic algorithm

public simulatedannealing(int iPOPSIZE,int iNVARS,int iN,double Ti) { T=Ti; T0=T; POPSIZE=iPOPSIZE; NVARS=iNVARS; // number of variables equation to solve MAX=20; N=iN; // number of bits represent each variable for example 32 Vn=new genotype1[POPSIZE+1]; local=false; GLIMIT=10; BESTLIMIT=3; k=100.0; k0=k; bestcounter=0; } public void setVc(double low[],double up[],f_xj fi) { //select originalgene andomly Vc=new genotype1(NVARS,low,up,N,fi); } public void setBest(genotype1 gi,f_xj fi) { //select originalgene andomly best=new genotype1(gi,fi); } public void setoldBest(f_xj fi) { //select originalgene andomly oldbest=new genotype1(best,fi); } public void setVc(genotype1 gi,f_xj fi) { //select original gene from other gene Vc=new genotype1(gi,fi); } public void setVn(f_xj fi) {//select neighbouring genes similar to the original one for(int i=0;i<POPSIZE+1;i++) { Vn[i]=new genotype1(Vc,fi);} mutate(fi); } public void evaluate(f_xj fi) { int mem; int j; double x[]=new double[NVARS]; for(mem=0;mem<POPSIZE;mem++) { //System.out.println(Vn[mem].toString()); Vn[mem].setFitness(fi); if(Vn[mem].getFitness() > Vn[POPSIZE].getFitness()) { Vn[POPSIZE]=Vn[mem]; for(j=0;j<NVARS;j++)

{ Vn[POPSIZE].G[j].copyGene(Vn[mem].G[j]); }//end of for(j=0.. }//end of if }//end of for(mem=0;.. } public void setRFCF(genotype1 Pi[]) { //calculates relative and cumulative fitness functions int mem; int PN=Pi.length; double sum=0.0; //total fitness of Vn for(mem=0;mem<PN;mem++) {sum+=Pi[mem].Fitness;} //System.out.println("sum="+sum); //calculate relative fitness of each genotype1 for(mem=0;mem<PN;mem++) {Pi[mem].setRFitness(sum);} //calculate cumulative fitness Pi[0].setCFitness(Pi[0].getRFitness()); for(mem=1;mem<PN;mem++) {Pi[mem].setCFitness(Pi[mem-1].getCFitness()+Pi[mem].getRFitness()); } } public void toString(genotype1 Pi[]) { int mem; int PN=Pi.length; for(mem=0;mem<PN;mem++) { //list them System.out.println("Vn["+mem+"]="+Pi[mem].toString()); } }

public void mutate(f_xj fi) { int i; double lbound,hbound; int nmutations; int member; int var; nmutations=(int)((double)(POPSIZE*NVARS*N)); for(i=0;i<nmutations;i++) { member=(int)(Math.random()*POPSIZE); var=(int)(Math.random()*NVARS); //replace the old value with a new mutated one Vn[member].G[var].mutate(); //after mutation recalculate the function value Vn[member].setFitness(fi); } }

public double[] getBest(genotype1 b) { double aa[]=new double[NVARS+1]; for(int i=0;i < NVARS;i++) { aa[i]=b.getGene(i); } aa[NVARS]=b.getFitness(); return aa; } public double[] calculate(double low[],double up[],f_xj fi,boolean report) { int good_swap=0; setVc(low,up,fi);// new set of random selection in each time setBest(Vc,fi); do { setoldBest(fi); do { setVn(fi); evaluate(fi); if(Vn[POPSIZE].getFitness() > Vc.getFitness()) { setVc(Vn[POPSIZE], fi); if(Vn[POPSIZE].getFitness() > best.getFitness()) {setBest(Vn[POPSIZE],fi); } if(oldbest.getFitness() == best.getFitness()) bestcounter++; if(bestcounter>BESTLIMIT) break; good_swap++; } else if(Math.random()<Math.exp((Vn[0].getFitness()-Vc.getFitness())/T)) { setVc(Vn[POPSIZE],fi);} if(report) System.out.println(Matrix.toString(getBest(best))); } while(good_swap<GLIMIT); k+=1.0; double lk=Math.log(k); T=T-Math.log(k0)/(k*lk*lk); setVc(low,up,fi); //new random value } while(k<MAX); return getBest(best); } }

Program 5.21-2 Simulated annealing with binary gene example program NA114.java
// simulated annealing // import java.io.*; import javax.swing.JOptionPane;

class fa extends f_xj { double func(double x[]) { double ff= 21.5+x[0]*Math.sin(4.0*Math.PI*x[0])+x[1]*Math.sin(20.0*Math.PI*x[1]); return ff; }

} class fb extends f_xj { public double func(double x[]) { double ff; fa fx=new fa(); return -fx.func(x); } } class NA114 { public static void main(String args[]) throws IOException { int N=18; int POPSIZE=1; int MAXGENS=400; int NVARS=2; fa fax=new fa(); fb fbx=new fb(); simulatedannealing xx=new simulatedannealing(POPSIZE,NVARS,N,20); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-3.0;high[0]=12.1; low[1]=4.1;high[1]=5.8; double a[]=xx.calculate(low,high,fax,false); double x0[]=new double[NVARS]; for(int i=0;i<NVARS;i++) x0[i]=a[i]; String s="simulated annealing scholastic optimization =\n"+Matrix.toStringT(a); String s2="simulated annealing scholastic optimization "; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); } }

The second version will be using Gauss random gene to calculate neighbour point. Program 5.21-3 Simulated annealing with Gauss random gene
import java.io.*; import java.text.*; import java.util.Locale; public class SA { // Maximizing a function // simulated annealing // a version of simulated annealing statistical approach

double T; //Temperature double Vc[]; double Vn[][]; double best[]; int MAX;//number int GLIMIT;//arka arkaya baarl steplerin says boolean local; int NVARS;//numberof variables int N;//number of bit representation for each variable double T0; double k,k0;//time index int POPSIZE; double low[]; double high[]; //f_xj f; // function to be avaluated by genetic algorithm public SA(int iPOPSIZE,int iNVARS,int iN,double Ti) { T=Ti; T0=T; POPSIZE=iPOPSIZE; NVARS=iNVARS; // number of variables equation to solve MAX=20; N=iN; // number of bits represent each variable for example 32 local=false; GLIMIT=30; k=100.0; k0=k; Vc=new double[NVARS]; Vn=new double[POPSIZE+1][NVARS]; best=new double[NVARS]; low=new double[NVARS]; high=new double[NVARS]; } public static double gaussrandom(double xort,double r) { int iset=0; double gset=0; double fac,rsq,v1,v2; double aret=0; if(iset==0) { do{ v1=2.0*Math.random()-1.0; v2=2.0*Math.random()-1.0; rsq=v1*v1+v2*v2; }while(rsq>=1.0 || rsq==0.0); fac=Math.sqrt(-2.0*Math.log(rsq)/rsq); gset=v1*fac; iset=1; aret=v2*fac; } else { iset=0; aret=gset;

} return aret/4.0*r+xort; }

public void setVc(double ilow[],double ihigh[],f_xj fi) { //select original gene randomly for(int i=0;i<NVARS;i++) {low[i]=ilow[i];high[i]=ihigh[i];Vc[i]=(high[i]-low[i])*Math.random();} } public void setBest(double gi[],f_xj fi) { for(int i=0;i<NVARS;i++) {best[i]=gi[i];} } public void setVc(double gi[],f_xj fi) { //select original gene from other gene for(int i=0;i<NVARS;i++) {Vc[i]=gi[i];} } public void setVn(f_xj fi) {//select neighbouring genes similar to the original one for(int i=0;i<POPSIZE+1;i++) { for(int j=0;j<NVARS;j++) {Vn[i][j]=Vc[i];}} mutate(fi); } public void mutate(f_xj fi) { int i; double lbound,hbound; int nmutations; int member; int var; nmutations=(int)((double)(POPSIZE*NVARS*N)); for(i=0;i<nmutations;i++) { member=(int)(Math.random()*POPSIZE); var=(int)(Math.random()*NVARS); //replace the old value with a new mutated one //neigboring factor double d1=high[var]-Vn[member][var]; double d2=Vn[member][var]-low[var]; double d=Math.min(d1,d2); double R=gaussrandom(Vn[member][var],d*Math.random()); Vn[member][var]= R; if(Vn[member][var]>high[var]) Vn[member][var]=high[var]-Math.random()*0.22561462; else if (Vn[member][var]<low[var]) Vn[member][var]=low[var]+Math.random()*0.22561462;; //after mutation recalculate the function value } }

public void evaluate(f_xj fi) { int mem; int j; double x[]=new double[NVARS]; for(mem=0;mem<POPSIZE;mem++) { //System.out.println(Vn[mem].toString()); if(getFitness(Vn[mem],fi) > getFitness(Vn[POPSIZE],fi)) { //Vn[POPSIZE]=Vn[mem]; for(j=0;j<NVARS;j++) { Vn[POPSIZE][j]=Vn[mem][j]; }//end of for(j=0.. }//end of if }//end of for(mem=0;.. } public double[] getBest(double[] b,f_xj fi) { double aa[]=new double[NVARS+1]; for(int i=0;i < NVARS;i++) { aa[i]=b[i]; } aa[NVARS]=getFitness(b,fi); return aa; } public double getFitness(double []xi,f_xj fi) { return fi.func(xi); } public double[] calculate(double low[],double up[],f_xj fi,boolean report) { int good_swap=0; setVc(low,up,fi);// new set of random selection in each time setBest(Vc,fi); do { do { setVn(fi); evaluate(fi); if(getFitness(Vn[POPSIZE],fi) > getFitness(Vc,fi)) { setVc(Vn[POPSIZE], fi); if(getFitness(Vn[POPSIZE],fi) > getFitness(best,fi)) setBest(Vn[POPSIZE],fi); good_swap++; } else if(Math.random()<Math.exp((getFitness(Vn[0],fi)-getFitness(Vc,fi))/(k*T))) { setVc(Vn[POPSIZE],fi);} if(report) System.out.println(Matrix.toString(getBest(best,fi))); } while(good_swap<GLIMIT); k+=1.0;

double lk=Math.log(k); T=T-Math.log(k0)/(k*lk*lk); setVc(low,up,fi); //new random value } while(k<MAX); return getBest(best,fi); } }

Program 5.21-4 Simulated annealing with Gauss random gene example program NA115.java
// simulated annealing // import java.io.*; import javax.swing.JOptionPane;

class fa extends f_xj { //zm istenen fonksiyon double func(double x[]) { double ff= 21.5+x[0]*Math.sin(4.0*Math.PI*x[0])+x[1]*Math.sin(20.0*Math.PI*x[1]); return ff; //maksimum testi } } class fb extends f_xj { public double func(double x[]) { double ff; fa fx=new fa(); return -fx.func(x); } } class NA115 { public static void main(String args[]) throws IOException { int N=18; int POPSIZE=1; int MAXGENS=400; int NVARS=2; fa fax=new fa(); fb fbx=new fb(); SA xx=new SA(POPSIZE,NVARS,N,20); double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-3.0;high[0]=12.1; low[1]=4.1;high[1]=5.8; double a[]=xx.calculate(low,high,fax,false); double x0[]=new double[NVARS]; for(int i=0;i<NVARS;i++) x0[i]=a[i]; String s="simulated annealing method =\n"+Matrix.toStringT(a); String s2="simulated annealing optimization :"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); } }

5.22 NON LINEAR OPTIMIZATION WITH BOUNDARY RESTRICTIONS A non-linear optimization problem with boundary restrictions can be defined as Minimum f(x) Boundary restrictions : p(x)=0 q(x) 0 In general these type of boundary restrictions are added up into the general equation as For the first equation p(x)=0 define
2 P ( x) = P( x0 , x1 ,..) = pk ( x) k =0 r

For the second equation q(x) 0 iin

Q( x) = Q( x0 , x1 ,..) = min 2 {0, qk ( x)}


k =0

Fuction min selects the minimum of 0 and qk(x). The general function then becomes

F ( x) = f ( x) + [ P( x) + Q( x)] . in the equation is called punishment coefficient.


For the second equation for q(x) 0 equation Q(x) can be also written as

Q( x) = Q( x0 , x1 ,..) = min 2 {0, [qk ( x) ]} . In this equation >0 is a small number. With this small
k =0

change refusal of the function when the value is exactly equal to the restriction limit can be evoided. In the sample problem Fletcher Reves Optimization method is used, but any unrestricted optimization methods can be used for this purpose.

Program 5.22-1 Non-Restricted boundary fletcher reeves optimization


import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*;

import javax.swing.*; //OPTIMIZATION WITH BOUNDARY RESTRICTION class f1 extends f_xj { public double func(double x[]) { double ff; double mu=0.0; ff=(x[0]-1.0)*(x[0]-1.0)+(x[1]-2.0)*(x[1]-2.0)+(x[2]-3.0)*(x[2]-3.0)+mu*(P(x)+Q(x)); return ff; } public double P(double x[]) {// p(x)=0 boundary restriction int n=x.length; return (x[0]+x[1]-2.0)*(x[0]+x[1]-2.0); } public double Q(double x[]) {// q(x)>=0 boundary restriction double eps=1.0e-6; int n=x.length; double ax=x[1]-x[2]-3.0-eps; double bx=0; if(ax<0) bx=ax*ax; return bx; } } public class NA116 { public static double VT_V(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; } } return aa; }

public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } public static double turev(f_xj f,double x[],int denklem_ref,int x_ref) { double h0=0.256808; int i,m; int n=7; double f1[]; f1=new double[x.length]; double f2[]; f2=new double[x.length]; double x1[]; x1=new double[x.length]; double x2[]; x2=new double[x.length]; for(i=0;i<x.length;i++) { x1[i]=x[i]; x2[i]=x[i]; } double T[][]; T=new double[n][n]; double h[]; h=new double[n]; for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]*1.0094847; f1=df(f,x1); f2=df(f,x2); T[i][0]=( f1[denklem_ref] - f2[denklem_ref])/(2.0*h[i]); x1[x_ref]=x[x_ref]; x2[x_ref]=x[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) {

T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] df(f_xj f,double xxi[]) { //Gredyen vektor // f(x0,x1,x2,...xn) founksiyonunun trev vektr // [df/dx0, df/dx1,df/dx2,....,df/dxn] // df/dxj j=0...x.length double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=turev(f,xxi,i); } return a; } public static double turev(f_xj f,double xxi[],int x_ref) { // df/dxj double h0=0.256808; int i,m; int n=7; double x1[]; x1=new double[xxi.length]; double x2[]; x2=new double[xxi.length]; for(i=0;i<xxi.length;i++) { x1[i]=xxi[i]; x2[i]=xxi[i]; } double T[][]; T=new double[n][n]; double h[]; h=new double[n]; for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { //h[i]=h0*Math.pow(r,i); h0/=2.0; h[i]=h0; } //first turev (difference formula) for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i];

T[i][0]=(f.func(x1)-f.func(x2))/(2.0*h[i]); x1[x_ref]=xxi[x_ref]; x2[x_ref]=xxi[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] turev(f_xj f_deriv,double x[]) { // df/dxj j=0...x.length // This method calculates turev of a function with more than one independent variable. // Accuracy of method can be adjusted by changing variables h0 and n // function input should be in the form given in abstract class // f_xj,j=0...x.length = df/dx(x_ref) double a[]=new double[x.length]; for(int x_ref=0;x_ref<x.length;x_ref++) { a[x_ref]=turev(f_deriv,x,x_ref); } return a; } public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);}

public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-4; int n=p.length; double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0]; xx=aa[1]; bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[][] linminiter(f_xj f,double p[],double xi[]) { // yeni iterasyon noktasn verir int n=p.length; double xmin=linmin(f,p,xi);

double aa[][]=new double[2][n]; for (int j=0;j<n;j++) { xi[j] *= xmin; p[j] += xi[j]; aa[0][j]=p[j]; aa[1][j]=xi[j]; } return aa; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { // ax,bx,cx adet tahmin noktas (bxax ile cx arasnda yer almaldr) // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; // SHFT(double a,double b,double c,d) (a)=(b);(b)=(c);(c)=(d); int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else { d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d));

fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("BRENT metodunda maksimum iterasyon says ald"); xmin=x; //minumum deer a[0] ile fonksiyon deeri a[1] ile geridndrlmektedir aa[0]=xmin; aa[1]=fx; return aa; } public static double[] mnbrak(f_x f,double ax,double dx) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seilen uzaklk return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seiln uzaklk return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { // ax,bx,cx adet tahmin noktas verildiinde iinde minimum bulunan ax,bx,cx adetnoktay bize arama // sonucu bulur // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0; double TINY=1.0e-20; double fa,fb,fc; fa=0; fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx);

if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;} //SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax; aa[1]=bx; aa[2]=cx; } return aa; } public static double brent(f_x f,double ax,double bx,double cx,double tol)

{ double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } // 4. fletcher Reeves metodu //________________________________________________________________ public static double[] fletcher_reeves( f_xj f,double x[]) { int i,j,ii,jj,k; int nmax=500; double gamma=1.0; double tolerance=1.0e-4; int n=x.length; double alpha; double g[]=new double[n]; double ge[]=new double[n]; double d[]=new double[n]; double beta; double alpha_d; double Q[]=new double[n]; int nn=15; i=0; j=0;k=0; while( j++ < nmax && gamma > tolerance ) { g=turev(f,x); gamma=norm(g); if(k<1) {for(i=0;i<n;i++){d[i]=-g[i];};ge=g;} else {beta=VT_V(g)/VT_V(ge); for(i=0;i<n;i++){Q[i]=beta*d[i];}; } for(i=0;i<n;i++){d[i]=-g[i]+Q[i];} alpha=linmin(f,x,d); for(i=0;i<n;i++){x[i]+=alpha*d[i];} j++;k++; if(k>=nn) k=0; } if(j >= nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); return x; } public static double[] verigir(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; }

public static void main (String args[]) { String s="enter initial root estimations with one space n between: "; double [] x0=verigir(s); f1 f_x=new f1(); double [] r1= fletcher_reeves(f_x,x0); s=" zm seti : \n"+Matrix.toStringT(r1); String s2="Fletcher-Reeves multidimensional restricted boundary optimization:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.out.println("y="+f_x.func(r1)); System.exit(0); } }

Our first example is without any restriction our equation is :


f(x)=(x0-1.0)2+(x1 - 2.0)2+(x2-3.0)2

Now two boundary restriction will apply to the same function p(x)=x0+x1-2.0=0 q(x)= x1- x2-3.0- 0 with this boundary restrictions equation becomes f(x)=(x0-1.0)2+(x1 - 2.0)2+(x2-3.0)2+( p2(x)+q2(x)) For =10

For =100

As it is seen from the result for different values of the results are differs. is the weight function and the effects of restrictions increased when a bigger value of is used. 5.23 TRAVELLING SALESMAN PROBLEM (TSP) Travelling salesman problem is a special kind of optimization problem. Problem satement is relatively simple. A travelling salesman will have to visit n citie in order to sel his products. The coordinates of the cities are set. How salesman can make the shortest travel? Problem can be considered as two basic form. In the first form starting and ending cities can be set. In the second form nothing is set. The simplest way of the definitions of the function:

Dis tan ce = ( xi xi 1 ) 2 + ( yi yi 1 ) 2
i =1

]
]

1/ 2

In this equation xi and yi is coordinates of nth city .

If the same Formula is written for a constan satrting and ending cities

Dis tan ce = ( x0 xstart ) 2 + ( y0 ystart ) 2

1/ 2

+ ( xi xi 1 ) 2 + ( yi yi 1 ) 2
i =1

1/ 2

[( x

final

xn ) 2 + ( y final yn ) 2

1/ 2

In this equation (xstart, ystart) is the starting city coordinate and (xfinal, yfinal) is the destination city coordinate. In the normal version of the TSP problem every city must be travelled only one time, but in some real problem this restriction does not have to be applied. The logical exact solution seems to be measured the distance from first city to the distance of all remaining cities travel there and continue on the process, but when the number of possibilities are concerned it is not practical. It should be also not that the distance between the cities does not have to fit the linear approah given in here (actual roads can be longer than the linear path). In this case a cross-reference table of the distances should be given to calculate it. In here linear path will be assumed. 5.23.1 SOLUTION OF TSP BY GENETIC ALGORITHMS Genetic algorithms can be applied to the TSP problem but, the definition of the gene structure and breeding procedures should be defined specifically to this problem, previous definition of Genetic algorithms was valid for a continious problem, but this of is a combination problem. The basic gene structure is formed by using ArrayList vector structure of Java. In order to do that a list of city corrdinates are needed as input. For example assume that the following city coordinates are given (it is stored in the file cities.txt)

100.0 0.0 100.0 0.0 95.10565162951535 80.90169943749474 30.901699437494738 58.778525229247315

58.778525229247315 80.90169943749474 30.901699437494745 95.10565162951535 6.123233995736766E-15 100.0

-30.901699437494734 95.10565162951536 -58.7785252292473 -80.90169943749473 -95.10565162951535 80.90169943749474 58.77852522924732 30.901699437494752

-100.0 1.2246467991473532E-14 -95.10565162951538 -80.90169943749474 -58.77852522924732 -30.90169943749469 -58.7785252292473 -80.90169943749473

-30.901699437494756 -95.10565162951535 -1.8369701987210297E-14 -100.0

30.901699437494724 -95.10565162951536 58.77852522924729 80.90169943749473 95.10565162951535 -80.90169943749476 -58.778525229247336 -30.901699437494763

In this list the first line is the starting cities coordines, the second line is destination cities coordinates. The remaining is the cities to be visited in between starting and destination. The cities will be given a coordinate code according to the spacing in the given list. For example index of the third city (95.10565162951535, 30.901699437494738) is 0, the fourth city (80.90169943749474,58.778525229247315) is 1 etc. In the Array list visiting order of the index is tha basic gene combination. For example assume that ArrayList contains the following list 2 7 3 8 1 4 6 5

The actual travel is Starting city-2-7-3-8-1-4-6-5-destination city. List can be arranged so that a repetation of the same city is restricted. Program mainly consist of two basic program TSP_gene and TSP_genetic. TSP_gene defines the gene structure as given above, breeding and mutation processes aealso included. Each individual has only one gene. The second program TSP_Genetic defines the defines genetic processes (select parents, breed, mutate new generations etc.) . The best solution is kept and copied to the new generation. A plot method is added to treck the path of the travel. List of numbers does not have much meaning in a case like this. It should be noted that due to statistical nature of GA the best result or the same result in each run should not be expected from the method. 5.23-1 SOLUTION OF TSP BY GENETIC ALGORITHMS, TSP_GENE CLASS
import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; //k ve var noktalar belirlenmi tur public class TSP_gene { public int n; public double xcikilan,ycikilan; public double xvarilan,yvarilan; public double x[],y[]; public double Fitness; // uyum deeri public double RFitness; // relatif uyum public double CFitness; // kmlatif uyum public boolean Breeder; ArrayList<String> v; String dizi; public TSP_gene(double xi[],double yi[]) {n=xi.length-2; v=new ArrayList<String>(n); x=new double[n]; y=new double[n]; xcikilan=xi[0]; ycikilan=yi[0]; xvarilan=xi[1]; yvarilan=yi[1]; Breeder=false; for(int i=0;i<n;i++) {x[i]=xi[i+2];y[i]=yi[i+2];v.add(""+i);} Collections.shuffle(v); //swap(); distance(); } public void mutate() { int n1=(int)(n*Math.random()); int n2=(int)(n*Math.random()); //System.out.println("n="+n+"n1="+n1+"n2="+n2); if(n1!=n2) Collections.swap(v, n1, n2); }

public void swap() {int pos[]=new int[n]; int key[]=new int[n]; int p,k; int n1=0; for(int i=0;i<n;i++) { p=Collections.binarySearch(v,""+i); if(p==i) {pos[i]=p;n1++;} } for(int i=0;i<n1;i+=2) {Collections.swap(v,pos[i],pos[i+1]);} } public TSP_gene(TSP_gene anne) {n=anne.n; v=kopye(anne.v); xcikilan=anne.xcikilan; ycikilan=anne.ycikilan; xvarilan=anne.xvarilan; yvarilan=anne.yvarilan; x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=anne.x[i];y[i]=anne.y[i];} distance(); } public TSP_gene(int a[],double xi[],double yi[]) { ArrayList<String> anne=add(a); v=kopye(anne); Collection torba=v; n=xi.length-2; xcikilan=xi[0]; ycikilan=yi[0]; xvarilan=xi[1]; yvarilan=yi[1]; x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=xi[i+2];y[i]=yi[i+2];} distance(); } public TSP_gene(String dosya) {double a[][]=Text.readDouble(dosya); n=a[0].length-2; v=new ArrayList<String>(n); x=new double[n]; y=new double[n]; xcikilan=a[0][0]; ycikilan=a[1][0]; xvarilan=a[0][1]; yvarilan=a[1][1]; for(int i=0;i<n;i++) {x[i]=a[0][i+2];y[i]=a[1][i+2];v.add(""+i);} Collections.shuffle(v); distance(); } public TSP_gene(TSP_gene anne, TSP_gene baba) {

n=anne.n; xcikilan=anne.xcikilan; ycikilan=anne.ycikilan; xvarilan=anne.xvarilan; yvarilan=anne.yvarilan; x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=anne.x[i];y[i]=anne.y[i];} ArrayList<String> cocuk=s_cocuk(anne.v,baba.v); v=kopye(cocuk); distance(); } public TSP_gene(ArrayList<String> anne,double xi[],double yi[]) { v=kopye(anne); Collection torba=v; n=xi.length-2; xcikilan=xi[0]; ycikilan=yi[0]; xvarilan=xi[1]; yvarilan=yi[1]; x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=xi[i+2];y[i]=yi[i+2];} distance(); } public void plot() { double xi[]=new double[n+2]; double yi[]=new double[n+2]; int a=0; int b=0; String s1=""; String s2=""; Iterator i=v.iterator(); int j=0; double distance; dizi="0"; s1=(String)i.next(); a=Integer.parseInt(s1); distance=Math.sqrt((x[a]-xcikilan)*(x[a]-xcikilan)+(y[a]-ycikilan)*(y[a]-ycikilan)); xi[j]=xcikilan; yi[j]=ycikilan; j++; xi[j]=x[a]; yi[j]=y[a]; j++; while(i.hasNext()) { s2=(String)i.next(); b=Integer.parseInt(s2); distance+=Math.sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])); xi[j]=x[b]; yi[j]=y[b];

j++; a=b; } xi[j]=xvarilan; yi[j]=yvarilan; distance+=Math.sqrt((x[b]-xvarilan)*(x[b]-xvarilan)+(y[b]-yvarilan)*(y[b]-yvarilan)); Fitness=1.0/distance; Plot pp=new Plot(xi,yi); double wx[]=new double[2]; double wy[]=new double[2]; wx[0]=xi[0]; wy[0]=yi[0]; wx[1]=xi[n+1]; wy[1]=yi[n+1]; pp.addData(wx,wy); pp.addData(xi,yi); pp.setPlotType(1,32); pp.setPlotType(2,22); pp.setPlabel("Travellin Salesman Problem distance = "+distance); pp.setXlabel("x, km"); pp.setYlabel("y, km"); pp.plot(); } public double distance() { int a=0; int b=0; String s1=""; String s2=""; Iterator i=v.iterator(); int j=0; double distance; dizi="0"; s1=(String)i.next(); a=Integer.parseInt(s1); distance=Math.sqrt((x[a]-xcikilan)*(x[a]-xcikilan)+(y[a]-ycikilan)*(y[a]-ycikilan)); while(i.hasNext()) { s2=(String)i.next(); b=Integer.parseInt(s2); distance+=Math.sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])); a=b; } distance+=Math.sqrt((x[b]-xvarilan)*(x[b]-xvarilan)+(y[b]-yvarilan)*(y[b]-yvarilan)); Fitness=1.0/distance; return distance; } public void setFitness() {Fitness=1.0/distance();} public double getFitness() {return Fitness;} public void setRFitness(double RFitnessi) {RFitness=RFitnessi;}

public double getRFitness() {return RFitness;} public void setCFitness(double CFitnessi) {CFitness=CFitnessi;} public double getCFitness() {return CFitness;} public static String print(Collection c) { String s=""; Iterator i=c.iterator(); while(i.hasNext()) {s+=i.next()+"\n";} return s; } public String dizi() {return print(v);} public ArrayList<String>kopye(ArrayList<String>list1) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); while(n1.hasNext()) { s1=(String)n1.next(); bilesimVectoru.add(s1); } return bilesimVectoru; } public static ArrayList<String> add(int a[]) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1; for(int i=0;i<a.length;i++) { s1=""+a[i]; if(!bilesimVectoru.contains(s1)) bilesimVectoru.add(s1); } return bilesimVectoru; } public ArrayList<String> bilesim(ArrayList<String>list1, int a) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); String b=new String(""+a); while(n1.hasNext()) { s1=(String)n1.next(); bilesimVectoru.add(s1); } s2=(String)b; if(!bilesimVectoru.contains(s2))

bilesimVectoru.add(s2); return bilesimVectoru; } public ArrayList<String> bilesim(ArrayList<String> list1, ArrayList<String> list2) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); Iterator n2=list2.iterator(); while(n1.hasNext()) { s1=(String)n1.next(); bilesimVectoru.add(s1); } while(n2.hasNext()) { s2=(String)n2.next(); if(!bilesimVectoru.contains(s2)) bilesimVectoru.add(s2); } return bilesimVectoru; } public ArrayList<String> altkme(ArrayList<String> list1, int nn) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); for(int i=0;i<nn;i++) { s1=(String)n1.next(); bilesimVectoru.add(s1); } return bilesimVectoru; } public ArrayList<String> kalankme(ArrayList<String> list1, int nn) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); for(int i=0;i<nn;i++) { s1=(String)n1.next();} while(n1.hasNext()) { s1=(String)n1.next(); bilesimVectoru.add(s1); } return bilesimVectoru; } public ArrayList<String> s_cocuk(ArrayList<String> anne, ArrayList<String> baba) {int nn=(int)(n*Math.random())+1; //System.out.println("nn="+nn); ArrayList<String> anne1=altkme(anne,nn);

ArrayList<String> anne2=kalankme(anne,nn); ArrayList<String> baba1=altkme(baba,nn); ArrayList<String> baba2=kalankme(baba,nn); ArrayList<String> cocuk=bilesim(anne1,baba1); ArrayList<String> cocuk1=bilesim(cocuk,anne2); ArrayList<String> cocuk2=bilesim(cocuk1,baba2); return cocuk2; } public String ciktiS() { Iterator enm = v.iterator(); String s=" ("+xcikilan+","+ycikilan+")\n"; String s1=""; int a=0; while(enm.hasNext()) { s1=(String)enm.next(); a=Integer.parseInt(s1); s+=s1+" ("+x[a]+","+y[a]+") \n"; } s+=" ("+xvarilan+","+yvarilan+") \n"; return s; } public String[] ciktiSA() { Iterator enm = v.iterator(); String s[]=new String[n]; int i=0; while(enm.hasNext()) { s[i]= (String)enm.next()+" ";i++;} return s; } public String toString() { String s=ciktiS()+" distance = "+distance(); return s; } }

5.23-2 SOLUTION OF TSP BY GENETIC ALGORITHMS, TSP_GENETIC CLASS


import java.io.*; import java.text.*; import java.util.Locale; class TSP_genetic { public TSP_gene G[]; // string of variables makes a genotype1 public boolean Breeder; // public int POPSIZE; // population size int MAXGENS; // maximum number of generations; double PMUTATION; // proportion of mutated variables double PXOVER; // probability of crossover; int Generation; // Current generation number int NBreeder; // Number of survivors in a generation

int Best; //The best genotype1 in the population double toplameniyi=0; double nesileniyi=0; double xi[],yi[]; //City coordinates public TSP_genetic(String name,int POPSZEi,int MAXGENSi,double PXOVERi,double PMUTATIONi ) {double a[][]=Text.readDoubleT(name); double xi[]=a[0]; double yi[]=a[1]; int best; POPSIZE=POPSZEi; PXOVER=PXOVERi; MAXGENS=MAXGENSi; PMUTATION=PMUTATIONi; G=new TSP_gene[POPSIZE+1]; double totalfitness=0; for(int i=0;i<(POPSIZE+1);i++) {G[i]=new TSP_gene(xi,yi);G[i].setFitness();totalfitness+=G[i].getFitness();} double xR,xC=0; for(int i=0;i<(POPSIZE+1);i++) {xR=G[i].getRFitness()/totalfitness;xC+=xR;G[i].setRFitness(xR);G[i].setCFitness(xC);} } public void setPopulation() { double totalfitness=0; for(int i=0;i<POPSIZE;i++) {G[i]=new TSP_gene(xi,yi);} evaluate(); } public void evaluate() { nesileniyi=0; double totalfitness=0; for(int i=0;i<POPSIZE+1;i++) {G[i].setFitness(); double distance=1.0/G[i].getFitness(); if(distance>nesileniyi) nesileniyi=distance; if(distance>toplameniyi) toplameniyi=distance; totalfitness+=G[i].getFitness(); } double xR=0,xC=0,xRP=G[POPSIZE].getFitness()/totalfitness; for(int i=0;i<POPSIZE;i++) {xR=G[i].getFitness()/totalfitness;xC+=xR;G[i].setRFitness(xR);G[i].setCFitness(xC); if(xR > xRP) { Best=i; G[POPSIZE]=new TSP_gene(G[i]); xRP=G[POPSIZE].getFitness()/totalfitness; } } } public TSP_gene[] copyPopulation() { TSP_gene Po[]=new TSP_gene[POPSIZE+1]; for(int i=0;i<(POPSIZE+1);i++) { Po[i]=new TSP_gene(G[i]);

Po[i].Fitness=G[i].Fitness; Po[i].RFitness=G[i].RFitness; Po[i].CFitness=G[i].CFitness; } return Po; } public void select() { double r,r1,r2,gf0,gf1; evaluate(); TSP_gene Po[]=new TSP_gene[POPSIZE+1]; NBreeder=0; int i,j; for(i=0;i<(POPSIZE+1);i++) { r=Math.random(); gf0=0; for(j=0;j<POPSIZE;j++) { gf1=G[j].getCFitness(); if((r>=gf0) && (r<=gf1)) {Po[NBreeder]=new TSP_gene(G[j]); NBreeder++;break;} gf0=gf1; } } int breed=(int)(POPSIZE*PXOVER); for(i=0;i<breed;i++) { int a=(int)(Math.random()*NBreeder); int b=(int)(Math.random()*NBreeder); G[i]=new TSP_gene(Po[a],Po[b]); r2=Math.random(); //mutation probability : PMUTATION if(r2 < PMUTATION) G[i].mutate(); } for(i=breed;i<POPSIZE;i++) { int a=(int)(Math.random()*NBreeder); G[i]=new TSP_gene(Po[a]); } } public void generations() { for(int i=0;i<MAXGENS;i++) {select();} } public TSP_gene selected_generation() { generations(); G[POPSIZE].plot(); return G[POPSIZE]; }

public static void main(String arg[]) { TSP_genetic sg=new TSP_genetic("cities.txt",1000,1000,1.0,0.05 );

System.out.println("selected generation : "+sg.selected_generation().toString()); } } ---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" TSP_genetic selected generation : (100.0,0.0) 18 (95.10565162951535,-30.901699437494763) 17 (80.90169943749473,-58.778525229247336) 0 (95.10565162951535,30.901699437494738) 1 (80.90169943749474,58.778525229247315) 2 (58.778525229247315,80.90169943749474) 3 (30.901699437494745,95.10565162951535) 4 (6.123233995736766E-15,100.0) 5 (-30.901699437494734,95.10565162951536) 6 (-58.7785252292473,80.90169943749474) 7 (-80.90169943749473,58.77852522924732) 8 (-95.10565162951535,30.901699437494752) 9 (-100.0,1.2246467991473532E-14) 10 (-95.10565162951538,-30.90169943749469) 11 (-80.90169943749474,-58.7785252292473) 12 (-58.77852522924732,-80.90169943749473) 13 (-30.901699437494756,-95.10565162951535) 14 (-1.8369701987210297E-14,-100.0) 15 (30.901699437494724,-95.10565162951536) 16 (58.77852522924729,-80.90169943749476) (100.0,0.0) distance = 744.7602740406497 -142.35668789808918 122.48243559718966

5.23-3 SOLUTION OF TSP BY GENETIC ALGORITHMS, TSP_GENE1 CLASS


import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; //k ve var noktalar belirlenmemi tur public class TSP_gene1 { public int n; public double x[],y[]; public double Fitness; // uyum deeri

public double RFitness; // relatif uyum public double CFitness; // kmlatif uyum public boolean Breeder; ArrayList<String> v; String dizi; public TSP_gene1(double xi[],double yi[]) {n=xi.length; v=new ArrayList<String>(n); x=new double[n]; y=new double[n]; Breeder=false; for(int i=0;i<n;i++) {x[i]=xi[i];y[i]=yi[i];v.add(""+i);} Collections.shuffle(v); mesafe(); } public void mutate() { int n1=(int)(n*Math.random()); int n2=(n1+1)%n; //int n2=(int)(n*Math.random()); Collections.swap(v, n1, n2); }

public void swap() {int pos[]=new int[n]; int key[]=new int[n]; int p,k; int n1=0; for(int i=0;i<n;i++) { p=Collections.binarySearch(v,""+i); if(p==i) {pos[i]=p;n1++;} } for(int i=0;i<n1;i+=2) {Collections.swap(v,pos[i],pos[i+1]);} } public TSP_gene1(TSP_gene1 mother) {n=mother.n; v=kopye(mother.v); x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=mother.x[i];y[i]=mother.y[i];} mesafe(); } public TSP_gene1(int a[],double xi[],double yi[]) { ArrayList<String> mother=add(a); v=kopye(mother); Collection torba=v; n=xi.length; x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=xi[i];y[i]=yi[i];}

mesafe(); } public TSP_gene1(String dosya) {double a[][]=Text.readDouble(dosya); n=a[0].length; v=new ArrayList<String>(n); x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=a[0][i];y[i]=a[1][i];v.add(""+i);} Collections.shuffle(v); mesafe(); } public TSP_gene1(TSP_gene1 mother, TSP_gene1 father) { n=mother.n; x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=mother.x[i];y[i]=mother.y[i];} ArrayList<String> cocuk=s_cocuk(mother.v,father.v); v=kopye(cocuk); mesafe(); } public TSP_gene1(ArrayList<String> mother,double xi[],double yi[]) { v=kopye(mother); Collection torba=v; n=xi.length; x=new double[n]; y=new double[n]; for(int i=0;i<n;i++) {x[i]=xi[i];y[i]=yi[i];} mesafe(); } public void plot() { double xi[]=new double[n]; double yi[]=new double[n]; int a=0; int b=0; String s1=""; String s2=""; Iterator i=v.iterator(); int j=0; double mesafe=0; dizi="0"; s1=(String)i.next(); a=Integer.parseInt(s1); xi[j]=x[a]; yi[j]=y[a]; j++; while(i.hasNext()) { s2=(String)i.next(); b=Integer.parseInt(s2); mesafe+=Math.sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));

xi[j]=x[b]; yi[j]=y[b]; j++; a=b; } Plot pp=new Plot(xi,yi); double wx[]=new double[2]; double wy[]=new double[2]; wx[0]=xi[0]; wy[0]=yi[0]; wx[1]=xi[n-1]; wy[1]=yi[n-1]; pp.addData(wx,wy); pp.addData(xi,yi); pp.setPlotType(1,32); pp.setPlotType(2,22); pp.setPlabel("TSP problem distance = "+mesafe); pp.setXlabel("x, km"); pp.setYlabel("y, km"); pp.plot(); } public double mesafe() { int a=0; int b=0; String s1=""; String s2=""; Iterator i=v.iterator(); int j=0; double mesafe; dizi="0"; s1=(String)i.next(); a=Integer.parseInt(s1); mesafe=0.0; while(i.hasNext()) { s2=(String)i.next(); b=Integer.parseInt(s2); mesafe+=Math.sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])); a=b; } Fitness=1.0/mesafe; return mesafe; } public void setFitness() {Fitness=1.0/mesafe();} public double getFitness() {return Fitness;} public void setRFitness(double RFitnessi) {RFitness=RFitnessi;} public double getRFitness() {return RFitness;}

public void setCFitness(double CFitnessi) {CFitness=CFitnessi;} public double getCFitness() {return CFitness;} public static String print(Collection c) { String s=""; Iterator i=c.iterator(); while(i.hasNext()) {s+=i.next()+"\n";} return s; } public String dizi() {return print(v);} public ArrayList<String>kopye(ArrayList<String>list1) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); while(n1.hasNext()) { s1=(String)n1.next(); bilesimVectoru.add(s1); } return bilesimVectoru; } public static ArrayList<String> add(int a[]) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1; for(int i=0;i<a.length;i++) { s1=""+a[i]; if(!bilesimVectoru.contains(s1)) bilesimVectoru.add(s1); } return bilesimVectoru; } public ArrayList<String> bilesim(ArrayList<String>list1, int a) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); String b=new String(""+a); while(n1.hasNext()) { s1=(String)n1.next(); bilesimVectoru.add(s1); } s2=(String)b; if(!bilesimVectoru.contains(s2)) bilesimVectoru.add(s2); return bilesimVectoru; }

public ArrayList<String> bilesim(ArrayList<String> list1, ArrayList<String> list2) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); Iterator n2=list2.iterator(); while(n1.hasNext()) { s1=(String)n1.next(); bilesimVectoru.add(s1); } while(n2.hasNext()) { s2=(String)n2.next(); if(!bilesimVectoru.contains(s2)) bilesimVectoru.add(s2); } return bilesimVectoru; } public ArrayList<String> altkme(ArrayList<String> list1, int nn) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); for(int i=0;i<nn;i++) { s1=(String)n1.next(); bilesimVectoru.add(s1); } return bilesimVectoru; } public ArrayList<String> kalankme(ArrayList<String> list1, int nn) { ArrayList<String> bilesimVectoru = new ArrayList<String>(); String s1,s2; Iterator n1=list1.iterator(); for(int i=0;i<nn;i++) { s1=(String)n1.next();} while(n1.hasNext()) { s1=(String)n1.next(); bilesimVectoru.add(s1); } return bilesimVectoru; } public ArrayList<String> s_cocuk(ArrayList<String> mother, ArrayList<String> father) {int nn=(int)(n*Math.random())+1; //System.out.println("nn="+nn); ArrayList<String> mother1=altkme(mother,nn); ArrayList<String> mother2=kalankme(mother,nn); ArrayList<String> father1=altkme(father,nn); ArrayList<String> father2=kalankme(father,nn);

ArrayList<String> cocuk=bilesim(mother1,father1); ArrayList<String> cocuk1=bilesim(cocuk,mother2); ArrayList<String> cocuk2=bilesim(cocuk1,father2); return cocuk2; } public String ciktiS() { Iterator enm = v.iterator(); String s=""; int a=0; String s1=""; while(enm.hasNext()) { s1=(String)enm.next(); a=Integer.parseInt(s1); s+=s1+" ("+x[a]+","+y[a]+") \n"; } return s; } public String[] ciktiSA() { Iterator enm = v.iterator(); String s[]=new String[n]; int i=0; while(enm.hasNext()) { s[i]= (String)enm.next()+" ";i++;} return s; } public String toString() { String s=ciktiS()+" mesafe = "+mesafe(); return s; } }

If starting and final destination is not important , TSP will change slightly 5.23-4 SOLUTION OF TSP BY GENETIC ALGORITHMS, TSP_GENETIC1 CLASS
import java.io.*; import java.text.*; import java.util.Locale; // class TSP_genetic1 { public TSP_gene1 G[]; // string of variables makes a genotype1 public boolean Breeder; // public int POPSIZE; // population size int MAXGENS; // maximum number of generations; double PMUTATION; // proportion of mutated variables double PXOVER; // probability of crossover; int Generation; // Current generation number int NBreeder; // Number of survivors in a generation int Best; //The best genotype1 in the population double toplameniyi=0;

double nesileniyi=0; double xi[],yi[]; //ehir adresleri public TSP_genetic1(String name,int POPSZEi,int MAXGENSi,double PXOVERi,double PMUTATIONi ) {double a[][]=Text.readDoubleT(name); double xi[]=a[0]; double yi[]=a[1]; int best; POPSIZE=POPSZEi; PXOVER=PXOVERi; MAXGENS=MAXGENSi; PMUTATION=PMUTATIONi; G=new TSP_gene1[POPSIZE+1]; double totalfitness=0; for(int i=0;i<(POPSIZE+1);i++) {G[i]=new TSP_gene1(xi,yi);G[i].setFitness();totalfitness+=G[i].getFitness();} double xR,xC=0; for(int i=0;i<(POPSIZE+1);i++) {xR=G[i].getRFitness()/totalfitness;xC+=xR;G[i].setRFitness(xR);G[i].setCFitness(xC);} } public void setPopulation() { double totalfitness=0; for(int i=0;i<POPSIZE;i++) {G[i]=new TSP_gene1(xi,yi);} evaluate(); } public void evaluate() { nesileniyi=0; double totalfitness=0; for(int i=0;i<POPSIZE+1;i++) {G[i].setFitness(); double mesafe=1.0/G[i].getFitness(); if(mesafe>nesileniyi) nesileniyi=mesafe; if(mesafe>toplameniyi) toplameniyi=mesafe; totalfitness+=G[i].getFitness(); } double xR=0,xC=0,xRP=G[POPSIZE].getFitness()/totalfitness; for(int i=0;i<POPSIZE;i++) {xR=G[i].getFitness()/totalfitness;xC+=xR;G[i].setRFitness(xR);G[i].setCFitness(xC); if(xR > xRP) { Best=i; G[POPSIZE]=new TSP_gene1(G[i]); xRP=G[POPSIZE].getFitness()/totalfitness; } } } public TSP_gene1[] copyPopulation() { TSP_gene1 Po[]=new TSP_gene1[POPSIZE+1]; for(int i=0;i<(POPSIZE+1);i++) { Po[i]=new TSP_gene1(G[i]); Po[i].Fitness=G[i].Fitness; Po[i].RFitness=G[i].RFitness;

Po[i].CFitness=G[i].CFitness; } return Po; } public void select() { double r,r1,r2,gf0,gf1; evaluate(); //anne baba aday adaylarn se TSP_gene1 Po[]=new TSP_gene1[POPSIZE+1]; NBreeder=0; int i,j; for(i=0;i<(POPSIZE+1);i++) { r=Math.random(); gf0=0; for(j=0;j<POPSIZE;j++) { gf1=G[j].getCFitness(); if((r>=gf0) && (r<=gf1)) {Po[NBreeder]=new TSP_gene1(G[j]); //System.out.println("j= "+j+"gf0="+gf0+"gf1="+gf1+"r="+r+"NBreeder="+NBreeder); NBreeder++;break;} gf0=gf1; } } // seilen anne, baba aday adaylarnn yeni ocuk yapmalarna olanak ver int breed=(int)(POPSIZE*PXOVER); for(i=0;i<breed;i++) { int a=(int)(Math.random()*NBreeder); int b=(int)(Math.random()*NBreeder); G[i]=new TSP_gene1(Po[a],Po[b]); //System.out.println("a="+a+"b="+b+"cocuk="+G[i].toString()); r2=Math.random(); //mutasyon olasl : PMUTATION if(r2 < PMUTATION) G[i].mutate(); //System.out.println(NBreeder+" "+G[POPSIZE].mesafe()); } //listenin gerisini seilen anne baba adaylaryla doldur for(i=breed;i<POPSIZE;i++) { int a=(int)(Math.random()*NBreeder); G[i]=new TSP_gene1(Po[a]); } } public void generations() { for(int i=0;i<MAXGENS;i++) {select();} } public TSP_gene1 selected_generation() { generations(); G[POPSIZE].plot(); return G[POPSIZE]; }

public static void main(String arg[]) { TSP_genetic1 sg=new TSP_genetic1("cities1.txt",1000,1000,1.0,0.03 ); System.out.println("selected generation : "+sg.selected_generation().toString()); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" TSP_genetic1 Selected heneration : 16 (30.901699437494724,-95.10565162951536) 15 (-1.8369701987210297E-14,-100.0) 14 (-30.901699437494756,-95.10565162951535) 13 (-58.77852522924732,-80.90169943749473) 12 (-80.90169943749474,-58.7785252292473) 11 (-95.10565162951538,-30.90169943749469) 10 (-100.0,1.2246467991473532E-14) 9 (-95.10565162951535,30.901699437494752) 8 (-80.90169943749473,58.77852522924732) 7 (-58.7785252292473,80.90169943749474) 6 (-30.901699437494734,95.10565162951536) 5 (6.123233995736766E-15,100.0) 4 (30.901699437494745,95.10565162951535) 3 (58.778525229247315,80.90169943749474) 2 (80.90169943749474,58.778525229247315) 1 (95.10565162951535,30.901699437494738) 0 (100.0,0.0) 19 (95.10565162951535,-30.90169943749476) 18 (80.90169943749473,-58.778525229247336) 17 (58.77852522924729,-80.90169943749476) mesafe = 594.4509671528771

5.23-3 SOLUTION OF TSP BY MONTE-CARLO ALGORITHM Because TSP is a combinatory problem Monte-Carlo Method, like direct combinatoral calculations, can not give us a good solution. In order to show that a Monte-Carlo algoritm is developed and tried with very high number of iterations (100 million iteration). It is clear from the solution that, it is not enough to solve this problem only through randomness.
import java.io.*; import java.text.*;

import java.util.Locale; class TSP_montecarlo { public TSP_gene G; // string of variables makes a genotype1 public int POPSIZE; // population size double xi[],yi[]; //ehir adresleri public TSP_montecarlo(String name,int POPSZEi) {double a[][]=Text.readDoubleT(name); double xi[]=a[0]; double yi[]=a[1]; POPSIZE=POPSZEi; TSP_gene G1=new TSP_gene(xi,yi); double toplameniyi=1e20; double distance; int counter; for(int i=0;i<POPSIZE;i++) {G1=new TSP_gene(xi,yi); distance=G1.distance(); counter=i%1000000; if(counter==0) System.out.println("i="+i); if(distance < toplameniyi) {toplameniyi=distance;G=new TSP_gene(G1);System.out.println("i="+i+"\n"+G.toString());} } } public static void main(String arg[]) { TSP_montecarlo sg=new TSP_montecarlo("cities.txt",100000000); //System.out.println("selected generations : "+sg.G.toString()); sg.G.plot(); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" TSP_montecarlo i=0 i=0 (100.0,0.0) 17 (80.90169943749473,-58.778525229247336) 9 (-100.0,1.2246467991473532E-14) 10 (-95.10565162951538,-30.90169943749469) 7 (-80.90169943749473,58.77852522924732) 4 (6.123233995736766E-15,100.0) 15 (30.901699437494724,-95.10565162951536) 3 (30.901699437494745,95.10565162951535) 6 (-58.7785252292473,80.90169943749474) 1 (80.90169943749474,58.778525229247315) 2 (58.778525229247315,80.90169943749474) 8 (-95.10565162951535,30.901699437494752) 12 (-58.77852522924732,-80.90169943749473) 13 (-30.901699437494756,-95.10565162951535) 14 (-1.8369701987210297E-14,-100.0) 5 (-30.901699437494734,95.10565162951536) 16 (58.77852522924729,-80.90169943749476) 0 (95.10565162951535,30.901699437494738)

18 (95.10565162951535,-30.901699437494763) 11 (-80.90169943749474,-58.7785252292473) (100.0,0.0) distance = 2400.9357446270287 i=3 (100.0,0.0) 17 (80.90169943749473,-58.778525229247336) 14 (-1.8369701987210297E-14,-100.0) 12 (-58.77852522924732,-80.90169943749473) 11 (-80.90169943749474,-58.7785252292473) 13 (-30.901699437494756,-95.10565162951535) 10 (-95.10565162951538,-30.90169943749469) 16 (58.77852522924729,-80.90169943749476) 6 (-58.7785252292473,80.90169943749474) 1 (80.90169943749474,58.778525229247315) 2 (58.778525229247315,80.90169943749474) 3 (30.901699437494745,95.10565162951535) 5 (-30.901699437494734,95.10565162951536) 9 (-100.0,1.2246467991473532E-14) 8 (-95.10565162951535,30.901699437494752) 0 (95.10565162951535,30.901699437494738) 18 (95.10565162951535,-30.901699437494763) 7 (-80.90169943749473,58.77852522924732) 4 (6.123233995736766E-15,100.0) 15 (30.901699437494724,-95.10565162951536) (100.0,0.0) distance = 2030.1843617772342 i=502 (100.0,0.0) 0 (95.10565162951535,30.901699437494738) 2 (58.778525229247315,80.90169943749474) 12 (-58.77852522924732,-80.90169943749473) 13 (-30.901699437494756,-95.10565162951535) 10 (-95.10565162951538,-30.90169943749469) 7 (-80.90169943749473,58.77852522924732) 11 (-80.90169943749474,-58.7785252292473) 14 (-1.8369701987210297E-14,-100.0) 16 (58.77852522924729,-80.90169943749476) 15 (30.901699437494724,-95.10565162951536) 18 (95.10565162951535,-30.901699437494763) 4 (6.123233995736766E-15,100.0) 9 (-100.0,1.2246467991473532E-14) 6 (-58.7785252292473,80.90169943749474) 3 (30.901699437494745,95.10565162951535) 17 (80.90169943749473,-58.778525229247336) 8 (-95.10565162951535,30.901699437494752) 5 (-30.901699437494734,95.10565162951536) 1 (80.90169943749474,58.778525229247315) (100.0,0.0) distance = 2012.5374983077775 i=575 (100.0,0.0) 2 (58.778525229247315,80.90169943749474) 0 (95.10565162951535,30.901699437494738) 1 (80.90169943749474,58.778525229247315) 3 (30.901699437494745,95.10565162951535) 5 (-30.901699437494734,95.10565162951536) 6 (-58.7785252292473,80.90169943749474)

4 (6.123233995736766E-15,100.0) 16 (58.77852522924729,-80.90169943749476) 7 (-80.90169943749473,58.77852522924732) 18 (95.10565162951535,-30.901699437494763) 8 (-95.10565162951535,30.901699437494752) 10 (-95.10565162951538,-30.90169943749469) 11 (-80.90169943749474,-58.7785252292473) 13 (-30.901699437494756,-95.10565162951535) 17 (80.90169943749473,-58.778525229247336) 9 (-100.0,1.2246467991473532E-14) 12 (-58.77852522924732,-80.90169943749473) 14 (-1.8369701987210297E-14,-100.0) 15 (30.901699437494724,-95.10565162951536) (100.0,0.0) distance = 1949.9796077260353 i=1148 (100.0,0.0) 16 (58.77852522924729,-80.90169943749476) 17 (80.90169943749473,-58.778525229247336) 18 (95.10565162951535,-30.901699437494763) 2 (58.778525229247315,80.90169943749474) 11 (-80.90169943749474,-58.7785252292473) 12 (-58.77852522924732,-80.90169943749473) 10 (-95.10565162951538,-30.90169943749469) 8 (-95.10565162951535,30.901699437494752) 9 (-100.0,1.2246467991473532E-14) 13 (-30.901699437494756,-95.10565162951535) 5 (-30.901699437494734,95.10565162951536) 7 (-80.90169943749473,58.77852522924732) 6 (-58.7785252292473,80.90169943749474) 14 (-1.8369701987210297E-14,-100.0) 0 (95.10565162951535,30.901699437494738) 15 (30.901699437494724,-95.10565162951536) 1 (80.90169943749474,58.778525229247315) 3 (30.901699437494745,95.10565162951535) 4 (6.123233995736766E-15,100.0) (100.0,0.0) distance = 1945.2569392748203 i=1473 (100.0,0.0) 18 (95.10565162951535,-30.901699437494763) 12 (-58.77852522924732,-80.90169943749473) 11 (-80.90169943749474,-58.7785252292473) 2 (58.778525229247315,80.90169943749474) 1 (80.90169943749474,58.778525229247315) 14 (-1.8369701987210297E-14,-100.0) 17 (80.90169943749473,-58.778525229247336) 16 (58.77852522924729,-80.90169943749476) 6 (-58.7785252292473,80.90169943749474) 9 (-100.0,1.2246467991473532E-14) 5 (-30.901699437494734,95.10565162951536) 7 (-80.90169943749473,58.77852522924732) 8 (-95.10565162951535,30.901699437494752) 3 (30.901699437494745,95.10565162951535) 4 (6.123233995736766E-15,100.0) 10 (-95.10565162951538,-30.90169943749469) 13 (-30.901699437494756,-95.10565162951535) 15 (30.901699437494724,-95.10565162951536)

0 (95.10565162951535,30.901699437494738) (100.0,0.0) distance = 1914.7548822898239 i=2518 (100.0,0.0) 2 (58.778525229247315,80.90169943749474) 4 (6.123233995736766E-15,100.0) 3 (30.901699437494745,95.10565162951535) 5 (-30.901699437494734,95.10565162951536) 6 (-58.7785252292473,80.90169943749474) 7 (-80.90169943749473,58.77852522924732) 12 (-58.77852522924732,-80.90169943749473) 8 (-95.10565162951535,30.901699437494752) 9 (-100.0,1.2246467991473532E-14) 13 (-30.901699437494756,-95.10565162951535) 1 (80.90169943749474,58.778525229247315) 16 (58.77852522924729,-80.90169943749476) 15 (30.901699437494724,-95.10565162951536) 10 (-95.10565162951538,-30.90169943749469) 14 (-1.8369701987210297E-14,-100.0) 17 (80.90169943749473,-58.778525229247336) 18 (95.10565162951535,-30.901699437494763) 0 (95.10565162951535,30.901699437494738) 11 (-80.90169943749474,-58.7785252292473) (100.0,0.0) distance = 1909.6232492935656 i=4442 (100.0,0.0) 0 (95.10565162951535,30.901699437494738) 2 (58.778525229247315,80.90169943749474) 1 (80.90169943749474,58.778525229247315) 8 (-95.10565162951535,30.901699437494752) 12 (-58.77852522924732,-80.90169943749473) 13 (-30.901699437494756,-95.10565162951535) 14 (-1.8369701987210297E-14,-100.0) 11 (-80.90169943749474,-58.7785252292473) 16 (58.77852522924729,-80.90169943749476) 18 (95.10565162951535,-30.901699437494763) 17 (80.90169943749473,-58.778525229247336) 9 (-100.0,1.2246467991473532E-14) 10 (-95.10565162951538,-30.90169943749469) 7 (-80.90169943749473,58.77852522924732) 3 (30.901699437494745,95.10565162951535) 5 (-30.901699437494734,95.10565162951536) 6 (-58.7785252292473,80.90169943749474) 4 (6.123233995736766E-15,100.0) 15 (30.901699437494724,-95.10565162951536) (100.0,0.0) distance = 1707.860830280625 i=9414 (100.0,0.0) 2 (58.778525229247315,80.90169943749474) 1 (80.90169943749474,58.778525229247315) 8 (-95.10565162951535,30.901699437494752) 7 (-80.90169943749473,58.77852522924732) 6 (-58.7785252292473,80.90169943749474) 3 (30.901699437494745,95.10565162951535) 4 (6.123233995736766E-15,100.0)

5 (-30.901699437494734,95.10565162951536) 15 (30.901699437494724,-95.10565162951536) 16 (58.77852522924729,-80.90169943749476) 18 (95.10565162951535,-30.901699437494763) 17 (80.90169943749473,-58.778525229247336) 9 (-100.0,1.2246467991473532E-14) 11 (-80.90169943749474,-58.7785252292473) 14 (-1.8369701987210297E-14,-100.0) 10 (-95.10565162951538,-30.90169943749469) 13 (-30.901699437494756,-95.10565162951535) 12 (-58.77852522924732,-80.90169943749473) 0 (95.10565162951535,30.901699437494738) (100.0,0.0) distance = 1644.5621964282616 i=14512 (100.0,0.0) 9 (-100.0,1.2246467991473532E-14) 13 (-30.901699437494756,-95.10565162951535) 14 (-1.8369701987210297E-14,-100.0) 11 (-80.90169943749474,-58.7785252292473) 12 (-58.77852522924732,-80.90169943749473) 10 (-95.10565162951538,-30.90169943749469) 8 (-95.10565162951535,30.901699437494752) 7 (-80.90169943749473,58.77852522924732) 4 (6.123233995736766E-15,100.0) 5 (-30.901699437494734,95.10565162951536) 1 (80.90169943749474,58.778525229247315) 2 (58.778525229247315,80.90169943749474) 0 (95.10565162951535,30.901699437494738) 18 (95.10565162951535,-30.901699437494763) 3 (30.901699437494745,95.10565162951535) 6 (-58.7785252292473,80.90169943749474) 17 (80.90169943749473,-58.778525229247336) 16 (58.77852522924729,-80.90169943749476) 15 (30.901699437494724,-95.10565162951536) (100.0,0.0) distance = 1630.24632213183 i=93035 (100.0,0.0) 4 (6.123233995736766E-15,100.0) 6 (-58.7785252292473,80.90169943749474) 5 (-30.901699437494734,95.10565162951536) 7 (-80.90169943749473,58.77852522924732) 12 (-58.77852522924732,-80.90169943749473) 8 (-95.10565162951535,30.901699437494752) 13 (-30.901699437494756,-95.10565162951535) 11 (-80.90169943749474,-58.7785252292473) 10 (-95.10565162951538,-30.90169943749469) 9 (-100.0,1.2246467991473532E-14) 16 (58.77852522924729,-80.90169943749476) 17 (80.90169943749473,-58.778525229247336) 15 (30.901699437494724,-95.10565162951536) 14 (-1.8369701987210297E-14,-100.0) 18 (95.10565162951535,-30.901699437494763) 2 (58.778525229247315,80.90169943749474) 3 (30.901699437494745,95.10565162951535) 1 (80.90169943749474,58.778525229247315) 0 (95.10565162951535,30.901699437494738)

(100.0,0.0) distance = 1514.4486633644028 i=1000000 i=1399005 (100.0,0.0) 16 (58.77852522924729,-80.90169943749476) 15 (30.901699437494724,-95.10565162951536) 14 (-1.8369701987210297E-14,-100.0) 13 (-30.901699437494756,-95.10565162951535) 11 (-80.90169943749474,-58.7785252292473) 12 (-58.77852522924732,-80.90169943749473) 1 (80.90169943749474,58.778525229247315) 2 (58.778525229247315,80.90169943749474) 8 (-95.10565162951535,30.901699437494752) 9 (-100.0,1.2246467991473532E-14) 7 (-80.90169943749473,58.77852522924732) 10 (-95.10565162951538,-30.90169943749469) 4 (6.123233995736766E-15,100.0) 5 (-30.901699437494734,95.10565162951536) 6 (-58.7785252292473,80.90169943749474) 3 (30.901699437494745,95.10565162951535) 17 (80.90169943749473,-58.778525229247336) 0 (95.10565162951535,30.901699437494738) 18 (95.10565162951535,-30.901699437494763) (100.0,0.0) distance = 1513.1324982330177 i=2000000 i=2171692 (100.0,0.0) 18 (95.10565162951535,-30.901699437494763) 17 (80.90169943749473,-58.778525229247336) 13 (-30.901699437494756,-95.10565162951535) 9 (-100.0,1.2246467991473532E-14) 10 (-95.10565162951538,-30.90169943749469) 11 (-80.90169943749474,-58.7785252292473) 12 (-58.77852522924732,-80.90169943749473) 14 (-1.8369701987210297E-14,-100.0) 16 (58.77852522924729,-80.90169943749476) 1 (80.90169943749474,58.778525229247315) 6 (-58.7785252292473,80.90169943749474) 7 (-80.90169943749473,58.77852522924732) 8 (-95.10565162951535,30.901699437494752) 5 (-30.901699437494734,95.10565162951536) 2 (58.778525229247315,80.90169943749474) 4 (6.123233995736766E-15,100.0) 3 (30.901699437494745,95.10565162951535) 15 (30.901699437494724,-95.10565162951536) 0 (95.10565162951535,30.901699437494738) (100.0,0.0) distance = 1498.1779064811512 i=2419255 (100.0,0.0) 0 (95.10565162951535,30.901699437494738) 1 (80.90169943749474,58.778525229247315) 3 (30.901699437494745,95.10565162951535) 6 (-58.7785252292473,80.90169943749474) 5 (-30.901699437494734,95.10565162951536) 2 (58.778525229247315,80.90169943749474)

4 (6.123233995736766E-15,100.0) 10 (-95.10565162951538,-30.90169943749469) 11 (-80.90169943749474,-58.7785252292473) 17 (80.90169943749473,-58.778525229247336) 18 (95.10565162951535,-30.901699437494763) 15 (30.901699437494724,-95.10565162951536) 14 (-1.8369701987210297E-14,-100.0) 13 (-30.901699437494756,-95.10565162951535) 12 (-58.77852522924732,-80.90169943749473) 8 (-95.10565162951535,30.901699437494752) 7 (-80.90169943749473,58.77852522924732) 9 (-100.0,1.2246467991473532E-14) 16 (58.77852522924729,-80.90169943749476) (100.0,0.0) distance = 1449.5497865351686 i=3000000 i=3436411 (100.0,0.0) 0 (95.10565162951535,30.901699437494738) 1 (80.90169943749474,58.778525229247315) 2 (58.778525229247315,80.90169943749474) 5 (-30.901699437494734,95.10565162951536) 6 (-58.7785252292473,80.90169943749474) 11 (-80.90169943749474,-58.7785252292473) 9 (-100.0,1.2246467991473532E-14) 10 (-95.10565162951538,-30.90169943749469) 8 (-95.10565162951535,30.901699437494752) 3 (30.901699437494745,95.10565162951535) 4 (6.123233995736766E-15,100.0) 7 (-80.90169943749473,58.77852522924732) 14 (-1.8369701987210297E-14,-100.0) 13 (-30.901699437494756,-95.10565162951535) 12 (-58.77852522924732,-80.90169943749473) 15 (30.901699437494724,-95.10565162951536) 18 (95.10565162951535,-30.901699437494763) 16 (58.77852522924729,-80.90169943749476) 17 (80.90169943749473,-58.778525229247336) (100.0,0.0) distance = 1353.0320496763034 i=4000000 i=5000000 i=6000000 i=6469430 (100.0,0.0) 18 (95.10565162951535,-30.901699437494763) 0 (95.10565162951535,30.901699437494738) 11 (-80.90169943749474,-58.7785252292473) 10 (-95.10565162951538,-30.90169943749469) 8 (-95.10565162951535,30.901699437494752) 6 (-58.7785252292473,80.90169943749474) 5 (-30.901699437494734,95.10565162951536) 4 (6.123233995736766E-15,100.0) 1 (80.90169943749474,58.778525229247315) 2 (58.778525229247315,80.90169943749474) 3 (30.901699437494745,95.10565162951535) 7 (-80.90169943749473,58.77852522924732) 9 (-100.0,1.2246467991473532E-14) 14 (-1.8369701987210297E-14,-100.0)

15 (30.901699437494724,-95.10565162951536) 12 (-58.77852522924732,-80.90169943749473) 13 (-30.901699437494756,-95.10565162951535) 16 (58.77852522924729,-80.90169943749476) 17 (80.90169943749473,-58.778525229247336) (100.0,0.0) distance = 1319.5094061059226 i=7000000 i=7827971 (100.0,0.0) 2 (58.778525229247315,80.90169943749474) 4 (6.123233995736766E-15,100.0) 6 (-58.7785252292473,80.90169943749474) 8 (-95.10565162951535,30.901699437494752) 7 (-80.90169943749473,58.77852522924732) 5 (-30.901699437494734,95.10565162951536) 1 (80.90169943749474,58.778525229247315) 3 (30.901699437494745,95.10565162951535) 9 (-100.0,1.2246467991473532E-14) 12 (-58.77852522924732,-80.90169943749473) 10 (-95.10565162951538,-30.90169943749469) 11 (-80.90169943749474,-58.7785252292473) 13 (-30.901699437494756,-95.10565162951535) 14 (-1.8369701987210297E-14,-100.0) 16 (58.77852522924729,-80.90169943749476) 15 (30.901699437494724,-95.10565162951536) 17 (80.90169943749473,-58.778525229247336) 18 (95.10565162951535,-30.901699437494763) 0 (95.10565162951535,30.901699437494738) (100.0,0.0) distance = 1266.711996027475 i=8000000 i=9000000 i=10000000 i=11000000 i=12000000 i=13000000 i=14000000 i=15000000 i=16000000 i=17000000 i=18000000 i=19000000 i=20000000 i=21000000 i=22000000 i=23000000 i=24000000 i=25000000 i=26000000 i=27000000 i=27211995 (100.0,0.0) 0 (95.10565162951535,30.901699437494738) 18 (95.10565162951535,-30.901699437494763) 11 (-80.90169943749474,-58.7785252292473) 10 (-95.10565162951538,-30.90169943749469) 9 (-100.0,1.2246467991473532E-14)

3 (30.901699437494745,95.10565162951535) 2 (58.778525229247315,80.90169943749474) 1 (80.90169943749474,58.778525229247315) 4 (6.123233995736766E-15,100.0) 6 (-58.7785252292473,80.90169943749474) 5 (-30.901699437494734,95.10565162951536) 7 (-80.90169943749473,58.77852522924732) 8 (-95.10565162951535,30.901699437494752) 12 (-58.77852522924732,-80.90169943749473) 14 (-1.8369701987210297E-14,-100.0) 13 (-30.901699437494756,-95.10565162951535) 15 (30.901699437494724,-95.10565162951536) 16 (58.77852522924729,-80.90169943749476) 17 (80.90169943749473,-58.778525229247336) (100.0,0.0) distance = 1232.0491774494658 i=28000000 i=29000000 i=30000000 i=31000000 i=32000000 i=33000000 i=34000000 i=35000000 i=36000000 i=37000000 i=38000000 i=39000000 i=40000000 i=41000000 i=42000000 i=43000000 i=44000000 i=45000000 i=46000000 i=47000000 i=48000000 i=49000000 i=50000000 i=51000000 i=52000000 i=53000000 i=54000000 i=55000000 i=56000000 i=57000000 i=58000000 i=59000000 i=60000000 i=61000000 i=62000000 i=63000000 i=64000000 i=65000000 i=66000000 i=67000000 i=68000000 i=69000000

i=70000000 i=71000000 i=72000000 i=73000000 i=74000000 i=75000000 i=76000000 i=77000000 i=78000000 i=79000000 i=80000000 i=81000000 i=82000000 i=83000000 i=84000000 i=85000000 i=86000000 i=87000000 i=88000000 i=89000000 i=90000000 i=91000000 i=92000000 i=93000000 i=94000000 i=95000000 i=96000000 i=97000000 i=98000000 i=99000000

5.24 LINEER MULTIVARIABLE OPTMZATION, SMPLEX METHOD

As the last topic in optimization, Linear optimization will be investigated. Linear problems are existed only with the boundary restrictions. The general definition of the problem : Maximum Z=a0x0+ a1x1+ a2x2+ a3x3++ anxn Subject to: b00x0+ b01x0+ b02x0+ b03x0++ b0nx0 <= A0 b10x0+ b11x0+ b12x0+ b13x0++ b1nx0 <= A1

b20x0+ b21x0+ b22x0+ b23x0++ b2nx0 <= A2

bn0x0+ bn,1x0+ bn2x0+ bn3x0++ bnnx0 <= An

If it is minimum instead of maximum we can simply change the sign of the function. An example case can be given as

Maximum Z=60x+50y Subject to : 2x+4y<=80 3x+2y<=60

In order solve equation in Simplex method, form of the equation is changed as follows: Z-a0x0- a1x1- a2x2- a3x3-- anxn = 0 Subject to : b00x0+ b01x1+ b02x2+ b03x3++ b0nxn + W0 = A0 b10x0+ b11x1+ b12x2+ b13x3++ b1nxn + W1 = A1 b20x0+ b21x1+ b22x2+ b23x3++ b2nxn + W2 = A2

bn0x0+ bn,1x1+ bn2x2+ bn3x3++ bnnxn + Wn = An where Ws are additional slack variables. If the equation is written in matrix format

a0 b 00 b10 ... bn 0

a1 b01 b11 ... bn1

a2 b02 b12 ... bn 2

.... an .... b0 n .... b1n .... ... .... bnn

0 1 0 ... 0

0 0 1 ...

... ... ... ...

0 0 0 ...

0 ... 1

x0 x 1 x2 0 .... A x 0 n = A1 W0 W .... 1 An W 2 .... Wn

Due to inequality of the row and columns matrix can not be solved directly. Simplex method is used to tolve the system as follows:

1. In the main equation coefficient with the biggest negative number is selected. The column with this number is called critical column. 2. The value in the right hand side vector is divide by the value in the critical column. The row with the minimum value is selected as critical row. 3. Critical raw and column intersection point is called the critical value 4. Critical row is normalised by dividing to critical number 5. The following process applied to raws outside the critical row New value = old value [value in the critical column and the same row]*[value in the critical raw in the same column] / critical number 6. Step 1 to 5 repeated until all values in the main equation becomes positive We can look at the process through an example : Maximum of Z=60x+50y Subject to: 2x+4y<=80 3x+2y<=60 Simplex format : Z-60x-50y+0WA+0WB = 0 2x+4y + WA+0WB= 80 3x+2y +0WA+ WB=60 matrix format:
60 50 0 0 0 2 4 1 0 80 3 2 0 1 60

As the first iteration critical column is the column with number -60. In order to find the critical row t1=80/2=40 and t2=60/3=20 compared, since 20 is smaller raw 3 is selected as critical raw. Critical value is then selected as 3. Third row is diveded by 3 becomes
1 2 3 0 1 3 20

In the rawsother than critical raw, a gauss elimination type process aplied to make column values 0 For example in order to make coefficient of x value in the first row equal to 0 following procedure is applied 3rd row(critical row)multiplied with 50 and Added to first row. In this case the values of the first row becomes: 0 10 0 20 1200 olacaktr. At the end of these processes matrix becomes
0 10 0 20 1200 8 2 1 40 0 3 3 2 1 1 0 20 3 3

The same iterative process is repeated as a second time and result will be
15 0 0 4 3 0 1 8 1 0 1 4 35 1350 2 1 15 4 1 10 2

If Wa and Wb values are replaced with 0 value x=10 and y=15 is found. The f-unction value becomes 1350 . Now the same problem will be solved by using the computer program Program 5.24-1 Lineer simplex optimization class simplex { public static double[] simplex(double[][] a) { int n=a.length; //row int m=a[0].length; //column System.out.println("m="+m+"n="+n); int m1=m-1;// int n1=n-1; int m2=m1+n1; int m3=m2+1; int keyrow=0; int keycolumn=0; double keynumber=0.0; double ratio=1.0; double b[][]=new double[n][m3]; double d[]=new double[m]; String s[]=new String[m]; for(int i=0;i<n;i++) { b[i][m2]=a[i][m1]; } for(int i=0;i<n-1;i++) { for(int j=0;j<m1;j++) {b[i][j]=a[i][j];} for(int j=m1;j<m2;j++) {if(i==(j-m1)) b[i+1][j]=1.0;System.out.println("i="+i+"j="+j+"bij="+b[i+1][j]);} } double max; double min; System.out.println(Matrix.toString(b)); for(int k=0;k<n1;k++) { max=-9.999e30; min=9.999e30; for(int j=0;j<m1;j++) {if( (b[0][j]<0) && Math.abs(b[0][j])>max) {max=Math.abs(b[0][j]);keycolumn=j;}} for(int i=1;i<n;i++) { ratio=b[i][m2]/b[i][keycolumn]; if(ratio<min) {min=ratio;keyrow=i;} } keynumber=b[keyrow][keycolumn]; double c[][]=new double[n][m3]; for(int i=0;i<n;i++) { for(int j=0;j<m3;j++) {c[i][j]=b[i][j];}} for(int i=0;i<n;i++)

{ if(i!=keyrow) {for(int j=0;j<m3;j++) {b[i][j]=c[i][j]c[keyrow][j]*c[i][keycolumn]/keynumber;}} else {for(int j=0;j<m3;j++) {b[i][j]=c[i][j]/keynumber;}} } System.out.println("keynumber = "+keynumber); System.out.println("keyrow = "+keyrow+" keycolumn = "+keycolumn); System.out.println(Matrix.toString(b)); } d[m1]=b[0][m2]; s[m1]="y=f(x)"; for(int j=0;j<m1;j++) { s[j]="x["+j+"]";} for(int i=1;i<n;i++) { for(int j=0;j<m1;j++) { if((b[i][j]==1.0)&& (b[0][j]==0.0)) { d[j]=b[i][m2];} } } Text.print(d,s,"Simplex algorithm"); return d; } public static void main(String arg[]) { double a[][]=Text.readDouble_TextArea(); double d[]=simplex(a); System.out.println(Matrix.toString(d)); } }

---------- Capture Output ---------> "C:\co\java\bin\java.exe" OPO25 -60.000000000000000 -50.000000000000000 0.000000000000000 0.000000000000000 2.000000000000000 4.000000000000000 0.000000000000000 80.000000000000000 3.000000000000000 2.000000000000000 1.000000000000000 60.000000000000000

0.000000000000000 1.000000000000000 0.000000000000000

keynumber = 3.0 keyrow = 2 keycolumn = 0 0.000000000000000 -10.000000000000000 20.000000000000000 1200.000000000000000 0.000000000000000 2.666666666666667 0.666666666666667 40.000000000000000 1.000000000000000 0.666666666666667 0.333333333333333 20.000000000000000 keynumber = 2.666666666666667 keyrow = 1 keycolumn = 1 0.000000000000000 0.000000000000000 17.500000000000000 1350.000000000000000 0.000000000000000 1.000000000000000 0.250000000000000 14.999999999999998 1.000000000000000 0.000000000000000 0.500000000000000 10.000000000000002 10.000000000000002

0.000000000000000 1.000000000000000 0.000000000000000 -

3.750000000000000 0.375000000000000 -0.250000000000000 -

14.999999999999998

1350.000000000000000

5.25 SOLUTION OF THE NONLINEAR SYSTEM OF EQUATIONS BY USING OPTIMIZATION ALGORITHMS Non-Linear system of equations can also be solved by using optimization methods through an adaptation function To solve function fi(xj)=0

g(x j ) =

n _ equation i =0 i

f (x ) *f (x )
j i j

The derivative of this equation is the root of non-linear system of equation again became fi(xj)=0, therefore

optimum of g(xj) is the same problem as solution of fi(xj)=0.


As the first adaptation algorithm Nelder-Mead Method will be given. Nelder_Mead method was given at subchapter 5.10 as NA41:

Program 5.25-1 Solution of Non-Linear system of equations by using Nelder-Mead optimization method.
// Solution of system of equation by using Nelder-Mead Method // import java.util.*; import java.awt.*;

import java.applet.Applet; import java.awt.event.*; import javax.swing.*;

class f1 extends f_xj {//adaptation function fi_xi ff1; public f1(fi_xi ffi) {ff1=ffi;} public double func(double x[]) { double ff=0.0; double fa[]=ff1.func(x); for(int i=0;i<fa.length;i++) ff+=fa[i]*fa[i]; return ff; } } class fa extends fi_xi { // Nonlinear system of equation public double[] func(double x[]) { double ff[]=new double[3]; ff[0]=3.0*x[0]-Math.cos(x[1]*x[2])-0.5; ff[1]=x[0]*x[0]-81.0*(x[1]+0.1)*(x[1]+0.1)+Math.sin(x[2])+1.06; ff[2]=Math.exp(-x[0]*x[1])+20.0*x[2]+(10.0*Math.PI-3.0)/3.0; return ff; } } public class NA117 { public static void main (String args[]) { String s="leave a space between initial root guess values: "; double [] x0=NA41.inputdata(s); double [] dx0=new double[x0.length]; for(int i=0;i<x0.length;i++) dx0[i]=0.2*x0[i]; fa ff=new fa(); f1 f=new f1(ff); double [] r1= NA41.nelder(f,x0,dx0); s=" solution set : \n"+Matrix.toStringT(r1); String s2="Nelder-Mead optimization method to solve system of non-linear equation:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

Program 5.25-2 Solution of Non-Linear system of equations by using Genetic Algorithms optimization method.
//Genetic algorithms to solve system of equations import java.io.*; class f1 extends f_xj {//adaptor class fi_xi ff1; public f1(fi_xi ffi) {ff1=ffi;} public double func(double x[]) { double ff=0.0; double fa[]=ff1.func(x); for(int i=0;i<fa.length;i++) ff+=fa[i]*fa[i]; return 10-ff; //minimum deil maxsimum istendiinden - ile arptk } } class fa extends fi_xi { public double[] func(double x[]) { //zm istenen fonksiyon double ff[]=new double[3]; ff[0]=3.0*x[0]-Math.cos(x[1]*x[2])-0.5; ff[1]=x[0]*x[0]-81.0*(x[1]+0.1)*(x[1]+0.1)+Math.sin(x[2])+1.06; ff[2]=Math.exp(-x[0]*x[1])+20.0*x[2]+(10.0*Math.PI-3.0)/3.0; return ff; } } class NA118 { public static void main(String args[]) throws IOException { int N=18; int POPSIZE=500; int MAXGENS=500; int NVARS=3; double PXOVER=0.2; double PMUTATION=0.05; fa ff=new fa(); f1 f_x=new f1(ff); Genetic1 xx=new Genetic1(POPSIZE,MAXGENS,NVARS,N,PXOVER,PMUTATION);

double low[]=new double[NVARS]; double high[]=new double[NVARS]; low[0]=-1.0;high[0]=1.0; low[1]=-1.0;high[1]=1.0; low[2]=-1.0;high[2]=1.0; xx.setPopulation(low,high,f_x); System.out.println("best=\n"+Matrix.toStringT(xx.calculate(f_x,false))); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA118 best= 0.499959945525915 -0.000003814711818 -0.523595899947739 9.999999981442672

> Terminated with exit code 0.

// Solution of system of equation by using Steepest descent Method // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*;

class f2 extends f_xj {//adaptation function fi_xi ff1; public f2(fi_xi ffi) {ff1=ffi;} public double func(double x[]) { double ff=0.0; double fa[]=ff1.func(x); for(int i=0;i<fa.length;i++) ff+=fa[i]*fa[i]; return ff; } } class fa extends fi_xi { // Nonlinear system of equation public double[] func(double x[]) { double ff[]=new double[3]; ff[0]=3.0*x[0]-Math.cos(x[1]*x[2])-0.5; ff[1]=x[0]*x[0]-81.0*(x[1]+0.1)*(x[1]+0.1)+Math.sin(x[2])+1.06; ff[2]=Math.exp(-x[0]*x[1])+20.0*x[2]+(10.0*Math.PI-3.0)/3.0; return ff; } }

public class NA119 { public static void main (String args[]) { String s="leave a space between initial root guess values: "; double [] x0=NA43.inputdata(s); fa ff=new fa(); f2 f=new f2(ff); double [] r1= NA43.steepestdescent(f,x0); s=" solution set : \n"+Matrix.toStringT(r1); String s2="Steepest-descent optimization method to solve system of non-linear equation:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

// Solution of system of equation by using Davidon-Fletcher-Powell Method // import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*;

class f2 extends f_xj {//adaptation function fi_xi ff1; public f2(fi_xi ffi) {ff1=ffi;} public double func(double x[]) { double ff=0.0; double fa[]=ff1.func(x); for(int i=0;i<fa.length;i++) ff+=fa[i]*fa[i]; return ff; } } class fa extends fi_xi { // Nonlinear system of equation public double[] func(double x[]) { double ff[]=new double[3]; ff[0]=3.0*x[0]-Math.cos(x[1]*x[2])-0.5;

ff[1]=x[0]*x[0]-81.0*(x[1]+0.1)*(x[1]+0.1)+Math.sin(x[2])+1.06; ff[2]=Math.exp(-x[0]*x[1])+20.0*x[2]+(10.0*Math.PI-3.0)/3.0; return ff; } } public class NA120 { public static void main (String args[]) { String s="leave a space between initial root guess values: "; double [] x0=NA43D.inputdata(s); fa ff=new fa(); f2 f=new f2(ff); double [] r1= NA43D.davidon_fletcher_powell(f,x0); s=" solution set : \n"+Matrix.toStringT(r1); String s2="Davidon-Fletcher-Powell optimization method to solve system of non-linear equation:"; JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

5.26 PROBLEMS PROBLEM 1 In a distillation tank operates with saturated steam benzen and toluen is seperated. In oreder to ontain purest toluen in liquid phase (maximize the toluen in the mixture) what would be the ideal teperature? x tolenPdoyma_tolen + x benzenPdoyma_benzen = P = 760 mmHg log10(Pdoyma_ benzen) =6.905 1211/(T+221) log10(Pdoyma_tolen) =6.953 1344/(T+219) temperature degree C, pressure mmHg

PROBLEM 2 Two dimensional distribution of pollution in a channel can be given by the equation C(x,y)=7.9+0.13x+0.21y-0.05x2-0.016y2-0.007xy Pollution is didtributed in a region -10 <= x <= 10 and 0 <= y <= 20 , find the pace where the pollution is maximum

PROBLEM 3 A petrochemical company developing a new fuel additive for commercial airplanes. Additive is consist of three components X,Y and Z. Fort he best performance total additive in the fuel should be at least 6 mL/L. Due o security reasons the total of most flammable X and Y components should be less than 3 mL/L. Componnt X should always be equal or more than the component Y, and componet Z should be more than half of the component Y. The cost of X,Y and Z components are 0.15,0.025 ve 0.05 TL respectively. Find the mixing component tha will give the minimum cost. PROBLEM 4 A food company is trying to design a new container box for preserved food that will minimise the cost of material. Total 320 cm3 volume is requested. Estetic concers limits diameter of the box in between 3 to 10 cm. and the height in between 2 to 10 cm. Box will be made of steel sheet of 0.4 mm thich with density of 7800 kg/m3. The cost of box material is 37 TL. Assume that box including top and bottom covers are made of the same material, find the box size that will be given the minimum cost. And the cost per box. PROBLEM 5 Air enters at 1 bar 300K to a 3 stage compressor system. After each stage air is cooled down again to 300 K before entering to the next stage. Calculate internal pressure for each stage to minimize work input to the compressors. The work requirement for eah stage of the compressor: W=Cp(Ti Te)=CpTi(1-Te/Ti)=kRTi/(k-1)*[1-(Pe/Pi)(k-1)/k] Total work fort he thre stages. W=kRTi/(k-1)*[1-(P2/P1)(k-1)/k]+ kRTi/(k-1)*[1-(P3/P2)(k-1)/k]+ kRTi/(k-1)*[1-(P4/P3)(k-1)/k] W= kRTi/(k-1)*[3-(P2/P1)(k-1)/k-(P3/P2)(k-1)/k-(P4/P3)(k-1)/k] (P2/P1) *(P3/P2)*(P4/P3)= (P4/P1)=27 k=1.4 Cp=1.005 KJ/kg K

Work written as java function : class f1 extends f_xj { public double func(double x[]) { double ff=1.4*8.3145*300/(1.4-1)*(1-x[0]*(1.4-1)/1.4)+ 1.4*8.3145*300/(1.4-1)*(1-(x[1]/x[0])*(1.4-1)/1.4) +1.4*8.3145*300/(1.4-1)*(1-(27/x[1])*(1.4-1)/1.4); return ff; //minimum deil maxsimum istendiinden - ile arptk }} answer : 3 bar and 9 bar PROBLEM 6

By using the data below find the aximum point. You can use an interpolation polynomial then mximise it. X Y 0 2 1 6 2 8 3 12 4 14 5 13 6 11 7 9 8 13 9 14.5 10 7 PROBLEM 7 Function below is given, find the maximum. -1<x [0]<1 -1<x [1]<1 double func(double x[]) { double ff= 34+x[0]*(Math.sin(3.0*Math.PI*(x[0]-x[1]))+x[1]*Math.sin(20.0*Math.PI*x[1])); return ff; }

PROBLEM 8 An antibiotic producing microorganism has a specific growth rate(g) is function of food concentration (c) If g = 2*c/(4+0.8c+c2+0.2c3) Find the c that will give te maximum growth rate. In low food concentrations growth rate drops to zero, similarly in very high food concentrations growth rate will drop to zero due to food poisining effect. Because of this reason, values bigger than c = 10 mg/L should not be investigated. PROBLEM 9 Find minimum of function f(x)=-(1.0/((x-0.3)*(x-0.3)+0.01)+1.0/((x-0.9)*(x-0.9)+0.04))

PROBLEM 10

Function

z ( x, y ) =

sin( x 2 + y 2 + ) x2 + y2 +

20 x 20 ve 20 y 20
is given. Find the maximum of the function for =1e-2 starting from P(18,18)

PROBLEM 11 Function

z ( x, y ) = x 2 y e ( x
With boundaries 4 x 4 ve

y2 )

4 y 4
is given. Find the minimum of the function starting from P(3,3)

PROBLEM 12

In order to construct water channel in a house , a flat sheet will be banded as shown in the figure. Water carrying capacity of the channel can be written as V(d,) = Z(d2sin cos + (L-2d)dsin) In this equation z is the length of the sheet (sheet area is zL). Find the d and to maximize water holding capacity of the channel. PROBLEM 13 Function below is given f(x) = 2500x6-15000x5+14875x4+40500x3-97124x2+73248x-19008 find the minimum between x0=0.8, x1=1.2 values.

PROBLEM 14 Cities, A,B,C, and D located at corrdinates(0,0),(1,5),(5,2) and (7,3) respectively. The roads from all cities are merged at point P. Find point P that makes the total distance of the roads minimum. PROBLEM 15 Function below is given f(x0, x1, x2, x3) = (x0+10 x1)2 +5(x2-10 x3)2+(x1-2 x2)4+10(x1- x4)4 find the minimum starting from P(1,-1,0,1) PROBLEM 16 Function below is given f(x0, x1) = 3(x0 - 1)2 +2(x1-1) (x0-1)(x1-2)+(x1- 2)4 Minimum of the function is given at point (1,2). Located the minimum at 3D plot, then by using any 3D nonlinear optimization method find the minimum points starting at P (0,0) PROBLEM 17 Find theminimum of function

f ( x) =

15 x (4 x 3x + 4)
2

fin the range of 0 to10.

PROBLEM 18

One of the very basic optimization problem is the minimum cost of container problem. The cost of a box usually is a function of the surface area. Therefore we should minimize the area for a given volume For example if the volume of the container V=0.5 liter=0.5x10-3 m3 : Volume V =

D 2
4

h or from this equation h, height is obtained as h =

4V

D 2

Surface area of the cylinder : A = 2

D 2
4

+ Dh =

D 2
2

4V . D

Analytical solution of the minimization problem

dA 4V = D 2 = 0 dD D D=3 4V

. From here solution is D = 3

4 * 0.5 x10 3

=0.086025401 m and .

h=

4 * 0.5 x10 3 = 0.086025401 m. D 2

Now obtain this mresult by using nuerical optimization methods. For the range of 0.01<=D<=0.2 a) Graphic method b) Golden ratio (Fibonnachi ) c) Newton-Raphson root finding d) Bisection method e) Newton-Raphson method f) Secant method g) Brent method h) Second degree polynomials i) Third degree polynomials Note: Some of these methods are defined only as root finding method, is not defined as optimization methods. PROBLEM 19

An isolation covered a wire carrying electrical current. The outlet diameter of electrical insulation is r. The electrical resistance on the wire caused heat. Thi heat crried through electrical insulation to the air around the wire with a temperature of T . The temperature of the wire can be calculated as

T=

q ln(r / a) 1 + + T In this equation 2 k hr

q = heat generation in the wire = 50 W/m a = diameter of the wire = 5 mm = 5x10-3 m k = coeeficient of thermal conductivity of insulation = 0.2 W/(mK) h=thermal convetivity coefficient for air = 30 W/(m2K) T = environmental temperature = 300 K. Find radius r that wil make temperature T minimum. PROBLEM 20

In order to make a cartoon box, the shape above should be used.After bending from the dotted linet he volume of the box will be 1 m3. In order to spent the minimum amount of the cartoon, what a and b dimensions should be? PROBLEM 21

A cylindrical container has a mass of M and has an empty center of gravity of C=0.43H. If the height of water in the cup is x, In order to have the common center of gravity of water and the cup as low as possible, what sholud be the value of x ?

PROBLEM 22

Two cylindrical beams with two different diameters is placed as in the figure. It is desired beam to have the minimum cross sectional area. Operational condions are: 1 180 MPa, 2 180 MPa, and bending 25 mm dir

8PL =maximum stress in the left beam r13 8 PL 2 = = maximum stress in the right beam r23

1 =

4 PL3 7 1 4 + 4 =bending at the end of the beam r 3 E 1 r2

ve E =Young modulus=200 GPa. Calculate r1 and r2. PROBLEM 23

It is desired to have volume of the cone base shape shown in the figure as 1 m3. Calculate r,h and b dimensions to minimise the surface area.

b V = r2 + h 3 S = r 2h + b 2 + r 2

)
r

PROBLEM 24 Lennard-Jones potential in between two molecules can be expressed as

12 6 V = 4 r r

In this equation and values are constants. Find

values that makes. V minimum

PROBLEM 25 Wave function for hydrogen atoms

= C (27 18 + 2 2 )e / 3 = zr / a0
1 C= 81 3 z a 0
2/3

In this equation

z = nucleus electrical load a0 = Bohr diameter r =radial distance. Find value that make wave function minimum. PROBLEM 26 Find optimum of function

f = e x1 x1 x2 + x22 g = 2 x1 + x2 2 0

h = x12 + x22 4 = 0
With -4<=x<=3 and -4<=y<=4 boundaries.

PROBLEM 27 Find the minimum of

f = ( x 2) 2 + ( x 2 y 2 ) 2 .

PROBLEM 28 Find the minimum of

f = 25 x 2 12 x 4 + 6 xy + 25 y 2 24 x 2 y 2 12 y 4 .

6. CURVE FITTING Data is obtained by measurement in the scientific world. Scientits are generalized the measurement results by using various formulas and theories. Creating functions by using experimental numerical data is called curve fitting. Formulas are generally formed from the theories of physical laws or freely selected by the researchers. In the fitted equation usually a good fit to the experimental data is a primary requirement. Fitting means assigning values to unknown coefficients in the equations. The coefficients can be lineerly or non-lineerly dependent the the variables of the function(s). Interpolation is also similar process, but in the interpolation a function is not always required. Interpolation could be just a repeatetive calculation process by following a certain rule. Different curve fitting methods will be investigated in this chapter. 6.1 LINEAR CURVE FITTING : LEAST SQUARE METHODS WITH ONE VARIABLE Least square methods are one of the most used curve fitting methods. Polinomial equation version is widely used. Assuming having a linear function f(aj,x) where aj are m linear coefficient and x are independent variable and j (x) are m sub-funtions linearly multiplied with the coefficients

f (a j , x) = a (jm ) j ( x)
j =0

It is desired to fit data xi, fi, i=0..n into the equation so that the difference between data and fitted function dependent values for all points will be minimum. In order to establish this, the following function H will be minimized with respect to aj

H (a ,...., a

(m) 0

(m) m

m ) = w( xi ) f i a (j m ) j ( xi ) i =1 j =0 n

Where w(xi) values in the equation are called weight function. In order to minimiz the function root of the derivative of the function can be calculated.
( n m H (a0( m ) ,...., a mm ) ) = 2 w( xi ) f i a (j m ) j ( xi ) k ( xi ) = 0 (m) i =1 j =0 a k

k = 0,....., m

m w( x ) ( x ) ( x ) a ( m ) = n w( x ) ( x ) f 0 i j i k i j i k i i i =1 j=

[ ]

k = 0,....., m

For weight function to be taken equal to unity , w( xi ) =1,equation in the open form can be written in the
following form.

n 2 0 ( xi ) i =1 n 0 ( xi )1 ( xi ) i =1 n 0 ( xi )2 ( xi ) i =1 .... n 0 ( xi )m ( xi ) i =1

( x ) ( x ) ( x ) ( x )
i =1 0 i 1 i i =1 n 0 i 2 i

.... .... .... ....

i =1 n i =1 1

( xi )
2 i

( x ) ( x )
i =1 1 i 2 i

( x ) ( x )
i

i =1 n 2

2 2

( xi )

....

....
m

( x )
i =1 1 i

( xi )

( x )
i =1 i

( xi )

....

n ( xi ) 0 ( xi ) f ( xi ) i =1 i =1 n a0 n 1 ( xi )m ( xi ) a 1 ( xi ) f ( xi ) i =1 i =1 1 n a2 = n 2 ( xi )m ( xi ) .... 2 ( xi ) f ( xi ) i =1 i =1 .... .... am n n 2 (x ) f (x ) m ( xi ) i m i i =1 i =1

( x )
0 i

This equation is an m+1 linear system of equation. It can easily be solved by using a system of equation solving method. Aa a special form of the above equation a form of a polinomial can be
j assumed. If j ( x) = x , equation becomes

f ( x) = a (j m ) x j
j =0

In this case the minimisation equation becomes


( n m H (a0( m ) ,...., amm ) ) = 2 f i a (j m ) x ij ( m) i =1 j =0 ak

x k = 0 i

k = 0,....., m

n m n (m) k j+k a j xi = fi xi i =1 i =1 k =0

k = 0,....., m

This equation can be written in the open format as:

n n x i i =1 n x3 i =1 i .... n m x i i =1

xi xi
i =1 n i =1 i =1 n 2

xi
i =1 n i =1 n

xi xi
i =1

xi .... xi
i =1 n

....
m+ 2

m +1

xi
i =1

n n m x i f i i =1 a0 i =1 n n x f m +1 .... x i a1 i i i =1 i =1 n n 2 m + 2 a = 2 .... x i xi fi i =1 i =1 .... .... .... .... n m n 2m a x i m x i f i i =1 i =1

....

Program 6.1-1 Polynomial least square curve fitting


mport java.io.*; import javax.swing.*; class NA44 { // Polynomial least square public static double[] gausswithpartialpivot(double a[][],double b[]) { //Gauss elimination with partial pivoting int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk;

double dummy=0; //gauss elimination int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //partial pivoting p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } // for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //backward substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static double[] PolynomialLSQ(double xi[],double yi[],int n) { //Polynomial least square int l=xi.length; int i,j,k; int np1=n+1; double A[][]; A=new double[np1][np1]; double B[]; B=new double[np1]; double X[]; X=new double[np1]; for(i=0;i<n+1;i++) { for(j=0;j<n+1;j++) {if(i==0 && j==0) A[i][j]=l; else for(k=0;k<l;k++) A[i][j] += Math.pow(xi[k],(i+j)); }

for(k=0;k<l;k++) { if(i==0) B[i]+= yi[k]; else B[i] += Math.pow(xi[k],i)*yi[k];} } X=gausswithpartialpivot(A,B); double max=0; for(i=0;i<n+1;i++) if(Math.abs(X[i]) > max) max = Math.abs(X[i]); for(i=0;i<n+1;i++) if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0; return X; } public static double funcPolynomialLSQ(double e[],double x) { // this function calculates the value of // least square curve fitting function int n=e.length; double ff; if(n!=0.0) { ff=e[n-1]; for(int i=n-2;i>=0;i--) { ff=ff*x+e[i]; } } else ff=0; return ff; } public static double error(double x[],double y[],double e[]) { //calculates absolute square root error of a least square approach double n=x.length; int k; double total=0; for(k=0;k<n;k++) { total+=(y[k]-funcPolynomialLSQ(e,x[k]))*(y[k]-funcPolynomialLSQ(e,x[k])); } total=Math.sqrt(total); return total; } public static double[][] funcPolynomialLSQ(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double E[]=PolynomialLSQ(xi,yi,polinomkatsayisi); System.out.println("katsaylar :\n"+Matrix.toStringT(E)); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) {z[0][k]=xi[i];z[1][k]=funcPolynomialLSQ(E,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0);

z[0][k]=z[0][k-1]+dx;z[1][k]=funcPolynomialLSQ(E,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcPolynomialLSQ(E,z[0][k]); return z; }

public static void main(String args[]) throws IOException { double x[]; double y[]; String s1=JOptionPane.showInputDialog("Data file name (input1.txt)"); // JFileChooser fc=new JFileChooser(); // if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); } double a[][]=Text.readDoubleT(s1); x=a[0]; y=a[1]; double z[][]=funcPolynomialLSQ(x,y,4,2); System.out.println("Polynomial Least Square Curve fitting\n"+Matrix.toStringT(z)); Plot pp=new Plot(a[0],a[1]); pp.setPlabel("Polynomial Least Square Curve fitting"); pp.setXlabel("x"); pp.setYlabel("y=f(x)"); pp.setPlotType(0,0); pp.addData(z[0],z[1]); pp.setPlotType(6,22); pp.setGrid(1,1); pp.plot(); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA44 katsaylar : 0.000000000000000 -0.000000000000000 1.000000000000000 -0.000000000000000 -0.000000000000000 Polynomial Least Square Curve fitting 1.0000000000 1.0000000000 1.3333333333 1.7777777778 1.6666666667 2.7777777778 2.0000000000 4.0000000000 2.3333333333 5.4444444444 2.6666666667 7.1111111111 3.0000000000 9.0000000000 3.3333333333 11.1111111111 3.6666666667 13.4444444444 4.0000000000 16.0000000000 4.3333333333 18.7777777778 4.6666666667 21.7777777778 5.0000000000 25.0000000000 5.3333333333 28.4444444444 5.6666666667 32.1111111111

6.0000000000 6.3333333333 6.6666666667 7.0000000000 7.3333333333 7.6666666667 8.0000000000

36.0000000000 40.1111111111 44.4444444444 49.0000000000 53.7777777778 58.7777777778 64.0000000000

A general function version of the same program will look like : Program 6.1-2 General least square curve fitting
import java.io.*; import java.util.*; import javax.swing.*; //prototype function abstract class f_xr { abstract double func(double x,int equation_ref); } class fa extends f_xr { double func(double T,int i) { double xx=1.0; if(i==0) xx=1.0; else if(i==1) xx=1.0/T; else if(i==2) xx=1.0/(T*T); else if(i==3) xx=1.0/(T*T*T); else if(i==4) xx=T; else if(i==5) xx=T*T; else if(i==6) xx=T*T*T; return xx; }

} class NA45 { //general least square curve fitting public static double[] gausswithpartialpivot(double a[][],double b[]) { //Gauss elimination with partial pivoting int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss elimination int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //partial pivoting p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } // for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //backward substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; }

public static double[] GeneralLeastSquare(double xi[],double yi[],int n) { fa f=new fa();

int l=xi.length; int i,j,k; int np1=n+1; double A[][]; A=new double[np1][np1]; double B[]; B=new double[np1]; double X[]; X=new double[np1]; for(i=0;i<n+1;i++) { B[i]=0; for(j=0;j<np1;j++) { A[i][j]=0.0; for(k=0;k<l;k++) A[i][j]+=f.func(xi[k],i)*f.func(xi[k],j); } for(k=0;k<l;k++) B[i]+= f.func(xi[k],i)*yi[k]; } //System.out.println("A = \n"+Matrix.toString(A)); //System.out.println("B = \n"+Matrix.toStringT(B)); X=gausswithpartialpivot(A,B); //X=B/A; double max=0; for(i=0;i<n+1;i++) if(Math.abs(X[i]) > max) max = Math.abs(X[i]); for(i=0;i<n+1;i++) if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0; Text.printT(X); return X; } public static double funcGeneralLeastSquare(double e[],double x) { // this function calculates the value of // least square curve fitting function fa f=new fa(); int n=e.length; double ff=0; if(n!=0.0) { for(int i=n-1;i>=0;i--) {ff+=e[i]*f.func(x,i);} } return ff; } public static double hata(double x[],double y[],double e[]) { //calculates absolute square root error of a least square approach double n=x.length; int k; double total=0; for(k=0;k<n;k++) { total+=(y[k]-funcGeneralLeastSquare(e,x[k]))*(y[k]-funcGeneralLeastSquare(e,x[k])); } total=Math.sqrt(total); return total;

} public static double[][] funcGeneralLeastSquare(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double E[]=GeneralLeastSquare(xi,yi,polinomkatsayisi); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) {z[0][k]=xi[i];z[1][k]=funcGeneralLeastSquare(E,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcGeneralLeastSquare(E,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcGeneralLeastSquare(E,z[0][k]); return z; } public static void main(String args[]) throws IOException { String s1=JOptionPane.showInputDialog("file name : "); double c[][]=Text.readDoubleT(s1); double b[]=GeneralLeastSquare(c[0],c[1],6); System.out.println("General Least Square Coefficient : \n"+Matrix.toStringT(b)); double z[][]=funcGeneralLeastSquare(c[0],c[1],6,2); System.out.println("General Least Square Solution \n"+Matrix.toStringT(z)); Plot pp=new Plot(c[0],c[1]); pp.setPlabel("General Least Square"); pp.setXlabel("x"); pp.setYlabel("y=f(x)"); pp.addData(z[0],z[1]); pp.plot(); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA45 General Least Square Coefficient : 0.000000000000000 0.000000000000000 0.000000000000000 -0.000000000000000 -0.000000000000000

1.000000000000000 -0.000000000000000 General Least Square Solution 1.0000000000 1.0000000000 1.3333333333 1.7777777778 1.6666666667 2.7777777778 2.0000000000 4.0000000000 2.3333333333 5.4444444444 2.6666666667 7.1111111111 3.0000000000 9.0000000000 3.3333333333 11.1111111111 3.6666666667 13.4444444444 4.0000000000 16.0000000000 4.3333333333 18.7777777778 4.6666666667 21.7777777778 5.0000000000 25.0000000000 5.3333333333 28.4444444444 5.6666666667 32.1111111111 6.0000000000 36.0000000000 6.3333333333 40.1111111111 6.6666666667 44.4444444444 7.0000000000 49.0000000000 7.3333333333 53.7777777778 7.6666666667 58.7777777778 8.0000000000 64.0000000000

6.2 ORTOGONAL POLYNOMIAL LEAST SQUARE In the previous subsection , polynomial least square method is investigated. In order to find coefficient of polynomial least square a matrix solution process should be carried out. Furthermore, the equation tends to create ill matrices, so that chance of having an error in the coefficient while solving the matrix is high. Orthogonal polynomials has the following usefull property

pi ( x) * p j ( x) = 0 if i i j pi ( x) * p j ( x) = A if i i = j
Now Assume to have a jth order orthogonal polynomial p j (x) . And assume that least square polynomial eqaution is
y m ( x) = b (j m ) p j ( x) If xi, fi i=1,,n is given as a data to be curve fit, The minimization function can be
j =0 m

defined as
( H (b0( m ) ,...., bmm ) ) = w( xi )[ f i y m ( xi )] n i =1 2

In order to minimize the function, derivative can be set as equal to zero


( n m H (b0( m ) ,...., bmm ) ) = 2 w( xi ) f i b (j m ) p j ( xi ) p k ( xi ) = 0 (m) i =1 j =0 bk

k = 0,....., m

This equation came to the following matrix form

m n w( x ) p ( x ) p ( x ) b ( m ) = n w( x ) p ( x ) f 0 i j i k i j i k i i i =1 j = i =1

[ ]

k = 0,....., m

Or for a shorter notation new variables vcan be defined as:


d jk = w( xi ) p j ( xi ) p k ( xi )
i =0

k = 0,....., m

jk [b (j m ) ] = w( xi ) p k ( xi ) f i

i =1

k = 0,....., m

Now the equation becomes:


( m) d jk b j = k j =0 m

[ ]

k = 0,....., m .

Writing the equation this way does not give me an advantage, But considering that p k ( xi ) is an orthogonal function, due to the propertiy of the orthogonal functions If j k
w( xi ) p j ( xi ) p k ( xi ) = 0 In this case the equation that formed a matrix converted to a simple
n

i =0

multiplication equation
d kk b (j m ) = k

[ ]

k = 0,....., m

To solve bj(m)

[b ] =
(m) j

k
d kk

k = 0,....., m

Several different orthogonal polynomials are available. The orthogonal polynomial that we will utilise in our program has the following form. This equation is taken from A First Course in Numerical Analysis, Anthony Ralston,Philip Rabinowitz, Mc Graw Hill ISBN 0-07-051158-6 pj+1(x) = (x - j+1)pj(x) - jpj-1(x) j = 0,1,. p0(x) = 1 p-1(x) = 0

j+1 and j are polynomial constants to be calculated.


k =
w( xi ) xi p k 1 ( xi ) p k ( xi )
i =1 n

w( xi ) xi [ p k 1 ( xi )]
i =1

w( xi )[ p k 1 ( xi )]
i =1

i =1 n

w( xi )[ p k ( xi )]

k =

w( xi ) xi [ p k ( xi )]
i =1 n

w( xi )[ p k 1 ( xi )]
i =1

Least square algorithm can be given as follows:

y m ( x) = b j p j ( x)
i =1

bj =

j j
n

j = wi f i p j ( xi )
i =1

j = wi [ p j ( xi )]2
i =1

Because Orthogonal polynomial equations are not solving any system of equations, roundoff errors due to matrix solutions will not be existed. But the polynomial equation used has a more complex form compare to polynomial form. Program 6.2-1 Orthogonal polynomial least square, Ralston & Rabinowitz equation
import java.io.*; class NA121 { public static double[][] OPEKK(double xi[],double fi[],int m) //ortogonal polinomial least square method //Referance : A First Course in Numerical Analysis // Anthony Ralston,Philip Rabinowitz, Mc Graw Hill ISBN 0-07-051158-6 // m degree of polynomial xi fi input data { int i,j,k; int n=xi.length; int mp2=n+2; int mp1=n+1; double p[][]=new double[mp2][n]; double gamma[]=new double[mp1]; double beta[]=new double[mp1]; double omega[]=new double[mp1]; double alpha[]=new double[mp1]; double b[]=new double[mp1]; double wi[]=new double[n]; double a[][]=new double[3][mp1]; for(i=0;i<n;i++) { p[1][i]=1.0; p[0][i]=0.0; wi[i]=1.0; } gamma[0]=0; for(i=0;i<n;i++) { gamma[0]+=wi[i]; } beta[0]=0.0; for(j=0;j<m+1;j++)

{ omega[j]=0; for(i=0;i<n;i++) {omega[j]+=wi[i]*fi[i]*p[j+1][i];} b[j]=omega[j]/gamma[j]; if( j != m) { alpha[j+1]=0; for(i=0;i<n;i++) { alpha[j+1]+=wi[i]*xi[i]*p[j+1][i]*p[j+1][i]/gamma[j];} for(i=0;i<n;i++) {p[j+2][i]=(xi[i]-alpha[j+1])*p[j+1][i]-beta[j]*p[j][i];} gamma[j+1]=0; for(i=0;i<n;i++) {gamma[j+1]+=wi[i]*p[j+2][i]*p[j+2][i];} beta[j+1]=gamma[j+1]/gamma[j]; } }//end of j for(j=0;j<m+1;j++) { a[0][j]=b[j]; a[1][j]=alpha[j]; a[2][j]=beta[j]; } return a; } public static double funcOPEKK(double a[][],double x) { // polinom deerleri hesaplama fonksiyonu double yy=0; int k; int m=a[0].length-1; int mp2=m+2; double q[]; q=new double[mp2]; //vector<double> q(m+2,0.0); for(k=m-1;k>=0;k--) { q[k]=a[0][k]+(x-a[1][k+1])*q[k+1]-a[2][k+1]*q[k+2]; yy=q[k]; } return yy; } public static double[][] funcOPEKK(double xi[],double yi[],int polynomialcoefficient,int numberofmidpoints) { //number of midpoints: x--o--o--x--o--o--x x data points midpoints are 2 int n=xi.length; int nn=(n-1)*(numberofmidpoints+1)+1; double z[][]=new double[2][nn]; double E[][]=OPEKK(xi,yi,polynomialcoefficient); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) {z[0][k]=xi[i];z[1][k]=funcOPEKK(E,z[0][k]);k++; for(int j=0;j<numberofmidpoints;j++)

{dx=(xi[i+1]-xi[i])/((double)numberofmidpoints+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcOPEKK(E,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcOPEKK(E,z[0][k]); return z; } public static void main(String args[]) throws IOException { double x[]={0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}; double y[]={0.0,0.099833,0.1986693,0.2955202,0.38941834,0.479425,0.5646424,0.6442176,0.717356,0.783326,0.84147}; double z3[][]=funcOPEKK(x,y,3,8); Text.print(Text.T(z3),"Orthogonal polynomial least square "); Plot pp=new Plot(x,y); pp.setPlabel("Orthogonal polynomial least square "); pp.setPlotType(0,20); pp.addData(z3[0],z3[1]); pp.plot(); } }

x 0 0.011111111 0.022222222 0.033333333 0.044444444 0.055555556 0.066666667 0.077777778 0.088888889 0.1 0.111111111 0.122222222 0.133333333 0.144444444 0.155555556 0.166666667 0.177777778 0.188888889 0.2 0.211111111 0.222222222 0.233333333 0.244444444 0.255555556 0.266666667 0.277777778 0.288888889 0.3 0.311111111 0.322222222 0.333333333 0.344444444 0.355555556 0.366666667 0.377777778 0.388888889 0.4 0.411111111 0.422222222 0.433333333 0.444444444

y -1.33E-04 0.011022274 0.022172031 0.033314774 0.044449318 0.055574478 0.066689069 0.077791906 0.088881804 0.099957578 0.111018044 0.122062016 0.13308831 0.14409574 0.155083123 0.166049271 0.176993002 0.18791313 0.19880847 0.209677837 0.220520046 0.231333912 0.242118251 0.252871877 0.263593606 0.274282253 0.284936632 0.295555559 0.306137849 0.316682317 0.327187778 0.337653048 0.34807694 0.358458271 0.368795855 0.379088508 0.389335044 0.399534279 0.409685028 0.419786106 0.429836327

0.455555556 0.466666667 0.477777778 0.488888889 0.5 0.511111111 0.522222222 0.533333333 0.544444444 0.555555556 0.566666667 0.577777778 0.588888889 0.6 0.611111111 0.622222222 0.633333333 0.644444444 0.655555556 0.666666667 0.677777778 0.688888889 0.7 0.711111111 0.722222222 0.733333333 0.744444444 0.755555556 0.766666667 0.777777778 0.788888889 0.8 0.811111111 0.822222222 0.833333333 0.844444444 0.855555556 0.866666667 0.877777778 0.888888889 0.9 0.911111111 0.922222222 0.933333333 0.944444444 0.955555556 0.966666667 0.977777778 0.988888889 1

0.439834508 0.449779463 0.459670007 0.469504955 0.479283123 0.489003325 0.498664376 0.508265092 0.517804288 0.527280779 0.53669338 0.546040905 0.555322171 0.564535991 0.573681182 0.582756559 0.591760935 0.600693128 0.60955195 0.618336219 0.627044748 0.635676353 0.644229849 0.652704051 0.661097774 0.669409833 0.677639043 0.68578422 0.693844178 0.701817733 0.709703699 0.717500892 0.725208127 0.732824219 0.740347982 0.747778233 0.755113785 0.762353455 0.769496058 0.776540407 0.783485319 0.790329609 0.797072091 0.803711581 0.810246894 0.816676844 0.823000248 0.82921592 0.835322674 0.841319327

A second example to orthogonal polynomial curve fitting will be Chebchev polynomial curve fitting. Chebchev polynomials defined at (-1, 1) region.General definition of Chebchev polynomials Tn(x)=cos(n arccos(x)) If this equation is written in open form T0(x)=1 T1(x)=x T2(x)=2x2-x .. Tn+1(x)=2x Tn(x)- Tn-1(x) It is clear that it is a serial equation. The weight function for the Chebchev polynomials

w( x) =

1 1 x2

The root of Chebchev polynomials

t k = cos(

2k + 1 ) k=0,..n. n in this equation indicates the degree of polynomial. In order to 2n + 2

curve fit data y using this set as an orthogonal polynomial data should be given at the root points. Furthermore due to regional limit of the data, to apply this equation to real data a convertion process is required. If the real data is in region (a,b) The convertion equations will be

ba a+b xk = t k + 2 2 x a t k = 2 k 1 b 1
If xk and corresponding yk ,is given

Coefficient of polynomial Pn ( x) =

C T ( x) can be calculated by using least sqaure method. Due to


k =0 k k

orthogonality of the polynomials, matrix solution is not required, coefficients is found directly.

C0 = Cj =

1 n ykT0 (tk ) n + 1 k =0 2 n ykT j (tk ) n + 1 k =0


j=1,2,n

As an exmple problem f(x)=ex function in the limit 0 to 2 will be investigated. If Chebchev polynomials are to be used in curve fitting, experiments should be design so that the data should be taken exactly in the roots of the Chebchev polynomials.
xk 0.010178558 0.090368005 0.244250426 0.459359183 0.718267443 1 1.281732557 1.540640817 1.755749574 1.909631995 1.989821442 yk 1.010231 1.094577 1.276664 1.583059 2.050877 2.718282 3.602877 4.66758 5.787784 6.750604 7.314228

Program 6.2-2 Orthogonal polynomial least square, Chebychev equation


import java.io.*; //Chevychev orthogobnal polynomial least square class NA122 { public static double Ti(double x,int equation_ref) {//Chebysev function double T[]=new double[equation_ref+1]; T[0]=1.0; if(equation_ref>=1) T[1]=x; for(int i=2;i<=equation_ref;i++) {T[i]=2.0*x*T[i-1]-T[i-2]; } return T[equation_ref]; } public static double[] xi(double a,double b,int N) { int nn=N+1; double x[]=new double[nn]; double t[]=new double[nn]; for(int k=0;k<nn;k++) {t[k]=Math.cos((2.0*N+1-2.0*k)*Math.PI/(2.0*N+2.0)); x[k]=(b-a)/2.0*t[k]+(a+b)/2.0; } return x; } public static double[] Chebyshev(double a,double b,double y[],int N)

{ //region a b int nn=N+1; System.out.println("N="+N); int k,j; double d=Math.PI/(2.0*N+2); double x[]=new double[nn]; double t[]=new double[nn]; // -1<=x<=1 region convertion for(k=0;k<nn;k++) {t[k]=Math.cos((2.0*N+1-2.0*k)*Math.PI/(2.0*N+2.0)); x[k]=(b-a)/2.0*t[k]+(a+b)/2.0; } double C[]=new double[nn]; double z=0; for(k=0;k<nn;k++) { z+=y[k];} C[0]=z/(N+1); for(j=1;j<=N;j++) {z=0; for(k=0;k<=N;k++) { z+=y[k]*Ti(t[k],j);} C[j]=2.0*z/(N+1); } return C; } public static double funcChebyshev(double C[],double a,double b,double x) { double t=2.0*(x-a)/(b-a)-1; int n=C.length; double ff=0; if(n!=0.0) { for(int i=0;i<n;i++) {ff+=C[i]*Ti(t,i);} } return ff; } public static double[][] funcChebyshev(double a,double b,double y[],int polinomkatsayisi,int verisayisi) { int n=verisayisi+1; double z[][]=new double[2][n]; double C[]=Chebyshev(a,b,y,polinomkatsayisi); double dx=(b-a)/(double)verisayisi; z[0][0]=a; for(int i=0;i<=verisayisi;i++) {if(i==0) z[0][i]=a; else z[0][i]=z[0][i-1]+dx; z[1][i]=funcChebyshev(C,a,b,z[0][i]); } return z; } public static void main(String args[]) throws IOException { double a=0; double b=2.0; double y[]={1.010230536,1.094577019,1.276664,1.583059208,2.05087687,2.718281828, 3.60287651,4.66758038,5.787784491,6.750604088,7.314227631}; double x[]= xi(a,b,10);

double z[][]=funcChebyshev(a,b,y,10,50); Text.print(Text.T(z),"Chebyschev orthogonal least square curve fitting"); Plot pp=new Plot(x,y); pp.setPlabel("Chebyschev orthogonal least square curve fitting"); pp.setPlotType(0,20); pp.addData(z[0],z[1]); pp.plot(); } }

x 0 0.04 0.08 0.12 0.16 0.2 0.24 0.28 0.32 0.36 0.4 0.44 0.48 0.52 0.56 0.6 0.64 0.68 0.72 0.76 0.8 0.84 0.88 0.92 0.96 1 1.04 1.08 1.12 1.16 1.2 1.24 1.28 1.32 1.36 1.4 1.44 1.48 1.52 1.56 1.6 1.64 1.68 1.72 1.76 1.8 1.84 1.88 1.92 1.96 2

y 1 1.040810774 1.083287068 1.127496852 1.173510871 1.221402758 1.27124915 1.323129812 1.377127764 1.433329415 1.491824698 1.552707219 1.616074402 1.68202765 1.750672501 1.822118801 1.896480879 1.973877732 2.054433211 2.13827622 2.225540928 2.316366976 2.410899706 2.509290389 2.611696473 2.718281828 2.829217014 2.944679551 3.064854203 3.189933276 3.320116923 3.455613465 3.596639726 3.743421378 3.896193302 4.055199967 4.220695817 4.392945681 4.572225195 4.758821245 4.953032424 5.155169512 5.365555971 5.584528464 5.812437394 6.049647464 6.296538261 6.553504862 6.82095847 7.099327065 7.389056099

6.3 CURVE FITTING : LEAST SQUARE METHOD WITH MULTI VARIABLES


Curve fitting of a multivariable function basically the same of general curve fitting of the single variable function. The only basic difference is definition of the function to be used. Assume that
f ( x0 , x1 ,..., x n 1 , x n ) = a (j m ) j ( x0 , x1 ,..., x n 1 , x n ) j th degree function is given. It is desired to fit x0, x1, x2,, xn ,fi,
j =0 m

i=0...n data set into this function. The best fitted aj(m) values to be found. For this purpose Minimum value of
n m ( H (a0( m ) ,...., a mm ) ) = w( x0 , x1 ,..., x n 1 , x n ) f i a (j m ) j ( x0 , x1 ,..., x n 1 , x n ) function should be found. w i =1 j =0

w ( x0 , x1 ,..., xn1 , xn ) is called weight function and it should satisfy the condition w( x0 , x1 ,..., xn 1 , xn ) 0 i = 1,...., n olmaldr. The minimum of the function is the root of the
derivative of the function. ( n m H (a0( m ) ,...., a mm ) ) = 2 w( x0 , x1 ,..., x n 1 , x n ) f i a (j m ) j ( x0 , x1 ,..., x n 1 , x n ) k ( x0 , x1 ,..., x n 1 , x n ) = 0 ( m) i =1 j =0 a k
k = 0,....., m k = 0,.., m

m w( x , x ,..., x , x ) ( x , x ,..., x , x ) ( x , x ,..., x , x ) a ( m ) = n w( x , x ,..., x , x ) ( x , x ,..., x , x ) f 0 0 1 0 1 0 1 0 1 0 1 n 1 n j n 1 n k n 1 n j n 1 n k n 1 n i i =1 j=

[ ]

If unit value is taken as weight function, equation becomes:


m ( x , x ,..., x , x ) ( x , x ,..., x , x ) a ( m ) = n ( x , x ,..., x , x ) f 0 j 0 1 n 1 n k n 1 n j n 1 n i 0 1 k 0 1 i =1 j=

[ ]

k = 0,.., m

As an example a surface polinomial can be defined as f(x,y) = a0 + a1x + a2y + a3x2 + a4y2 + a5xy + a6x3 + a7y3 + a8x2y + a9xy2 + a10x4 + a11y4 + a12 x2y2 + a13x3y + a14xy2 This polynomial is defined in the example problem
Problem 6.3-1 Multidimensional general least square curve fitting
mport java.io.*; import java.util.*; import javax.swing.*; import java.awt.event.*; // surface curve fitting //f(x,y)=a0+a1*x+a2*y+a3*x^2+a4*y^2+a5*xy+a6*x^3+a7*y^3+a8*x^2y // +a9*xy^2+a10*x^4+a11*y^4+a12*x^2y^2+a13x^3y+a14*xy^3 abstract class f_xir { abstract double func(double x[],int equation_ref);

} class fa extends f_xir { double func(double x[],int i) {//a general surface curve fitting model double xx=0.0; //density function if(i==0) {xx=1.0; } else if (i==1) {xx=x[0]; } else if (i==2) {xx=x[1]; } else if (i==3) {xx=x[0]*x[1]; } else if (i==4) {xx=x[1]*x[1]; } else if (i==5) {xx=x[0]*x[1]; } else if (i==6) {xx=x[0]*x[0]*x[0];} else if (i==7) {xx=x[1]*x[1]*x[1];} else if (i==8) {xx=x[0]*x[0]*x[1];} else if (i==9) {xx=x[0]*x[1]*x[1];} else if (i==10){xx=x[0]*x[0]*x[0]*x[0];} else if (i==11){xx=x[1]*x[1]*x[1]*x[1];} else if (i==12){xx=x[0]*x[0]*x[1]*x[1];} else if (i==13){xx=x[0]*x[0]*x[0]*x[1];} else if (i==14){xx=x[0]*x[1]*x[1]*x[1];} return xx; } } class NA46 { // General least square curve fitting with multivariable public static double[][] Transpose(double [][] left) { //transpose matrix (if A=a(i,j) Transpose(A)=a(j,i) int i,j; int n=left.length; int m=left[0].length; double b[][]; b=new double[m][n]; for(i=0;i<n;i++) {for(j=0;j<m;j++) {b[j][i]=left[i][j];} } return b; } public static double[] gausswithpartialpivot(double a[][],double b[]) { //Gauss elimination with partial pivoting int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss elimination int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //partial pivoting p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;}

} if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } // for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //backward substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static double[] GeneralLeastSquare(double c[][],int n) { int n1=c.length; int n2=c[0].length-1; System.out.println("n1="+n1+"n2="+n2); double xi[][]=new double[n1][n2]; double yi[]=new double[n1]; for(int i=0;i<n1;i++){for(int j=0;j<n2;j++){xi[i][j]=c[i][j];};yi[i]=c[i][n2];} return GeneralLeastSquare(xi,yi,n); } public static double[] GeneralLeastSquare(double xi[][],double yi[],int n) { // n dimensional surface general least square curve fitting fa f=new fa(); int l=xi.length; int i,j,k; int np1=n+1; double A[][]; A=new double[np1][np1]; double B[]; B=new double[np1]; double X[]; X=new double[np1]; for(i=0;i<n+1;i++) { B[i]=0; for(j=0;j<np1;j++)

{ A[i][j]=0.0; for(k=0;k<l;k++) A[i][j]+=f.func(xi[k],i)*f.func(xi[k],j); } for(k=0;k<l;k++) B[i]+= f.func(xi[k],i)*yi[k]; } X=gausswithpartialpivot(A,B); //X=B/A; double max=0; for(i=0;i<n+1;i++) if(Math.abs(X[i]) > max) max = Math.abs(X[i]); for(i=0;i<n+1;i++) if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0; Text.printT(X); return X; } public static double funcGeneralLeastSquare(double e[],double x[]) { // multidimensional function calculation fa f=new fa(); int n=e.length; double ff=0; if(n!=0.0) { for(int i=n-1;i>=0;i--) {ff+=e[i]*f.func(x,i);} } return ff; } public static double[] funcGeneralLeastSquare(double e[],double xi[][]) { // multidimensional function calculation fa f=new fa(); int n=e.length; double ff[]=new double[xi.length]; for(int k=0;k<xi.length;k++) { if(n!=0.0) { for(int i=n-1;i>=0;i--) {ff[k]+=e[i]*f.func(xi[k],i);} } } return ff; } public static double[][] cikti(double c[][],int polinomkatsayisi,int aradegersayisi) { int n1=c.length; int n2=c[0].length-1; System.out.println("n1="+n1+"n2="+n2); double xi[][]=new double[n1][n2]; double yi[]=new double[n1]; for(int i=0;i<n1;i++){for(int j=0;j<n2;j++){xi[i][j]=c[i][j];};yi[i]=c[i][n2]; } return cikti(xi,yi,polinomkatsayisi,aradegersayisi); }

public static double[][] cikti(double xi[][],double yi[],int polinomkatsayisi,int aradegersayisi) { int n=xi.length; int nk=xi[0].length; int nn=(n-1)*(aradegersayisi+1)+1; double E[]=GeneralLeastSquare(xi,yi,polinomkatsayisi); double x[][]=new double[nn][nk]; double c[][]=new double[nn][nk+1]; double yy[]=new double[nn]; double dx[]=new double[nk]; int k=0; int i,j,w; for(i=0;i<(n-1);i++) { for(w=0;w<nk;w++){x[k][w]=xi[i][w];dx[w]=(xi[i+1][w]-xi[i][w])/((double)aradegersayisi+1.0);};k++; for(j=0;j<aradegersayisi;j++) { for(w=0;w<nk;w++){x[k][w]=x[k-1][w]+dx[w];};k++;} } for(w=0;w<nk;w++){x[k][w]=xi[i][w];} yy=funcGeneralLeastSquare(E,x); for(i=0;i<x.length;i++) {for(w=0;w<nk;w++) c[i][w]=x[i][w]; c[i][nk]=yy[i]; } return c; } public static double funcGeneralLeastSquare(double e[],double x,double y) { // multidimensiona surface fitting double xx[]=new double[2]; xx[0]=x; xx[1]=y; return funcGeneralLeastSquare(e,xx); } public static double hata(double c[][],double e[]) {int n1=c.length; int n2=c[0].length-1; System.out.println("n1="+n1+"n2="+n2); double xi[][]=new double[n1][n2]; double yi[]=new double[n1]; for(int i=0;i<n1;i++){for(int j=0;j<n2;j++){xi[i][j]=c[i][j];};yi[i]=c[i][n2]; } return hata(xi,yi,e); } public static double hata(double x[][],double y[],double e[]) { //calculates absolute square root error of a least square approach double n=x.length; int k; double total=0; for(k=0;k<n;k++) { total+=(y[k]-funcGeneralLeastSquare(e,x[k]))*(y[k]-funcGeneralLeastSquare(e,x[k])); }

total=Math.sqrt(total); return total; } public static void main(String[] args) { String s1=JOptionPane.showInputDialog("file name : "); double c[][]=Text.readDouble(s1); double b[]=GeneralLeastSquare(c,4); double xi[][]=cikti(c,4,5); System.out.println("output \n"+Matrix.toString(xi)); System.out.println("error \n"+Matrix.toString(hata(c,b))); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA46 n1=90n2=2 n1=90n2=2 output 0.000000000000000 0.000000000000000 0.000000000000000 0.066666666666667 0.000000000000000 0.133333333333333 0.000000000000000 0.200000000000000 0.000000000000000 0.266666666666667 0.000000000000000 0.333333333333333 0.000000000000000 0.400000000000000 0.000000000000000 0.466666666666667 0.000000000000000 0.533333333333333 0.000000000000000 0.600000000000000 0.000000000000000 0.666666666666667 0.000000000000000 0.733333333333333 0.000000000000000 0.800000000000000 0.000000000000000 0.866666666666667 0.000000000000000 0.933333333333333 0.000000000000000 1.000000000000000 0.000000000000000 1.066666666666667 0.000000000000000 1.133333333333333 0.000000000000000 1.200000000000000 0.000000000000000 1.266666666666667 0.000000000000000 1.333333333333333 0.000000000000000 1.400000000000000 0.000000000000000 1.466666666666667 0.000000000000000 1.533333333333333 0.000000000000000 1.600000000000000 3.200000000000000 3.133333333333334 3.200000000000000 3.200000000000000 3.200000000000000 3.266666666666667

-1.901333333332597 -2.774399999999312 -3.576355555554915 -4.307199999999405 -4.966933333332783 -5.555555555555047 -6.073066666666200 -6.519466666666240 -6.894755555555169 -7.198933333332985 -7.431999999999686 -7.593955555555279 -7.684799999999755 -7.704533333333122 -7.653155555555374 -7.530666666666516 -7.337066666666544 -7.072355555555461 -6.736533333333265 -6.329599999999955 -5.851555555555532 -5.302400000000000 -4.682133333333356 -3.990755555555593 -3.228266666666718 150.057244444444650 154.190400000000200 158.394666666666840

3.200000000000000 3.200000000000000 3.200000000000000 3.200000000000000 3.200000000000000 n1=90n2=2 error 419.074496785476100

3.333333333333333 3.400000000000000 3.466666666666666 3.533333333333332 3.600000000000000

162.670044444444670 167.016533333333570 171.434133333333620 175.922844444444730 180.482666666667100

6.4 INTERPOLATION Newton Interpolation Polynomial Basic difference of intrepolation compare to least square curve fitting is that interpolation is always passing through the data points. Newton interpolation Formula of degree n+1 is given as: fn(x)=b0+b1(x-x0)+.+bn(x-x0) (x-x1) (x-xn) b0=f(x0) b1=f[x1,x0] b2= f[x2,x1,x0] bn= f[xn,xn-1,.,x0] In this definition :

f[x i , x j ] =

f(x i ) f ( x j ) xi x j f[x i , x j ] f [ x j , xk ] xi xk f[x n , x n-1 ,...., x1 , x 0 ] f [ x n , x n-1 ,...., x1 , x 0 ] xn x0

f[x i , x j , x k ] =

Similarly nth degree divided difference Formula can be given as:

f[x n , x n-1 ,...., x1 , x 0 ] =

This equation called as Newtons interpolation polynomial. An example problem : Population of City A is given in the table below. Calculate population of the city for year 1940 1950 1960 1970 1980 1990 Population 132165 151336 179323 203302 226542 249633 Newtons divided difference table : Xi yi f[xi,xj] f[xi,xj,xk] f[xi,xj,xk,xl] f[xi,xj,xk,xl,xn] f[xi,xj,xk,xl,xn,xm] 1940 132165 1917.1 44.08 2.137333333 0.067054167 -0.001564333 1950 151336 2798.7 -20.04 0.544833333 -0.0111625 1960 179323 2397.9 -3.695 0.098333333 1970 203302 2324 -0.745 1980 226542 2309.1 1990 249633 Solution with increasing order can be given as:
132165+(1945-1940)*1917.1= 141750.5 141750.5+(1945-1940)* (1945-1950)* 44.08=140648.5

140648.5+(1945-1940)(1945-1950)*(1945-1960)* -2.137333333=139847 139847+(1945-1940)(1945-1950)*(1945-1960)*(1945-1970)* 0.067054167 =139218.4 139218.4+(1945-1940)(1945-1950)*(1945-1960)*(1945-1970)*(1945-1980)* -0.001564333=138705.1

The most important advantage of Newton intrepolation formula is the additive characters. In order to calculate (n+1) th degree of polynomial all you need is nth degree polynomial difference values. Example code is giving the result of the same problem Problem 6.4-1 Newton Interpolation formula
import java.io.*; class NA47 { public static double[][] Newton(double xi[],double yi[]) { int n=xi.length; double F[][]=new double[n+1][n]; for(int i=0;i<n;i++) F[i][0]=yi[i]; for(int i=1;i<n;i++) { for(int j=1;j<=i;j++) {F[i][j]=(F[i][j-1]-F[i-1][j-1])/(xi[i]-xi[i-j]);} } for(int i=0;i<n;i++) {F[n][i]=xi[i];} //System.out.println(Matrix.toString(F)); return F; } public static double funcNewton(double F[][],double x) { int n=F[0].length; double carpim=1.0; double toplam=0; for(int i=0;i<n;i++) { carpim=F[i][i]; for(int j=0;j<=(i-1);j++) {carpim*=(x-F[n][j]);} toplam+=carpim; } return toplam; } public static double[][] funcNewton(double xi[],double yi[],int numberofmidpoints) { //numberofmidpoints : in x--o--o--x--o--o--x chain if x's are esxperimental points // numberofmidpoints is 2 int n=xi.length; int nn=(n-1)*(numberofmidpoints+1)+1; double z[][]=new double[2][nn]; double Q[][]=Newton(xi,yi); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=xi[i];z[1][k]=funcNewton(Q,z[0][k]);k++; for(int j=0;j<numberofmidpoints;j++) {dx=(xi[i+1]-xi[i])/((double)numberofmidpoints+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcNewton(Q,z[0][k]);k++;} }

z[0][k]=xi[i];z[1][k]=funcNewton(Q,z[0][k]); return z; } public static void main(String args[]) throws IOException { double x[]={1940,1950,1960,1970,1980,1990}; double y[]={132165,151336,179323,203302,226542,249633}; double z1[][]=funcNewton(x,y,9); System.out.println("Newton interpolation\n"+Matrix.toStringT(z1)); Text.print(Text.T(z1),"Newton interpolation"); Plot pp=new Plot(x,y); pp.setPlabel("Newton interpolation"); pp.setPlotType(0,20); pp.addData(z1[0],z1[1]); pp.plot(); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA47 Newton interpolation

1940.0000000000132165.0000000000 1941.0000000000132684.8307565000 1942.0000000000133658.2814080000 1943.0000000000135023.3483545000 1944.0000000000136723.0162560000 1945.0000000000138705.0703125000 1946.0000000000140921.9085440000 1947.0000000000143330.3540705000 1948.0000000000145891.4673920000 1949.0000000000148570.3586685000 1950.0000000000151336.0000000000 1951.0000000000154161.0377065000 1952.0000000000157021.6046080000 1953.0000000000159897.1323045000 1954.0000000000162770.1634560000 1955.0000000000165626.1640625000 1956.0000000000168453.3357440000 1957.0000000000171242.4280205000 1958.0000000000173986.5505920000 1959.0000000000176680.9856185000 1960.0000000000179323.0000000000 1961.0000000000181911.6576565000 1962.0000000000184447.6318080000 1963.0000000000186933.0172545000 1964.0000000000189371.1426560000 1965.0000000000191766.3828125000 1966.0000000000194123.9709440000 1967.0000000000196449.8109705000 1968.0000000000198750.2897920000 1969.0000000000201032.0895685000 1970.0000000000203302.0000000000 1971.0000000000205566.7306065000 1972.0000000000207832.7230080000 1973.0000000000210105.9632045000 1974.0000000000212391.7938560000 1975.0000000000214694.7265625000 1976.0000000000217018.2541440000 1977.0000000000219364.6629205000 1978.0000000000221734.8449920000 1979.0000000000224128.1105185000 1980.0000000000226542.0000000000 1981.0000000000228972.0965565000 1982.0000000000231411.8382080001 1983.0000000000233852.3301545001 1984.0000000000236282.1570560000 1985.0000000000238687.1953125000 1986.0000000000241050.4253440000 1987.0000000000243351.7438705000 1988.0000000000245567.7761920000 1989.0000000000247671.6884685000 1990.0000000000249633.0000000000 > Terminated with exit code 1.

Lagrange Interpolation Polynomial

Lagrange interpolation polynomial is the reformulated version of the Newtons interpolation polynomial. Lagrange Formula do not require calculation of divided differences.

f n ( x) = Li ( x) f ( xi ) Where
i =0
n

Li ( x) =
j =0 j i

x xj xi x j

indicates multiplication process.

Open form of the equation; for the first degree (linear) polinomial (n=1)

f1 ( x) =

x x1 x x0 f ( x0 ) + f ( x1 ) x0 x1 x1 x0

for the first degree (quadratic) polinomial (n=2)

f 2 ( x) =

( x x1 )( x x2 ) ( x x0 )( x x2 ) ( x x0 )( x x1 ) f ( x0 ) + f ( x1 ) + f ( x2 ) ( x2 x0 )( x2 x1 ) ( x0 x1 )( x0 x2 ) ( x1 x0 )( x1 x2 )

Program 6.4-2 Lagrange Interpolation formula


//Lagrange interpolation polynomial import java.io.*; class NA48 { public static double[][] Lagrange(double xi[],double yi[]) { int n=xi.length; double L[][]=new double[2][n]; for(int i=0;i<n;i++) {L[0][i]=yi[i]; for(int j=0;j<n;j++) {if(i!=j) L[0][i]/=(xi[i]-xi[j]);} } for(int i=0;i<n;i++) {L[1][i]=xi[i];} return L; }

public static double funcLagrange(double L[][],double x) { int n=L[0].length; double carpim=1.0; double toplam=0; for(int i=0;i<n;i++) {carpim=L[0][i]; for(int j=0;j<n;j++) {if(i!=j) carpim*=(x-L[1][j]);} toplam+=carpim; } return toplam; }

public static double[][] funcLagrange(double xi[],double yi[],int numberofmidpoints) { //numberofmidpoints : in x--o--o--x--o--o--x chain if x's are esxperimental points // numberofmidpoints is 2 int n=xi.length; int nn=(n-1)*(numberofmidpoints+1)+1; double z[][]=new double[2][nn]; double Q[][]=Lagrange(xi,yi); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=xi[i];z[1][k]=funcLagrange(Q,z[0][k]);k++; for(int j=0;j<numberofmidpoints;j++) {dx=(xi[i+1]-xi[i])/((double)numberofmidpoints+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcLagrange(Q,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcLagrange(Q,z[0][k]); return z; } public static void main(String args[]) throws IOException { double x[]={1.3,1.6,1.9}; double y[]={0.280086,0.4554022,0.281818186}; double z1[][]=funcLagrange(x,y,8); System.out.println("Lagrange interpolation\n"+Matrix.toStringT(z1)); Text.print(Text.T(z1),"Curve fitting with Lagrange interpolation formula"); Plot pp=new Plot(x,y); pp.setPlabel("Curve fitting with Lagrange interpolation formula"); pp.setPlotType(0,20); pp.addData(z1[0],z1[1]); pp.plot(); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA48 Lagrange interpolation 1.3000000000 0.2800860000 1.3333333333 0.3167952180 1.3666666667 0.3491970259 1.4000000000 0.3772914238 1.4333333333 0.4010784116 1.4666666667 0.4205579894 1.5000000000 0.4357301571 1.5333333333 0.4465949148 1.5666666667 0.4531522624 1.6000000000 0.4554022000 1.6333333333 0.4533447275 1.6666666667 0.4469798450 1.7000000000 0.4363075524 1.7333333333 0.4213278498 1.7666666667 0.4020407372

1.8000000000 1.8333333333 1.8666666667 1.9000000000

0.3784462144 0.3505442817 0.3183349389 0.2818181860

Hermit Interpolation Polynomial

Hermit interpolation requires and uses derivative data in addition to the function data. General formula H(x) = Q0,0+ Q1,1(x-x0)+ Q2,2(x-x0)2+ Q3,3(x-x0)2(x-x1)+ Q4,4(x-x0)2(x-x1)2++ Q2n+1,2n+1(x-x0)2(x-x1)2(x-xn-1)2(x-xn) Calculation of the coefficients is explain at Algorithm 6.4-1 and Table 6.4-1. Algorithm 6.4-1 Hermit interpolation
INPUT x0,x1,.,xn deerleri; f(x0), f(x1), ,f(xn) deerleri; f(x0), f(x1), ,f(xn) deerleri OUTPUT : Q0,0,Q1,1,.., Q2n+1,2n+1 deerleri H(x)= Q0,0+ Q1,1(x-x0)+ Q2,2(x-x0)2+ Q3,3(x-x0)2(x-x1)+ Q4,4(x-x0)2(x-x1)2++ Q2n+1,2n+1(x-x0)2(x-x1)2(x-xn-1)2(x-xn) Step 1: for i=0,1,,n { Step 2: z2i=xi; z2i+1=xi; Q2i,0=f(xi); Q2i+1,0=f(xi); Q2i+1,0=f(xi); Step 3: if i 0 then

Q2i , j =

Q2i ,0 Q2i1, 0 z 2i z 2i1

} End of Step 1 in sonu Step 4: for i=2,3,,2n+1 { for j=2,3,,i

Qi , j =

Qi , j 1 Qi 1, j 1 z i zi j

Table 6.4-1 Hermit interpolation difference formulation


Z Z0 = x0 Z1 = x0 Z2 = x1 Z3 = x1 Z4 = x2 f[z0]=f(x0) f[z1]=f(x0) f[z2]=f(x1) f[z3]=f(x1) f[z4]=f(x2) f(z) First difference equation Second difference equation

f[z 0 , z 1 ] = f(x 0 )

f[z 0 , z1 , z 2 ] =

f [z1 , z 2 ] f [z 0 , z1 ] z2 z0

Z5 = x2

f[z5]=f(x2)

f[z z ]

f [ z 2 ] f [ z1 ]

f[

f [z 2 , z 3 ] f [z1 , z 2 ]

An example problem : k 0 1 2 Difference table: 1.3 0.620086 1.3 1.6 1.6 1.9 1.9 0.620086 0.4554022 0.4554022 0.2818186 0.2818186

1.3 1.6 1.9

xk

f(xk) 0.6200860 0.4554022 0.2818186

f(xk) -0.5220232 -05698959 -0.5811571

-0.5220232 -0.548946 -0.5698959 -0.578612 -0.5811571

-0.069833 -

0.06636556 0.06796556 0.06856667

0.00266667 0.00100185

Interpolation value at x=1.5: H5(1.5) =0.620086+(1.5-1.3)*( -0.5220232)+ (1.5-1.3)2*(1.5-1.6)*( 0.06636556+ (1.5-1.3)2* (1.5-1.6)2*(0.00266667)+ 1.5-1.3)2* (1.5-1.6)2*(1.5-1.9) (-0.00277469)=0.5118277 A program carrying out Hermit interpolation is given Program 6.4-3 Hermite polynomial interpolation formula
//Hermit Interpolation import java.io.*; class NA123 { public static double [] Hermite(double xi[],double fi[],double dfi[]) { //m degree of polynomial int i,j; int m=xi.length; int n=m-1; double Q[][]=new double[2*n+2][2*n+2]; double QQ[]=new double[2*n+2]; double z[]=new double[2*n+2]; for(i=0;i<=n;i++) { z[2*i]=xi[i]; z[2*i+1]=xi[i]; Q[2*i][0]=fi[i]; Q[2*i+1][0]=fi[i]; Q[2*i+1][1]=dfi[i]; if(i!=0) { Q[2*i][1]=(Q[2*i][0]-Q[2*i-1][0])/(z[2*i]-z[2*i-1]);} } for(i=2;i<=(2*n+1);i++) {for(j=2;j<=i;j++){Q[i][j]=(Q[i][j-1]-Q[i-1][j-1])/(z[i]-z[i-j]);} } for(i=0;i<=(2*n+1);i++) {QQ[i]=Q[i][i];} //System.out.println(QQ.length+"\n"+Matrix.toStringT(QQ));

return QQ; } public static double funcHermite(double QQ[],double xi[],double x) { //Hermit polynomial int i,j; int m=QQ.length-1; double xx=1; double toplam=QQ[0]; for(i=1;i<m;i+=2) { j=(i-1)/2; xx*=(x-xi[j]); toplam+=QQ[i]*xx; xx*=(x-xi[j]); toplam+=QQ[i+1]*xx; } return toplam; } public static double[][] funcHermite(double xi[],double yi[],double dyi[],int aradegersayisi) { //number of midpoints: x--o--o--x--o--o--x x data points midpoints are 2 int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double Q[]=Hermite(xi,yi,dyi); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=xi[i];z[1][k]=funcHermite(Q,xi,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcHermite(Q,xi,z[0][k]);k++; } } z[0][k]=xi[i];z[1][k]=funcHermite(Q,xi,z[0][k]); return z; } public static void main(String args[]) throws IOException { double x[]={1.3,1.6,1.9}; double y[]={0.620086,0.4554022,0.281818186}; double dy[]={-0.5220232,-0.5698959,-0.5811571}; double z1[][]=funcHermite(x,y,dy,8); Text.print(Text.T(z1),"Interpolation by using Hermite polynomials"); System.out.println("Hermite polynomial interpolation\n"+Matrix.toStringT(z1)); Plot pp=new Plot(x,y); pp.setPlabel("Interpolation by using Hermite polynomials"); pp.setPlotType(0,20); pp.addData(z1[0],z1[1]); pp.plot(); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA123 Hermite polynomial interpolation 1.3000000000 0.6200860000 1.3333333333 0.6025660584 1.3666666667 0.5848174157 1.4000000000 0.5668545838 1.4333333333 0.5486921534 1.4666666667 0.5303447937 1.5000000000 0.5118272527 1.5333333333 0.4931543568 1.5666666667 0.4743410113 1.6000000000 0.4554022000 1.6333333333 0.4363529853 1.6666666667 0.4172085082 1.7000000000 0.3979839884 1.7333333333 0.3786947244 1.7666666667 0.3593560931 1.8000000000 0.3399835500 1.8333333333 0.3205926294 1.8666666667 0.3011989443 1.9000000000 0.2818181860

6.5 CUBIC SPLINE INTERPOLATION POLYNOMIALS Another way for the interpolation process is to fit a different polynomial in between each two point. If a third degree polinomial is considered: rk(x)=ak(x-xk)3+ bk(x-xk)2+ ck(x-xk)+yk 1 k n In the interpolation proses polinoms should be passing through all data points rk(xk+1)=yk+1 1 k n

In the same time the first derivative of the polynomial should also be continious while passing from one polynomial to the next one at the data point rk-1(xk)= rk(xk) 1 k n For the third degree polinomial second derivative of the polynomial should also be continious while passing from one polynomial to the next one at the data point rk-1(xk)= rk(xk) 1 k n

All these conditions are not enough to solve the coefficients of the polinomials. Two more conditions are required. This two additional conditions (A and B of the following equation) can be given by user r1(x1)=A rn-1(xn)=B They are the second derivatives at the both hand of the series of polinomials. If A and B values are taken equals to 0, it is called a natural cubic spline. Other end conditions such as the ones depends one the first derivatives can also be set to solve the system of equations. Defining hk=xk+1-xk 1 k n Stystem of equations become: akhk3+ bkhk2+ckhk = yk+1-yk, , , 1 k n 1 k n 1 k n

3ak-1hk-12+ 2bk-1hk-1+ck-1-ck = 0 6ak-1hk-1+ 2bk-1+2bk 2b0 6an-1hn-1+ 2bn-1 =0 =0 =0

This set contains 3n-3 equations. This could a considerable load to the system of equation solving programs. To make calculation load simpler a special third degree polinomial can be considered. If our cubic polinomial is in the form of: sk(x)=ak(x-xk)+ bk(xk+1-x)+ [(x-xk)3 ck+1 +(xk+1-x)3 ck]/(6hk) then derivative equations becomes sk(x)=ak- bk+ [(x-xk)2 ck+1 - (xk+1-x)2 ck]/(2hk) sk(x)=[(x-xk) ck+1 + (xk+1-x) ck]/hk ak ve bk coefficients can be expressed as a function of ck bk=[6yk-hkck]/(6hk), ak=[6yk+1-hk2ck+1]/(6hk), 1 k n 1 k n 1 k n 1 k n 1 k n

In this case only ck terms left in the system of equations to be solved. hk-1ck-1+ 2( hk-1- hk )ck+ hk ck+1 = 6

yk +1 yk yk yk 1 hk 1 hk

1 k n

This system of equation has only n-2 terms to be solved. By making definition

wk =

yk +1 yk , hk

1 k n

System of equation becomes

0 0 1 h 2( h + h ) h2 1 2 1 0 2(h2 + h3 ) h2 ... ... ... 0 0 0 0 0 0 0 0 0

... ... ... ... ... ... ...

0 c0 A c 6( w w ) 0 0 0 1 2 1 0 0 0 c2 6( w3 w2 ) ... ... ... ... = ... 2(hn3 + hn2 ) 0 cn2 6( wn2 wn3 ) hn2 2(hn2 + hn1 ) hn1 cn1 6( wn1 wn2 ) hn2 0 0 1 cn B 0 0

Where A and B are the second derivative end conditions. A and B should be defined by user. Another important property of the above matrix is that it is a band matrix, therefore less amount of calculation is required to solve it (by using band matrix algorithms such as Thomas algorithm) compare to gauss elimination type of solution methods. Program 6.5-1 Cubic spline curve fitting
import java.io.*; class NA49 { public static double [] thomas(double f[],double e[],double g[],double r[]) { // 3 band matrix system of equation solving algorithm int n=f.length; double x[]=new double[n]; for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; }

public static double [][] cubic_spline(double xi[],double yi[],double c0,double cn) { int n=xi.length; double h[]=new double[n]; double w[]=new double[n]; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double d[]=new double[n]; double x[]=new double[n]; double S[][]=new double[4][n]; int k; for(k=0;k<(n-1);k++) {h[k]=xi[k+1]-xi[k]; w[k]=(yi[k+1]-yi[k])/h[k];

} d[0]=c0; d[n-1]=cn; for(k=1;k<(n-1);k++) {d[k]=6.0*(w[k]-w[k-1]);} f[0]=1.0; f[n-1]=1.0; g[0]=0.0; g[n-1]=0.0; e[0]=0.0; e[n-1]=0.0; for(k=1;k<(n-1);k++) {f[k]=2.0*(h[k]+h[k-1]);e[k]=h[k-1];g[k]=h[k];} S[2]=thomas(f,e,g,d); S[3]=xi; for(k=0;k<(n-1);k++) {S[0][k]=(6.*yi[k+1]-h[k]*h[k]*S[2][k+1])/(6.0*h[k]); S[1][k]=(6.*yi[k]-h[k]*h[k]*S[2][k])/(6.0*h[k]); } return S; } public static double funcSpline(double S[][],double x) { int n=S[0].length; double xx1=0; double xx2=0; double y=0; double hk=0; for(int k=0;k<(n-1);k++) {if(S[3][k]<=x && x<=S[3][k+1]) {hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); break; } } if(y==0 && S[3][n-2]<=x ) { int k=n-2; hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); } return y; } public static double[][] funcSpline(double xi[],double yi[],int numberofmidpoints) { //numberofmidpoints : in x--o--o--x--o--o--x chain if x's are esxperimental points // numberofmidpoints is 2 int n=xi.length; int nn=(n-1)*(numberofmidpoints+1)+1; double z[][]=new double[2][nn]; double S[][]=cubic_spline(xi,yi,0,0); double dx=0;

int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);k++; for(int j=0;j<numberofmidpoints;j++) {dx=(xi[i+1]-xi[i])/((double)numberofmidpoints+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcSpline(S,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]); return z; } public static void main(String args[]) throws IOException { double x[]={0.9,1.3,1.9,2.1,2.6,3.0,3.9,4.4,4.7,5.0,6.0,7.0,8.0,9.2,10.5,11.3,11.6,12.0,12.6,13.0,13.3}; double y[]={1.3,1.5,1.85,2.1,2.6,2.7,2.4,2.15,2.05,2.1,2.25,2.3,2.25,1.95,1.4,0.9,0.7,0.6,0.5,0.4,0.25}; double z3[][]=funcSpline(x,y,20); System.out.println("Cubic spline \n"+Matrix.toStringT(z3)); Text.print(Text.T(z3),"Curve fitting with cubic spline interpolation polinomials"); Plot pp=new Plot(x,y); pp.setPlabel("Curve fitting with cubic spline interpolation polinomials"); pp.setPlotType(0,20); pp.addData(z3[0],z3[1]); pp.plot(); } }

6.6 B SPLINE INTERPOLATION

b-Spline interpolation Formula will be investigated in this chapter. Consider a set of infinite points.

Figure 6.6-1 B0i spline definition ..< t-2 < t-1 < t0 < t1 < t2 < Definition of zeroth order interpolation polynomial B0i defined for this points will be
t i x < t i +1 t i > x t i +1 This definition is shown in figure 7.6-1. The value of the function is 1 at point ti and 0 at point ti+1 and also 0 at the rest of the region. This is a diract-delta function. B spline is also positive defined in all the region. That means for all the given x and i values
1 Bi0 ( x) = 0

Bi0 ( x) 0 and in the same time for all the x values


i = i = 0 Bi ( x) = 1

After defining a B0i , polynomials of higher degrees can easily be defined by using this basic definition.

x ti Bik ( x) = t t i +1 i

k 1 t x k 1 Bi ( x) + i + 2 t t Bi +1 ( x) i + 2 i +1

0 x ti + 2 veya x ti x ti Bik ( x) = ti < x < ti +1 ti +1 ti ti + 2 x t < x < t i +2 t t i +1 i + 2 i +1


B1i B-spline interpolation equation is shown in figure 7.6-2.

Figure 6.6-2 B1i erit tanm The basic relation

Bi ( x) = 1 is also valid fort his spline function as well. i =


1

i =

B spline interpolation Formula for degree k spline

f ( x) = Cik Bik ( x) when this equation combines with the equations above, it became
i =

i =

x ti i = f ( x) = Cik i = ti +1 ti
i = i =

t x k 1 k 1 Bi ( x) + i + 2 t t Bi +1 ( x) and from here it can be converted to i +2 i +1

f ( x) = Cik 1 Bik 1 ( x) form, The coefficients became x ti Ci j 1 = Ci j t t i+ j i


i =

t x j + Ci 1 i + j t t i+ j i

By using this basic formulations B- Spline interpolation formulas can be created stepwise additions

S ( x) = Ai Bikk ( x) ise i =
Assume that following data is given and X t0 Y y0 t1 y1 . tn Yn

We would like to have S(ti)=yi 0 i n . If it is started from Bi0 spline


1 Bi0 (t j ) = ij = 0
n

i = j i j

Coefficients become: Ai=yi 0 i n and equation is

S ( x) = y i Bi0 ( x) Then first degree B spline can be defined i =0


1 Bi11 (t j ) = ij = 0

i = j i j

S ( x) = yi Bi11 ( x) and again Ai=yi 0 i n . For higher degree polynomials i =0


i = i = k Ai Bi 2 (t j ) =

i =n

1 Aj (t j +1 t j ) + Aj +1 (t j t j 1 ) In this case the following system of equation is t j +1 t j 1

obtained.

A j (t j +1 t j ) + A j +1 (t j t j 1 ) = y j (t j +1 t j 1 ) 0 j n One way of solving these equation is to assign


value to A0 and solve remaining accordingly. In this case: Coefficients of the equation

Aj +1 = j + j Aj

0 jn .

j = yi j =

t j +1 t j 1 t t j j 1 0 jn t j t j +1 t j t j 1

In order to keep coefficients small enough, it is recommended to select A0 to minimize function

= Ai2 . In order to find A0 from this equation (Remember that all Ai values are derived from A0) i =0
following equation can be written

n +1

Aj +1 = j + j A0 0 j n
j and j values in the equation can be found by

0 = 0 0 = 0 j = j + j j 1 j = j j 1 0 j n
= A02 + A12 + A22 ... An2 = A02 + ( 0 + 0 A0 ) 2 + ( 1 + 1 A0 ) 2 + ... + ( n + n A0 ) 2 In order to select minimum A0 derivative of
this equation should be set equal to zero.

d = 2 A0 + 2( 0 + 0 A0 ) 0 + 2( 1 + 1 A0 ) 1 + ... + 2( n + n A0 ) 1 = 0 dA0
This equation can be written in short as

qA0 + p = 0

q = 1 + 02 + 12 + ... + n2 p = 0 0 + 1 1 + 2 2 + ... + n n
With this defination equations to define B spline interpolation can be set. Program 6.6-1 B-spline curve fitting
//B spline curve fitting import java.io.*; import javax.swing.*; class NA124 { public static double[] hi(double ti[]) { int n=ti.length-1;//ti 0..n int nn=n+2;

double h[]=new double[nn]; for(int i=1;i<=n;i++) {h[i]=ti[i]-ti[i-1]; } h[0]=h[1]; h[n+1]=h[n]; return h; } public static double [] B_Spline(double ti[],double yi[]) { int n=ti.length-1; int nn=n+2; int i; double delta,gamma,p,q,r; double a[]=new double[nn]; double h[]=new double[nn]; h=hi(ti); delta=-1; gamma=2.0*yi[0]; p=delta*gamma; q=2.0; for(i=1;i<=n;i++) {r=h[i+1]/h[i]; delta=-r*delta; gamma=-r*gamma+(r+1)*yi[i]; p=p+gamma*delta; q=q+delta*delta; } a[0]=-p/q; for(i=1;i<=(n+1);i++) {a[i]=((h[i-1]+h[i])*yi[i-1]-h[i]*a[i-1])/h[i-1];} return a; } public static double funcB_Spline(double ti[],double a[],double x) { double h[]=hi(ti); int i; int n=ti.length-1; double nn=n+2; double d,e; for(i=(n-1);i>=1;i--) {if((x-ti[i])>=0) break;} i++; d=(a[i+1]*(x-ti[i-1])+a[i]*(ti[i]-x+h[i+1]))/(h[i]+h[i+1]); e=(a[i]*(x-ti[i-1]+h[i-1])+a[i-1]*(ti[i-1]-x+h[i]))/(h[i-1]+h[i]); double bs=(d*(x-ti[i-1])+e*(ti[i]-x))/h[i]; return bs; } public static double[][] funcB_Spline(double ti[],double yi[],int aradegersayisi) { int n=ti.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double A[]=B_Spline(ti,yi); double dx=0; int k=0;

int i; for(i=0;i<(n-1);i++) { z[0][k]=ti[i];z[1][k]=funcB_Spline(ti,A,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(ti[i+1]-ti[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcB_Spline(ti,A,z[0][k]);k++;} } z[0][k]=ti[i];z[1][k]=funcB_Spline(ti,A,z[0][k]); return z; } public static void main(String args[]) throws IOException { double x[]={0.0, 0.6 ,1.5 ,1.7 ,1.9 ,2.1,2.3 ,2.6 ,2.8,3.0 ,3.6 ,4.7 ,5.2 ,5.7 ,5.8 , 6.0 , 6.4 ,6.9,7.6,8.0}; double y[]={-0.8,-0.34,0.59,0.59,0.23,0.1,0.28,1.03,1.5,1.44,0.74,-0.82,-1.27,-0.92,-0.92,-1.04,-0.79,-0.06,1.0,0.0}; int n=x.length; double z1[][]=funcB_Spline(x,y,20); Plot pp=new Plot(z1[0],z1[1]); pp.setPlabel("B-spline curve fitting"); pp.setXlabel("x"); pp.setYlabel("y"); pp.setPlotType(0,0); pp.addData(x,y); pp.setPlotType(1,24); pp.plot(); } }

6.7 LEAST SQUARE CURVE FITTING OF NONLINEAR EQUATIONS If curvefitting to a function y = f (a; x) with non-linear coefficients is required. Process will be a little bit different compare to equations with linear coefficients.

y = f (a; x) is given and data xi,fi, i=0...n will be fit into the equation. In this equation coefficients
are placed in a non-linear form. For example : y = a0 (1 e Least square error formula was defined as
( ( ( ( H (a0m ) ,...., amm ) ) = w( x i0 ) yi f (a0m ) ,...., amm ) , x i0 ) i =1 n

a1 x

This equation should be solved by using one of the multidimensional optimization methods such as Nelder-Mead, Newton-Raphson, Steepest descent sample : (input3.txt)
5.0 16.0 10.0 25.0 15.0 32.0 20.0 33.0 25.0 38.0 30.0 36.0

A nonlinear function to fit the data: double pi=ai[0]*(1.0-Math.exp(-ai[1]*x)); In this function a iare coefficient of the equations and x is independent variable. Program 6.7-1 Nelder-Mead one independent variable least square curve fitting
import java.io.*; import javax.swing.*; abstract class f_xj { // single function multi independent variable // a single value is returned ( // example f=x[0]+sin(x[1]) // func(x) returns the value of f abstract public double func(double x[]); } class yy extends f_xj { double xi[]; // independent variable data set double yi[]; //dependent variable data set double a[]; //fit function coefficient set int nn; public yy(String filename,double ia[]) { //read the data to curvefit //get the data file and initial fit coefficient when class is defined xi=new double[500]; yi=new double[500]; int n=ia.length; a=new double[n]; seta(ia); int i=-1; try{ BufferedReader fin=new BufferedReader(new FileReader(filename)); try { while(fin != null) { i++; xi[i]=Text.readDouble(fin); yi[i]=Text.readDouble(fin); } } catch(EOFException e_eof) {System.out.println("end of file"); }

}catch(IOException e_io) {System.out.println("dosya bulunamad"); } nn=i; a=ia; } public void seta(double ia[]) { //assign new fit coefficient set for(int ii=0;ii<nn;ii++) a[ii]=ia[ii]; } public double[] geta() { // return fit coefficient set return a; } double Ps(double x,double ai[]) { //sample specific function // saturation pressure as a function of saturation temperature a=ai; double pi=ai[0]*(1.0-Math.exp(-ai[1]*x)); return pi; //kPa } public double func(double ai[]) { double ff=0; double w; double yy; for(int i=0;i<nn;i++) {w=Ps(xi[i],ai); yy=yi[i]; //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); w-=yy; ff+=w*w; //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); } return ff; } } class zz extends yy { public zz(String filename,double ia[]) {super(filename,ia);} public double func(double ai[]) { return -super.func(ai); } } public class NA50 { public static void main(String args[]) throws IOException { //String in_name=JOptionPane.showInputDialog(" enter name of the input file : ");

String in_name="input3.txt"; double a[]; a=new double[2]; a[0] = 30.0; a[1] = 1.0; double da[]; da=new double[2]; da[0] =1.0; da[1] =0.2; yy f=new yy(in_name,a); int i=0; for(double t=-100;t<101;t+=1) {System.out.println("t="+t+"ti="+f.xi[i]+"Pi="+f.yi[i]+"P="+f.Ps(t,a));i++;}; System.out.println("f input = "+f.func(a)); double p[]=NA41.nelder(f,a,da,1000,1e-20,1); String s1=" coefficient of the curve fitting equation : \n"+Matrix.toStringT(p)+"\n"; s1+="value of the error least square function = "+f.func(a)+"\n"; s1+="function value = "+f.func(p); String s2="Nelder-Mead nonlinear least square curve fitting : "; JOptionPane.showMessageDialog(null,s1,s2,JOptionPane.PLAIN_MESSAGE); } }

The problem can be solved by using different optimization programs. For example id DavidonFletcher-Powell algorithms is used: Program 6.7-2 Davison-Fletcher-Powell one independent variable nonlinear least square curve fitting
// Davidon-Fletcher-Powell optimization // Method Referance : Numerical Analysis, 8th edition Richard L. Burden, J. Douglas Faires 3 // page 617 import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; import java.io.*; class fa extends f_xj {

double xi[]; // independent variable data set double yi[]; //dependent variable data set double a[]; //fit function coefficient set int nn; public fa(String filename,double ia[]) { //read the data to curvefit //get the data file and initial fit coefficient when class is defined xi=new double[500]; yi=new double[500]; int n=ia.length; a=new double[n]; seta(ia); int i=-1; try{ BufferedReader fin=new BufferedReader(new FileReader(filename)); try { while(fin != null) { i++; xi[i]=Text.readDouble(fin); yi[i]=Text.readDouble(fin); } } catch(EOFException e_eof) {System.out.println("end of file"); } }catch(IOException e_io) {System.out.println("dosya bulunamad"); } nn=i; a=ia; } public void seta(double ia[]) { //assign new fit coefficient set for(int ii=0;ii<nn;ii++) a[ii]=ia[ii]; } public double[] geta() { // return fit coefficient set return a; } double Ps(double x,double ai[]) { //sample specific function // saturation pressure as a function of saturation temperature a=ai; double pi=ai[0]*(1.0-Math.exp(-ai[1]*x)); //pi=Math.pow(10.0,pi); return pi; //kPa } public double func(double ai[]) { double ff=0; double w; double yy; for(int i=0;i<nn;i++)

{w=Ps(xi[i],ai); yy=yi[i]; //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); w-=yy; ff+=w*w; //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); } return ff; } } public class NA125 { public static double VT_V(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; } } return aa; } public static double VT_X(double [] left,double [] right) { //multiplies a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];}

return Math.sqrt(total); } public static double turev(f_xj f,double x[],int denklem_ref,int x_ref) { // // verilen fonksiyonun ikinci trevlerinin matrisi // fonksiyon f in denklem_ref sayl fonksiyonunun(saylar 0 dan balar) // x[x_ref] deikenine gre trevi (saylar 0 dan balar) // df_denklem_ref(x)/d_x_ref // bu metod newtond metodu iinde kullanlmak iindir. double h0=0.256808; int i,m; int n=7; double f1[]; f1=new double[x.length]; double f2[]; f2=new double[x.length]; double x1[]; x1=new double[x.length]; double x2[]; x2=new double[x.length]; for(i=0;i<x.length;i++) { x1[i]=x[i]; x2[i]=x[i]; } //turev of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; //vector<double> h(n,0); for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) { h[i]=h0*Math.pow(r,i); } for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]*1.0094847; f1=df(f,x1); f2=df(f,x2); T[i][0]=( f1[denklem_ref] - f2[denklem_ref])/(2.0*h[i]); x1[x_ref]=x[x_ref]; x2[x_ref]=x[x_ref]; } for(m=1;m<n;m++) {

for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] - h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] df(f_xj f,double xxi[]) { //Gredyen vektr // f(x0,x1,x2,...xn) founksiyonunun trev vektr // [df/dx0, df/dx1,df/dx2,....,df/dxn] // df/dxj j=0...x.length // // bu fonksiyon bilgisayar tarafndan hesaplanmaktadr // kullanc tarafndan tanmlanan f(x) fonksiyonunun // saysal olarak alnan trevidir. // double a[]=new double[xxi.length]; for(int i=0;i<xxi.length;i++) { a[i]=turev(f,xxi,i); } return a; } public static double turev(f_xj f,double xxi[],int x_ref) { // df/dxj double h0=0.256808; int i,m; int n=7; double x1[]; x1=new double[xxi.length]; double x2[]; x2=new double[xxi.length]; for(i=0;i<xxi.length;i++) { x1[i]=xxi[i]; x2[i]=xxi[i]; } //turev of a simple function double T[][]; T=new double[n][n]; double h[]; h=new double[n]; for(i=0;i<n;i++) { h[i]=0; for(int j=0;j<n;j++) T[i][j]=0; } h[0]=h0; double r=0.5; for( i=1;i<n;i++) {

//h[i]=h0*Math.pow(r,i); h0/=2.0; h[i]=h0; } //first turev (difference formula) for(i=0;i<n;i++) { x1[x_ref]+=h[i]; x2[x_ref]-=h[i]; T[i][0]=(f.func(x1)-f.func(x2))/(2.0*h[i]); x1[x_ref]=xxi[x_ref]; x2[x_ref]=xxi[x_ref]; } for(m=1;m<n;m++) { for(i=0;i<n-m;i++) { T[i][m]=(h[i]*h[i]*T[i+1][m-1] h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i] - h[i+m]*h[i+m]); } } double xx=T[0][n-1]; return xx; } public static double[] turev(f_xj f_deriv,double x[]) { // df/dxj j=0...x.length // This method calculates turev of a function with more than one independent variable. // Accuracy of method can be adjusted by changing variables h0 and n // function input should be in the form given in abstract class // f_xj,j=0...x.length = df/dx(x_ref) double a[]=new double[x.length]; for(int x_ref=0;x_ref<x.length;x_ref++) { a[x_ref]=turev(f_deriv,x,x_ref); } return a; } public static double SIGN(double a,double b) {return (b > 0.0 ? Math.abs(a) : -Math.abs(a));} public static double MAX(double a,double b) {return (a > b ? a : b);}

public static double linmin(f_xj f,double p[],double xi[]) { double tol=2.0e-4; int n=p.length; double pcom[],xicom[]; int j; double xx,xmin,bx,ax; f1dim f1=new f1dim(f,p,xi); ax=0.0; xx=1.0; bx=2.0; double aa[]=mnbrak(f1,ax,xx,bx); ax=aa[0]; xx=aa[1];

bx=aa[2]; xmin=brent(f1,ax,xx,bx,tol); return xmin; } public static double[][] linminiter(f_xj f,double p[],double xi[]) { // yeni iterasyon noktasn verir int n=p.length; double xmin=linmin(f,p,xi); double aa[][]=new double[2][n]; for (int j=0;j<n;j++) { xi[j] *= xmin; p[j] += xi[j]; aa[0][j]=p[j]; aa[1][j]=xi[j]; } return aa; } public static double[] brentf(f_x f,double ax,double bx,double cx,double tol) { // ax,bx,cx adet tahmin noktas (bxax ile cx arasnda yer almaldr) // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double CGOLD=(3.0-Math.sqrt(5))/2.0; //altn oran double ZEPS=1.0e-10; double xmin; double aa[]=new double[2]; // SHFT(double a,double b,double c,d) (a)=(b);(b)=(c);(c)=(d); int iter; double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; double e=0.0; d=0.0; a=((ax < cx) ? ax : cx); b=((ax > cx) ? ax : cx); x=w=v=bx; fw=fv=fx=f.func(x); for (iter=1;iter<=ITMAX;iter++) { xm=0.5*(a+b); tol2=2.0*(tol1=tol*Math.abs(x)+ZEPS); if (Math.abs(x-xm) <= (tol2-0.5*(b-a))) { xmin=x; aa[0]=xmin; aa[1]=fx; return aa; } if (Math.abs(e) > tol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.0*(q-r); if (q > 0.0) p = -p; q=Math.abs(q); etemp=e; e=d; if (Math.abs(p) >= Math.abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=CGOLD*(e=(x >= xm ? a-x : b-x)); else {

d=p/q; u=x+d; if (u-a < tol2 || b-u < tol2) d=SIGN(tol1,xm-x); } } else { d=CGOLD*(e=(x >= xm ? a-x : b-x)); } u=(Math.abs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); fu=f.func(u); if (fu <= fx) { if (u >= x) a=x; else b=x; {v=w;w=x;x=u;} {fv=fw;fw=fx;fx=fu;} } else { if (u < x) a=u; else b=u; if (fu <= fw || w == x) { v=w; w=u; fv=fw; fw=fu; } else if (fu <= fv || v == x || v == w) { v=u; fv=fu; } } } System.out.println("BRENT metodunda maksimum iterasyon says ald"); xmin=x; //minumum deer a[0] ile fonksiyon deeri a[1] ile geridndrlmektedir aa[0]=xmin; aa[1]=fx; return aa; } public static double[] mnbrak(f_x f,double ax,double dx) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seilen uzaklk return mnbrak(f,ax,(ax-dx),(ax+dx)); } public static double[] mnbrak(f_x f,double ax) { // dx ikinci derece polinom iin gerekli iki ek noktay oluturmak iin // ana noktadan seiln uzaklk return mnbrak(f,ax,(ax-0.1),(ax+0.1)); } public static double[] mnbrak(f_x f,double ax,double bx,double cx) { // ax,bx,cx adet tahmin noktas verildiinde iinde minimum bulunan ax,bx,cx adetnoktay bize arama // sonucu bulur // f : fonksiyon (f_x abstract snfnda tanmlanm) // tol tolerans int ITMAX=100; double xmin; double GOLD=(Math.sqrt(5.0)+1.0)/2.0;; double GLIMIT=100.0;

double TINY=1.0e-20; double fa,fb,fc; fa=0; fb=0; fc=0; double ulim,u,r,q,fu,dum; double aa[]=new double[3]; fa=f.func(ax); fb=f.func(bx); if (fb > fa) { //SHFT(dum,*ax,*bx,dum) {dum=ax;ax=bx;bx=dum;} //SHFT(dum,*fb,*fa,dum) {dum=fb;fb=fa;fa=dum;} } cx=(bx)+GOLD*(bx-ax); fc=f.func(cx); aa[0]=ax; aa[1]=bx; aa[2]=cx; while (fb > fc) { r=(bx-ax)*(fb-fc); q=(bx-cx)*(fb-fa); u=(bx)-((bx-cx)*q-(bx-ax)*r)/ (2.0*SIGN(MAX(Math.abs(q-r),TINY),q-r)); ulim=(bx)+GLIMIT*(cx-bx); if ((bx-u)*(u-cx) > 0.0) { fu=f.func(u); if (fu < fc) { ax=bx; bx=u; fa=fb; fb=fu; return aa; } else if (fu > fb) { cx=u; fc=fu; return aa; } u=(cx)+GOLD*(cx-bx); fu=f.func(u); } else if ((cx-u)*(u-ulim) > 0.0) { fu=f.func(u); if (fu < fc) { //SHFT(bx,cx,u,cx+GOLD*(cx-bx)) {bx=cx;cx=u;u=cx+GOLD*(cx-bx);} //SHFT(fb,fc,fu,f.func(u)) {fb=fc;fc=fu;fu=f.func(u);} } } else if ((u-ulim)*(ulim-cx) >= 0.0) { u=ulim; fu=f.func(u); } else { u=cx+GOLD*(cx-bx); fu=f.func(u); } //SHFT(ax,bx,cx,u) {ax=bx;bx=cx;cx=u;}

//SHFT(fa,fb,fc,fu) {fa=fb;fb=fc;fc=fu;} aa[0]=ax; aa[1]=bx; aa[2]=cx; } return aa; } public static double brent(f_x f,double ax,double bx,double cx,double tol) { double aa[]=brentf(f,ax,bx,cx,tol); return aa[0]; } public static double[][] I(int n) { //unit matrix double b[][]; b=new double[n][n]; for(int i=0;i<n;i++) b[i][i]=1.0; return b; } // Davidon - Fletcher-Powell metodu //________________________________________________________________ public static double[] davidon_fletcher_powell( f_xj f,double x[]) { int i,j,k; int nmax=500; double gamma=1.0; double tolerance=1.0e-12 ; int n=x.length; double alpha; double dx[]=new double[n]; double g[] =new double[n]; double ge[]=new double[n]; double dg[]=new double[n]; double d[] =new double[n]; double r[] =new double[n]; double rdg,dxdg; double beta; double alpha_d; double Q[][]=I(n); double Q1[][]=new double[n][n]; double Q2[][]=new double[n][n]; int nn=15; i=0; j=0;k=0; g=turev(f,x); while( j++ < nmax && gamma > tolerance ) { for(int ii=0;ii<n;ii++) { d[ii]=0.0; for(int kk=0;kk<n;kk++) {d[ii]=d[ii]-Q[ii][kk]*g[kk];} } System.out.println("j="+j+"d="+Matrix.toString(d));

alpha=linmin(f,x,d); System.out.println("alfa="+alpha); if(alpha==0) break; for(i=0;i<n;i++){dx[i]=alpha*d[i];x[i]+=dx[i];ge[i]=g[i];} System.out.println("j="+j+"dx=\n"+Matrix.toString(dx)); System.out.println("j="+j+"x=\n"+Matrix.toString(x)); g=turev(f,x); System.out.println("j="+j+"g=\n"+Matrix.toString(g)); for(i=0;i<n;i++){dg[i]=g[i]-ge[i];} gamma=norm(g); for(int ii=0;ii<n;ii++) {r[ii]=0.0; for(int kk=0;kk<n;kk++) {r[ii]+=Q[ii][kk]*dg[kk];} } rdg=VT_X(r,dg); dxdg=VT_X(dx,dg); Q1=V_VT(r); Q2=V_VT(dx); for(int ii=0;ii<n;ii++) for(int jj=0;jj<n;jj++) Q[ii][jj]=Q[ii][jj]-Q1[ii][jj]/rdg+Q2[ii][jj]/dxdg; gamma=norm(dx); System.out.println("j="+j+"Q=\n"+Matrix.toString(Q)); j++;k++; if(k>=nn) {k=0;Q=I(n);} } if(j >= nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); return x; } public static double[] davidon_fletcher_powell_V1( f_xj f,double x[]) { int i,j,k; int nmax=500; double gamma=1.0; double tolerance=1.0e-12 ; int n=x.length; double alpha; double dx[]=new double[n]; double g[] =new double[n]; double ge[]=new double[n]; double dg[]=new double[n]; double d[] =new double[n]; double r[] =new double[n]; double rdg,dxdg; double beta; double alpha_d; double Q[][]=I(n); double Q1[][]=new double[n][n]; double Q2[][]=new double[n][n]; int nn=15; i=0; j=0;k=0; g=turev(f,x); while( j++ < nmax && gamma > tolerance ) {

for(int ii=0;ii<n;ii++) { d[ii]=0.0; for(int kk=0;kk<n;kk++) {d[ii]=d[ii]-Q[ii][kk]*g[kk];} } alpha=linmin(f,x,d); if(alpha==0) break; for(i=0;i<n;i++){dx[i]=alpha*d[i];x[i]+=dx[i];ge[i]=g[i];} g=turev(f,x); for(i=0;i<n;i++){dg[i]=g[i]-ge[i];} gamma=norm(g); for(int ii=0;ii<n;ii++) { r[ii]=0.0; for(int kk=0;kk<n;kk++) {r[ii]+=Q[ii][kk]*dg[kk];} } rdg=VT_X(r,dg); dxdg=VT_X(dx,dg); beta=dxdg/rdg; Q1=V_VT(r); Q2=V_VT(dx); for(int ii=0;ii<n;ii++) for(int jj=0;jj<n;jj++) Q[ii][jj]=beta*(Q[ii][jj]-Q1[ii][jj]/rdg)+Q2[ii][jj]/dxdg; gamma=norm(dx); j++;k++; if(k>=nn) {k=0;Q=I(n);} } if(j >= nmax) JOptionPane.showMessageDialog(null,"Uyar maximum iterasyon says ald \n"+ " zm geerli olmyabilir","MAKSMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); return x; } public static double[] verigir(String s) { String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static void main (String args[]) { String s="write root estimates with one space in between: "; double [] x0=verigir(s); fa f_x=new fa("a.txt",x0); double [] r1= davidon_fletcher_powell(f_x,x0); double [] r2= davidon_fletcher_powell_V1(f_x,x0); s=" solution set DFP : \n"+Matrix.toStringT(r1); s+="solution set corrected DFP : \n"+Matrix.toStringT(r2); String s2="Davidon - Fletcher - Powell multivariable optimization:";

JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

A real engineering problem will be defined here to show how actual need would arise and solution

As a research project, drying of sliced apple by a heat pump is investigated. The data includes t time(hour), slice thickness h(mm) and P is the retaining moisture in the apple slice(kg moisture/kg fresh apple moisture). When the previous researchs are investigated, it is found that the following equations are used. a=a0+a1h k=a2+a3h P=ae(-kt) In order to compare we would like to fit our data into the same equations. The data (T,h,P) 4 10 0.328 3 8 0.332 0 6 1.000 4.5 10 0.287 3.5 8 0.276 0.5 6 0.736 5 10 0.251 4 8 0.229 1 6 0.603 5.5 10 0.218 4.5 8 0.192 1.5 6 0.482 6 10 0.185 5 8 0.160 2 6 0.390 6.5 10 0.161 5.5 8 0.128 2.5 6 0.307 7 10 0.138 6 8 0.102 3 6 0.245 7.5 10 0.117 6.5 8 0.083 3.5 6 0.192 8 10 0.101 7 8 0.067 4 6 0.143 8.5 10 0.086 7.5 8 0.055 4.5 6 0.108 9 10 0.071 8 8 0.046 5 6 0.081 9.5 10 0.063 0 10 1.000 5.5 6 0.059 10 10 0.056 0.5 10 0.824 6 6 0.044 10.5 10 0.051 1 10 0.718 0 8 1.000 11 10 0.045 1.5 10 0.618 0.5 8 0.765 2 10 0.542 1 8 0.649 2.5 10 0.474 1.5 8 0.541 3 10 0.424 2 8 0.456 3.5 10 0.372 2.5 8 0.382

Let us fit data into the above equation: Program 6.7-3 Nelder-Mead one independent variable nonlinear least square curve fitting, apple slice drying problem
import java.io.*; import javax.swing.*; class fxa extends f_xj { double xi[][]; // m independent variable data set double yi[]; //dependent variable data set double a[]; //fit function coefficient set int nn;//number of data int m; //number of idependent variables xi double xmax; public fxa(String filename,double ia[])

{ //read the data to curvefit //last column is treated as independent data set //get the data file and initial fit coefficient when class is defined int n=ia.length; a=new double[n]; seta(ia); xmax=-1.0e99; double b[][]=Text.readDoubleT(filename); m=b.length-1; yi=b[m]; nn=yi.length; //System.out.println("m="+m); xi=new double[m][nn]; for(int i=0;i<nn;i++) { xi[0][i]=b[0][i];if(xi[0][i]>xmax) {xmax=xi[0][i];} xi[1][i]=b[1][i]; yi[i]=b[m][i]; } //System.out.println("b=\n"+Matrix.toString(Matrix.T(b))); System.out.println("xi=\n"+Matrix.toString(xi)); System.out.println("yi=\n"+Matrix.toString(yi)); nn=yi.length; a=ia; } public void seta(double ia[]) { //assign new fit coefficient set for(int ii=0;ii<nn;ii++) a[ii]=ia[ii]; } public double[] geta() { // return fit coefficient set return a; } double Ps(double x[],double ai[]) { // sample specific function m dimensional independent variable // independent variables : x[0],x[1]...x[m-1] // x[0] t // x[1] kalnlk // ai[0] a // a=ai; double t=x[0]; double h=x[1]; double a=ai[0]+ai[1]*h; double k=ai[2]+ai[3]*h; double pi=a*Math.exp(-k*t); return pi; //kPa } public double func(double ai[]) { double ff=0;

double w; double yy; double xx[]=new double[xi.length]; for(int i=0;i<nn;i++) { for(int j=0;j<m;j++) {xx[j]=xi[j][i];} w=Ps(xx,ai); yy=yi[i]; //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); w-=yy; ff+=w*w; //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); } return ff; } } public class NA126 {//2. Nelder ve Mead method //________________________________________________________________ public static double[] nelder(f_xj fnelder,double a[],double da[],int maxiteration,double tolerance,int printlist) { double x[][]=new double[a.length+1][a.length]; for(int i=0;i<x.length;i++) {for(int j=0;j<x[0].length;j++) {if(i==j){x[i][j]=a[j]+da[j];} else {x[i][j]=a[j]; } } } // Nelder mead multidimensional simplex minimization method // Reference : Nelder & Mead 1965 Computer J, v.7, 308-313. int NDIMS = x.length-1; int NPTS = x.length; int FUNC = NDIMS; int ncalls = 0; ////// balang simplexini olutur ////////////////// double p[][]=new double[NPTS][NPTS]; // [row][col] = [whichvx][coord,FUNC] double z[]=new double[NDIMS]; double best = 1E99; //////////////// iilk fonksiyon deerlerini hesapla //////////////// for (int i=0; i<NPTS; i++) { for (int j=0; j<NDIMS; j++) {p[i][j] = x[i][j];} p[i][NDIMS] = fnelder.func(p[i]); } int iter=0; for (iter=1; iter<maxiteration; iter++) { /////////// lo, nhi, hi noktalarn tanmla ////////////// int ilo=0, ihi=0, inhi = -1; // -1 means missing double flo = p[0][FUNC]; double fhi = flo; double pavg,sterr; for (int i=1; i<NPTS; i++) { if (p[i][FUNC] < flo) {flo=p[i][FUNC]; ilo=i;} if (p[i][FUNC] > fhi)

{fhi=p[i][FUNC]; ihi=i;} } double fnhi = flo; inhi = ilo; for (int i=0; i<NPTS; i++) if ((i != ihi) && (p[i][FUNC] > fnhi)) {fnhi=p[i][FUNC]; inhi=i;} ////////// k kriteri ////////////// if ((iter % 4*NDIMS) == 0) { //yi nin standart hata kriteri set deerinden (tolerance) // kk olmal // ortalama deeri hesapla (en byk deer de dahil olmak zere) pavg=0; for(int i=0;i<NPTS;i++) pavg+=p[i][FUNC]; pavg/=NPTS; double tot=0; if(printlist!=0) { System.out.print(iter); for (int j=0; j<=NDIMS; j++) { System.out.print(p[ilo][j]+" ");} System.out.println(""); } for(int i=0;i<NPTS;i++) { tot=(p[i][FUNC]-pavg)*(p[i][FUNC]-pavg);} sterr=Math.sqrt(tot/NPTS); //if(sterr < tolerance) { for (int j=0; j<NDIMS; j++) { z[j]=p[ilo][j];} //break; } best = p[ilo][FUNC]; } ///// ave[] vektorn en byk deeri hari tutarak hesapla ////// double ave[] = new double[NDIMS]; for (int j=0; j<NDIMS; j++) ave[j] = 0; for (int i=0; i<NPTS; i++) if (i != ihi) for (int j=0; j<NDIMS; j++) ave[j] += p[i][j]; for (int j=0; j<NDIMS; j++) ave[j] /= (NPTS-1);

///////// reflect //////////////// double r[] = new double[NDIMS]; for (int j=0; j<NDIMS; j++) r[j] = 2*ave[j] - p[ihi][j]; double fr = fnelder.func(r); if ((flo <= fr) && (fr < fnhi)) // in zone: accept { for (int j=0; j<NDIMS; j++)

p[ihi][j] = r[j]; p[ihi][FUNC] = fr; continue; } if (fr < flo) //// genilet, else kabul et { double e[] = new double[NDIMS]; for (int j=0; j<NDIMS; j++) e[j] = 3*ave[j] - 2*p[ihi][j]; double fe = fnelder.func(e); if (fe < fr) { for (int j=0; j<NDIMS; j++) p[ihi][j] = e[j]; p[ihi][FUNC] = fe; continue; } else { for (int j=0; j<NDIMS; j++) p[ihi][j] = r[j]; p[ihi][FUNC] = fr; continue; } } ///////////// daralt: if (fr < fhi) { double c[] = new double[NDIMS]; for (int j=0; j<NDIMS; j++) c[j] = 1.5*ave[j] - 0.5*p[ihi][j]; double fc = fnelder.func(c); if (fc <= fr) { for (int j=0; j<NDIMS; j++) p[ihi][j] = c[j]; p[ihi][FUNC] = fc; continue; } else /////// daralt { for (int i=0; i<NPTS; i++) if (i != ilo) { for (int j=0; j<NDIMS; j++) p[i][j] = 0.5*p[ilo][j] + 0.5*p[i][j]; p[i][FUNC] = fnelder.func(p[i]); } continue; } } if (fr >= fhi) /// { double cc[] = new double[NDIMS];

for (int j=0; j<NDIMS; j++) cc[j] = 0.5*ave[j] + 0.5*p[ihi][j]; double fcc = fnelder.func(cc); if (fcc < fhi) { for (int j=0; j<NDIMS; j++) p[ihi][j] = cc[j]; p[ihi][FUNC] = fcc; continue; } else ///////// { for (int i=0; i<NPTS; i++) if (i != ilo) { for (int j=0; j<NDIMS; j++) p[i][j] = 0.5*p[ilo][j] + 0.5*p[i][j]; p[i][FUNC] = fnelder.func(p[i]); } } } } return z; } public static double[] nelder(f_xj fnelder,double a[],double da[],double tolerance) {return nelder(fnelder,a,da,300,tolerance,0);} public static double[] nelder(f_xj fnelder,double a[],double da[]) {return nelder(fnelder,a,da,500,1.0e-20,0);} public static double[] nelder(f_xj fnelder,double x0[]) { double [] dx0=new double[x0.length]; for(int i=0;i<x0.length;i++) dx0[i]=0.2*x0[i]; return nelder(fnelder,x0,dx0,500,1.0e-15,0); } public static double[] nelder(f_xj fnelder,double x0[],double tolerance) { double [] dx0=new double[x0.length]; for(int i=0;i<x0.length;i++) dx0[i]=0.2*x0[i]; return nelder(fnelder,x0,dx0,tolerance); } public static double[] nelder(f_xj fnelder,double x0[],int maxiteration,double tolerance) { double [] dx0=new double[x0.length]; for(int i=0;i<x0.length;i++) dx0[i]=0.2*x0[i]; return nelder(fnelder,x0,dx0,maxiteration,tolerance,0); } public static void main(String args[]) throws IOException { //String in_name=JOptionPane.showInputDialog(" veri dosyasnn isminigiriniz : "); String in_name="kalinlik1.txt"; double a[]; double x[][],y[]; //uydurulan veri double xu1[][],yu1[]; //uydurulan veri double xu2[][],yu2[];

double xu3[][],yu3[]; a=new double[4]; a[0] = 1.0; a[1] = 1.0; a[2] = 1.0; a[3] = 1.0; fxa f=new fxa(in_name,a); int i=0,j=0; double p[]=nelder(f,a); f=new fxa(in_name,p); double tmax=f.xmax; double dt=0.1; //ZAMAN ARALII int n=(int)(tmax/dt); System.out.println("length="+f.nn+"tmax="+tmax+"n="+n); xu1=new double[f.m][n]; yu1=new double[n]; xu2=new double[f.m][n]; yu2=new double[n]; xu3=new double[f.m][n]; yu3=new double[n]; double z[]=new double[2]; for(i=0;i<n;i++) { xu1[0][i]=dt*i; xu1[1][i]=6; z[0]=xu1[0][i]; z[1]=xu1[1][i]; yu1[i]=f.Ps(z,p); System.out.println("x0="+xu1[0][i]+"x1="+xu1[1][i]+"y="+yu1[i]); } for(i=0;i<n;i++) { xu2[0][i]=dt*i; xu2[1][i]=8; z[0]=xu2[0][i]; z[1]=xu2[1][i]; yu2[i]=f.Ps(z,p); System.out.println("x0="+xu2[0][i]+"x1="+xu2[1][i]+"y="+yu2[i]); } for(i=0;i<n;i++) { xu3[0][i]=dt*i; xu3[1][i]=10; z[0]=xu3[0][i]; z[1]=xu3[1][i]; yu3[i]=f.Ps(z,p); System.out.println("x0="+xu3[0][i]+"x1="+xu3[1][i]+"y="+yu3[i]); } Plot pp=new Plot(f.xi[0],f.yi); pp.setPlabel("Moistre Ratio - time plot with thickness parameter"); pp.setXlabel("t, time hour"); pp.setYlabel("MR Mositure Ratio "); pp.setPlotType(0,24); pp.addData(xu1[0],yu1); pp.setPlotType(1,0); pp.addData(xu2[0],yu2); pp.setPlotType(1,1); pp.addData(xu3[0],yu3);

pp.setPlotType(1,2); //pp.addData(x,y); //pp.setPlotType(1,24);// gen olarak iz pp.plot(); String s1=" Optimization value : \n"+Matrix.toStringT(p)+"\n"; s1+="Value of the original function = "+f.func(a)+"\n"; s1+="Function vlue = "+f.func(p); s1+="standard deviation= "+Math.sqrt(f.func(p)/(f.nn-1)); String s2="Nelder-Mead non linear least square curve fitting : "; System.out.println(s1); } }

Resulting curve : a0= 0.990155880325089 a1= -0.002517036051923 0.765464843543500 a2= a3= -0.048774936274541 veri ve erinin grafik gsterimi : 6.8 USAGE OF PARAMETRIC VARIABLES IN CURVE FITTING x and y values are given in Table 6.8-1. If it is tried to curve fit and equation to this data, it will fail no matter which method will be used. For the same x values two different y values are defined. In cases like this in order to curve fit the data an artificial parameter is added and x and y curvefit seperately with this artificial parametric variable.

Table 6.8-1 input data a.txt i 0 T 0 X -1 Y 0

1 1 0 1

2 2 1 0.5

3 3 0 0

4 4 1 -1

Program 6.8-1 Cubis spline interpolation with indirect spline interpolation


import java.io.*; import javax.swing.*; class NA127 { public static double [] thomas(double a[][],double r[]) { // int n=a.length; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double x[]=new double[n]; for(int i=0;i<n;i++) {f[i]=a[i][i];} for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];} for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];} for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; } public static double [] thomas(double f[],double e[],double g[],double r[]) { int n=f.length; double x[]=new double[n]; for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; }

public static double [][] cubic_spline(double xi[],double yi[],double c0,double cn)

{ int n=xi.length; double h[]=new double[n]; double w[]=new double[n]; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double d[]=new double[n]; double x[]=new double[n]; double S[][]=new double[4][n]; int k; for(k=0;k<(n-1);k++) {h[k]=xi[k+1]-xi[k]; w[k]=(yi[k+1]-yi[k])/h[k]; } d[0]=c0; d[n-1]=cn; for(k=1;k<(n-1);k++) {d[k]=6.0*(w[k]-w[k-1]);} f[0]=1.0; f[n-1]=1.0; g[0]=0.0; g[n-1]=0.0; e[0]=0.0; e[n-1]=0.0; for(k=1;k<(n-1);k++) {f[k]=2.0*(h[k]+h[k-1]);e[k]=h[k-1];g[k]=h[k];} S[2]=thomas(f,e,g,d); S[3]=xi; for(k=0;k<(n-1);k++) {S[0][k]=(6.*yi[k+1]-h[k]*h[k]*S[2][k+1])/(6.0*h[k]); S[1][k]=(6.*yi[k]-h[k]*h[k]*S[2][k])/(6.0*h[k]); } return S; } public static double funcSpline(double S[][],double x) { int n=S[0].length; double xx1=0; double xx2=0; double y=0; double hk=0; for(int k=0;k<(n-1);k++) {if(S[3][k]<=x && x<=S[3][k+1]) {hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); break; } } if(y==0 && S[3][n-2]<=x ) { int k=n-2; hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x);

y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); } return y; } public static double[][] funcSpline(double xi[],double yi[],int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double S[][]=cubic_spline(xi,yi,0,0); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcSpline(S,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]); return z; } public static void main(String args[]) throws IOException { double x[]; double y[]; String s1="a.txt"; JFileChooser fc=new JFileChooser(); if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); } double a[][]=Text.readDoubleT(s1); x=a[0]; y=a[1]; int n=x.length; double t[]=new double [n]; for(int i=0;i<n;i++) t[i]=i; double z1[][]=funcSpline(t,x,20); double z2[][]=funcSpline(t,y,20); System.out.println(" z1\n"+Matrix.toString(z1)); System.out.println(" z2\n"+Matrix.toString(z1)); Plot pp=new Plot(z1[1],z2[1]); pp.setPlabel("Parametric curve fitting by using cubic spline method"); pp.setXlabel("x"); pp.setYlabel("y"); pp.setPlotType(0,0); pp.addData(z1[1],z2[1]); pp.plot(); } }

6.9 MOVNG LEAST SQUARES Polynomial least square equation was defined as

a
k =0

( m) j

x
i =1

j+k
i

n k = wi f i x i i =1

k = 0,....., m and this equation was solved for weight function w=1. If

it is desired that the accuracy of the equation to be improved, it will be seen that increasing coeefficients will not always give beter accuracy. To see this effect assume the following data and make 3rd 4th and 5th degree corve fitting into the data. x f 1.47 2.09 1.83 1.92 3.02 2.19 3.56 2.64 5.86 3.19 8.75 3.13 9.45 3.61

Program 6.9-1 3,4 ve 5th degree polynomial least square curve fitting
import java.io.*; import javax.swing.*; class NA128 { //polynomial leat square public static double[] pivotlugauss(double a[][],double b[]) { //gauss elimination with partial pivoting int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //pivotlama p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p];

b[p]=b[k]; b[k]=dummy; } for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //back substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static double[] LSTSQR(double xi[],double yi[],int n) { int l=xi.length; int i,j,k; int np1=n+1; double A[][]; A=new double[np1][np1]; double B[]; B=new double[np1]; double X[]; X=new double[np1]; for(i=0;i<n+1;i++) { for(j=0;j<n+1;j++) {if(i==0 && j==0) A[i][j]=l; else for(k=0;k<l;k++) A[i][j] += Math.pow(xi[k],(i+j)); } for(k=0;k<l;k++) { if(i==0) B[i]+= yi[k]; else B[i] += Math.pow(xi[k],i)*yi[k];} } System.out.println(Matrix.toString(A)); System.out.println(Matrix.toStringT(B)); X=pivotlugauss(A,B); //X=B/A; double max=0; for(i=0;i<n+1;i++) if(Math.abs(X[i]) > max) max = Math.abs(X[i]); for(i=0;i<n+1;i++) if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0; return X; } public static double funcLSTSQR(double e[],double x) { // this function calculates the value of // least square curve fitting function

int n=e.length; double ff; if(n!=0.0) { ff=e[n-1]; for(int i=n-2;i>=0;i--) { ff=ff*x+e[i]; } } else ff=0; return ff; } public static double error(double x[],double y[],double e[]) { //calculates absolute square root error of a least square approach double n=x.length; int k; double total=0; for(k=0;k<n;k++) { total+=(y[k]-funcLSTSQR(e,x[k]))*(y[k]-funcLSTSQR(e,x[k])); } total=Math.sqrt(total); return total; } public static double[][] funcLSTSQR(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double E[]=LSTSQR(xi,yi,polinomkatsayisi); System.out.println("coefficients :"+Matrix.toStringT(E)); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) {z[0][k]=xi[i];z[1][k]=funcLSTSQR(E,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcLSTSQR(E,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcLSTSQR(E,z[0][k]); return z; }

public static void main(String args[]) throws IOException { double x[]; double y[]; String s1="a.txt"; //JFileChooser fc=new JFileChooser(); //if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); } double a[][]=Text.readDoubleT(s1);

x=a[0]; y=a[1]; int n=x.length; double t[]=new double [n]; for(int i=0;i<n;i++) t[i]=i; double z1[][]=funcLSTSQR(x,y,3,10); System.out.println("Least Square. z1\n"+Matrix.toString(z1)); System.out.println("Least Square. z2\n"+Matrix.toString(z1)); Plot pp=new Plot(z1[0],z1[1]); pp.addData(x,y); z1=funcLSTSQR(x,y,4,10); pp.addData(z1[0],z1[1]); z1=funcLSTSQR(x,y,5,10); pp.addData(z1[0],z1[1]); pp.setPlabel("Polynomial leat square curve fitting"); pp.setXlabel("x"); pp.setYlabel("y"); pp.setPlotType(0,0); pp.setPlotType(1,18); pp.plot(); } }

In here all the equations given different kind of results. Which one is correct or closer to the data then? It is not always easy to answer. As a different approach to this curve fitting, lets apply a continiously changing weight factor

wi ( x) =
infinity.

1 x xi
k

In this function when x value become equal to xi the value of weight factor will go to

x xi wi (x) . If it is assumed that for x=xi , f=fi and solve the least square fort he remaining points
solution will be existed. But in this type of conditions due to the fact that wi was changing in each region, least square curve fitting coefficients should calculate for each point(region) separetely. This type of least square method is called moving least squares.

Program 6.9-2 Moving least square curve fitting


import java.io.*; import javax.swing.*; class NA129 { //polynomial moving least square public static double[] pivotlugauss(double a[][],double b[]) { //gauss elimination with pivoting int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss eleme int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //pivotlama p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } //gauss elemeyi zme for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //geriye doru yerine koyma x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static double movingLSTSQR(double x,double xi[],double yi[],int n,int kk) {

int l=xi.length; int i,j,k; int np1=n+1; double A[][]; A=new double[np1][np1]; double B[]; B=new double[np1]; double X[]; X=new double[np1]; double w[]=new double[l]; for(i=0;i<l;i++) {if(x==xi[i]) return yi[i];} for(i=0;i<l;i++) {w[i]=1.0/Math.pow(Math.abs(x-xi[i]),kk);} for(i=0;i<np1;i++) { for(j=0;j<np1;j++) {if(i==0 && j==0) A[i][j]=l; else for(k=0;k<l;k++) A[i][j] += w[k]*Math.pow(xi[k],(i+j)); } for(k=0;k<l;k++) { if(i==0) B[i]+= w[k]*yi[k]; else B[i] += w[k]*Math.pow(xi[k],i)*yi[k];} } X=pivotlugauss(A,B); double max=0; for(i=0;i<n+1;i++) if(Math.abs(X[i]) > max) max = Math.abs(X[i]); for(i=0;i<n+1;i++) if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0; return funcEKK(X,x); } public static double funcEKK(double e[],double x) { // this function calculates the value of // least square curve fitting function int n=e.length; double ff; if(n!=0.0) { ff=e[n-1]; for(int i=n-2;i>=0;i--) { ff=ff*x+e[i]; } } else ff=0; return ff; } public static double[][] funcEKK(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi,int kk) { int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) {z[0][k]=xi[i];z[1][k]=movingLSTSQR(z[0][k],xi,yi,polinomkatsayisi,kk);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=movingLSTSQR(z[0][k],xi,yi,polinomkatsayisi,kk);k++;}

} z[0][k]=xi[i];z[1][k]=movingLSTSQR(z[0][k],xi,yi,polinomkatsayisi,kk); return z; }

public static void main(String args[]) throws IOException { double x[]; double y[]; String s1="a.txt"; //JFileChooser fc=new JFileChooser(); //if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); } double a[][]=Text.readDoubleT(s1); x=a[0]; y=a[1]; int n=x.length; double t[]=new double [n]; for(int i=0;i<n;i++) t[i]=i; double z1[][]=funcEKK(x,y,4,10,2); //System.out.println("E.K.K. z1\n"+Matrix.toString(z1)); Plot pp=new Plot(z1[0],z1[1]); pp.setPlabel("moving polynomial least square curve fitting"); pp.setXlabel("x"); pp.setYlabel("y"); pp.setPlotType(0,0); pp.addData(x,y); pp.setPlotType(1,21); pp.plot(); } }

The same data in the previous example will be now evaluated by moving least square of 4th order polynomial With the weight factor wi ( x) =

1 x xi
2

If this data is covered with different degree of polynomials(3rd, 4th and 5th degrees) :

The biggest disadvantage of this method is the requirement of matrix solution for each region seperately. The equations are passing through each point. Therefore, this method is closer to spline interpolation compare to least square interpolations.

6.10 BERNSTEIN POLINOMIALS AND BEZER CURVES Bernstein polynomials are a serial function approximation using the following form in a range of (a,b)
n n Bn ( f ; x) = ( x a)i (b x) n i f ( xi ) i =0 i

xi in the equation can be written for constant point differences xi=a+ih=a+(i/n)(b-a) i=0..n . If the sae function is written in (0,1) range:
n n n Bn ( f ; x) = x i (1 x) n i f (i / n) = f (i / n) Bn ,i ( x) i =0 i i =0

n Bn ,i ( x) = x i (1 x) n i i
When Bernstein polynomials are used for curve fitting approximations, it is seen that convertion rates are very low. Very high number of iterations are required for relatively good approximations. They will pass from starting and end points exactly but for the rest of the points it approximate slowly. But if mid-points are only taken as a reference. A practical method for drawing complicated shapes is obtained. This type of Bernstein polynomial applications have a special name: Bezier curves. In Bezier curves uses parametric variables. If for any (xi , yi) set fro the parametric variable t

x(t ) = xi Bn ,i (t )
i =0 n

y (t ) = yi Bn ,i (t )
i =0

Let us use the following Example data to create th Bezier curve: -1.0000000000 0.0000000000

-0.6666666667 -0.3333333333 0.3333333333 0.6666666667 1.0000000000 0.6666666667 0.3333333333 -0.3333333333 -0.6666666667 -1.0000000000

0.6666666667 0.6666666667 -0.6666666667 -0.6666666667 0.0000000000 0.6666666667 0.6666666667 -0.6666666667 -0.6666666667 0.0000000000

Program 6.10-1 Bezier curve generator


import java.io.*; import javax.swing.*; class NA130 { //Bernstein polinomials and Bezier curves public static double[] B(int n,double t) { // 0<=t<=1 double b[]=new double[n+1]; b[0]=1.0; for(int i=1;i<=n;i++) {b[i]=t*b[i-1]; for(int j=i-1;j>=1;j--) {b[j]=(1.0-t)*b[j]+t*b[j-1];} b[0]=(1.0-t)*b[0]; } return b; } public static double funcB(double a[],double t) { double xx=0; int n=a.length-1; double b[]=B(n,t); for(int i=0;i<=n;i++) {xx+=a[i]*b[i];} return xx; } public static double[][] Bezier(double xi[],double yi[],int noktasayisi) { int n=xi.length; double z[][]=new double[2][noktasayisi]; double dt=1.0/(double)noktasayisi; double t=0; int k=0; int i; for(i=0;i<noktasayisi;i++) {z[0][i]=funcB(xi,t);z[1][i]=funcB(yi,t); t=t+dt; } return z; }

public static void main(String args[]) throws IOException { double x[]; double y[]; String s1="a.txt"; //JFileChooser fc=new JFileChooser(); //if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); } double a[][]=Text.readDoubleT(s1); x=a[0]; y=a[1]; int n=x.length; //for(double t=0;t<=1.0;t+=0.1) {System.out.println(funcB(4,x,t)+" "+funcB(4,y,t));} double z1[][]=Bezier(x,y,100); //System.out.println("E.K.K. z1\n"+Matrix.toString(Matrix.T(z1))); Plot pp=new Plot(z1[0],z1[1]); pp.setPlabel("Bezier curve fitting"); pp.setXlabel("x"); pp.setYlabel("y"); pp.setPlotType(0,0); pp.addData(x,y); pp.addData(x,y); pp.setPlotType(2,18); pp.plot(); } }

By changing reference points very complicated shapes can be drawn For example 00 35 52 30 52 35 00

00 35 52 30 5 -2 3 -5 00

6.11 PROBLEMS PROBLEM 1 Fit a linear line by using polynomial least square curve fitting X 2 3 4 7 8 Y 9 6 5 10 9

9 11

5 2

5 3

PROBLEM 2 For R22 refrigerant liquid viscosity values are required. The values are defined as the table below. R22 soutucu akkan iin sv vizkozite deerleri gerekmektedir. Fit a curve to input viscosity into computer as a function. Calculate the amount of error.
-101.1111111 -95.55555556 -90 -84.44444444 -78.88888889 -73.33333333 -70.55555556 -67.77777778 -65 -62.22222222 -59.44444444 -56.66666667 -53.88888889 -51.11111111 -48.33333333 -45.55555556 -42.77777778 -40.81111111 -40 8.6519780084E-04 7.7466826506E-04 6.9943367368E-04 6.3536025795E-04 5.8079451036E-04 5.3325616965E-04 5.1176057211E-04 4.9150510520E-04 4.7290314580E-04 4.5512794014E-04 4.3817948824E-04 4.2205779009E-04 4.0717622256E-04 3.9312140879E-04 3.7947997189E-04 3.6666528874E-04 3.5426398247E-04 3.4599644496E-04 3.4268942995E-04 -28.88888889 -26.11111111 -23.33333333 -20.55555556 -17.77777778 -15 -12.22222222 -9.444444444 -6.666666667 -3.888888889 -1.111111111 1.666666667 4.444444444 7.222222222 10 12.77777778 15.55555556 18.33333333 21.11111111 3.0093836551E-04 2.9143069737E-04 2.8233640610E-04 2.7365549171E-04 2.6538795420E-04 2.5712041668E-04 2.4926625605E-04 2.4182547228E-04 2.3479806540E-04 2.2777065851E-04 2.2074325162E-04 2.1412922161E-04 2.0792856848E-04 2.0172791534E-04 1.9552726220E-04 1.8973998595E-04 1.8395270969E-04 1.7857881030E-04 1.7320491092E-04 32.22222222 35 37.77777778 40.55555556 43.33333333 46.11111111 48.88888889 51.66666667 54.44444444 57.22222222 60 62.77777778 65.55555556 68.33333333 71.11111111 73.88888889 76.66666667 79.44444444 82.22222222 1.5253606713E-04 1.4798892150E-04 1.4302839899E-04 1.3848125336E-04 1.3393410773E-04 1.2938696209E-04 1.2483981646E-04 1.2070604770E-04 1.1615890207E-04 1.1202513331E-04 1.0747798768E-04 1.0334421892E-04 9.9210450167E-05 9.5076681410E-05 9.0529535778E-05 8.6395767021E-05 8.1848621388E-05 7.7714852631E-05 7.2754330123E-05

-37.22222222 -34.44444444 -31.66666667

3.3152825431E-04 3.2078045554E-04 3.1044603365E-04

23.88888889 26.66666667 29.44444444

1.6783101153E-04 1.6287048902E-04 1.5749658964E-04

85 87.77777778 90.55555556 93.33333333

6.8207184490E-05 6.2833285106E-05 5.7046008846E-05 5.0018601959E-05

PROBLEM 3 Saturated vapor pressure for refrigerant R134a is required. These values are given below as table format. Fit a curve to this values that will minimize error.
-99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76 -75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60 -59 -58 -57 -56 -55 -54 -53 -52 -51 0.01 0.01 0.01 0.01 0.01 0.02 0.02 0.02 0.03 0.03 0.04 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.13 0.14 0.16 0.18 0.2 0.23 0.25 0.28 0.31 0.34 0.37 0.41 0.45 0.49 0.54 0.59 0.64 0.7 0.76 0.83 0.9 0.97 1.05 1.14 1.23 1.33 1.43 1.54 1.66 1.79 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 2.06 2.21 2.37 2.54 2.72 2.91 3.11 3.32 3.54 3.78 4.03 4.29 4.57 4.86 5.17 5.5 5.84 6.2 6.58 6.98 7.4 7.84 8.3 8.78 9.29 9.82 10.38 10.96 11.58 12.22 12.88 13.58 14.32 15.08 15.88 16.71 17.58 18.48 19.43 20.41 21.44 22.5 23.61 24.77 25.97 27.22 28.52 29.87 31.27 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 32.73 34.24 35.81 37.43 39.12 40.86 42.67 44.54 46.48 48.49 50.57 52.72 54.94 57.23 59.61 62.06 64.59 67.2 69.9 72.68 75.55 78.52 81.57 84.71 87.95 91.29 94.73 98.27 101.92 105.66 109.52 113.49 117.57 121.76 126.07 130.49 135.04 139.71 144.51 149.43 154.48 159.66 164.98 170.43 176.03 181.76 187.63 193.65 199.82 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 212.61 219.24 226.02 232.96 240.07 247.33 254.77 262.38 270.15 278.1 286.23 294.54 303.03 311.7 320.57 329.62 338.86 348.3 357.93 367.77 377.81 388.05 398.5 409.17 420.04 431.13 442.45 453.98 465.74 477.72 489.93 502.38 515.06 527.98 541.15 554.55 568.21 582.11 596.27 610.68 625.35 640.28 655.47 670.94 686.67 702.68 718.96 735.53 752.38 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 786.93 804.64 822.65 840.96 859.56 878.47 897.69 917.22 937.07 957.23 977.71 998.52 1019.66 1041.12 1062.92 1085.05 1107.52 1130.35 1153.52 1177.04 1200.91 1225.15 1249.75 1274.71 1300.04 1325.74 1351.82 1378.28 1405.13 1432.36 1459.98 1488 1516.41 1545.23 1574.46 1604.1 1634.15 1664.62 1695.52 1726.84 1758.6 1790.79 1823.42 1856.5 1890.03 1924.01 1958.46 1993.37 2028.74 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 2100.93 2137.75 2175.06 2212.87 2251.19 2290.01 2329.35 2369.21 2409.61 2450.54 2492.01 2534.04 2576.63 2619.8 2663.54 2707.88 2752.81 2798.36 2844.54 2891.36 2938.83 2986.97 3035.81 3085.29 3135.59 3186.64 3238.47 3291.12 3344.62 3399.02 3454.38 3510.78 3568.29 3627.05

-50

1.92

49

206.14

99

769.51

149

2064.6

PROBLEM 4 Fit a parametric curve to the values given in the table P4


Table P.4 Girdi verisi 0 T 0 X -1 Y 0 1 1 0 1 2 2 1 0.5 3 3 0 0 4 4 1 -1

PROBLEM 5 X y 0 -2.20 0.1 -1.90 0.2 0 0.3 -0.1 0.4 -0.01 0.5 -0.3 0.6 -0.2 0.7 -0.09 0.8 0.4 0.9 -0.1 1.0 -1.1 1.1 -1.3 1.2 -2 1.3 -1.8 1.4 -0.02 1.5 1 Data in the above table is given a) Fit a polynomial leat square curve b) Use interpolation polynomials to fit the data PROBLEM 6 X y 0 0 1 1 2 4 3 8 Fit cubic spline to the data. PROBLEM 7 X 0 1 y 0 1

2 0 1 -1 Fit cubic spline to the data. PROBLEM 8 Ethylene gylicol-water mixture density values are given in the table below for different mixture percentages. By using formula (x,t)= a0+a1*x+a2*t+a3*x2+a4*t2+a5*xt+a6*x3+a7*t3+a8*x2t +a9*xt2+a10*x4+a11*t4+a12*x2t2+a13x3t+a14*xt3
t -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 0% x %volume ratio ethylene gylicol concentration 10% 20% 30% 40% 50% 60% 1090 1089 1088 1102 1072 1087 1101 1071 1086 1100 1054 1070 1084 1098 1037 1053 1068 1083 1096 1019 1036 1052 1067 1081 1095 1018 1034 1050 1065 1079 1093 1016 1033 1049 1063 1077 1091 1015 1031 1047 1062 1075 1089 1013 1030 1045 1060 1073 1086 1012 1028 1043 1058 1071 1084 1010 1026 1041 1055 1069 1081 1008 1024 1039 1053 1066 1079 1006 1022 1037 1051 1064 1076 1004 1020 1034 1048 1061 1073 1002 1017 1032 1045 1058 1070 999.2 1015 1029 1043 1055 1067 996.7 1012 1026 1040 1052 1064 994.1 1009 1023 1037 1049 1060 991.4 1006 1020 1033 1045 1057 988.5 1003 1017 1030 1042 1053 985.5 1000 1014 1027 1038 1049 982.4 997 1011 1023 1035 1046 979.2 993.6 1007 1019 1031 1042 975.8 990.1 1003 1016 1027 1038 972.3 986.5 999.7 1012 1023 1033 968.7 982.8 995.8 1008 1019 1029 965 978.9 991.8 1004 1014 1025 961.2 974.9 987.7 999.3 1010 1020 957.2 970.8 983.4 994.9 1005 1015 953.1 966.6 979.1 990.4 1001 1010 70% 1105 1104 1116 1115 1113 1111 1109 1108 1105 1103 1101 1098 1096 1093 1090 1088 1084 1081 1078 1075 1071 1067 1064 1060 1056 1052 1047 1043 1038 1034 1029 1024 1019 80% 1119 1117 1129 1128 1126 1124 1122 1120 1118 1115 1113 1110 1107 1105 1102 1098 1095 1092 1088 1085 1081 1077 1073 1069 1065 1061 1056 1052 1047 1042 1038 1032 1027 90% 1132 1131 1142 1140 1138 1136 1134 1132 1129 1127 1124 1121 1118 1115 1112 1109 1106 1102 1098 1095 1091 1087 1083 1078 1074 1070 1065 1060 1055 1050 1045 1040 1035

999.8 999.9 999.7 999.1 998.2 997 995.6 994 992.2 990.2 988 985.7 983.2 980.5 977.7 974.8 971.8 968.6 965.3 961.9 958.3 954.7 950.9 947.1 943.1 939

PROBLEM 9 Liquid flows through a sphere. Friction drag coefficient CD is given as a function of Reynolds number. Fit a cubic spline interpolation polynomial to the data. Find CD values for Re = 5, 50, 500 and 5000 . Note: Logarithmic scale can be used. Re CD 0 .2 103 2 13.9 20 2.72 200 0.800 2000 0.401 20 000 0.433

PROBLEM 10 Solve problem 9 by using 4 point Lagrange and Newton interpolation formula

PROBLEM 11 The depth and profile of a channel is given below x, m 0 2 4 6 8 H, m 0 1.8 2 4 4 U m/s 0 0.03 0.045 0.055 0.065
0 0 5 10 15 20 25

10 6 0.12

12 4 0.07

14 3.6 0.06

16 3.4 0.05

18 2.8 0.04

20 0 0

-1

-2

derinlik m

-3 Seri 2 -4

-5

-6

-7 sol kydan uzaklk m

Finf H(x) and U(x) cunve fitting functions by using a) Cubic spline interpolation formula b) Polynomial least square c) Lagrange interpolation formula PROBLEM 12 Temperature distribution of a solid substance is given as
Time , t, s Temperature T C 0 90 5 55 10 40 15 28 20 24 25 22 30 21.5 35 20.6

Find the temperature distribution at t=13 sn and t=23 sn t=33 sn, t=43sn ve t=53 sn deki scaklk dalmn bulunuz. PROBLEM 13 A hard shape coordinates are given below, curvefit data to these coordintes
Xi 241 258 283 307 322 333 334 Yi 127 107 96 97 106 120 138 Xi 331 323 312 298 283 267 241 yi 157 178 197 217 232 246 258 Xi 215 199 184 170 159 151 148 yi 246 232 217 197 178 157 138 xi 149 160 175 199 224 241 Yi 120 106 97 96 107 127

Note: the same values are repeated.

PROBLEM 14 Distance required to stop a car is function of velocty. This relation is given in the table below

Velocity (km/h) Stopping distance (m)

24 4.8

32 6.0

40 10

48 12

If the vehicle travels with a velocity of 38 km/h, calculate stopping distance by using Newton interpolation formula. PROBLEM 15 Thermal conductivity coefficient as a function of temperature is given in the table below.
Scaklk T . [K]
.

275 0.574

290 0.598

305 0.620

320 0.640

340 0.660

360 0.674

Thermal conductivity coefficient k. [W/(m K)]

a-) Curve fit a quadratic polynomial to data

k = AT2 +BT+C
b-) Estimate the thermal conductivity coefficient at 300 K PROBLEM 16 For Etylenegylicol Prandtl (Pr) is given as a function of temperature in the below table. Derve a polynomial least square curve fitting and calculate Pr number for T= 42 C Temperature (K) 280 300 320 340 Pr number 400 151 73.5 42.8 PROBLEM 17 Saturated water temperature of water as a function of temperature t oC will be curvefit to the following equation
h f = A + Bt + Ct 2 + Dt 3

Following table is taken from the thermodynamic property table 10 30 50 70 t, oC hf , kJ/kg 41.99 125.66 209.26 292.97 Calculate the coefficients by using the least square curve fitting

90 376.94

PROBLEM 18 According to Hookes law Force applied to a linear spring is F=kx. In this equation k is spring constant, x is the displacement in the spring. Displacement versus Force in a real spring is given by the below table find the k spring constant X, m 0.02 0.04 0.06 0.08 0.1 F, kN 3.1 6.1 9.2 12 15.1 PROBLEM 19 Fit below values into function f(x) = (Ax+B)-2 X -1 0 Y 13.46 3.02

1 0.68

2 0.14

PROBLEM 20 2.7 x 107 W/m3 heat is generated in 5 mm thick plate by a heat souce. Thermal conductivity of the plate is k=25 W/m K. Surfce temperatures of the plate (at x=0 mm) 161.5 C, and (x=5 mm) 166.9 C. Temperature distribution insde the plate is given in the table below. Give o polinomial function for the temperature distribution inside the plate. Use Newton interpolation Formula. Calculate temperature at x=4 mm.

x (mm) T (C)

0 161.5

0.5 166.9

2.5 177.7

4.5 171.22

5.0 166.9

PROBLEM 21 For very low pressure values (P<10 kPa) establish a Formula for the enthalpy for dry steam as h=A+BT where A and B constants , h ,(kJ/kg) enthalpy and T , (oC) is temperature. 2510.6 2519.8 2528.9 2538.1 2547.2 2556.3 h (kJ/kg) o 5 10 15 20 25 30 T ( C)

2565.3 35

PROBLEM 22 In a direct current electrical motor, Moment(F) and electrical current(I) is related with the relation I=A (FB) Experimental measurements of F and I is given in the table below. Calculate A and B coefficients. F (Nm) 100 200 300 400 I (Amper) 8 11.31 13.85 16 Bu ilikiyi en kk kareler yntemi ile belirleyiniz. PROBLEM 23 Derive and equation given the density of dry air (kg/ m3) at one athmosphere pressure by using Lagrange interpolation formula and then calculate the density at 22.5 C. t (C) (kg/ m3) 0 1,2930 20 1,2045 40 1,1267 60 1,0595

PROBLEM 24 For variable z the required function is given as z=ax+by+cxy By using least square method find a,b,c values and the function. z x y 0.1 1 1 -0.9 2.0 -1.8 1 2 3 2 2 1

PROBLEM 25 Saturation pressure of water vapor(steam) changes as a function of temperature. One of the relation used for this rlation is known as Dupperet Formula is

Psaturation

t = A saturation 100

In this equation tsaturation :oC is saturation temperature, Psaturation, kPa is saturation pressure. Find coefficient A.
t, oC P kPa 10 1.2276 30 4.246 60 19.940 90 70.14 110 143.27 140 361.3 180 1002.1 200 1553.8 250 3973 300 8581 340 14586 370 21030

PROBLEM 26 The vibration(x) of a machine as a function of time(t) is measured and results are given as the table below. i 1 2 3 4 0 2 4 6 ti 5,0 3,7 2,7 2,0 xi For the vibration of this machine find an equation is in the form of

x(t ) = ae bt
Where a and b are constants. PROBLEM 27 In order to develop a new machine, shear stress () is required to be measured. In the experiment the plate above is moved with a velocity of 2 m/s while the plate below remained constant. The distance between the plates are (h) 0.025 m . The shear stres is related to viscosity as follows:

= (V / h)
Viskosity () N-s/m2, is function of temperature and viscosity temperature relation is obtained in the laboratory as : T (C) 5 20 30 50 55 2 (N-s/m ) 0,08 0,015 0,009 0,006 0,005 a) By using Lagrange interpolation find the shear stres at 38 b) By using a polynomial least square, obtain an eqaution for sheat stress PROBLEM 28 A pump manufacturer is measured for the new pump the following data between volumetric flow rate(Q) and pump pressure (P) Q (lt/s) 0,5 1,0 1,5 2,0 2,5 3,0 3,5 P (kPa) 275 290 280 255 229 205 178 a) Fit a second degree least square polynomial [ P=f(Q)=aQ2+bQ+c ] b-) Pompa 2,2 lt/s debide su basarken oluacak basnc tahmin ediniz. b) Find the flow rate fort he maximum pressure? PROBLEM 29 Yanma gazlar younluunun (: kg/m3), scaklkla (T:C) deiimi aada verilmitir.

: kg/m3
T:C

0,950 100

0,748 200

0,617 300

0,525 400

Newton enterpolasyon yntemini kullanarak 1den 4 dereceye kadar polinom Denklemlerini bulunuz. 285 Cda yanma gazlarnn younluunu hesaplaynz. PROBLEM 30

An orifice is used to measure volumetric flow rate. Such a device is shown in the figure. Following data is used to design the orifice-meter. A : Orifice cross sectional area. : pipe cross sectional area<. A1 : CcA (shrinked area behind the orifice). A2 Cc is shrinkage coefficient is a function of (A/A1) ratio. Experimental results fort his coefficient is given below: Find a least sqaure crve fiitng equation for [Cc=f(A/A1)] Cc A/A1 0.1 0.62 0.2 0.63 0.3 0.64 0.4 0.66 0.5 0.68 0.6 0.71 0.7 0.76 0.8 0.81 0.9 0.89 1.00 1.00 PROBLEM 31 The life of the cutting tool for a lathe is T(minutes) and cutting speed V(m/minutes). The life test is carried out and the following data is obtained. Cutting speed, V (m/minutes) Life of the cutting tool,T (minutes) 140 80 160 70 180 55 200 50 220 35 If the general relation between cutting speed and life of the cutting tool is: V=(b/T)(1/a) Find the coefficients by using least square curve fitting PROBLEM 32 Fit the data below the following equation by using least square curve fitting y=axb by using least square curve fitting 1 2 3 4 X 1 1 2 4 Y PROBLEM 33 By using data in the following table answer the quaestions below. X f(X) 1 4,75 2 4 3 5,25 5 19,75 6 36

5 4

a) Obtain fourth order polynomial equation by using Newton interpolation formula b) Obtain cubic spline for the second subregion

c) PROBLEM 34 Fit a line to following data by using least square method. x 5 10 15 20 25 30 35 40 y 16 25 32 33 38 36 3 40

45 42

50 42

PROBLEM 35 Kinematic viscosities of oil SAE30 is given in the table below. Scaklk T(C) 5 10 15 20 25 30 35 40 45 2 16 25 32 33 38 36 3 40 42 Vizkozite (m /s) a) Fit the data into =ae(b/T) equation b) Fit the data into = PROBLEM 36 For f ( x) =

50 42

a T equation b 1+ T

20 function in the range of -3<=x<=3 by using points xi=-3,-2,-1,0,1,2,3 1 + 2x2

a) Find cubic spline interpolation formulas b) Find quadratic B-spline formulas PROBLEM 37 In a smooth sphere, the following relation is existed between Reynolds number(Re) and friction coefficient (C) Re 0.1 1 10 100 1000 10000 C 210 30 4 1 0.5 0.4 Find an equation to represent the data properly PROBLEM 38 By using bezier curve construct the letter . PROBLEM 39 By using bezier curve construct the letter . PROBLEM 40 By using two symmetric bezier curve construct the hard shape.

PROBLEM 41
The tuition for 12 units at St. Anford University has been increasing from 1998 to 2008 as shown in the table below:

Year Tuition per year


1998 $21,300 1999 $23,057 2000 $24,441 2001 $25,917 2002 $27,204

2003 $28,564 2004 $29,847 2005 $31,200 2006 $32,994 2007 $34,800 2008 $36,030 (a) Plot the given data points and intuitively interpolate (draw) a smooth curve through them. (b) Interpolate the data with the Lagrange polynomial. Plot the polynomial and the data points. Use the polynomial to predict the tuition in 2010. This is an extrapolation problem; discuss the utility of Lagrange polynomials for extrapolation. (c) Repeat (b) with a cubic spline interpolation and compare your results.

PROBLEM 42
The concentration of a certain toxin in a system of lakes downwind of an industrial area has been monitored very accurately at intervals from 1993 to 2007 as shown in the table below. It is believed that the concentration has varied smoothly between these data points.

Year Toxin Concentration


1993 12.0 1995 12.7 1997 13.0 1999 15.2 2001 18.2 2003 19.8 2005 24.1 2007 28.1 2009 ??? (a) Interpolate the data with the Lagrange polynomial. Plot the polynomial and the data points. Use the polynomial to predict the condition of the lakes in 2009. Discuss this prediction. (b) Interpolation may also be used to fill holes in the data. Say the data from 1997 and 1999 disappeared. Predict these values using the Lagrange polynomial fitted through the other known data points. (c) Repeat (b) with a cubic spline interpolation. Compare and discuss your results.

7 INTEGRATION AND DERIVATIVES

7.1 NEWTON-COTES FORMULATIONS : TRAPEZOIDAL, BOLE, SMPSON 1/3 VE 3/8 METHODS If function f(x) is given and ,it is desired to calculate integral of the function between integration limits a and b. Definition of the integral is the area nder the function between integration limits. The simplest approximation is to connect f(a) and f(b) points with a linear line and calculate the area of the trapezoidal under the line. This approach is called trapezoidal rule of Newton-Cotes formulations (Area finding formulations)

Figure 7.1-1 Finding integration with trapezoidal rule (yellow area) and error created by the method(blue area) Integration formula :

I = f ( x)dx = (b a)
a

f (b) + f (a) 2

Figure 7.1-1 Finding integration with trapezoidal rule (yellow area) with multiple trapezoidal rule

If total area is divided into smaller areas amount of error will be reduced due to the fact that smaller piece of functions are closer to the line compare to a big piece of function. The same integration Formula for multipart equation will be:

I = a f ( x)dx = (b a)
b

f ( x 0 ) + 2 f ( xi ) + f ( x n )
i =1

n 1

2n

Now assume an example integration function of

I = 0

1 x 2 dx = 1.0

Since the value of the function is known exactly, it will be easy to evaluate the error of numerical function evaluation process. Program 7.1-1 Trapezoidal intergral rule of Newton-Cotes approximation n=1
//====================================================== // Numerical Analysis package in java // Newton-Cotes integral formula n points // trapezoidal rule // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { double func(double x) {return 4.0/Math.PI*Math.sqrt(1.0-x*x);} } class NA51 { public static double newton_cotes2(f_x ff,double a,double b, int n) { //Newton-cotes integral 2 points //trapezoidal rule // ff integrating function // a,b integral limits // n : number of sub division h=(b-a)/n; double h=(b-a)/n; double h1=h; double sum=0; int p,i; double x[]=new double[3]; double f[]=new double[3]; for(p=0;p<n;p++) { {for(i=0;i<2;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}} sum+=h*(f[0]+f[1])/2.0; } return sum; }

public static void main(String args[]) throws IOException { f1 b1=new f1(); double i1= newton_cotes2(b1,0.0,1.0,1); double error=1-i1; System.out.println("integral of class f1 : "+i1+"error = "+error); } }

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA51 integral of class f1 : 0.6366197723675814error = 0.3633802276324186

> Terminated with exit code 0.

In order to see the error happened in the function visiually a graphical output form of the same program is created.

Program 7.1-2 Trapezoidal integral rule of Newton-Cotes approximation with plotting n=1
//====================================================== // Numerical Analysis package in java // Newton-Cotes integral formula 4 points , n=3 // trapezoidal rule // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { double func(double x) {return 4.0/Math.PI*Math.sqrt(1.0-x*x);} }

class NA51A { public static double newton_cotes2(f_x ff,double a,double b, int n) { //Newton-cotes integral 2 points //trapezoidal rule double h=(b-a)/n; double h1=h; double sum=0; int p,i; int nn=2*n; double x[]=new double[2]; double f[]=new double[2]; double xi[]=new double[nn]; double fi[]=new double[nn]; int j=0; for(p=0;p<n;p++) { {for(i=0;i<2;i++) {x[i]=a+i*h1+p*h;xi[j]=x[i];f[i]=ff.func(x[i]);fi[j]=f[i];j++;}}

sum+=h*(f[0]+f[1])/2.0; } Plot pp=new Plot(xi,fi); pp.addFunction(ff,0.0,1.0,2000,2); pp.plot(); return sum; } public static void main(String args[]) throws IOException { f1 b1=new f1(); double i1= newton_cotes2(b1,0.0,1.0,1); double error=1-i1; System.out.println("integral of class f1 : "+i1+"error = "+error); } }

If the integral zone (0 to 1) is divided into two equal regions


---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA51A integral of class f1 : 0.8696387816055828 error = 0.13036121839441717

If the integral zone (0 to 1) is divided into three equal regions


---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA51A integral of class f1 : 0.9286860839333196error = 0.07131391606668036


If the integral zone (0 to 1) is divided into ten equal regions
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA51A integral of class f1 : 0.9881988750835945error = 0.011801124916405503

If the integral zone (0 to 1) is divided into hundred equal regions


---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA51A integral of class f1 : 0.9996257879552262error = 3.742120447738362E-4

As it is seen from the example when trapezoidal steps are increased, approximation is getting better but even with 100 steps there are stil some error in the approximation. The next step in the approach is to use higher degrees of polinomials to connect points of integration. Quadratic (second degree) polinomial rule is called Simpson 1/3 rule. If step size of formulation is taken as h h= ( b a )/n Quadratic polynomial Newton-Cotes formula(Simpson 1/3 rule)
I = f ( x)dx =
a b

(b a) [ f ( x0 ) + 4 f ( x1 ) + f ( x2 )] 6

or

h=

(b a) (b a) = n 2

I = a f ( x)dx =
b

h [ f ( x0 ) + 4 f ( x1 ) + f ( x2 )] (n=2 or three points are 3

given in this Formula) Quadratic polynomial Newton-Cotes formula(Simpson 1/3 rule) with multipoint approach

I = a f ( x)dx = (b a )
b

f ( x 0 ) + 4 f ( xi ) + f ( x j ) + f ( x n )
i =1, 3 , 5 j =2, 4,6

n 1

n2

Cubic polynomial Newton-Cotes formula(Simpson 3/8 rule)

I = a f ( x)dx =
b

(b a) [ f ( x0 ) + 3 f ( x1 ) + 3 f ( x2 ) + f ( x3 )] or 8

h=

(b a) (b a) = n 3

I = a f ( x)dx =
b

3h [ f ( x0 ) + 3 f ( x1 ) + 3 f ( x2 ) + f ( x2 )] 8

(n=3 or four points are required for this formula) Fourth order polynomial Newton-Cotes formula(Bode rule)

I = a f ( x)dx =
b

(b a) [7 f ( x0 ) + 32 f ( x1 ) + 12 f ( x2 ) + 32 f ( x3 ) + 7 f ( x4 )] 90
I = a f ( x)dx =
b

h=

(b a) (b a) = n 4

2h [7 f ( x0 ) + 32 f ( x1 ) + +12 f ( x2 ) + 32 f ( x3 ) + 7 f ( x4 )] 45

(n=4 or five points are used in this Formula) Fifth order polynomial Newton-Cotes formula(Boole rule)
I = a f ( x)dx =
b

(b a) [19 f ( x0 ) + 75 f ( x1 ) + 50 f ( x2 ) + 50 f ( x3 ) + 75 f ( x4 ) + 19 f ( x5 )] 288

or
h= (b a) (b a ) = n 5 I = a f ( x)dx =
b

5h [19 f ( x0 ) + 75 f ( x1 ) + 50 f ( x2 ) + 50 f ( x3 ) + 75 f ( x4 ) + 19 f ( x5 )] 288

(n=5 or six points are used in this Formula) Eight order polynomial Newton-Cotes formula

(b a) [989 f ( x0 ) + 5888 f ( x1 ) 928 f ( x2 ) + 10496 f ( x3 ) 4540 f ( x4 ) 28350 + 10496 f ( x5 ) 928 f ( x6 ) + 5888 f ( x7 ) + 989 f ( x8 )] I = a f ( x)dx =
b

or
h= (b a ) (b a ) 4h b I = a f ( x)dx = [989 f ( x0 ) + 5888 f ( x1 ) 928 f ( x 2 ) + 10496 f ( x3 ) 4540 f ( x4 ) = n 8 14175 + 10496 f ( x5 ) 928 f ( x6 ) + 5888 f ( x7 ) + 989 f ( x8 )]

(n=8 or nine points are used in this Formula) Programs for this approaches are given below: Program 7.1-3 quadratic polynomial Newton - Cotes Formula (Simpson 1/3 rule)
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration ( integral ) // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { double func(double x) {return 4.0/Math.PI*Math.sqrt(1.0-x*x);} } class NA52 { public static double newton_cotes3(f_x ff,double a,double b, int n) { //Newton-cotes integral 3 points //Simpson 1/3 rule double h=(b-a)/n; double h1=h/2.0; double sum=0; int p,i; double x[]=new double[3]; double f[]=new double[3]; for(p=0;p<n;p++) { {for(i=0;i<3;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}} sum+=h*(f[0]+4.0*f[1]+f[2])/6.0; } return sum; } public static void main(String args[]) throws IOException { f1 b1=new f1(); double i1= newton_cotes3(b1,0.0,1.0,1); double error =1-i1; System.out.println("integral of class f1 : "+i1+"error="+error); } }

If the quadratic Newton-Cotes Formula (Simpson 1/3 rule) applied as one time integral zone (0 to 1)
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA52 integral of class f1 : 0.9473117846849166error=0.052688215315083387

> Terminated with exit code 0.

If the quadratic Newton-Cotes Formula (Simpson 1/3 rule) applied as two times integral zone (0 to 1) and divided up into 2
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA52 integral of class f1 : 0.9815388228080559error=0.018461177191944067 > Terminated with exit code 0.

Program 7.1-4 cubic polynomial Newton - Cotes Formula (Simpson 3/8 rule)

//====================================================== // Numerical Analysis package in java // Newton-Cotes integral formula 4 points , n=3 // Simpson 3/8 rule // Dr. Turhan Coban // ===================================================== import java.io.*; class f1 extends f_x { double func(double x) { return 4.0/Math.PI*Math.sqrt(1.0-x*x); } } class NA53 { public static double newton_cotes4(f_x ff,double a,double b, int n) { //Newton-cotes integral 4 points //Simpson 3/8 rule // ff integrating function // a,b integral limits // n : number of sub division h=(b-a)/n; double h=(b-a)/n; double h1=h/3.0; double sum=0; int p,i; double x[]=new double[4]; double f[]=new double[4]; for(p=0;p<n;p++) { {for(i=0;i<4;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}} sum+=h*(f[0]+3.0*f[1]+3.0*f[2]+f[3])/8.0; } return sum; } public static void main(String args[]) throws IOException { f1 b1=new f1(); double i1= newton_cotes4(b1,0.0,1.0,2); double error=1-i1; System.out.println("integral of class f1 : "+i1+"error = "+error); } }

If the cubic Newton-Cotes Formula (Simpson 3/8 rule) applied as one time integral zone (0 to 1)
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA53 integral of class f1 : 0.965194372879037error = 0.03480562712096302

> Terminated with exit code 0.

If the cubic Newton-Cotes Formula (Simpson 3/8 rule) applied as two times integral zone (0 to 1) and divided up into 2
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA53 integral of class f1 : 0.9877870530380952error = 0.01221294696190478 > Terminated with exit code 0.

Program 7.1-5 fourth order polynomial Newton - Cotes Formula (Boole rule)
//====================================================== // Numerical Analysis package in java // Newton-Cotes integral formula 5 points , n=4 // Boole formula // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { double func(double x) { return 4.0/Math.PI*Math.sqrt(1.0-x*x); } }

class NA54 { public static double boole(f_x ff,double a,double b, int n) { return newton_cotes5(ff,a,b,n);} public static double newton_cotes5(f_x ff,double a,double b, int n) { //Newton-cotes integral 5 points //Boole rule // ff integrating function // a,b integral limits // n : number of sub division h=(b-a)/n; double h=(b-a)/n; double h1=h/4.0; double sum=0; int p,i; double x[]=new double[5]; double f[]=new double[5]; for(p=0;p<n;p++) { {for(i=0;i<5;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}} sum+=h*(7.0*f[0]+32.0*f[1]+12.0*f[2]+32.0*f[3]+7.0*f[4])/90.0; } return sum; } public static void main(String args[]) throws IOException {

f1 b1=new f1(); double i1= newton_cotes5(b1,0.0,1.0,1); double i2 = 1.0; double error =i2-i1; System.out.println("integral of class f1 : "+i1+" error="+error); } }

If the fourth order polynomial Newton-Cotes Formula (Boolee rule) applied as one time integral zone (0 to 1)
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA54 integral of class f1 : 0.9838206253495985 error=0.016179374650401535

> Terminated with exit code 0.

If the fourth order polynomial Newton-Cotes Formula (Boole rule) applied as two times integral zone (0 to 1) and divided up into 2
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA54 integral of class f1 : 0.9943031391613758 error=0.005696860838624196 > Terminated with exit code 0.

Program 7.1-6 fifth order polynomial Newton - Cotes Formula


//====================================================== // Numerical Analysis package in java // Newton-Cotes integral formula 6 points, n=5 // // Dr. Turhan Coban // ===================================================== import java.io.*; class f1 extends f_x { double func(double x) { return 4.0/Math.PI*Math.sqrt(1.0-x*x); } } class NA55 { public static double newton_cotes6(f_x ff,double a,double b, int n) { //Newton-cotes integral 6 points, n=5 // ff integrating function // a,b integral limits // n : number of sub division h=(b-a)/n; double h=(b-a)/n;

double h1=h/5.0; double sum=0; int p,i; double x[]=new double[6]; double f[]=new double[6]; for(p=0;p<n;p++) { {for(i=0;i<6;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}} sum+=h*(19.0*f[0]+75.0*f[1]+50.0*f[2]+50.0*f[3]+75.0*f[4]+19.0*f[5])/288.0; } return sum; } public static void main(String args[]) throws IOException { f1 b1=new f1(); double i1= newton_cotes6(b1,0.0,1.0,1); double i2 = 1.0; double error =i2-i1; System.out.println("integral of class f1 : "+i1+" error="+error); } }

If the fifth order polynomial Newton-Cotes Formula applied as one time integral zone (0 to 1)
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA55 integral of class f1 : 0.9872489399864514 error=0.012751060013548643 > Terminated with exit code 0.

If the fifth order polynomial Newton-Cotes Formula applied as two times integral zone (0 to 1) and divided up into 2
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA55 integral of class f1 : 0.9955081956385212 error=0.0044918043614787795 > Terminated with exit code 0.

Program 7.1-7 eight order polynomial Newton - Cotes Formula


//====================================================== // Numerical Analysis package in java // Newton-Cotes integral formula 9 points, n=8 // // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { double func(double x) { return 4.0/Math.PI*Math.sqrt(1.0-x*x); } }

class NA56 { public static double newton_cotes9(f_x ff,double a,double b, int n) { //Newton-cotes integral 9 points, n=8 // ff integrating function // a,b integral limits // n : number of sub division h=(b-a)/n; double h=(b-a)/n; double h1=h/8.0; double sum=0; int p,i; double x[]=new double[9]; double f[]=new double[9]; for(p=0;p<n;p++) { {for(i=0;i<9;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}} sum+=h/28350.0*(989.0*f[0]+5888.0*f[1]-928.0*f[2]+10496.0*f[3]-4540.0*f[4]+ 10496.0*f[5]-928.0*f[6]+5888.0*f[7]+989.0*f[8]); } return sum; } public static void main(String args[]) throws IOException { f1 b1=new f1(); double i1= newton_cotes9(b1,0.0,1.0,1); double i2 = 1.0; double error =i2-i1; System.out.println("integral of class f1 : "+i1+" error="+error); } }

If the eigth order polynomial Newton-Cotes Formula applied as one time integral zone (0 to 1)
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA56 integral of class f1 : 0.9952701383291108 error=0.004729861670889179 > Terminated with exit code 0.

If the eigth order polynomial Newton-Cotes Formula applied as two times integral zone (0 to 1) and divided up into 2
---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA56 integral of class f1 : 0.998330566008359 error=0.0016694339916409895 > Terminated with exit code 0.

The results will be summarised as one table Table 7.1-1 comparison of Newton-Cotes results for differnt degree of integrating polnomials

I = 0

1 x 2 dx = 1.0
Error (Iyaklam-Itam) 0.3633802276324186 0.052688215315083387 0.03480562712096302 0.016179374650401535 0.012751060013548643 0.004729861670889179 0.13036121839441717 0.018461177191944067 0.01221294696190478 0.005696860838624196 0.0044918043614787795 0.0044918043614787795 0.011801124916405503 0.0016378919548054593 0.001085084707006967 5.07836476729473E-4 4.005677486974557E-4 1.4911530329830835E-4

N Integral approximation Region is not divided up calculated as one piece 1 (Trapezoidal) 0.6366197723675814 2 (Simpson 1/3) 0.9473117846849166 3 (Simpson 3/8) 0.965194372879037 4 (Bode) 0.9838206253495985 5 0.9872489399864514 8 0.9952701383291108 Region is divided into 2 equal pieces (0-0.5 ve 0.5-1) 1 (Trapezoidal) 0.8696387816055828 2 (Simpson 1/3) 0.9815388228080559 3 (Simpson 3/8) 0.9877870530380952 4 (Bode) 0.9943031391613758 5 0.9955081956385212 8 0.998330566008359 Region is divided into 10 equal pieces (0-0.1,0.1-0.2,,0.9-1) 1 (Trapezoidal) 0.9881988750835945 2 (Simpson 1/3) 0.9983621080451945 3 (Simpson 3/8) 0.998914915292993 4 (Bode) 0.9994921635232705 5 0.9995994322513025 8 0.9998508846967017

7.2 RCHARDSON EXTRAPOLATION (ROMBERG INTEGRATION) FORMULA Richardson extrapolation formula can be applied to decrease integration error. Richardson extrapolation Formula has the folloving form :

I I (h2 ) +

1 [ I (h2 ) I (h1 )] (h1 / h2 ) 2 1

In this Formula h2 should be smaller than h1 . Ifh2=h1/2 equation becomes:

4 1 I (h2 ) I (h1 )] 3 3

As a starting interpolation any of the Newton-Cotes formulations can be taken. A General form of the above equation can be written as

I j ,k

4 k 1 I j +1,k 1 I j ,k 1 4 k 1 1

If trapezoidal rule is used as starting point of this iterative formulation, it is called Romberg Integration Program 7.2-1 Romberg Integration formula
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions

//Romberg Integration Formula // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { double func(double x) { return 2.0/(1.0+2.0*x*x); } } class NA57 { public static double integral_romberg(f_x ff,double a,double b) { //integral f(x)dx //romberg integration int n=8;//increase n to increase accuracy double R[][]; R=new double[n+1][n+1]; int m=1; double h=b-a; double close=1; double tol=1e-40; int j=0,k=0; double ret=0; R[0][0]=h/2.0*(ff.func(a)+ff.func(b)); do { j++; h/=2.0; trap(ff,a,h,j,m,R); m*=2; for(k=1;k<=j;k++) { R[j][k]=R[j][k-1]+(R[j][k-1]-R[j-1][k-1])/(Math.pow(4,k)-1.0); close=Math.abs(R[j-1][j-1]-R[j][j]); ret=R[j][k]; } }while(close>tol && j<n); return ret; } public static void trap(f_x ff,double a,double h, int j,int m,double R[][]) { //Newton-cotes integral 2 points // successive trapezoidal integration rule // this program will be utilised in romberg integration double sum=0; int p; for(p=1;p<=m;p++) { sum+=ff.func(a+h*(2*p-1)); } R[j][0]=R[j-1][0]/2.0+h*sum; }

public static void main(String args[]) throws IOException { //Romberg integrali f1 b1=new f1(); System.out.println("integral of class f1 : "+ integral_romberg(b1,-3.0,3.0)); }}

Romberg integration f(x)= 2.0/(1.0+2.0*x*x)


---------- Capture Output ---------> "C:\co\java\bin\java.exe" integ7 integral of class f1 : 3.7881660832213675 > Terminated with exit code 0.

If the iterative results of Romberg integration is given as a table


Table 7.2-1 Romberg integration of I =
6.3157895 4.2488038 3.8058295 3.7866756 3.7877768 3.7880687 3.7881417 3.78816 8.2105263 3.5598086 3.6581714 3.7802909 3.7881439 3.788166 3.7881661 3.7881661 3.2497608 3.664729 3.7884322 3.7886674 3.7881675 3.7881661 3.7881661

2 dx 2 31 + 2 x
3

3.6713158 3.7903958 3.7886711 3.7881596 3.7881661 3.7881661 3.7908628 3.7886644 3.7881576 3.7881661 3.7881661 3.7886622 3.7881571 3.7881661 3.7881661 3.7881569 3.7881661 3.7881661 3.7881661 3.7881661 3.7881661

Solving of another integral I = sin( x)dx = 2 by romberg Integration Formula


0

Table 8.2-2 Romberg integrali I = sin( x)dx = 2


0

1.5707963267948900 1.8961188979370300 1.9742316019455500 1.9935703437723300


1.9983933609701400

2.0943951023931900 2.0045597549844200 2.0002691699483800 2.0000165910479300


2.0000010333694100

1.9985707318238300 1.9999831309459800 1.9999997524545700


1.9999999961908400

2.0000055499796700 2.0000000162880400
2.0000000000596700

1.9999999945872900
1.9999999999960300 2.0000000000013200

1.9995983886400300 1.9998996001842000 1.9999749002350500

2.0000000645300000 2.0000000040322500 2.0000000002520000

1.9999999999407000 1.9999999999990700 1.9999999999999800

2.0000000000002200 2.0000000000000000 1.9999999999999900

1.9999999999999900 2.0000000000000000 1.9999999999999900

1.9999999999999900 2.0000000000000000 1.9999999999999900

1.9999999999999900 2.0000000000000000 1.9999999999999900 2.0000000000000000 1.9999999999999900 1.9999999999999900

7.3 AITKEN EXTRAPOLATION WITH NEWTON COTES INTEGRATION FORMULAS Another extrapolation Formula is te Aitken extrapolation. This equation is basically derived from Euler-McLaurin Formula. Aitken interpolation has the following form:

I = I 4n I 4n

(I 4n I 2n )2 ( I 4n I 2n ) ( I 2n I n )

By combining this equation with Simpson 1/3 formulation given above, more accurate results can be obtain Program 7.3-1 SIMPSON 1/3 formula with aitken extrapolation
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Simpson integration with Aitken extrapolation // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { double func(double x) {return x*Math.sqrt(x);} }

class NA58 { public static double newton_cotes3(f_x ff,double a,double b, int n) { //Newton-cotes integral 3 points //Simpson 1/3 rule double h=(b-a)/n; double h1=h/2.0; double sum=0; int p,i; double x[]=new double[3]; double f[]=new double[3]; for(p=0;p<n;p++) { {for(i=0;i<3;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}} sum+=h*(f[0]+4.0*f[1]+f[2])/6.0; } return sum; } public static double Aitken_simpson1_3(f_x ff,double a,double b, int n) { //Aitken interpolation formula double I4n=newton_cotes3(ff,a,b,4*n); double I2n=newton_cotes3(ff,a,b,2*n); double In=newton_cotes3(ff,a,b,n); double I=I4n-(I4n-I2n)*(I4n-I2n)/((I4n-I2n)-(I2n-In)); //System.out.println("I4n="+I4n+"I2n="+I2n+"In="+In+"I="+I); return I; } public static void main(String args[]) throws IOException { f1 b1=new f1(); for(int n=2;n<257;n*=2) System.out.println("n : "+n+"I = "+Aitken_simpson1_3(b1,0.0,1.0,n)); } }

---------- Capture Output ---------- ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA58 n : 2I = 0.39999984745800915 n : 4I = 0.3999999902487645 n : 8I = 0.3999999993877008 n : 16I = 0.3999999999617067 n : 32I = 0.3999999999976067 n : 64I = 0.3999999999998505 n : 128I = 0.3999999999999909 n : 256I = 0.4 > Terminated with exit code 0. If Aitken intrepolation is combined with 8th degree polynomial Formula: Program 7.3-2 Newton_Cotes 8th degree polynomial Formula with Aitken extrapolation //====================================================== // Numerical Analysis package in java // Newton-Cotes integral formula 9 points, n=8 // with Aitken extrapolation formula // Dr. Turhan Coban // ===================================================== import java.io.*; class f1 extends f_x { double func(double x) {return x*Math.sqrt(x);} } class NA59 { public static double newton_cotes9(f_x ff,double a,double b, int n) { //Newton-cotes integral 9 points, n=8 // ff integrating function // a,b integral limits // n : number of sub division h=(b-a)/n; double h=(b-a)/n; double h1=h/8.0; double sum=0; int p,i; double x[]=new double[9]; double f[]=new double[9]; for(p=0;p<n;p++) { {for(i=0;i<9;i++) {x[i]=a+i*h1+p*h;f[i]=ff.func(x[i]);}} sum+=h/28350.0*(989.0*f[0]+5888.0*f[1]-928.0*f[2]+10496.0*f[3]-4540.0*f[4]+ 10496.0*f[5]-928.0*f[6]+5888.0*f[7]+989.0*f[8]); } return sum; } public static double Aitken_newton_cotes9(f_x ff,double a,double b, int n) { //Aitken interpolation formula double I4n=newton_cotes9(ff,a,b,4*n); double I2n=newton_cotes9(ff,a,b,2*n); double In=newton_cotes9(ff,a,b,n); double I=I4n-(I4n-I2n)*(I4n-I2n)/((I4n-I2n)-(I2n-In)); //System.out.println("I4n="+I4n+"I2n="+I2n+"In="+In+"I="+I); return I;

} public static void main(String args[]) throws IOException { f1 b1=new f1(); for(int n=2;n<128;n*=2) System.out.println("n : "+n+"I = "+Aitken_newton_cotes9(b1,0.0,1.0,n)); } } ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA59 n : 2I = 0.39999999999998676 n : 4I = 0.39999999999999986 n : 8I = 0.39999999999999997 n : 16I = 0.39999999999999986 n : 32I = 0.4000000000000001 n : 64I = 0.4 > Terminated with exit code 0.

7.4 QUADRATURES: GAUSS-LEGENDRE, GAUSS - CHEBYCHEV, GAUSS -JACOB, GAUSS -HERMT, GAUSS -LEQUERRE INTEGRATON FORMULAS A problem known from antique times, is to create a rectangle with the same area with a polynomial. IT is called a quadrature problem.

Figure 8.4-1a) a rectangle with the same area with a polynomial

Figure 8.4-1b) A rectangle with the same area with a circle If point x1 and x2 points intersecting the polynomial and rectangle is known, area calculation of the polynomial can be interchange with the area calculation of the rectangle. Or in more general means instead of integration process of summation can be substituted. As a general definition:

I w (1,1) 1 f ( x) w( x)dx ck f ( xk )
1 k =1

Can be written. In this equation w(x) is the weight factor. In the first of a such formulation, GaussLegendre integral formulation, weight factor can be taken as 1.

I w (1,1) 1 f ( x)dx ck f ( xk )
1 k =1

If this integration is solved for a general polynamial equation. Following relation is found:
k 1 x dx 1

1 (1) k +1 k +1
1 (1) k +1 , k +1 0 k 2n For a special case of n=2 equation becomes :

This equation can be written in open form as:


c1 x1k + c 2 x2k + .... + c n xnk =

f(x)=a0+a1x+a2x2+a3x3

I w (1,1)

f ( x)dx c1 f ( x1 ) + c2 f ( x2 ) = (a 0 + a1x + a 2 x 2 + a 3 x 3 )dx


1 2 3 2 3 1 1 1 1

c1 (a 0 + a1 x1 + a 2 x1 + a 3 x1 ) + c2 f (a 0 + a1 x2 + a 2 x2 + a 3 x2 ) = a 0 dx + a1 xdx + a 2 x 2 dx + a 3 x 3dx
1 1 1 1

c1+c2=2 c1 x1+c2 x2=0 c1 x12+c2 x22=2/3 c1 x13+c2 x23=0 If this system of equation is solved roots and coefficients can be found as: Roots: x1, 2 = 1 / 3 and Coefficients : c1,2=1 bulunur. The general solution of this problem can be defined with the Legendre polynomials. Legendre polynomials has a general definition as: (k + 1) Pk +1 ( x) = (2k + 1) xPk ( x) kPk 1 ( x), k 1 and P0(x)=1 ve P1(x)=x olarak The root of Legendre Polynomials is given the roots f the Gauss-Legendre integration Formula. For example if P2(x) value is calculated from the above general form: P2(x)=(3x2-1)/2 . The root of this is equal to x1, 2 = 1 / 3 . After finding the roots coeficients can be calculated as
c1 x12 j + c2 x22 j + .... + cn xn2 j = 2 , 2 j +1 0 j n or

ck =

2(1 xk2 ) [nPn1 ( xk )]2

As a last concept, consider that Gauss-Legendre integration limits are -1 and 1 Region x=[-1,1] can be converted to z=[a,b] by changing the variables
ba b+a z = = x+ x + 2 2

b+a ba = 2 2

In this case, integration Formula will became:

I w ( a, b)

f ( z ) dx c k f ( x k + )
k =1

First 10 Gauss-Legendre integration formulas are given in the table Table 8.4-1
N 2 3 4

Gauss Legendre integration roots and coefficients


xk -0.577350269189625 0.577350269189625 -0.774596669241483 0.000000000000000 0.774596669241483 -0.861136311594052 -0.339981043584856 0.339981043584856 0.861136311594052 -0.906179845938664 -0.538469310105683 0.000000000000000 0.538469310105683 0.906179845938664 -0.932469514203152 -0.661209386466264 -0.238619186083196 0.238619186083196 0.661209386466264 0.932469514203152 -0.949107912342758 -0.741531185599394 -0.405845151377397 0.000000000000000 0.405845151377397 0.741531185599394 0.949107912342758 -0.960289856497536 -0.796666477413626 -0.525532409916329 -0.183434642495649 0.183434642495649 0.525532409916329 0.796666477413626 0.960289856497536 -0.968160239507626 -0.836031107326635 -0.613371432700590 -0.324253423403808 0.000000000000000 0.324253423403808 0.613371432700590 0.836031107326635 0.968160239507626 -0.973906528517171 -0.865063366688984 -0.679409568299024 -0.433395394129247 -0.148874338981631 0.148874338981631 0.433395394129247 0.679409568299024 0.865063366688984 0.973906528517171 ck 1.000000000000000 1.000000000000000 0.555555555555552 0.888888888888888 0.555555555555552 0.347854845137447 0.652145154862546 0.652145154862546 0.347854845137447 0.236926885056181 0.478628670499366 0.568888888888888 0.478628670499366 0.236926885056181 0.171324492379162 0.360761573048138 0.467913934572691 0.467913934572691 0.360761573048138 0.171324492379162 0.129484966168862 0.279705391489276 0.381830050505119 0.417959183673469 0.381830050505119 0.279705391489276 0.129484966168862 0.101228536290369 0.222381034453374 0.313706645877887 0.362683783378362 0.362683783378362 0.313706645877887 0.222381034453374 0.101228536290369 0.081274388361569 0.180648160694857 0.260610696402935 0.312347077040002 0.330239355001259 0.312347077040002 0.260610696402935 0.180648160694857 0.081274388361569 0.066671344308684 0.149451349150580 0.219086362515982 0.269266719309996 0.295524224714752 0.295524224714752 0.269266719309996 0.219086362515982 0.149451349150580 0.066671344308684

10

The first Gauss-Legendre integration Formula will be written to use n=10 formula given in the table. Program 7.4-1 10 Point Gauss-Legendre integration
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions

// Gauss-Legendre 10 points integration formula // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { public double func(double x) { return x*Math.sqrt(x); } } class NA60 { public static double integral(f_x f_xnt,double a,double b) { //integral f(x)dx //integral of a function by using gauss-legendre quadrature //coefficients are pre-calculated for 60 terms for [-1,1] //band then utilises variable transform double r[],c[]; r=new double[10]; c=new double[10]; r[0]=-0.973906528517171; r[1]=-0.865063366688984; r[2]=-0.679409568299024; r[3]=-0.433395394129247; r[4]=-0.148874338981631; r[5]=0.148874338981631; r[6]=0.433395394129247; r[7]=0.679409568299024; r[8]=0.865063366688984; r[9]=0.973906528517171; c[0]=0.066671344308684; c[1]=0.149451349150580; c[2]=0.219086362515982; c[3]=0.269266719309996; c[4]=0.295524224714752; c[5]=0.295524224714752; c[6]=0.269266719309996; c[7]=0.219086362515982; c[8]=0.149451349150580; c[9]=0.066671344308684; int n=10; double z=0; double x,y; double k1=(b-a)/2.0; double k2=(b+a)/2.0; for(int i=0;i<n;i++) { x=k2+k1*r[i]; y=f_xnt.func(x); z+=k1*c[i]*y; }

return z; } public static void main(String args[]) throws IOException { //Gauss-Legendre integration formula with n=10 f1 b1=new f1(); System.out.println("integral of class f1 : "+integral(b1,0.0,1.0)); } } ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA60 integral of class f1 : 0.3999993424028001 > Terminated with exit code 0.

In the second examples coeeficients are drectly calculated from the Legendre polynomials, so storage of any coefficients is unnecessary. Program 7.4-1 variable Point Gauss-Legendre integration
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // desired Legendre polynomials coefficients are internally calculated // Gauss-Legendre n points integration formula // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_x { public double func(double x) { return x*Math.sqrt(x); } } class NA61 { public static double[][] gauss_legendre_coefficients(double x1,double x2,int n) { //calculates legendre gauss-coefficients as coefficients of the integral //for n terms double EPS=3.0e-15; int m,j,i; double z1,z,xm,xl,pp,p3,p2,p1; //double x[]=new double[n]; //double w[]=new double[n]; double a[][]=new double[2][n];//a[0][i]=x[i] a[1][i]=w[i] m=(n+1)/2; xm=0.5*(x2+x1); xl=0.5*(x2-x1); for (i=1;i<=m;i++) {

z=Math.cos(Math.PI*((i-0.25)/(n+0.5))); do { p1=1.0; p2=0.0; for (j=1;j<=n;j++) { p3=p2; p2=p1; p1=((2.0*j-1.0)*z*p2-(j-1.0)*p3)/j; } pp=n*(z*p1-p2)/(z*z-1.0); z1=z; z=z1-p1/pp; } while (Math.abs(z-z1) > EPS); a[0][i-1]=xm-xl*z; a[0][n-i]=xm+xl*z; a[1][i-1]=2.0*xl/((1.0-z*z)*pp*pp); a[1][n-i]=a[1][i-1]; } return a; } public static double integral(f_x f_xnt,double x1,double x2,int n) { //n : number of integral coefficients // this routine first generates gauss legendre coefficients // for [x1,x2] band // then calculates gauss legendre integral double a[][]=new double[2][n]; a=gauss_legendre_coefficients(x1,x2,n); Text.print(Text.T(a)); //System.out.println(Matrix.toString(a)); double z=0; for(int i=0;i<n;i++) { z+=a[1][i]*f_xnt.func(a[0][i]);} return z; } public static void main(String args[]) throws IOException { //Gauss-Legendre integral of any desired point (general) f1 b1=new f1(); System.out.println("integral : "+integral(b1,0.0,1.0,50)); } }

---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA61 integral : 0.39999999974904343 I the Gauss-Legendre integral formulation weight function was taken as 1.

I w (1,1) 1 f ( x) w( x)dx ck f ( xk )
1 k =1

w( x) =

1 1 x2

Gauss-Chebychev integration formula is created. This equation has the form of

I w (1,1)

f ( x) w( x)dx

1 1 x2

f ( x)dx ck f ( xk )
k =1

xk in the equation is the roots of chebcychev polynomials. General definition of the Chbychev polynomials are given as: P0(x)=1 P1(x)=x P2(x)=2x2-1 Pn(x)=2xPn-1(x) Pn-2(x), n=3,4,5.. It should be note that Chebychev polinomials are orthogonal polnomials. The roots of the polynomials are defined as:

1 (k + ) 2 ;k=0,1,2,.n-1 xk = cos n
Coeeficients of the Formula can be calculated as:

ck =

;k=0,1,2,.n-1 olarak hesaplanr.

x=[-1,1] integration range in Gauss-Chebychev Formula can be change to more general z=[a,b] range by using similar formulations used in Gauss-Legendre formulations. As an example program if following integral is to be solved:

I=

ex (1 x 2 )

dx

Program 7.4-2 Gauss-Chebychev integral


import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_x { public double func(double x) { return Math.exp(x); } } public class integ_gauss_chebychev { public static double[][] gauss_chebychev_coefficients(int n) { //Gauss-Chebychev quadratr katsaylarn hesaplar double sgngam=1; double a[][]=new double[2][n]; for(int j = 1; j <=n; j++) {int k=j-1; a[0][k]=Math.cos((2.0*j-1.0)*Math.PI/(2.0*n)); a[1][k]=Math.PI/(double)n;

} return a; } public static double gauss_chebychev_integral(f_x f_xnt,int n) { //n : number of integral coefficients // this routine first generates gauss chebychev coefficients // for [xa,xb] band // then calculates gauss chebychev integral double a[][]=new double[2][n]; a=gauss_chebychev_coefficients(n); //System.out.println(Matrix.toString(a)); double toplam=0; double ck; double x,y; double z1=0; for(int k=0;k<n;k++) { toplam+=a[1][k]*f_xnt.func(a[0][k]); } return toplam; } public static void main(String[] args) { f1 b1=new f1(); System.out.println("integral : "+gauss_chebychev_integral(b1,50)); } }

---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA62 integral : 3.9774632605064237 > Terminated with exit code 0. Gauss-Jacobi (Mehler ) integration Formula is in the form of

I w (1,1)

f ( x) w( x)dx
n

(1 x) (1 + x) f ( x)dx ck f ( xk )
k =0

n 1

In this equation xk is the root of jacobi polynomials Jacobi polynomials are in the form of

J n ( x; , ) = An x n . Coefficients are defined as:


i =0

An =

1 (n + + 1)(n + + 1) 2n (n + + + 1)

Gauss-Jacobi integration equation coefficients, cks are defined as :

(2n + + + 2)(n + + 1)(n + + 1) 2 + ck = eklinde ' (n + + + 1)(n + + + 1)(n + 1)! J n ( xk ; , ) J n +1 ( xk ; , )


tanmlanr. In this equation is the gamma fonction. One of the special form of equation is when =0 and =0. It is given Gauss_legendre equation. The second one is when =-0.5 and =-0.5. It is given

the Gauss-Chebychev formlation. AS it seen Gauss-JAcobi equation set is a kind of general solution with different possible solutions depends on and coefficients. In our example problem, the previous problem defined will be solved by using Gauss-Legendre rnek problemimizde bir nceki problemde kullandmz

I=

ex (1 x )
2

dx integralini tekrar Gauss-legendre seti katsaylarn kullanarak zelim.

Program 7.4-3 variable point Gauss-Jacobi integration with =0 and =0 (Gauss-Legendre)


import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_x { public double func(double x) { return Math.exp(x)/Math.sqrt(1-x*x); } } public class integ_gauss_jacobi { public static double [] lngamma(double x) {//logaritmik hata fonksiyonu //xx[0] hata fonksiyonunun logaritmasnn deerini //xx[1] hata fonksiyonunun kendisinin iaretini verir double xx[]=new double[2]; double result; double a; double b; double c; double p; double q; double u; double w; double z; int i; double logpi; double ls2pi; double tmp; double sgngam = 1; logpi = 1.14472988584940017414; ls2pi = 0.91893853320467274178; if( x<-34.0 ) { q = -x; double yy[]=lngamma(q); w = yy[0]; tmp=yy[1]; p = Math.floor(q); i = (int)Math.round(p); if( i%2==0 ) { sgngam = -1;

} else { sgngam = 1; } z = q-p; if( z>0.5 ) { p = p+1; z = p-q; } z = q*Math.sin(Math.PI*z); result = logpi-Math.log(z)-w; xx[0]=result; xx[1]=sgngam; return xx; } if( x<13 ) { z = 1; p = 0; u = x; while(u>=3) { p = p-1; u = x+p; z = z*u; } while(u<2) { z = z/u; p = p+1; u = x+p; } if( z<0 ) { sgngam = -1; z = -z; } else { sgngam = 1; } if( u==2 ) { result = Math.log(z); //return result; xx[0]=result; xx[1]=sgngam; return xx; } p = p-2; x = x+p; b = -1378.25152569120859100; b = -38801.6315134637840924+x*b; b = -331612.992738871184744+x*b; b = -1162370.97492762307383+x*b; b = -1721737.00820839662146+x*b;

b = -853555.664245765465627+x*b; c = 1; c = -351.815701436523470549+x*c; c = -17064.2106651881159223+x*c; c = -220528.590553854454839+x*c; c = -1139334.44367982507207+x*c; c = -2532523.07177582951285+x*c; c = -2018891.41433532773231+x*c; p = x*b/c; result = Math.log(z)+p; //return result; xx[0]=result; xx[1]=sgngam; return xx; } q = (x-0.5)*Math.log(x)-x+ls2pi; if( x>100000000 ) { result = q; //return result; xx[0]=result; xx[1]=sgngam; return xx; } p = 1/(x*x); if( x>=1000.0 ) { q = q+((7.9365079365079365079365*0.0001*p2.7777777777777777777778*0.001)*p+0.0833333333333333333333)/x; } else { a = 8.11614167470508450300*0.0001; a = -5.95061904284301438324*0.0001+p*a; a = 7.93650340457716943945*0.0001+p*a; a = -2.77777777730099687205*0.001+p*a; a = 8.33333333333331927722*0.01+p*a; q = q+a/x; } xx[0]= q; xx[1]=sgngam; return xx; } //==================== public static double[][] gauss_jacobi_coefficients(int n,double alpha,double beta) { double eps=1.0e-30; int i = 0; int j = 0; double r = 0; double r1 = 0; double t1 = 0; double t2 = 0; double t3 = 0; double p1 = 0; double p2 = 0; double p3 = 0;

double pp = 0; double an = 0; double bn = 0; double a = 0; double b = 0; double c = 0; double tmpsgn = 0; double alfbet = 0; double temp = 0; int its = 0; double A[][]=new double[2][n]; for(i=0; i<=n-1; i++) { if( i==0 ) { an = alpha/n; bn = beta/n; t1 = (1+alpha)*(2.78/(4+n*n)+0.768*an/n); t2 = 1+1.48*an+0.96*bn+0.452*an*an+0.83*an*bn; r = (t2-t1)/t2; } else { if( i==1 ) { t1 = (4.1+alpha)/((1+alpha)*(1+0.156*alpha)); t2 = 1+0.06*(n-8)*(1+0.12*alpha)/n; t3 = 1+0.012*beta*(1+0.25*Math.abs(alpha))/n; r = r-t1*t2*t3*(1-r); } else { if( i==2 ) { t1 = (1.67+0.28*alpha)/(1+0.37*alpha); t2 = 1+0.22*(n-8)/n; t3 = 1+8*beta/((6.28+beta)*n*n); r = r-t1*t2*t3*(A[0][0]-r); } else { if( i<n-2 ) { r = 3*A[0][i-1]-3*A[0][i-2]+A[0][i-3]; } else { if( i==n-2 ) { t1 = (1+0.235*beta)/(0.766+0.119*beta); t2 = 1/(1+0.639*(n-4)/(1+0.71*(n-4))); t3 = 1/(1+20*alpha/((7.5+alpha)*n*n)); r = r+t1*t2*t3*(r-A[0][i-2]); } else { if( i==n-1 ) { t1 = (1+0.37*beta)/(1.67+0.28*beta);

t2 = 1/(1+0.22*(n-8)/n); t3 = 1/(1+8*alpha/((6.28+alpha)*n*n)); r = r+t1*t2*t3*(r-A[0][i-2]); } } } } } } alfbet = alpha+beta; do { temp = 2+alfbet; p1 = (alpha-beta+temp*r)*0.5; p2 = 1; for(j=2; j<=n; j++) { p3 = p2; p2 = p1; temp = 2*j+alfbet; a = 2*j*(j+alfbet)*(temp-2); b = (temp-1)*(alpha*alpha-beta*beta+temp*(temp-2)*r); c = 2*(j-1+alpha)*(j-1+beta)*temp; p1 = (b*p2-c*p3)/a; } pp = (n*(alpha-beta-temp*r)*p1+2*(n+alpha)*(n+beta)*p2)/(temp*(1-r*r)); r1 = r; r = r1-p1/pp; } while( Math.abs(r-r1)>=eps*(1+Math.abs(r))*100 ); A[0][i] = r; double yy[]=lngamma(alpha+n); double w1 = yy[0]; tmpsgn=yy[1]; yy=lngamma(beta+n); double w2 = yy[0]; tmpsgn=yy[1]; yy=lngamma(n+1); double w3 = yy[0]; tmpsgn=yy[1]; yy=lngamma(n+alfbet+1); double w4 = yy[0]; tmpsgn=yy[1]; A[1][i] = Math.exp(w1+w2-w3-w4)*temp*Math.pow(2, alfbet)/(pp*p2); } return A; }

public static double gauss_jacobi_integral(f_x f_xnt,int n,double alpha,double beta) { //n : number of integral coefficients // this routine first generates gauss legendre coefficients // for [x1,x2] band // then calculates gauss hermite integral double a[][]=new double[2][n]; a=gauss_jacobi_coefficients(n,alpha,beta); System.out.println(Matrix.toString(a));

double z=0; for(int i=0;i<n;i++) { z+=a[1][i]*f_xnt.func(a[0][i]);} return z; } public static void main(String[] args) { f1 b1=new f1(); System.out.println(gauss_jacobi_integral(b1,5,0.0,0.0)); } }

Gauss - Jacobi integration with n=5 points ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA63 0.906179845938664 0.538469310105683 0.538469310105683 -0.906179845938664 0.236926885056189 0.478628670499367 0.478628670499367 0.236926885056189 3.4865363609790476 > Terminated with exit code 0.

0.000000000000000 0.568888888888889

As it is seen Gauss-Legendre coefficients are obtained here correctly. Since n = 5 only accuracy is not very high. If it is solved with more coefficients : Gauss - Jacobi integration with n=20 points =0 and =0 ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA63 3.846342341165288 > Terminated with exit code 0. Gauss - Jacobi integration with n=50 points =0 and =0 ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA63 n=50 integral = 3.9242400049441444 > Terminated with exit code 0. Gauss - Jacobi integration with n=100 points =0 and =0 ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA63 n=100 integral = 3.9506992376105177 > Terminated with exit code 0.

If we change function and parameter to solve the same program as =-0.5 and =-0.5 (GaussChebychev formula) Program 7.4-4 variable point Gauss-Jacobi integration with =-0.5 and =-0.5 (Gauss-Legendre) class f1 extends f_x { public double func(double x) { return Math.exp(x); } } System.out.println("n="+n+" integral = "+gauss_jacobi_integral(b1,n,-0.5,-0.5));

10 point variable point Gauss-Jacobi integration with =-0.5 and =-0.5 (Gauss-Legendre) ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA63 n=10 integral = 3.97746265002148 > Terminated with exit code 0. Gauss-Hermit formula : this formulation uses the weight function and calculates integrals from minus infinity to plus infinity.

w( x) = e

x2

I w ( , )

f ( x) w( x)dx

x2

f ( x)dx

c
k =0

n 1

f ( xk )

In this equation xk is the roots of the Hermite polynomials. Hermit Polinomials defined as: P-1(x)=0 P0(x)=1 P1(x)=2x P2(x)=4x2-2 P3(x)=8x3-12x Pn(x)=2xPn-1(x) 2(n-1)Pn-2(x), n=1,2,3,4,5.. Coefficients are given as:

ck =

2 n1 n! n 2 Pn1 ( xk ) 2

Coefficients for the first 7 sets are given in the following table Table 7.4-2 Gauss Hermit integration roots and coefficients
N xk ck

2 3 4 5 6 7

+/-0.707167811 0 +/- 1.224744871391589 +/- 0.524647623275290 +/- 1.650680123885784 0 +/-0.9585724646 +/-2.0201828705 +/- 2.350604973674492 +/- 1.335849074013697 +/- 0.436077411927616 +/- 2.651961356835233 +/- 1.673551628767471 +/- 0.816287882858965 0

0.886226926 1.181635901 0.295408975 0.80491409 0.081312835 0.945308721 0.393619323 0.019953242 0.00453001 0.15706732 0.724629595 0.000971781 0.054515583 0.425607253 0.810264618

Bir rnek programla Gauss_Hermite integral equationnin rneini verelim : Program 8.4-5 Gauss-Hermite integration import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_x { public double func(double x) { return 1/(1+x*x); } } public class NA64 { public static double[][] gauss_hermite_coefficients(int n) { double a[][]=new double[2][n]; int i; int j; double r=0; double r1; double p1; double p2; double p3; double dp3; double pipm4; double eps=1e-10; pipm4 = Math.pow(Math.PI, -0.25); for(i = 0; i <= (n+1)/2-1; i++) { if( i==0 ) { r =Math.sqrt((double)(2*n+1))-1.85575*Math.pow((double)(2*n+1), -1.0/6.0);}

else if( i==1 ) { r = r-1.14*Math.pow((double)(n), 0.426)/r; } else if( i==2 ) { r = 1.86*r-0.86*a[0][0];} else if( i==3 ) { r = 1.91*r-0.91*a[0][1];} else {r = 2*r-a[0][i-2];} do { p2 = 0; p3 = pipm4; for(j = 0; j <= n-1; j++) { p1 = p2; p2 = p3; p3 = p2*r*Math.sqrt(2.0/(double)(j+1))-p1*Math.sqrt((double)(j)/(double)(j+1)); } dp3 = Math.sqrt((double)(2.0*j))*p2; r1 = r; r = r-p3/dp3; } while(Math.abs(r-r1)>=eps*(1+Math.abs(r))*100.0); a[0][i] = r; a[1][i] = 2.0/(dp3*dp3); a[0][n-1-i] = -a[0][i]; a[1][n-1-i] = a[1][i]; } return a; } public static double gauss_hermite_integral(f_x f_xnt,int n) { //n : number of integral coefficients // this routine first generates gauss hermite coefficients // for [x1,x2] band // then calculates gauss hermite integral double a[][]=new double[2][n]; a=gauss_hermite_coefficients(n); System.out.println(Matrix.toString(a)); double z=0; for(int i=0;i<n;i++) { z+=a[1][i]*f_xnt.func(a[0][i])*Math.exp(a[0][i]*a[0][i]);} return z; } public static void main(String[] args) { f1 b1=new f1(); System.out.println("integral : "+gauss_hermite_integral(b1,20));

} } Output 7.3-8 Gauss-Hermite integrasyonu ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA64 5.387480890011233 4.603682449550744 3.944764040115625 3.347854567383216 2.788806058428131 2.254974002089276 1.738537712116586 1.234076215395323 0.737473728545394 0.245340708300901 -0.245340708300901 -0.737473728545394 -1.234076215395323 -1.738537712116586 -2.254974002089276 -2.788806058428131 -3.347854567383216 -3.944764040115625 -4.603682449550744 -5.387480890011233 0.000000000000223 0.000000000439934 0.000000108606937 0.000007802556261 0.000228338599065 0.003243773339370 0.024810520887341 0.109017206020022 0.286675503681034 0.462243669596553 0.462243669596553 0.286675503681034 0.109017206020022 0.024810520887341 0.003243773339370 0.000228338599065 0.000007802556261 0.000000108606937 0.000000000439934 0.000000000000223 integral : 2.804683301721163 > Terminated with exit code 0. Gauss-Laguerre formulation has the weight factor in the form of

w( x) = x e x
The integral limits of this formulation is in between o and infinity. The equation is in the form of

I w (0, )

f ( x) w( x)dx

x e f ( x)dx

c
k =0

n 1

f ( xk )

In this equation xk is the root of Laguerre polinomials. Leguerre polynomial has the form of: P-1(x)=0 P0(x)=1 (n+1)P n+1(x)=(-x+2*n++1)Pn(x) (n+)Pn-1(x), n=1,2,3,4,5... Coefficients,

ck =

1 dPn ( xk ) 2 ] xk [ dxk

Fort he first 5 values, roots and coefficient of the equation is given in Table 8.4-3 (=0), Table 8.4-4 (=1) and Table 8.4-5(=2) . In Gauss-Laguerre integral Formula for =0 integral equation can be changed as

I w ( a , )
N 2 3

f ( x) w( x)dx
xk 0.585786437626905 3.414213562373090 0.415774556783479

( x + a )

f ( x + a )dx e
ck 0.853553390592534 0.146446609406723 0.711093009915089

c
k =0

n 1

f ( xk + a )

Table 7.4-3

Gauss Laquerre integral roots and coefficients (=0)

2.294280360279040 6.289945082937470 0.322547689619392 1.745761101158340 4.536620296921120 9.395070912301130 0.263560319718141 1.413403059106510 3.596425771040720 7.085810005858830 12.640800844275700 xk 1.267949192431120 4.732050807568870 0.935822227524088 3.305407289332270 7.758770483143630 0.743291927981432 2.571635007646270 5.731178751689090 10.953894312683100 0.617030853278271 2.112965958578520 4.610833151017530 8.399066971204840 xk 2.000000000000000 6.000000000000000 1.517387080677410 4.311583133719520 9.171029785603060 1.226763263500300 3.412507358696940 6.902692605851610 12.458036771951100 1.031109144093380 2.837212823953820 5.620294272598700 9.682909837664020 15.828473921690000

0.278517733568676 0.010389256497354 0.603154104288259 0.357418692437802 0.038887908507759 0.000539294705556 0.521755610467045 0.398666821407258 0.075942449681708 0.003611758663878 0.000023369972386 ck 0.788675133123411 0.211324865405185 0.588681472855445 0.391216059222280 0.020102459732679 0.446870579513482 0.477635774492073 0.074177784726293 0.001315849683447 0.348014523429866 0.502280674138866 0.140915919102187 0.008719893025972 ck 1.500000000000000 0.500000000000000 1.037494961490390 0.905750004703039 0.056755033772202 0.725524997698604 1.063424292391060 0.206696130999709 0.004354579188558 0.520917396835042 1.066705933159050 0.383549720007093 0.028564233510280 0.000262712802303

Table 8.4-4 Gauss Laquerre integral roots and coefficients (=1)


N 2 3 4

Table 7.4-5
N 2 3 4

Gauss Laquerre integral roots and coefficients (=2)

If we investigate Gauss-Leguerre Formula with an example. We will calculate the integral

I =

x dx ex + 1 0

Exact integral value I= 0.822467.

Program 7.4-6 Gauss-Laguerre integration


import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; class f1 extends f_x { public double func(double x) { return x/(Math.exp(x)+1.0); } } public class NA65 {

public static double [] lngamma(double x) {//logaritmik hata fonksiyonu //xx[0] hata fonksiyonunun logaritmasnn deerini //xx[1] hata fonksiyonunun kendisinin iaretini verir double xx[]=new double[2]; double result; double a; double b; double c; double p; double q; double u; double w; double z; int i; double logpi; double ls2pi; double tmp; double sgngam = 1; logpi = 1.14472988584940017414; ls2pi = 0.91893853320467274178; if( x<-34.0 ) { q = -x; double yy[]=lngamma(q); w = yy[0]; tmp=yy[1]; p = Math.floor(q); i = (int)Math.round(p); if( i%2==0 ) { sgngam = -1; } else { sgngam = 1; } z = q-p; if( z>0.5 ) { p = p+1; z = p-q; } z = q*Math.sin(Math.PI*z); result = logpi-Math.log(z)-w; xx[0]=result; xx[1]=sgngam; return xx; } if( x<13 ) { z = 1; p = 0; u = x; while(u>=3) { p = p-1;

u = x+p; z = z*u; } while(u<2) { z = z/u; p = p+1; u = x+p; } if( z<0 ) { sgngam = -1; z = -z; } else { sgngam = 1; } if( u==2 ) { result = Math.log(z); //return result; xx[0]=result; xx[1]=sgngam; return xx; } p = p-2; x = x+p; b = -1378.25152569120859100; b = -38801.6315134637840924+x*b; b = -331612.992738871184744+x*b; b = -1162370.97492762307383+x*b; b = -1721737.00820839662146+x*b; b = -853555.664245765465627+x*b; c = 1; c = -351.815701436523470549+x*c; c = -17064.2106651881159223+x*c; c = -220528.590553854454839+x*c; c = -1139334.44367982507207+x*c; c = -2532523.07177582951285+x*c; c = -2018891.41433532773231+x*c; p = x*b/c; result = Math.log(z)+p; //return result; xx[0]=result; xx[1]=sgngam; return xx; } q = (x-0.5)*Math.log(x)-x+ls2pi; if( x>100000000 ) { result = q; //return result; xx[0]=result; xx[1]=sgngam; return xx; } p = 1/(x*x);

if( x>=1000.0 ) { q = q+((7.9365079365079365079365*0.0001*p2.7777777777777777777778*0.001)*p+0.0833333333333333333333)/x; } else { a = 8.11614167470508450300*0.0001; a = -5.95061904284301438324*0.0001+p*a; a = 7.93650340457716943945*0.0001+p*a; a = -2.77777777730099687205*0.001+p*a; a = 8.33333333333331927722*0.01+p*a; q = q+a/x; } xx[0]= q; xx[1]=sgngam; return xx; } public static double[][] gauss_laguerre_coefficients(int n,double alpha) { //Gauss-Laguerre quadratr katsaylarn hesaplar double sgngam=1; double a[][]=new double[2][n]; int i; int j; double r=0; double r1; double p1; double p2; double p3; double dp3; double tsg; double eps=1.0e-10; for(i = 0; i <= n-1; i++) { if( i==0 ) { r = (1.0+alpha)*(3.0+0.92*alpha)/(1+2.4*n+1.8*alpha); } else { if( i==1 ) {r = r+(15.0+6.25*alpha)/(1.0+0.9*alpha+2.5*n);} else {r = r+((1.0+2.55*(i-1))/(1.9*(i-1))+1.26*(i-1)*alpha/(1+3.5*(i-1)))/(1.0+0.3*alpha)*(r-a[0][i-2]);} } do { p2 = 0; p3 = 1; for(j = 0; j <= n-1; j++) { p1 = p2; p2 = p3; p3 = ((-r+2*j+alpha+1)*p2-(j+alpha)*p1)/(j+1); } dp3 = (n*p3-(n+alpha)*p2)/r; r1 = r;

r = r-p3/dp3; } while(Math.abs(r-r1)>=eps*(1+Math.abs(r))*100); a[0][i] = r; double lng[]=lngamma((double)n); double lng1[]=lngamma((double)(alpha+n)); a[1][i] = -Math.exp(lng1[0]-lng[0])/(dp3*n*p2); tsg=lng[1]; } return a; } public static double gauss_laquerre_integral(f_x f_xnt,double xa,int n,double alpha) { //n : number of integral coefficients // this routine first generates gauss legendre coefficients // for [x1,x2] band // then calculates gauss hermite integral double a[][]=new double[2][n]; a=gauss_laguerre_coefficients(n,alpha); //System.out.println(Matrix.toString(a)); double z=0; for(int i=0;i<n;i++) { z+=a[1][i]*f_xnt.func(a[0][i]+xa)*Math.exp(a[0][i])*Math.pow(a[0][i],-alpha);} return z; } public static double gauss_laquerre_integral(f_x f_xnt,double xa,int n) { double alpha=1.0; double a[][]=new double[2][n]; a=gauss_laguerre_coefficients(n,alpha); //System.out.println(Matrix.toString(a)); double z=0; for(int i=0;i<n;i++) { z+=a[1][i]*f_xnt.func(a[0][i]+xa)*Math.exp(a[0][i])/a[0][i];} return z; } public static void main(String[] args) { f1 b1=new f1(); System.out.println("integral : "+gauss_laquerre_integral(b1,0,30)+" exact= "+0.822467); } }

Gauss-Laguerre integration ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA65 integral : 0.8224670275951408 exact= 0.822467 > Terminated with exit code 0. 7.5 INTEGRATION FORMULATION WITH ADJUSTABLE ERROR

If integration in region [a,b] to be defined for a continious integrable function f(x), Integration can be written as:

I = f ( x)dx
a

If a subregion is defined for the integration. For example, can be redefined as

m=

( a b) definition is given integral 2

I = f ( x)dx + f ( x)dx
a m

For numerical integration it is generally assumed that the error of the second definition will be ess than the first definition. It is possible to continue this process by dividing each integral to subregions again. The important concept in here is to stop dividing integral into smaller parts when the desired accuracy is achieved. In this section several integration methods with error control will be investigated. 7.5.1 Adjustable Simpson 1/3 integral If simpson 1/3 integration equation is written in [a,b] region h I (a, b) = I ( x0 , x4 ) = [ f ( x0 ) + 4 f ( x2 ) + f ( x4 )] 3
5 f ( x) dx = I ( x0 , x4 ) h a b

f ( 4 ) ( d1 ) 90

5 Where h =(b-a)/2, x0=a,x2=(a+b)/2,x4=b and d1 is a number between a and b. and h

f ( 4 ) ( d1 ) 90 (4) equation gives amount of error in the integral equation. f term in the error equation is the fourth derivative of the function. If the integration region is divided into two subregion, equation becomes

h [ f ( x0 ) + 4 f ( x1 ) + f ( x2 )] 6 h I ( x2 , x4 ) = [ f ( x2 ) + 4 f ( x3 ) + f ( x4 )] 6 I ( x0 , x 2 ) =
x2

h 5 f ( 4 ) (d 2 ) f ( x ) dx + f ( x ) dx = I ( x0 , x2 ) + I ( x2 , x4 ) x0 x2 16 90
x4

In this equation x1=(a+b)/4, x3=3(a+b)/4 . d2 is stil a value in between a and b. If it is assumed that f ( 4 ) (d1 ) f ( 4 ) (d 2 ) Equation becomes

f ( 4 ) (d1 ) 16 [ I ( x0 , x2 ) + I ( x2 , x4 ) I ( x0 , x4 )] . 90 15 5 (4) h f (d1 ) 1 [ I ( x0 , x2 ) + I ( x2 , x4 ) I ( x0 , x4 )] 16 90 15 h5

If we substitute this into integration equation


x4 x0

f ( x) dx I ( x0 , x2 ) I ( x2 , x4 )

1 I ( x 0 , x 2 ) + I ( x 2 , x 4 ) I ( x0 , x 4 ) 15

In this case for error >0 , it can be written the equation

1 I ( x0 , x 2 ) + I ( x 2 , x 4 ) I ( x0 , x 4 ) < 15
If this condition is met, then approximately
x4 x0

f ( x )dx I ( x0 , x2 ) + I ( x2 , x4 ) can be written. If the given condition can not be met, division

process will be continue till the condition met. Described algorithm is used in the next program. Program 7.5-1 Simson 1/3 integration with adjustable error
// ====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Adaptive simpson 1/3 integration // Dr. Turhan Coban // ====================================================== import java.io.*; class f1 extends f_x { double func(double x) {return x*Math.sqrt(x);} }

class NA66 { public static double adaptive_simpson_integral(f_x ff,double a,double b) {return adaptive_simpson_integral(ff,a,b,1.0e-20,30);} public static double adaptive_simpson_integral(f_x ff,double a,double b,double eps) {return adaptive_simpson_integral(ff,a,b,eps,30);} public static double adaptive_simpson_integral(f_x ff,double a,double b,int MAX_ITERATION) {return adaptive_simpson_integral(ff,a,b,1.0e-12,MAX_ITERATION);} public static double adaptive_simpson_integral(f_x ff,double a,double b,double eps,int MAX_ITERATION) { int i; double h=(b-a); double h2=h/4.0; double x[]=new double[5]; double f[]=new double[5]; for(i=0;i<5;i+=2) {x[i]=a+i*h2;f[i]=ff.func(x[i]);} double sum=h*(f[0]+4.0*f[2]+f[4])/6.0; double Q=0; Q=simpson1_3(ff,x,f,sum,eps,0,MAX_ITERATION,Q); return Q; } public static double simpson1_3(f_x ff,double x[],double f[],double sum,double eps,int k,int MAX_ITERATION,double Q)

//Newton-cotes integral 3 points //adaptive Simpson 1/3 rule double h=(x[4]-x[0]); double h1=h/2.0; double h2=h/4.0; int p,i; double x1[]=new double[5]; double f1[]=new double[5]; double x2[]=new double[5]; double f2[]=new double[5]; k++; for(i=1;i<5;i+=2) {x[i]=x[0]+i*h2;f[i]=ff.func(x[i]);} double sum1=h1*(f[0]+4.0*f[1]+f[2])/6.0; double sum2=h1*(f[2]+4.0*f[3]+f[4])/6.0; x1[0]=x[0];f1[0]=f[0]; x1[2]=x[1];f1[2]=f[1]; x1[4]=x[2];f1[4]=f[2]; x2[0]=x[2];f2[0]=f[2]; x2[2]=x[3];f2[2]=f[3]; x2[4]=x[4];f2[4]=f[4]; if(1.0/15.0*Math.abs(sum1+sum2-sum)<eps || k>MAX_ITERATION) Q+=sum1+sum2; else Q+=simpson1_3(ff,x1,f1,sum1,eps,k,MAX_ITERATION,Q)+simpson1_3(ff,x2,f2,sum2,eps,k,MAX_ITERATION,Q); return Q; } public static void main(String args[]) throws IOException { f1 b1=new f1(); System.out.println("integral of class f1 : "+adaptive_simpson_integral(b1,0.0,1.0)); } }

Simson 1/3 integration with the adaptive simpson integration ---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA66 integral of class f1 : 0.4 > Terminated with exit code 0. If a version of the same program with actual plots of the calculating points are given, automatic adjustment process of the routine can be seen more clearly. Program 7.5-2 Simson 1/3 integration with adjustable error(graphing version)
// ====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Adaptive simpson 1/3 integration with plotting // Dr. Turhan Coban // ====================================================== import java.io.*; class f1 extends f_x {

double func(double x) {return 100.0/(x*x)*Math.sin(10.0/x); } } class NA67 { public static double adaptive_simpson_integral(f_x ff,double a,double b) {return adaptive_simpson_integral(ff,a,b,1.0e-20,30);} public static double adaptive_simpson_integral(f_x ff,double a,double b,double eps) {return adaptive_simpson_integral(ff,a,b,eps,30);} public static double adaptive_simpson_integral(f_x ff,double a,double b,int MAX_ITERATION) {return adaptive_simpson_integral(ff,a,b,1.0e-12,MAX_ITERATION);} public static double adaptive_simpson_integral(f_x ff,double a,double b,double eps,int MAX_ITERATION) { int i; double h=(b-a); double h2=h/4.0; double x[]=new double[5]; double f[]=new double[5]; double w1[]=new double[3]; double z1[]=new double[3]; int j; for(i=0;i<5;i+=2) {j=i/2;x[i]=a+i*h2;f[i]=ff.func(x[i]);w1[j]=x[i];z1[j]=f[i];} double sum=h*(f[0]+4.0*f[2]+f[4])/6.0; double Q=0; Plot pp=new Plot(ff,a,b,100); pp.setPlotType(0,0);// pp.addData(w1,z1,28); int k=0; Q=simpson1_3(ff,x,f,sum,eps,k,MAX_ITERATION,Q,pp); pp.plot(); return Q; } public static double simpson1_3(f_x ff,double x[],double f[],double sum,double eps,int k,int MAX_ITERATION,double Q,Plot pp) { //adaptive Simpson 1/3 rule double h=(x[4]-x[0]); double h1=h/2.0; double h2=h/4.0; int p,i; double x1[]=new double[5]; double f1[]=new double[5]; double x2[]=new double[5]; double f2[]=new double[5]; double w1[]=new double[2]; double z1[]=new double[2]; int j; for(i=1;i<5;i+=2) {j=i/2;x[i]=x[0]+i*h2;w1[j]=x[i];f[i]=ff.func(x[i]);z1[j]=f[i];} for(i=0;i<5;i++) {System.out.println(i+" "+x[i]+" "+f[i]);} {System.out.println("==================");} double sum1=h1*(f[0]+4.0*f[1]+f[2])/6.0; double sum2=h1*(f[2]+4.0*f[3]+f[4])/6.0; if(1.0/15.0*Math.abs(sum1+sum2-sum)<eps || k>MAX_ITERATION) Q+=sum1+sum2;

else { x1[0]=x[0];f1[0]=f[0]; x1[2]=x[1];f1[2]=f[1]; x1[4]=x[2];f1[4]=f[2]; x2[0]=x[2];f2[0]=f[2]; x2[2]=x[3];f2[2]=f[3]; x2[4]=x[4];f2[4]=f[4]; k++; Q+=simpson1_3(ff,x1,f1,sum1,eps,k,MAX_ITERATION,Q,pp)+ simpson1_3(ff,x2,f2,sum2,eps,k,MAX_ITERATION,Q,pp); } pp.addData(w1,z1,28); return Q; } public static void main(String args[]) throws IOException { f1 b1=new f1(); System.out.println("integral of class f1 : "+adaptive_simpson_integral(b1,1.0,3.0,1.0e-5,2)); } }

Adaptive Simson 1/3 integration (graphic output version)


---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA67 0 1.0 -54.40211108893698 1 1.5 16.62894358094311 2 2.0 -23.973106866578462 3 2.5 -12.108839924926851 4 3.0 -2.1174218097276154 ================== 0 1.0 -54.40211108893698 1 1.25 63.318927783896434 2 1.5 16.62894358094311 3 1.75 -17.590376763629592 4 2.0 -23.973106866578462 ================== 0 1.0 -54.40211108893698 1 1.125 40.34415237327668 2 1.25 63.318927783896434 3 1.375 44.20625835648312 4 1.5 16.62894358094311 ================== 0 1.0 -54.40211108893698 1 1.0625 1.1526986941820327 2 1.125 40.34415237327668 3 1.1875 59.814502478779694 4 1.25 63.318927783896434 ================== 0 1.25 63.318927783896434 1 1.3125 56.45523901688928 2 1.375 44.20625835648312 3 1.4375 30.17788460030764 4 1.5 16.62894358094311 ================== 0 1.5 16.62894358094311 1 1.625 -4.884405926189118 2 1.75 -17.590376763629592

3 1.875 -23.13470269347783 4 2.0 -23.973106866578462 ================== 0 1.5 16.62894358094311 1 1.5625 4.773855430676219 2 1.625 -4.884405926189118 3 1.6875 -12.280554478542038 4 1.75 -17.590376763629592 ================== 0 1.75 -17.590376763629592 1 1.8125 -21.101511468402688 2 1.875 -23.13470269347783 3 1.9375 -23.999652018301266 4 2.0 -23.973106866578462 ================== 0 2.0 -23.973106866578462 1 2.25 -19.048239346741298 2 2.5 -12.108839924926851 3 2.75 -6.278745267070935 4 3.0 -2.1174218097276154 ================== 0 2.0 -23.973106866578462 1 2.125 -22.144859946836526 2 2.25 -19.048239346741298 3 2.375 -15.542391685948601 4 2.5 -12.108839924926851 ================== 0 2.0 -23.973106866578462 1 2.0625 -23.29043436266789 2 2.125 -22.144859946836526 3 2.1875 -20.69068321637011 4 2.25 -19.048239346741298 ================== 0 2.25 -19.048239346741298 1 2.3125 -17.309323548876932 2 2.375 -15.542391685948601 3 2.4375 -13.79721050171754 4 2.5 -12.108839924926851 ================== 0 2.5 -12.108839924926851 1 2.625 -8.988488771999702 2 2.75 -6.278745267070935 3 2.875 -3.9966027013802914 4 3.0 -2.1174218097276154 ================== 0 2.5 -12.108839924926851 1 2.5625 -10.500944535981427 2 2.625 -8.988488771999702 3 2.6875 -7.579894531793721 4 2.75 -6.278745267070935 ================== 0 2.75 -6.278745267070935 1 2.8125 -5.085116345062419 2 2.875 -3.9966027013802914 3 2.9375 -3.0091045529709124 4 3.0 -2.1174218097276154 ================== integral of class f1 : -1.416364288717352

Graphic output of Adaptive simpson 1/3 integration

7.5.2 Adaptive Gauss-Legendre Integration Adjustable error version of the Gauss-Legendre integration is given below. The main idea here is to increase terms in the Gauss-Legendre integration till the arror level between two levels drop under the desired error level. Program 7.5-3 Gauss-Legendre integration with the adjustable error
// ====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Adaptive Gauss-legendre integration // Dr. Turhan Coban // ====================================================== import java.io.*;

class f1 extends f_x { public double func(double x) {return x*Math.sqrt(x);} }

class NA68 { public static double[][] gauss_legendre_coefficients(double x1,double x2,int n) { //calculates legendre gauss-coefficients as coefficients of the integral //for n terms double EPS=3.0e-15; int m,j,i; double z1,z,xm,xl,pp,p3,p2,p1; double a[][]=new double[2][n];

m=(n+1)/2; xm=0.5*(x2+x1); xl=0.5*(x2-x1); for (i=1;i<=m;i++) { z=Math.cos(Math.PI*((i-0.25)/(n+0.5))); do { p1=1.0; p2=0.0; for (j=1;j<=n;j++) { p3=p2; p2=p1; p1=((2.0*j-1.0)*z*p2-(j-1.0)*p3)/j; } pp=n*(z*p1-p2)/(z*z-1.0); z1=z; z=z1-p1/pp; } while (Math.abs(z-z1) > EPS); a[0][i-1]=xm-xl*z; a[0][n-i]=xm+xl*z; a[1][i-1]=2.0*xl/((1.0-z*z)*pp*pp); a[1][n-i]=a[1][i-1]; } return a; } public static double gauss_legendre_integral(f_x f,double x1,double x2,int n) { //n : number of integral coefficients // this routine first generates gauss legendre coefficients // for [x1,x2] band // then calculates gauss legendre integral double a[][]=new double[2][n]; a=gauss_legendre_coefficients(x1,x2,n); //Text.print(Text.T(a)); //System.out.println(Matrix.toString(a)); double z=0; for(int i=0;i<n;i++) { z+=a[1][i]*f.func(a[0][i]);} return z; } public static double adaptive_gauss_legendre_integral(f_x f,double x1,double x2) {return adaptive_gauss_legendre_integral(f,x1,x2,1.0e-12,100);} public static double adaptive_gauss_legendre_integral(f_x f,double x1,double x2,double eps) {return adaptive_gauss_legendre_integral(f,x1,x2,eps,100);} public static double adaptive_gauss_legendre_integral(f_x f,double x1,double x2,double eps,double MAXITER) { int n=1; double ans1=gauss_legendre_integral(f,x1,x2,n); double ans2; do {ans2=ans1;n++;ans1=gauss_legendre_integral(f,x1,x2,n);} while(Math.abs(ans2-ans1)>eps && n<MAXITER); if(n==MAXITER) System.out.println("istenilen hata seviyesi tutturulamad n="+n ); return ans2; }

public static void main(String args[]) throws IOException { //Gauss-Legendre integrali f1 b1=new f1(); System.out.println("integral : "+adaptive_gauss_legendre_integral(b1,0.0,1.0)); } }

Adaptive Gauss-Legendre integration ---------- Capture Output ---------> "C:\co\java\bin\java.exe" integ13 integral : 0.3999999999829868 > Terminated with exit code 0. 7.5.3 Gauss-Kronrod integration formula

I w (1,1)

f ( x)dx

c
k =1

f ( x ) + cn + k f ( xkK )
G k k =1

n +1

Gauss Kronrod integration Formula uses two terms. First term is Standard Gauss-Legendre integration term. In the second term coefficients are taken as same as the Gauss-Legendre term, but roots are different. In our example code constant integration of 15 terms will be used for each equation (total of 30 terms). Program utilises Gauss-Legendre terms as firs, and then uses Gauss-Kronroad terms. To improve error reduction equation can be continued with half dividing technique. Gauss-Kronrod Formula is prefferred method for many package programs such as mathlab. Program 7.5-4 Gauss-Kronrod integration formula
class f1 extends f_x { double func(double x) {return x*Math.sqrt(x);} } public class NA69 { public static double gauss_konrad_integral(f_x ff,double a,double b,double eps,int n) { //eps hata miktar int Aused=0; boolean sonuc; double integral=0; double A[][]; int Aw = 0; double toplamhata = 0; double[] c; double[] xg; double[] xk; int nn = 0; int ng = 0; int i = 0; int j = 0; int h = 0; double v = 0; double k1 = 0;

double k2 = 0; double intg = 0; double intk = 0; double ta = 0; double tb = 0; Aw = 4; A = new double[n-1+1][Aw-1+1]; nn = 61; ng = 15; c = new double[nn]; xk = new double[nn]; xg = new double[nn]; //Gauss katsaylar xg[0] = 0.007968192496166605615465883474674; xg[1] = 0.018466468311090959142302131912047; xg[2] = 0.028784707883323369349719179611292; xg[3] = 0.038799192569627049596801936446348; xg[4] = 0.048402672830594052902938140422808; xg[5] = 0.057493156217619066481721689402056; xg[6] = 0.065974229882180495128128515115962; xg[7] = 0.073755974737705206268243850022191; xg[8] = 0.080755895229420215354694938460530; xg[9] = 0.086899787201082979802387530715126; xg[10] = 0.092122522237786128717632707087619; xg[11] = 0.096368737174644259639468626351810; xg[12] = 0.099593420586795267062780282103569; xg[13] = 0.101762389748405504596428952168554; xg[14] = 0.102852652893558840341285636705415; c[0] = 0.999484410050490637571325895705811; c[1] = 0.996893484074649540271630050918695; c[2] = 0.991630996870404594858628366109486; c[3] = 0.983668123279747209970032581605663; c[4] = 0.973116322501126268374693868423707; c[5] = 0.960021864968307512216871025581798; c[6] = 0.944374444748559979415831324037439; c[7] = 0.926200047429274325879324277080474; c[8] = 0.905573307699907798546522558925958; c[9] = 0.882560535792052681543116462530226; c[10] = 0.857205233546061098958658510658944; c[11] = 0.829565762382768397442898119732502; c[12] = 0.799727835821839083013668942322683; c[13] = 0.767777432104826194917977340974503; c[14] = 0.733790062453226804726171131369528; c[15] = 0.697850494793315796932292388026640; c[16] = 0.660061064126626961370053668149271; c[17] = 0.620526182989242861140477556431189; c[18] = 0.579345235826361691756024932172540; c[19] = 0.536624148142019899264169793311073; c[20] = 0.492480467861778574993693061207709; c[21] = 0.447033769538089176780609900322854; c[22] = 0.400401254830394392535476211542661; c[23] = 0.352704725530878113471037207089374; c[24] = 0.304073202273625077372677107199257; c[25] = 0.254636926167889846439805129817805; c[26] = 0.204525116682309891438957671002025; c[27] = 0.153869913608583546963794672743256; c[28] = 0.102806937966737030147096751318001; c[29] = 0.051471842555317695833025213166723;

c[30] = 0.000000000000000000000000000000000; //Kronrod katsaylar xk[0] = 0.001389013698677007624551591226760; xk[1] = 0.003890461127099884051267201844516; xk[2] = 0.006630703915931292173319826369750; xk[3] = 0.009273279659517763428441146892024; xk[4] = 0.011823015253496341742232898853251; xk[5] = 0.014369729507045804812451432443580; xk[6] = 0.016920889189053272627572289420322; xk[7] = 0.019414141193942381173408951050128; xk[8] = 0.021828035821609192297167485738339; xk[9] = 0.024191162078080601365686370725232; xk[10] = 0.026509954882333101610601709335075; xk[11] = 0.028754048765041292843978785354334; xk[12] = 0.030907257562387762472884252943092; xk[13] = 0.032981447057483726031814191016854; xk[14] = 0.034979338028060024137499670731468; xk[15] = 0.036882364651821229223911065617136; xk[16] = 0.038678945624727592950348651532281; xk[17] = 0.040374538951535959111995279752468; xk[18] = 0.041969810215164246147147541285970; xk[19] = 0.043452539701356069316831728117073; xk[20] = 0.044814800133162663192355551616723; xk[21] = 0.046059238271006988116271735559374; xk[22] = 0.047185546569299153945261478181099; xk[23] = 0.048185861757087129140779492298305; xk[24] = 0.049055434555029778887528165367238; xk[25] = 0.049795683427074206357811569379942; xk[26] = 0.050405921402782346840893085653585; xk[27] = 0.050881795898749606492297473049805; xk[28] = 0.051221547849258772170656282604944; xk[29] = 0.051426128537459025933862879215781; xk[30] = 0.051494729429451567558340433647099; for(i=nn-1; i>=nn/2; i--) {c[i] = -c[nn-1-i];} for(i=nn-1; i>=nn/2; i--) { xk[i] = xk[nn-1-i]; } for(i=ng-1; i>=0; i--) { xg[nn-2-2*i] = xg[i]; xg[1+2*i] = xg[i]; } for(i=0; i<=nn/2; i++) { xg[2*i] = 0; } k1 = 0.5*(b-a); k2 = 0.5*(b+a); intg = 0; intk = 0; for(i=0; i<=nn-1; i++) { v = ff.func(k1*c[i]+k2); intk = intk+v*xk[i]; if( i%2==1 ) {intg = intg+v*xg[i];} } intk = intk*(b-a)*0.5; intg = intg*(b-a)*0.5; A[0][0] = Math.abs(intg-intk); A[0][1] = intk; A[0][2] = a;

A[0][3] = b; toplamhata = A[0][0]; if( toplamhata<eps ) { sonuc = true; integral = intk; Aused = 1; return integral; } Aused = 1; for(h=1; h<=n-1; h++) { Aused = h+1; gir(A, h, Aw); toplamhata = toplamhata-A[h-1][0]; ta = A[h-1][2]; tb = A[h-1][3]; A[h-1][2] = ta; A[h-1][3] = 0.5*(ta+tb); A[h][2] = 0.5*(ta+tb); A[h][3] = tb; for(j=h-1; j<=h; j++) { k1 = 0.5*(A[j][3]-A[j][2]); k2 = 0.5*(A[j][3]+A[j][2]); intg = 0; intk = 0; for(i=0; i<=nn-1; i++) { v = ff.func(k1*c[i]+k2); intk = intk+v*xk[i]; if( i%2==1 ) { intg = intg+v*xg[i];} } intk = intk*(A[j][3]-A[j][2])*0.5; intg = intg*(A[j][3]-A[j][2])*0.5; A[j][0] = Math.abs(intg-intk); A[j][1] = intk; toplamhata = toplamhata+A[j][0]; } cik(A, h-1, Aw); cik(A, h, Aw); if( toplamhata<eps ) {break;} } sonuc = toplamhata<eps; integral = 0; for(j=0; j<=Aused-1; j++) { integral = integral+A[j][1];} return integral; } public static void gir(double A[][],int n,int Awidth) { int i = 0; int p = 0; double t = 0; int maxcp = 0; if( n==1 )

{ return; } for(i=0; i<=Awidth-1; i++) { t = A[n-1][i]; A[n-1][i] = A[0][i]; A[0][i] = t; }//end of for p = 0; while( 2*p+1<n-1 ) { maxcp = 2*p+1; if( 2*p+2<n-1 ) { if( A[2*p+2][0]>A[2*p+1][0] ) { maxcp = 2*p+2; } } if( A[p][0]<A[maxcp][0] ) { for(i=0; i<=Awidth-1; i++) { t = A[p][i]; A[p][i] = A[maxcp][i]; A[maxcp][i] = t; } p = maxcp; } else { break; } } //end of while } public static void cik(double A[][],int n,int Awidth) { int i = 0; int p = 0; double t = 0; int kk = 0; if( n==0 ) {return;} p = n; while( p!=0 ) { kk = (p-1)/2; if( A[p][0]>A[kk][0] ) { for(i=0; i<=Awidth-1; i++) { t = A[p][i]; A[p][i] = A[kk][i]; A[kk][i] = t; } p = kk; } else {break;} } } public static void main(String axgs[]) { f1 b1=new f1(); System.out.println("integral of class } }

f1 : "+gauss_konrad_integral(b1,0.0,1.0,1.0e-20,50));

---------- Capture Output ----------

> "C:\co\java\bin\javaw.exe" adaptive_Gauss_Kronrod_integral integral of class f1 : 0.4 > Terminated with exit code 0. 7.5.4 Adaptive Newton-Cotes integration with Gander ve Gautschi[31] approach In the previous adaptive integration approaches program follows a stopping criteria such as if(Math.abs(I1-I2)< tolerance or if(Math.abs((I1-I2)/I1) < tolerance This type of stopping criteria might be unsuccessfull for some cases, and Maximum iteration error will be received. If we would like to further decrease the arror and reach the machine error level a different type of error control can be used. If an error control such as if(is+(I1-I2)==is) is used, this control Works only when I1 is equal to I2 with the machine accuracy. If this definition to be used with the halving approach

m=

( a b) with an additional control definition can even give a better exit criteria 2

if( (is+(I1-I2)==is) || (m<=a) || (b<=m) ) Now this criteria will be applied to Newton-Cotes formulas. In this algorithm Newton Cotes Formula with 8th degree polynomial and quadratic polinomial (Simpson 1/3) will be used. 8th degree Newton cotes Formula :

is = f ( x)dx =
a

(b a ) [989 f ( x0 ) + 5888 f ( x1 ) 928 f ( x2 ) + 10496 f ( x3 ) 4540 f ( x4 ) 28350 + 10496 f ( x5 ) 928 f ( x6 ) + 5888 f ( x7 ) + 989 f ( x8 )]
b

Simpson 1/3integral in iterative form will be: b (b a ) I1 = I = f ( x)dx = I [a, b] = [ f ( x0 ) + 4 f ( x2 ) + f ( x4 )] a 6

I 2 = I [a, m] + I [m, b] = f ( x)dx + f ( x)dx =


a m

(b m) (m a) [ f ( x2 ) + 4 f ( x3 ) + f ( x4 )] = [ f ( x0 ) + 4 f ( x1 ) + f ( x2 )] + 6 6 (b a ) [ f ( x0 ) + 4 f ( x1 ) + 2 f ( x2 ) + 4 f ( x3 ) + f ( x4 )] 12

Program 7.5-5 Gander and Gautschi adaptive Simpson 1/3 integral formulation
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions

// Adaptive Gander and Gautschi simpson 1/3 integration // Dr. Turhan Coban // ===================================================== import java.io.*; class f1 extends f_x { double func(double x) {return Math.sin(x);} } class NA70 { public static double adaptive_simpson_integral2(f_x ff,double a,double b) { int i; Plot pp=new Plot(ff,a,b,100); double h=(b-a); double h2=h/4.0; double h3=h/8.0; double m=(a+b)/2.0; double x[]=new double[9]; double y[]=new double[9]; for(i=0;i<9;i++) {x[i]=a+i*h3;y[i]=ff.func(x[i]);} double is=h/28350.0*(989.0*y[0]+5888.0*y[1]-928.0*y[2]+10496.0*y[3]-4540.0*y[4]+ 10496.0*y[5]-928.0*y[6]+5888.0*y[7]+989.0*y[8]); double fa = y[0]; double fm = y[4]; double fb = y[8]; if(is==0) is = b-a; double Q = adaptsimstp(ff,a,b,fa,fm,fb,is); return Q; } public static double adaptsimstp(f_x ff,double a,double b,double fa,double fm,double fb,double is) { double m = (a + b)/2; double h = (b - a)/4; double x[] = {a + h, b - h}; int n=x.length; double y[] = new double[n]; for(int i=0;i<n;i++) {y[i]=ff.func(x[i]);} double fml = y[0]; double fmr = y[1]; double i1 = h/1.5 * (fa + 4*fm + fb); double i2 = h/3 * (fa + 4*(fml + fmr) + 2*fm + fb); double Q=0; i1 = (16.0*i2 - i1)/15.0; //romberg integrasyonu ile iyiletirme if (((is + (i1-i2)) == is) || (m <= a) || (b<=m)) { if ((m <= a) || (b<=m)) { System.out.println("istenilen toleransa ulalamad");} Q = i1; } else { Q = adaptsimstp(ff,a,m,fa,fml,fm,is)+ adaptsimstp(ff,m,b,fm,fmr,fb,is); }

return Q; } public static void main(String args[]) throws IOException { f1 b1=new f1(); System.out.println("integral of class f1 : "+adaptive_simpson_integral2(b1,0.0,Math.PI)); } }

Gander and Gautschi adaptive Simpson 1/3 integral Formula with sample integral

I = sin( x)dx = 2
0

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA70 integral of class f1 : 2.0 > Terminated with exit code 0.

7.5-5 Adaptive Gauss-Lobatto integral with Gander ve Gautschi[31] approach Gauss-Lobatto integration Formula is similar to Gauss integration Formula. The most important difference is that Lobatto Formula also covers end points

I w (1,1)

f ( x)dx

c
k =0

n 1

f ( xk )

For various n values Gauss-Lobatto coefficients and roots are given Table 7.5-1 Gauss-Lobatto integration equation coefficients and roots
N 4 Xk -1.00000000000000000000000 1.00000000000000000000000 -0.44721359549995800000000 0.44721359549995800000000 -1.00000000000000000000000 1.00000000000000000000000 -0.65465367070797700000000 0.65465367070797700000000 0.00000000000000000000000 -1.00000000000000000000000 1.00000000000000000000000 -0.76505532392946500000000 0.76505532392946500000000 -0.28523151648063500000000 0.28523151648063500000000 -1.00000000000000000000000 1.00000000000000000000000 -0.81649658092772600000000 0.81649658092772600000000 -0.44721359549995800000000 -0.44721359549995800000000 0.00000000000000000000000 -1.00000000000000000000000 1.00000000000000000000000 -0.94288241569547900000000 0.94288241569547900000000 Ck 0.16666666666666700000000 0.16666666666666700000000 0.83333333333333300000000 0.83333333333333300000000 0.10000000000000000000000 0.10000000000000000000000 0.54444444444444400000000 0.54444444444444400000000 0.71111111111111100000000 0.06666666666666660000000 0.06666666666666660000000 0.37847495629784700000000 0.37847495629784700000000 0.55485837703548600000000 0.55485837703548600000000 0.05238095238095240000000 0.05238095238095240000000 0.29387755102040800000000 0.29387755102040800000000 0.42517006802721100000000 0.42517006802721100000000 0.45714285714285700000000 0.01582719197348010000000 0.01582719197348010000000 0.09427384021885000000000 0.09427384021885000000000

12

-0.81649658092772600000000 0.81649658092772600000000 -0.64185334234578100000000 0.64185334234578100000000 -0.44721359549995800000000 0.44721359549995800000000 -0.23638319966214900000000 0.23638319966214900000000 0.00000000000000000000000

0.15507198733658500000000 0.15507198733658500000000 0.18882157396018200000000 0.18882157396018200000000 0.19977340522685800000000 0.19977340522685800000000 0.22492646533333900000000 0.22492646533333900000000 0.24261107190140700000000

As the previous simpson 1/3 approach Gander ve Gautschi[31] approach will be used as a stopping criteria in this program also. For the interval [a,b] if( ( is+( I1 - I2 ) == is ) || (m<=a) || (b<=m) ) In this condition m is given as

m=
used.

( a b) eklindedir. . For the is fist integration value degree 12 Lobatto integration will be 2
b

is

f ( x)dx (b a ) * {0.0158271919734801 * [f(a) + f(b)] +

0.094273840218850 * [f(m h * 0.942882415695479) + f(m + h * 0.942882415695479)] + 0.155071987336585 * [f(m h * 0.816496580927726) + f(m + h * 0.816496580927726)] + 0.188821573960182 * [f(m h * 0.641853342345781) + f(m + h * 0.641853342345781)] + 0.199773405226858 * [f(m h * 0.447213595499958) + f(m + h * 0.447213595499958)] + 0.224926465333339 * [f(m h * 0.236383199662149) + f(m + h * 0.236383199662149)] + 0.242611071901407 * [f(0)]}
As iteration Formula 4th and 7th degree Gauss-Lobatto formulations will be used.

I1

f ( x)dx (b a ) * {0.166666666666667 * [f(a) + f(b)] +

0.833333333333333 * [f(m h * 0.447213595499958) + f(m + h * 0.447213595499958)]}

I2

f ( x)dx (b a ) * {0.0523809523809524 * [f(a) + f(b)] +

0.293877551020408 * [f(m h * 0.816496580927726) + f(m + h * 0.816496580927726)] + 0.425170068027211 * [f(m h * 0.447213595499958) + f(m + h * 0.447213595499958)] + 0.457142857142857 * [f(0)]}
Program code for this approach is given below. Program 7.5-5 Gander and Gautschi adaptive Gauss-Lobatto integral formula
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Gander and Gautschi adaptive Gauss-Lobatto integral // Dr. Turhan Coban // ===================================================== import java.io.*; class f1 extends f_x { double func(double x)

{//return 13.0*(x-x*x)*Math.exp(-1.5*x); return Math.sin(x); } } class NA71 { public static double adaptive_Lobatto_integral(f_x ff,double a,double b) { int i; double m=(a+b)/2.0; double h=(b-a)/2.0; double alpha=Math.sqrt(2.0/3.0); double beta=1.0/Math.sqrt(5.0); double x1,x2,x3; x1=0.94288241569547971905635175843185720232; x2=0.64185334234578130578123554132903188354; x3=0.23638319966214988028222377349205292599; double A,B,C,D,E,F,G; A=0.015827191973480183087169986733305510591; B=0.094273840218850045531282505077108171960; C=0.15507198733658539625363597980210298680; D=0.18882157396018245442000533937297167125; E=0.19977340522685852679206802206648840246; F=0.22492646533333952701601768799639508076; G=0.24261107190140773379964095790325635233; double x[]={a,(m-x1*h),(m-alpha*h),(m-x2*h),(m-beta*h),(mx3*h),m,(m+x3*h),(m+beta*h),(m+x2*h),(m+alpha*h),(m+x1*h),b}; int n=x.length; double y[]=new double[n]; for(i=0;i<n;i++) y[i]=ff.func(x[i]); double fa=y[0]; double fb=y[12]; double i2=(h/6.0)*(y[0]+y[12]+5.0*(y[4]+y[8])); double i1=(h/1470.0)*(77.0*(y[0]+y[12])+432.0*(y[2]+y[10])+ 625.0*(y[4]+y[8])+672.0*y[6]); double is=h*(A*(y[0]+y[12])+B*(y[1]+y[11])+C*(y[2]+y[10])+D*(y[3]+y[9])+E*(y[4]+y[8])+F*(y[5]+y[7])+G*y[6]); int s=(int)(is/Math.abs(is)); if(s==0) s=1; double erri1=Math.abs(i1-is); double erri2=Math.abs(i2-is); double R; R=erri1/erri2; //if(R>0 && R<1) tol=tol/R; //is=s*Math.abs(is)*tol/eps; if(is==0) is=b-a; double Q=0; Q=Lobatto(ff,a,b,fa,fb,is); return Q; } public static double Lobatto(f_x ff,double a,double b,double fa,double fb,double is) { double h=(b-a)/2; double m=(a+b)/2; double alpha=Math.sqrt(2.0/3.0); double beta=1.0/Math.sqrt(5.0); double mll=m-alpha*h; double ml=m-beta*h;

double mr=m+beta*h; double mrr=m+alpha*h; double x[]={mll,ml,m,mr,mrr}; int n=x.length; double y[]=new double[n]; for(int i=0;i<n;i++) y[i]=ff.func(x[i]); double fmll=y[0]; double fml=y[1]; double fm=y[2]; double fmr=y[3]; double fmrr=y[4]; double Q; double i2=(h/6)*(fa+fb+5*(fml+fmr)); double i1=(h/1470)*(77*(fa+fb)+432*(fmll+fmrr)+625*(fml+fmr)+672*fm); boolean c1,c2,c3; double w1=i1-i2;; double w2=is; double w3=w2+w1; c1=(w3==w2); c2=(mll<=a); c3=(b<=mrr); if( c1|| c2 ||c3 ) { if (((m <= a) || (b<=m))) { System.out.println("istenilen toleransa ulalamad"); } Q=i1; } else { Q=Lobatto(ff,a,mll,fa,fmll,is) + Lobatto(ff,mll,ml,fmll,fml,is)+ Lobatto(ff,ml,m,fml,fm,is) + Lobatto(ff,m,mr,fm,fmr,is)+ Lobatto(ff,mr,mrr,fmr,fmrr,is)+Lobatto(ff,mrr,b,fmrr,fb,is); } return Q; } public static void main(String args[]) throws IOException { f1 b1=new f1(); System.out.println("integral of class f1 : "+adaptive_Lobatto_integral(b1,0.0,Math.PI)); } }

Gander and Gautschi adaptive Gauss-Lobatto integral formula ---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA71 integral of class f1 : 2.0 > Terminated with exit code 0. In order to see how successfull the code will be a mathematical function test will be carried ourt as the next example. The definition of the error function :

erf ( ) =

e
0

Because of the difficulty of calculating this equation with integration Formula, it is generally calculated by serial solutions. In our example both serial solution and Gauss-Lobatto adaptive integration solution will be made and compared. Program 7.5-6 Calculation of error function with Gander and Gautschi adaptive Gauss-Lobatto integral formula
//====================================================== // Numerical Analysis package in java // error function calculation by using // Adaptive Gauss-Lobatto integral and serial solution // Referance : Adapted Quadrature - Revisited // Walter Gander - Walter Gautchi // ftp.inf.ethz.ch // Dr. Turhan Coban // ===================================================== import java.io.*; import javax.swing.*; class f1 extends f_x { double func(double x) {return 2.0/Math.sqrt(Math.PI)*Math.exp(-x*x);} } class NA72 { public static double gammp(double a,double x) { //incomplete gamma function P(a,x) double gamser,gammcf; if (x < 0.0 || a <= 0.0) System.out.println("Invalid arguments in routine GAMMP"); if (x < (a+1.0)) { gamser=gser(a,x); return gamser; } else { gammcf=gcf(a,x); return 1.0-gammcf; } } public static double gser(double a,double x) { int ITMAX=100; double EPS=3.0e-7; int n; double sum,del,ap; double gln=gammln(a); double gamser=0; if (x <= 0.0) { if (x < 0.0) System.out.println("x less than 0 in routine GSER"); gamser=0.0; return gamser; } else { ap=a;

del=sum=1.0/a; for (n=1;n<=ITMAX;n++) { ap += 1.0; del *= x/ap; sum += del; if (Math.abs(del) < Math.abs(sum)*EPS) { gamser=sum*Math.exp(-x+a*Math.log(x)-(gln)); return gamser; } } System.out.println("a too large, ITMAX too small in routine GSER"); return gamser; } } public static double gcf(double a,double x) { int ITMAX=100; double EPS=3.0e-7; int n; double gln; double gammcf=0; double gold=0.0,g,fac=1.0,b1=1.0; double b0=0.0,anf,ana,an,a1,a0=1.0; gln=gammln(a); a1=x; for (n=1;n<=ITMAX;n++) { an=(double) n; ana=an-a; a0=(a1+a0*ana)*fac; b0=(b1+b0*ana)*fac; anf=an*fac; a1=x*a0+anf*a1; b1=x*b0+anf*b1; if (a1!=0) { fac=1.0/a1; g=b1*fac; if (Math.abs((g-gold)/g) < EPS) { gammcf=Math.exp(-x+a*Math.log(x)-gln)*g; return gammcf; } gold=g; } } System.out.println("a too large, ITMAX too small in routine GCF"); return gammcf; } public static double gammln(double xx) { //gamma function double x,y,tmp,ser; double cof[]={76.18009172947146,-86.50532032941677, 24.01409824083091,-1.231739572450155, 0.1208650973866179e-2,-0.5395239384953e-5}; int j; y=x=xx; tmp=x+5.5; tmp -= (x+0.5)*Math.log(tmp);

ser=1.000000000190015; for (j=0;j<=5;j++) ser += cof[j]/++y; return -tmp+Math.log(2.5066282746310005*ser/x); } public static double erf(double x) { //error function from series solution return x < 0.0 ? -gammp(0.5,x*x) : gammp(0.5,x*x); } public static double erf_int(double x) { //error function from series solution f1 b1=new f1(); return adaptive_Lobatto_integral(b1,0.0,x); }

public static double adaptive_Lobatto_integral(f_x ff,double a,double b) {double tol=Double.MIN_VALUE; return adaptive_Lobatto_integral(ff,a,b,tol); } public static double adaptive_Lobatto_integral(f_x ff,double a,double b,double tol) { int i; double m=(a+b)/2.0; double h=(b-a)/2.0; double alpha=Math.sqrt(2.0/3.0); double beta=1.0/Math.sqrt(5.0); double x1,x2,x3; double eps =Double.MIN_VALUE; //System.out.println("eps="+eps); x1=0.94288241569547971905635175843185720232; x2=0.64185334234578130578123554132903188354; x3=0.23638319966214988028222377349205292599; double A,B,C,D,E,F,G; A=0.015827191973480183087169986733305510591; B=0.094273840218850045531282505077108171960; C=0.15507198733658539625363597980210298680; D=0.18882157396018245442000533937297167125; E=0.19977340522685852679206802206648840246; F=0.22492646533333952701601768799639508076; G=0.24261107190140773379964095790325635233; double x[]={a,(m-x1*h),(m-alpha*h),(m-x2*h),(m-beta*h),(mx3*h),m,(m+x3*h),(m+beta*h),(m+x2*h),(m+alpha*h),(m+x1*h),b}; int n=x.length; double y[]=new double[n]; for(i=0;i<n;i++) y[i]=ff.func(x[i]); double fa=y[0]; double fb=y[12]; double i2=(h/6.0)*(y[0]+y[12]+5.0*(y[4]+y[8])); double i1=(h/1470.0)*(77.0*(y[0]+y[12])+432.0*(y[2]+y[10])+ 625.0*(y[4]+y[8])+672.0*y[6]); double is=h*(A*(y[0]+y[12])+B*(y[1]+y[11])+C*(y[2]+y[10])+D*(y[3]+y[9])+E*(y[4]+y[8])+F*(y[5]+y[7])+G*y[6]); int s=(int)(is/Math.abs(is)); if(s==0) s=1; double erri1=Math.abs(i1-is); double erri2=Math.abs(i2-is); double R;

R=erri1/erri2; if(R>0 && R<1) tol=tol/R; //System.out.println("is="+is+"s="+s+"tol="+tol+"eps="+eps); if(is==0) is=b-a; double Q=0; Q=Lobatto(ff,a,b,fa,fb,is,tol); return Q; } public static double Lobatto(f_x ff,double a,double b,double fa,double fb,double is,double tol) { double h=(b-a)/2; double m=(a+b)/2; double alpha=Math.sqrt(2.0/3.0); double beta=1.0/Math.sqrt(5.0); double mll=m-alpha*h; double ml=m-beta*h; double mr=m+beta*h; double mrr=m+alpha*h; double x[]={mll,ml,m,mr,mrr}; int n=x.length; double y[]=new double[n]; for(int i=0;i<n;i++) y[i]=ff.func(x[i]); double fmll=y[0]; double fml=y[1]; double fm=y[2]; double fmr=y[3]; double fmrr=y[4]; double Q; double i2=(h/6)*(fa+fb+5*(fml+fmr)); double i1=(h/1470)*(77*(fa+fb)+432*(fmll+fmrr)+625*(fml+fmr)+672*fm); double di=Math.abs(i1-i2); if (((is + di) == is) || (mll <= a) || (b<=mrr) || di<tol) { if (((m <= a) || (b<=m))) { System.out.println("istenilen toleransa ulalamad delta i = "+di);} Q=i1; } else { Q=Lobatto(ff,a,mll,fa,fmll,is,tol) + Lobatto(ff,mll,ml,fmll,fml,is,tol)+ Lobatto(ff,ml,m,fml,fm,is,tol) + Lobatto(ff,m,mr,fm,fmr,is,tol)+ Lobatto(ff,mr,mrr,fmr,fmrr,is,tol)+Lobatto(ff,mrr,b,fmrr,fb,is,tol); } return Q; } public static void main(String args[]) throws IOException { double x=Double.parseDouble(JOptionPane.showInputDialog(" x = ")); System.out.println("erf("+x+") = "+erf(x)+"integral erf="+erf_int(x)); } }

Calculation of error function with Gander and Gautschi adaptive Gauss-Lobatto integral Formula output erf(0.1) = 0.1124629159993309 integral erf=0.1124629160182849

erf(0.4) = 0.4283923519241598 integral erf=0.42839235504666845 erf(1.0) = 0.8427007900291826 integral erf=0.8427007929497149 erf(2.0) = 0.995322264754383 integral erf=0.9953222650189527 7.6 ONE AND MULTI-DIMENSIONAL MONTE-CARLO INTEGRAL Monte Carlo intehral is based on a very simple principle

f ( x)dx = (b a) * f
a

a var age

x = a + (b a ) * random _ number(0,1)
f ortalama = i=0 f ( x) n
n

n >> 1

In the interval [a,b] a random x number is selected and function is evalueated in this point. When this sequaence is repeated for a very large number of times avarage value of the function can be calculated and directly used in the calculatetions of the integral. Integral is the calculateion of the avarage function value with the interval width. Due to fact that this method requires a huge amount of function evaluation, therefor it is not very paractical for one variable integration, but in multivariable form it becomes a paractical, sometimes only available approach. Program 7.6-1 One variable Monte Carlo integration
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Monte Carlo integration with one variable // Dr. Turhan Coban // ===================================================== import java.io.*; class f1 extends f_x { public double func(double x) {return 3.0*x*x;} } class NA73 { public static double monte_carlo(f_x ff,double a,double b, long n) { //Monte-carlo integrali double h=(b-a); double sum=0.0; double x=0.0; double f; for(int i=0;i<n;i++) { x=a+(b-a)*Math.random(); sum+=ff.func(x);

} return (b-a)*sum/(double)n; } public static void main(String args[]) throws IOException { f1 b1=new f1(); System.out.println("integral of class f1 : "+monte_carlo(b1,0.0,1.0,1000000)); } }

One variable Monte-carlo integration ---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA73 integral of class f1 : 0.9990266020806676 > Terminated with exit code 0. If Monte Carlo equation is written for two independent variables

x = ax + (bx ax ) * tesadfi _ say(0,1) y = ay + (by ay ) * tesadfi _ say(0,1)


n

f average =

f ( x, y )
i =0

n >> 1

ntegration subfield

( D) =
integration

by bx = ( x ) ay ax = ( x )

dy dx

I = ( D) f ortalama
As it is seen from the equation process fro one variable integration and two variable integraton is almost the same. THis is the main factor why it is frequently use as a high dimensional integration tool. Program 7.6-2 Monte carlo integration with two independent variables
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Monte Carlo integration with two variable // Dr. Turhan Coban // ===================================================== import java.io.*;

class f1 extends f_xj { public double func(double x[]) { //zm istenen fonksiyon double ff; ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1]; return -ff; //minimum deil maxsimum istendiinden - ile arptk } } class NA74 { public static double monte_carlo(f_xj ff,double a[],double b[], long n) { //Newton-cotes integral 6 points double x[]=new double[2]; double h=1.0; double sum=0; for(int j=0;j<2;j++) {h*=(b[j]-a[j]);} for(int i=0;i<n;i++) { for(int j=0;j<2;j++) {x[j]=a[j]+(b[j]-a[j])*Math.random();} sum+=ff.func(x); } return h*sum/(double)n; } public static void main(String args[]) throws IOException { f1 b1=new f1(); double a[]=new double[2]; double b[]=new double[2]; a[0]=-1; a[1]=-1; b[0]=1; b[1]=1; System.out.println("integral of class f1 : "+monte_carlo(b1,a,b,1000000)); }}

Output of Monte carlo integration with two independent variables ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA74 integral of class f1 : 4.009412425743507 > Terminated with exit code 0. 7.7 MULTIDIMENSIONAL INTEGRATION BY PARAMETRIC METHODS A two dimensional integration

I(D) = D f ( x, y ) dydx
Where D is a region in xy plane. If this region is defined as a x b and c( x) y d ( x) , one way two calculate two dimensional integration is to repeat one dimensional integration twice. In the first

step intehration function g(x) is calculated as an integration in y dimension and then function g(x) is solved to get integration result.

I =

b d ( x)

a c( x)

f ( x, y)dydx
d ( x) c( x)
b

g(x) =

f ( x, y)dy,

a xb

I(D) = g ( x ) dx
a

Any integration formulation can be used for this purpose. All it has to be done is to evaluate integration Formula twice onece for each independent variable. As our first example Gauss-Legendre formulation will be used to evaluate double integration. Two code is defined. In the first one GaussLegendre coefficients are defined as constants, in the second one they are calculated through Legendre polynomials. In the two dimensional integration x dimesion limits are stil given as constant values, but y dimension limits cx) and d(x) should be given as functions for proper evaluation of the integral.

I =
a

d ( x)

c( x)

(d ( x) c( x))rn , j + d ( x) + c( x) d ( x) c( x) n f ( x, y )dy = c n , j f ( x, )dx 2 2 J =1 a


b

In this equation rn,j root values and cn,j are equation coefficients. These values can be calculated from Legendre polynomials. Program 7.7-1 60 constant points two variable Gauss-Legendre integration formula
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Gauss-Legendre integration with two variables // constant number of coefficients (60 coefficients) // and roots // Dr. Turhan Coban // ===================================================== import java.io.*; abstract class fdi { public double a,b; public fdi(double ai,double bi) {a=ai;b=bi;} public fdi() {a=0;b=1;} public void setab(double ai,double bi) {a=ai;b=bi;} abstract public double c(double x); abstract public double d(double x); abstract public double f(double x[]); } class f1 extends fdi

{ public f1(double ai,double bi) {super(ai,bi);} //lower integration limit function public double c(double x) {return 1.0;} //upper integration limit function public double d(double x) {return 1.5;} public double f(double x[]) { //function required to integrate double ff; ff=Math.log(x[0]+2.0*x[1]); return ff; } }

class NA75 { public static double I(fdi f) { //integral f(x)dx //integral of a function by using gauss-legendre quadrature //coefficients are pre-calculated for 60 terms for [-1,1] //band then utilises variable transform int i,j; int n=60; double r[],c[]; r=new double[n]; c=new double[n]; r[ 0] = .15532579626752470000E-02; r[ 1] = .81659383601264120000E-02; r[ 2] = .19989067515846230000E-01; r[ 3] = .36899976285362850000E-01; r[ 4] = .58719732103973630000E-01; r[ 5] = .85217118808615820000E-01; r[ 6] = .11611128394758690000E+00; r[ 7] = .15107475260334210000E+00; r[ 8] = .18973690850537860000E+00; r[ 9] = .23168792592899010000E+00; r[10] = .27648311523095540000E+00; r[11] = .32364763723456090000E+00; r[12] = .37268153691605510000E+00; r[13] = .42306504319570830000E+00; r[14] = .47426407872234120000E+00; r[15] = .52573592127765890000E+00; r[16] = .57693495680429170000E+00; r[17] = .62731846308394490000E+00; r[18] = .67635236276543910000E+00; r[19] = .72351688476904450000E+00; r[20] = .76831207407100990000E+00; r[21] = .81026309149462140000E+00; r[22] = .84892524739665800000E+00; r[23] = .88388871605241310000E+00;

r[24] = .91478288119138420000E+00; r[25] = .94128026789602640000E+00; r[26] = .96310002371463720000E+00; r[27] = .98001093248415370000E+00; r[28] = .99183406163987350000E+00; r[29] = .99844674203732480000E+00; c[ 0] = .39840962480827790000E-02; c[ 1] = .92332341555455000000E-02; c[ 2] = .14392353941661670000E-01; c[ 3] = .19399596284813530000E-01; c[ 4] = .24201336415292590000E-01; c[ 5] = .28746578108808720000E-01; c[ 6] = .32987114941090080000E-01; c[ 7] = .36877987368852570000E-01; c[ 8] = .40377947614710090000E-01; c[ 9] = .43449893600541500000E-01; c[10] = .46061261118893050000E-01; c[11] = .48184368587322120000E-01; c[12] = .49796710293397640000E-01; c[13] = .50881194874202750000E-01; c[14] = .51426326446779420000E-01; c[15] = .51426326446779420000E-01; c[16] = .50881194874202750000E-01; c[17] = .49796710293397640000E-01; c[18] = .48184368587322120000E-01; c[19] = .46061261118893050000E-01; c[20] = .43449893600541500000E-01; c[21] = .40377947614710090000E-01; c[22] = .36877987368852570000E-01; c[23] = .32987114941090080000E-01; c[24] = .28746578108808720000E-01; c[25] = .24201336415292590000E-01; c[26] = .19399596284813530000E-01; c[27] = .14392353941661670000E-01; c[28] = .92332341555455000000E-02; c[29] = .39840962480827790000E-02; for(i=0;i<30;i++) {r[i+30]=-r[i];c[i+30]=c[i];} int m=n; double x[],h1,h2,J,a1,b1,c1,d1,k1,k2,JX,Q; x=new double[2]; //1========================= a1=f.a; b1=f.b; h1=(b1-a1)/2.0; h2=(b1+a1)/2.0; J=0; //2========================= for(i=0;i<m;i++) { //3========================= JX=0; x[0]=h1*r[i]+h2; d1=f.d(x[0]); c1=f.c(x[0]); k1=(d1-c1)/2.0; k2=(d1+c1)/2.0; //4========================= for(j=0;j<n;j++)

{ x[1]=k1*r[j]+k2; Q=f.f(x); JX+=c[j]*Q; } //System.out.println("JX="+JX); //========================= J=J+c[i]*k1*JX; } J=h1*J; return J; } public static void main(String args[]) throws IOException { f1 ff=new f1(1.4,2.0); System.out.println("integral of class f1 : "+I(ff)); } }

constant points two variable Gauss-Legendre integration formula output ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA75 integral of class f1 : 0.429554527548266 > Terminated with exit code 0. In the next example, instead of constant number of points Legendre transformation defined varible number of points ar taken. Program 7.7-2 variable points two variable Gauss-Legendre integration formula
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Gauss-Legendre integration with two variables // variable number of coefficients and roots // calculated by Legendre polynomials // Dr. Turhan Coban // ===================================================== import java.io.*; abstract class fdi { public double a,b; public fdi(double ai,double bi) {a=ai;b=bi;} public fdi() {a=0;b=1;} public void setab(double ai,double bi) {a=ai;b=bi;} abstract public double c(double x); abstract public double d(double x); abstract public double f(double x[]); }

class f1 extends fdi { public f1(double ai,double bi) {super(ai,bi);} public double c(double x) {return 1.0;} public double d(double x) {return 1.5;} public double f(double x[]) { //zm istenen fonksiyon double ff; ff=Math.log(x[0]+2.0*x[1]); return ff; } } class NA76 { public static double[][] gauss_legendre_coefficients(double x1,double x2,int n) { //calculates legendre gauss-coefficients as coefficients of the integral //for n terms double EPS=3.0e-15; int m,j,i; double z1,z,xm,xl,pp,p3,p2,p1; double a[][]=new double[2][n]; m=(n+1)/2; xm=0.5*(x2+x1); xl=0.5*(x2-x1); for (i=1;i<=m;i++) { z=Math.cos(Math.PI*((i-0.25)/(n+0.5))); do { p1=1.0; p2=0.0; for (j=1;j<=n;j++) { p3=p2; p2=p1; p1=((2.0*j-1.0)*z*p2-(j-1.0)*p3)/j; } pp=n*(z*p1-p2)/(z*z-1.0); z1=z; z=z1-p1/pp; } while (Math.abs(z-z1) > EPS); a[0][i-1]=xm-xl*z; a[0][n-i]=xm+xl*z; a[1][i-1]=2.0*xl/((1.0-z*z)*pp*pp); a[1][n-i]=a[1][i-1]; } return a; } public static double I(fdi f,int n,int m)

{ //integral f(x,y)dxdy //integral of a function by using gauss-legendre quadrature //coefficients are pre-calculated for n,m terms for [-1,1] //band then utilises variable transform int i,j; double rn[],cn[]; double rm[],cm[]; rn=new double[n]; rm=new double[m]; cn=new double[n]; double a[][]=new double[2][n]; a=gauss_legendre_coefficients(-1.0,1.0,n); rn=a[0]; cn=a[1]; a=gauss_legendre_coefficients(-1.0,1.0,m); rm=a[0]; cm=a[1]; double x[],h1,h2,J,a1,b1,c1,d1,k1,k2,JX,Q; x=new double[2]; a1=f.a; b1=f.b; h1=(b1-a1)/2.0; h2=(b1+a1)/2.0; J=0; for(i=0;i<m;i++) { JX=0; x[0]=h1*rm[i]+h2; d1=f.d(x[0]); c1=f.c(x[0]); k1=(d1-c1)/2.0; k2=(d1+c1)/2.0; //4========================= for(j=0;j<n;j++) { x[1]=k1*rn[j]+k2; Q=f.f(x); JX+=cn[j]*Q; } //System.out.println("JX="+JX); //5========================= J=J+cm[i]*k1*JX; } J=h1*J; return J; }

public static double I(fdi f,int n) {return I(f,n,n);} public static double I(fdi f) {return I(f,60,60);} public static void main(String args[]) throws IOException { f1 ff=new f1(1.4,2.0);

System.out.println("integral of class } }

f1 : "+I(ff));

variable points two variable Gauss-Legendre integration Formula output ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA76 integral of class f1 : 0.4295545275482747 > Terminated with exit code 0. For the two variable integration Newton-Cotes formulas can also be used. As a example a Simpson 1/3 two dimensional integration code is prepared. Dor the quadratic polynomial Newton-Cotes equation (simpson 1/3 method) equations can be written as follows; One dimensional orm was
h= (b a ) (b a ) = n 2
b

I = a f ( x)dx =

(b a ) [ f ( x0 ) + 4 f ( x1 ) + f ( x2 )] 6

If this Formula can be written in composite form.

I = f ( x)dx =
a

( n / 2 ) 1 ( n / 2) h f (a ) + 2 f ( x 2 j ) + 4 f ( x 2 j 1 ) 3 j =1 j =1

This Formula can be open to the second dimension similar to Gauss-Legendre integration By defining k =
d ( x)

I=

c( x)

(d ( x) c( x)) integration equation becomes m ( m / 2 ) 1 ( m / 2) k f ( x, y )dy = f ( x, y0 ) + 2 f ( x, y 2 j ) + 4 f ( x, y 2 j 1 ) + f ( x, y m ) 3 j =1 j =1

From here x integration can be carried out as:

I =
a

b ( m / 2 ) 1 ( m / 2) k f ( x, y )dy = f ( x, y 0 ) + 2 f ( x, y 2 j ) + 4 f ( x, y 2 j 1 ) + f ( x, y m ) dx 3 j =1 j =1 a

The same integration equation is then applied to the outer integration. Complete code is given in the program 8.7.3 . In the example the same function as in previous Gauss-Legendre integration is taken to compare the results Program 7.7-3 two dimensional Simpson 1/3 integration formula
//====================================================== // Numerical Analysis package in java // simpson integration of two dimensional function // and differentiation (derivative) functions // two dimensional Simpson 1/3 integration // (quadratic Newton-Cotes formula) // Dr. Turhan Coban // =====================================================

import java.io.*; abstract class fdi { public double a,b; public fdi(double ai,double bi) {a=ai;b=bi;} public fdi() {a=0;b=1;} public void setab(double ai,double bi) {a=ai;b=bi;} abstract public double c(double x); abstract public double d(double x); abstract public double f(double x[]); } class f1 extends fdi { public f1(double ai,double bi) {super(ai,bi);} public double c(double x) {return 1.0;} public double d(double x) {return 1.5;} public double f(double x[]) { //zm istenen fonksiyon double ff; ff=Math.log(x[0]+2.0*x[1]); return ff; } }

class NA77 { public static double I(fdi f,int m,int n) { //integral f(x)dx //integral of a function by using simpson metod double h=(f.b-f.a)/n; double J1=0.0; double J2=0.0; double J3=0.0; double x[]=new double[2]; double HX=0; double K1,K2,K3; double d,c; double Q,L,J; for(int i=0;i<=n;i++) { x[0]=f.a+i*h; d=f.d(x[0]); c=f.c(x[0]);

HX=(d-c)/m; x[1]=c;K1=f.f(x); x[1]=d;K1+=f.f(x); K2=0; K3=0; for(int j=1;j<=(m-1);j++) {x[1]=f.c(x[0])+j*HX; Q=f.f(x); if((j/2)*2==j) K2=K2+Q; else K3=K3+Q; } L=(K1+2.0*K2+4.0*K3)*HX/3.0; if((i==0)||(i==n)) J1=J1+L; else if ((i/2)*2==i) J2=J2+L; else J3=J3+L; } J=h*(J1+2.0*J2+4.0*J3)/3.0; return J; } public static void main(String args[]) throws IOException { f1 ff=new f1(1.4,2.0); System.out.println("integral of class f1 : "+I(ff,20,20)); } }

two dimensional Simpson 1/3 integration formula ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA77 integral of class f1 : 0.4295545273061155 > Terminated with exit code 0. Now the same principle can be used for three dimensional integration process. A three dimensional integral can be defined as:

I =

b d ( x) ( x, y ) a c( x) ( x, y )

f ( x, y, z )dzdydx

Gauss-Legendre integration will be used here as a solution method.

I = y 2 zdzdydx
0 x 0

1 1 y

Program 7.7-4 three dimensional Gauss-Legendre integration


//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions

// Gauss-Legendre integration with three independent // variable number of coefficients and roots // calculated by Legendre polynomials // Dr. Turhan Coban // ===================================================== import java.io.*; abstract class fdi3 { public double a,b; public fdi3(double ai,double bi) {setab(ai,bi);} public void setab(double ai,double bi) {a=ai;b=bi;} abstract public double c(double x); abstract public double d(double x); abstract public double alfa(double x[]); abstract public double beta(double x[]); abstract public double f(double x[]); } class f1 extends fdi3 { public f1(double ai,double bi) {super(ai,bi);} public double c(double x) {return x;} public double d(double x) {return 1.0;} public double alfa(double x[]) {return 0.0;} public double beta(double x[]) {return x[1];} public double f(double x[]) { //zm istenen fonksiyon double ff; ff=x[1]*x[1]*x[2]; return ff; } } class NA78 { public static double[][] gauss_legendre_coefficients(double x1,double x2,int n) { //calculates legendre gauss-coefficients as coefficients of the integral //for n terms double EPS=3.0e-15; int m,j,i; double z1,z,xm,xl,pp,p3,p2,p1; double a[][]=new double[2][n];//a[0][i]=x[i] a[1][i]=w[i] m=(n+1)/2;

xm=0.5*(x2+x1); xl=0.5*(x2-x1); for (i=1;i<=m;i++) { z=Math.cos(Math.PI*((i-0.25)/(n+0.5))); do { p1=1.0; p2=0.0; for (j=1;j<=n;j++) { p3=p2; p2=p1; p1=((2.0*j-1.0)*z*p2-(j-1.0)*p3)/j; } pp=n*(z*p1-p2)/(z*z-1.0); z1=z; z=z1-p1/pp; } while (Math.abs(z-z1) > EPS); a[0][i-1]=xm-xl*z; a[0][n-i]=xm+xl*z; a[1][i-1]=2.0*xl/((1.0-z*z)*pp*pp); a[1][n-i]=a[1][i-1]; } return a; } public static double I(fdi3 f,int n,int m,int p) { //integral f(x)dx //integral of a function by using gauss-legendre quadrature //coefficients are pre-calculated for n,m terms for [-1,1] //band then utilises variable transform int i,j; double rn[],cn[]; double rm[],cm[]; double rp[],cp[]; rp=new double[p]; cp=new double[p]; int nn=Math.max(n,Math.max(m,p)); int k; double x[]=new double[3]; double a[][]=new double[2][nn]; int mm=Math.max(Math.max(n,m),p); a=gauss_legendre_coefficients(-1.0,1.0,mm); rn=rm=rp=a[0]; cn=cm=cp=a[1]; double h1,h2,J,a1,b1,c1,d1,k1,k2,JX,JY,Q,l1,l2,alfa1,beta1; a1=f.a; b1=f.b; h1=(b1-a1)/2.0; h2=(b1+a1)/2.0; J=0; for(i=0;i<m;i++) { JX=0; x[0]=h1*rm[i]+h2; d1=f.d(x[0]); c1=f.c(x[0]); k1=(d1-c1)/2.0; k2=(d1+c1)/2.0;

//4========================= for(j=0;j<n;j++) { JY=0; x[1]=k1*rn[j]+k2; beta1=f.beta(x); alfa1=f.alfa(x); l1=(beta1-alfa1)/2.0; l2=(beta1+alfa1)/2.0; for(k=0;k<p;k++) { x[2]=l1*rp[k]+l2; Q=f.f(x); JY+=cp[k]*Q; } JX+=cn[j]*l1*JY; } //System.out.println("JX="+JX); //5========================= J=J+cm[i]*k1*JX; } J=h1*J; return J; } public static double I(fdi3 f,int n) {return I(f,n,n,n);} public static double I(fdi3 f) {return I(f,60,60,60);} public static void main(String args[]) throws IOException { f1 ff=new f1(0.0,1.0); ; System.out.println("ntegral="+I(ff)); } }

---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA78 Integral=0.08333333333333286 > Terminated with exit code 0. 7.8 DIRECT INTEGRATION OF DATA If integration of a numerical data set is given two approches an be adapted fort his process. The first one is to curve fit a function first and then integrate the fitted function. The second one is to apply an integration equation like Newton-Cotes equation directly to the data to achiv integrated result without an intermediate process. As an example to curve fitting integration a cubic spline integration will be described here. If a cubic polynomial of the following form is given : sk(x)=ak(x-xk)+ bk(xk+1-x)+ [(x-xk)3 ck+1 +(xk+1-x)3 ck]/(6hk) 1 k n Integration of the equation could be described with the equation

( x xk ) 2 ( xk +1 x) 2 ( x xk ) 4 ( xk +1 x) 4 Sk ( x)dx = ak 2 bk 2 + ck 24hk d k 24hk x=a a


b

For a, x0, x1, x2,. xn,b region. An example program using this approach is given as: Program 7.8-1 Direct integration of data by using cubic polynomial fitting
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Direct data integration by using cubic spline // curve fitting // Dr. Turhan Coban // ===================================================== import java.io.*; import javax.swing.*; class NA79 { public static double [] thomas(double a[][],double r[]) { // int n=a.length; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double x[]=new double[n]; for(int i=0;i<n;i++) {f[i]=a[i][i];} for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];} for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];} for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; } public static double [] thomas(double f[],double e[],double g[],double r[]) { int n=f.length; double x[]=new double[n]; for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--)

{x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; }

public static double [][] cubic_spline(double xi[],double yi[],double c0,double cn) { // c0 second derivative of the first point // cn second derivative of the last point int n=xi.length; double h[]=new double[n]; double w[]=new double[n]; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double d[]=new double[n]; double x[]=new double[n]; double S[][]=new double[4][n]; int k; for(k=0;k<(n-1);k++) {h[k]=xi[k+1]-xi[k]; w[k]=(yi[k+1]-yi[k])/h[k]; } d[0]=c0; d[n-1]=cn; for(k=1;k<(n-1);k++) {d[k]=6.0*(w[k]-w[k-1]);} f[0]=1.0; f[n-1]=1.0; g[0]=0.0; g[n-1]=0.0; e[0]=0.0; e[n-1]=0.0; for(k=1;k<(n-1);k++) {f[k]=2.0*(h[k]+h[k-1]);e[k]=h[k-1];g[k]=h[k];} S[2]=thomas(f,e,g,d); S[3]=xi; for(k=0;k<(n-1);k++) {S[0][k]=(6.*yi[k+1]-h[k]*h[k]*S[2][k+1])/(6.0*h[k]); S[1][k]=(6.*yi[k]-h[k]*h[k]*S[2][k])/(6.0*h[k]); } return S; } public static double funcSpline(double S[][],double x) { int n=S[0].length; double xx1=0; double xx2=0; double y=0; double hk=0; for(int k=0;k<(n-1);k++) {if(S[3][k]<=x && x<=S[3][k+1]) {hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); break;

} } if(y==0 && S[3][n-2]<=x ) { int k=n-2; hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); } return y; } // Derivative formulations // ================= public static double dfSpline(double xi[],double yi[],double c0,double cn,double x) { //kbik erit trev forml double S[][]=cubic_spline(xi,yi,c0,cn); return dfSpline(S,x); } public static double dfSpline(double S[][],double x) { //derivative of the cubic spline formula int n=S[0].length; double xx1=0; double xx2=0; double y=0; double hk=0; for(int k=0;k<(n-1);k++) {if(S[3][k]<=x && x<=S[3][k+1]) {hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]-S[1][k]+(xx1*xx1*S[2][k+1]-xx2*xx2*S[2][k])/(2.0*hk); break; } } if(y==0 && S[3][n-2]<=x ) { int k=n-2; hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); } return y; } public static double dfSpline(double xi[],double yi[],double x) { //natural cubic spline formula double S[][]=cubic_spline(xi,yi,0,0); return dfSpline(S,x); } // Integration formulas // =================== public static double intSpline(double S[][],double a,double b)

{ //cubic spline integration int n=S[0].length; double xx1=0; double xx2=0; double y1,y2; double hk=0; double toplam=0; y1=0;y2=0; for(int k=0;k<(n-1);k++) { hk=(S[3][k+1]-S[3][k]); //System.out.println("x1="+S[3][k]+"x2="+S[3][k+1]+"a="+a+"b="+b); if(a>S[3][k+1]) {toplam=0;//System.out.println("art 1 toplam = "+toplam); }//art1 else if(S[3][k]<=a && a<=S[3][k+1] && b>S[3][k+1])//art 2 { xx1=(a-S[3][k]); xx2=(S[3][k+1]-a); y1=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk); xx1=hk; xx2=0; y2=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk); toplam+=(y2-y1); //System.out.println("art 2toplam = "+toplam+"y1="+y1+"y2="+y2); } else if(S[3][k]<=a && a<=S[3][k+1] && S[3][k]<=b && b<=S[3][k+1])//art 3 { xx1=(a-S[3][k]); xx2=(S[3][k+1]-a); y1=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk); xx1=(b-S[3][k]); xx2=(S[3][k+1]-b); y2=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk); toplam+=(y2-y1); //System.out.println("art 3 toplam = "+toplam+"y1="+y1+"y2="+y2); } else if(a<S[3][k] && b>=S[3][k+1]) //art 4 { xx1=0; xx2=hk; y1=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk); xx1=hk; xx2=0; y2=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk); toplam+=(y2-y1); //System.out.println("art 4 toplam = "+toplam+"y1="+y1+"y2="+y2); } else if(S[3][k]<=b && b<=S[3][k+1])//art 5 {xx1=0; xx2=hk; y1=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk); xx1=(b-S[3][k]); xx2=(S[3][k+1]-b); y2=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk); toplam+=(y2-y1); //System.out.println("art 5 toplam = "+toplam+"y1="+y1+"y2="+y2); }

else break; } return toplam; } public static double intSpline(double xi[],double yi[],double c0,double cn,double a,double b) {double S[][]=cubic_spline(xi,yi,c0,cn); return intSpline(S,a,b); } public static void main(String args[]) throws IOException { double x[]; double y[]; String s1="a.txt"; //JFileChooser fc=new JFileChooser(); //if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); } double a[][]=Text.readDoubleT(s1); x=a[0]; y=a[1]; int n=x.length; double S[][]=cubic_spline(x,y,2.0,2.0); System.out.println("s="+intSpline(S,0.0,10.0)); } }

Following data is taken to integrate 00 11 24 5 25 6 36 7 49 8 64 8 81 10 100 Sample data is created from function f(x)=x2. Therefore integration between 0 and 10 will give 333.3333333 as result. Cubic spline direct data integration ---------- Capture Output ---------> "C:\co\java\bin\java.exe" SCO11FB s=333.3333333333334 > Terminated with exit code 0.

7.9 DERIVATION In order to approximate derivation difference equations can be used.Difernece equations can basically be derived from Taylor formulations

f ( xi +1 ) = f ( xi ) + f ' ( xi )h +

f " ( xi ) 2 f ( 3 ) ( xi ) 3 f ( 4 ) ( xi ) 4 h + h + h + ... 2 3! 4!

If the first derivative is solvedfrom this equation it is found as


f ' ( xi ) = f ( xi +1 ) f ( xi ) f " ( xi ) f ( 3 ) ( xi ) 2 f ( 4 ) ( xi ) 3 h h h + O(h 4 )... 2 3! 4! h f ( xi +1 ) f ( xi ) + O ( h) h

or
f ' ( xi ) =

Term O(h) is the order of error. Error is proprtional to the difference parameter h. When h is getting smaller, error wil also shrinked. F the second derivative is written the same way:
f " ( xi ) = f ( xi + 2 ) 2 f ( xi +1 ) + f ( xi ) + O ( h) h2

In order to decrease the error, if the second derivative difference equation is substituted into thefirst derivative Taylor equation a first derivative equation with a second order error can be obtained.
f ( xi +1 ) f ( xi ) f ( xi + 2 ) 2 f ( xi +1 ) + f ( xi ) h + O(h 2 ) h 2h 2 2 f ( xi +1 ) 2 f ( xi ) f ( xi + 2 ) + 2 f ( xi +1 ) f ( xi ) f ' ( xi ) = + O(h 2 ) h f ( xi + 2 ) + 4 f ( xi +1 ) 3 f ( xi ) f ' ( xi ) = + O(h 2 ) 2h f ' ( xi ) =

Differnce equation cn be written as forward or bakward way. The backward difference equation:
f ' ( xi ) = f ( xi ) f ( xi 1 ) + O(h) . By taken avarage of forward and bakward formulas can create a cenral h
f ( xi ) f ( xi 1 ) f ( xi ) f ( xi 1 ) f ( xi +1 ) f ( xi 1 ) h h + = + O(h 2 ) 2 2 2h

formula
f ' ( xi ) ileri + f ' ( xi ) geri f ' ( xi ) = = 2

Difference formulas for calculating derivatives are given (as coefficients) in the following table Derivative (Difference) equations i+4 i+3 i+2 f' f' f' f' f' f' f" f"

i i-1 i-2 1.000000 1.000000 -0.500000 2.000000 1.500000 1.000000 1.000000 1.500000 2.000000 0.500000 0.500000 0.500000 -0.083333 0.666667 0.666667 0.083333 1.000000 2.000000 1.000000 1.000000 4.000000 5.000000 2.000000

i+1

i-3

i-4 forward forward

backward

backward central central forward forward

f" f" f" f" f f f f f f 0.500000 0.166667 2.000000 1.000000 6.500000 0.250000 -0.083333 1.333333 1.000000 -3.000000 3.000000 -1.500000 7.000000 12.000000 9.000000

1.000000 2.000000 2.000000 5.000000 0.500000 2.500000 1.333333 1.000000 2.500000 1.000000 3.000000 2.500000 9.000000 0.000000 1.000000 9.333333 6.500000

1.000000 4.000000 0.250000 -0.083333 1.000000

backward

backward central central forward

forward 3.000000 1.000000 backward 12.000000 7.000000 1.500000 backward -0.500000 2.000000 0.166667 central central

A derivative calculating program is given as an example Program 7.9-1 derivative(usage of difference equations)
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions //Derivative formulas // Dr. Turhan Coban // ===================================================== import java.io.*; class f1 extends f_x { double func(double x) {return x*x*x*x*x;} } class NA81 { public static double derivative(f_x f,double x) { double h0=0.001; int i,m; int n=7; double T[][]=new double[n][n]; double h[]; h=new double[n]; h[0]=h0; double r=0.5; for( i=1;i<n;i++) {h[i]=h0*Math.pow(r,i);} for(i=0;i<n;i++) {T[i][0]=dfdx(f,x,h[i]);}

for(m=1;m<n;m++) { for(i=0;i<n-m;i++) {T[i][m]=(h[i]*h[i]*T[i+1][m-1]-h[i+m]*h[i+m]*T[i][m-1])/(h[i]*h[i]- h[i+m]*h[i+m]);} } double xx=T[0][n-1]; return xx; } public static double dfdx_richardson(f_x f,double x) { double h0=0.01; double h=h0; int i,m; int n=10; double T[][]=new double[n][n]; for(i=0;i<n;i++) {T[i][0]=dfdx(f,x,h);h/=2.0;} for(m=1;m<n;m++) { for(i=0;i<n-m;i++) {T[i][m]=(4.0/3.0*T[i+1][m-1]-1.0/3.0*T[i][m-1]);} } double xx=T[0][n-1]; return xx; }

public static double dfdx(f_x f,double x) { double h=0.00001; return dfdx(f,x,h); } public static double dfdx(f_x f,double x,double h) { double hh=1.0/h; return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/12.0*hh; } public static double d2fdx2(f_x f,double x) { double h=0.00001; return d2fdx2(f,x,h); } public static double d2fdx2(f_x f,double x,double h) { double hh=1.0/h; return (-f.func(x+2.0*h)+16.0*f.func(x+h)-30.0*f.func(x)+16.0*f.func(x-h)-f.func(x-2.0*h))/(12.0)*hh*hh;} public static double d3fdx3(f_x f,double x) { double h=0.00001; return d3fdx3(f,x,h);} public static double d3fdx3(f_x f,double x,double h) { double hh=1.0/h; return (-f.func(x+3.0*h)+8.0*f.func(x+2.0*h)-13.0*f.func(x+h)+13.0*f.func(x-h)-8.0*f.func(x-2.0*h)+f.func(x3.0*h))/(8.0)*hh*hh*hh;} public static void main(String args[]) { f1 b1=new f1(); System.out.println("derivative dfdx : "+dfdx(b1,1.0));

System.out.println("derivative d2dfx2 : "+d2fdx2(b1,1.0)); System.out.println("derivative d3dfx3 : "+d3fdx3(b1,1.0)); System.out.println("derivative dfdx : "+derivative(b1,1.0)); System.out.println("derivative dfdx : "+dfdx_richardson(b1,1.0)); } }

derivative(usage of difference equations) ---------- Capture Output ---------> "D:\java\bin\javaw.exe" NA81 derivative dfdx : 4.999999999989272 derivative d2dfx2 : 20.00000674332961 derivative d3dfx3 : 60.14633235906783 derivative dfdx : 5.000000000009831 derivative dfdx : 4.99999999994733 > Terminated with exit code 0. 7.10 RICHARDSON EXTRAPOLATION IN DERIVATION Richardson extrapolaton process that previously seen in integration can also be applied into derivation. Richardson extrapolation Formula was

D D ( h2 ) +

1 [ D(h2 ) D(h1 )] (h1 / h2 ) 2 1

If h2=h1/2 equation becomes

4 1 D(h2 ) D(h1 )] 3 3

This equation can be written in the iterative two dimensional form as:

D j ,k

4 k 1 D j +1,k 1 D j ,k 1 4 k 1 1

7.11 PADE APPROXIMATIONS One of the most accurate derivative approximations can be derived by using Taylor series procedures. If the functional values at certain points are given Formula can be include the derivatives as well. For example, we can ask for the most accurate formula that includes fj, fj+1 and fj-1 in addition to the functional values f j , f j+1, and f j1 in addition to the functional values f j , f j+1, and f j1. we would write

f j' + a0 f j + a1 f j +1 + a 2 f j 1 + a3 f j' +1 + a 4 f j' 1 = O(?)


And our task is then find the five coefficients a0, a1, . . . , a4 to maximize the order of this approximation. The Taylor table is
TAYLOR TABLE FOR A PADE SCHEME

fj a0fj a1fj+1 a2fj-1 a3fj+1 a4fj-1

fj 0 a0 a1 a2 0 0

fj 1 0 a1h -a2h a3 a4

fj 0 0 a1h2/2 a2h2/2 a3h -a4h

fj(3) 0 0 a1h3/6 -a2h3/6 a3h2/2 a4h2/2

fj(4) 0 0 a1h4/24 a2h4/24 a3h3/6 -a4h3/6

fj(5) 0 0 a1h5/120 -a2h5/120 a3h4/24 a4h4/24

we now sum all the rows and set as many of the lower order terms to zero as possible. We have five coefficients and can set the sum of the entries in columns 2 to 6 to zero. The linear equations for the coefficients are a0 + a1 + a2 = 0 a1h a2h + a3 + a4 = 1

h2 h2 a1 + a2 + a3 h a 4 h = 0 2 2 h h a1 a2 + a3 + a4 = 0 3 3 h h a1 + a2 + a3 a4 = 0 4 4
The solution set is:

a0=0 a1 =

3 3 a2 = 4h 4h

a3 = a 4 =

1 4

This solution will lead to Pad Formula for numerical differential as

f j'+1 + f j'1 + 4 f j' =

h 4 ( 5) 3 fj ( f j +1 f j 1 ) + h 30

This equation requires two more equation to solve complete set of differentiation. Additional equations can be taken as:

f 0' + 2 f1' = f n' + 2 f n' 1

1 5 1 ( f 0 + 2 f1 + f 2 ) h 2 2 1 5 1 = ( f n + 2 f n 1 f n 2 ) h 2 2 0 0 1 .. .. 1 5 0 f 0' f 0 + 2 f1 + f 2 2 2 3( f 2 f 0 ) 0 f1' 3( f 3 f1 ) 0 f 2' .. .. .. = .. .. .. ' 3( f n f n 1 ) 1 4 1 f n 1 2 1 f n' 5 f 2 f 1 f n n 1 n2 2 2 0 0 0 .. .. 0 0 0 .. ..

In matrix form equations can be written as

1 1 0 .. ..

2 4 1 .. ..

0 1 4 .. ..

This matrix is a tridiagonal matrix can be solved by using Thomas algorithm. Program 7.11-1 derivative by Pad Formula
import java.io.*; import javax.swing.*; class f1 extends f_x { double func(double x) {return Math.sin(5.0*x);} } class f2 extends f_x {//exact derivative of the function double func(double x) {return 5.0*Math.cos(5.0*x);} } class NA81A

{ public static double [] thomas(double a[][],double r[]) { //Pad formula int n=a.length; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double x[]=new double[n]; for(int i=0;i<n;i++) {f[i]=a[i][i];} for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];} for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];} for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; } public static double [] thomas(double f[],double e[],double g[],double r[]) { int n=f.length; double x[]=new double[n]; for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; } public static double[][] df(f_x ff,double x0,double xn, int n) { int nn=n+1; double h=(xn-x0)/n; double y[]=new double[nn]; double x[]=new double[nn]; double f[]=new double[nn]; double g[]=new double[nn]; double e[]=new double[nn]; for(int i=0;i<=n;i++) {x[i]=x0+h*i;} for(int i=0;i<=n;i++) { if(i==0) {f[i]=1;} else if(i==n) {f[i]=1;} else {f[i]=4;} if(i==1) {g[i]=2;} else if(i==n) {g[i]=0;} else {g[i]=1;} if(i==(n-1)) {e[i]=2;} else if(i==0) {e[i]=0;} else {e[i]=1;} if(i==0) {y[i]=1.0/h*(-2.5*ff.func(x[0])+2.0*ff.func(x[1])+0.5*ff.func(x[2]));} else if(i==n) {y[i]=1.0/h*(2.5*ff.func(x[n])-2.0*ff.func(x[n-1])-0.5*ff.func(x[n-1]));} else {y[i]=(1.0/h*3.0*(ff.func(x[i+1])-ff.func(x[i-1])));}

} double a[][]=new double[3][nn]; a[0]=x; a[1]=y; a[2]=thomas(f,e,g,y); return a; } public static void main(String args[]) throws IOException { double a[][]=df(new f1(),0.0,3.0, 15); Plot pp=new Plot(a[0],a[2]); int n=a[0].length; double y[]=new double[n]; f2 dy=new f2(); for(int i=0;i<n;i++) {y[i]=dy.func(a[0][i]); System.out.println(a[0][i]+" "+a[2][i]+" "+y[i]); } pp.addData(a[0],y); pp.plot(); } }

7.12 DIRECT DERIVATION OF THE NUMERICAL DATA If data is given mumerically, two different approach can be used to take the derivative of the function. The first approximation is to curve fit a function into the data and then take the derivative of the function. The secon one is to fit difference equations directly to the given data In order to define a function, interpolation equations can be used. For example if quadratic Lagrange Formula is used:

f 2 ( x) =

( x xi )( x xi +1 ) ( x xi 1 )( x xi +1 ) ( x xi 1 )( x xi ) f ( xi 1 ) + f ( xi ) + f ( xi +1 ) ( xi 1 xi )( xi 1 xi +1 ) ( xi xi 1 )( xi xi +1 ) ( xi +1 xi 1 )( xi +1 xi )

Derivative of this function will be:

f 2' ( x) =

(2 x xi xi +1 ) (2 x xi 1 xi +1 ) (2 x xi 1 xi ) f ( xi 1 ) + f ( xi ) + f ( xi +1 ) ( xi 1 xi )( xi 1 xi +1 ) ( xi xi 1 )( xi xi +1 ) ( xi +1 xi 1 )( xi +1 xi )

The same approach can be applied with differen degree of polynomials as well as differnet interpolation formulations As another example to function definition (Curve fitting) cubic spline interpolation can be applied. If the cubic polynomial is in the form of: sk(x)=ak(x-xk)+ bk(xk+1-x)+ [(x-xk)3 ck+1 +(xk+1-x)3 ck]/(6hk) The derivative of this equation will be sk(x)=ak- bk+ [(x-xk)2 ck+1 -(xk+1-x)2 ck]/(2hk) 1 k n 1 k n

Since cubic polynomial has continious first and second polynomials between each spliine function the derivative is also continious. An example program is given below. Data used is generated fron f(x)=x2 fonction.. Program 7.12-1 data derivative with cubic spline interpolation
import java.io.*; import javax.swing.*; class NA81 { public static double [] thomas(double a[][],double r[]) { // int n=a.length; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double x[]=new double[n]; for(int i=0;i<n;i++) {f[i]=a[i][i];} for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];} for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];} for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; } public static double [] thomas(double f[],double e[],double g[],double r[]) { int n=f.length; double x[]=new double[n]; for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; }

for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; }

public static double [][] cubic_spline(double xi[],double yi[],double c0,double cn) { // c0 second derivative of the first point in the first spline // cn second derivative of the last point in last spline int n=xi.length; double h[]=new double[n]; double w[]=new double[n]; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double d[]=new double[n]; double x[]=new double[n]; double S[][]=new double[4][n]; int k; for(k=0;k<(n-1);k++) {h[k]=xi[k+1]-xi[k]; w[k]=(yi[k+1]-yi[k])/h[k]; } d[0]=c0; d[n-1]=cn; for(k=1;k<(n-1);k++) {d[k]=6.0*(w[k]-w[k-1]);} f[0]=1.0; f[n-1]=1.0; g[0]=0.0; g[n-1]=0.0; e[0]=0.0; e[n-1]=0.0; for(k=1;k<(n-1);k++) {f[k]=2.0*(h[k]+h[k-1]);e[k]=h[k-1];g[k]=h[k];} S[2]=thomas(f,e,g,d); S[3]=xi; for(k=0;k<(n-1);k++) {S[0][k]=(6.*yi[k+1]-h[k]*h[k]*S[2][k+1])/(6.0*h[k]); S[1][k]=(6.*yi[k]-h[k]*h[k]*S[2][k])/(6.0*h[k]); } return S; } public static double funcSpline(double S[][],double x) { int n=S[0].length; double xx1=0; double xx2=0; double y=0; double hk=0; for(int k=0;k<(n-1);k++) {if(S[3][k]<=x && x<=S[3][k+1])

{hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); break; } } if(y==0 && S[3][n-2]<=x ) { int k=n-2; hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); } return y; } public static double dfSpline(double xi[],double yi[],double c0,double cn,double x) { //cubic spline formulations double S[][]=cubic_spline(xi,yi,c0,cn); return dfSpline(S,x); } public static double dfSpline(double S[][],double x) { //cubic spline derivative formulation int n=S[0].length; double xx1=0; double xx2=0; double y=0; double hk=0; for(int k=0;k<(n-1);k++) {if(S[3][k]<=x && x<=S[3][k+1]) {hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]-S[1][k]+(xx1*xx1*S[2][k+1]-xx2*xx2*S[2][k])/(2.0*hk); break; } } if(y==0 && S[3][n-2]<=x ) { int k=n-2; hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); } return y; } public static double dfSpline(double xi[],double yi[],double x) { //natura cubic spline derivative formula double S[][]=cubic_spline(xi,yi,0,0); return dfSpline(S,x); }

public static void main(String args[]) throws IOException { double x[]; double y[]; String s1="a.txt"; //JFileChooser fc=new JFileChooser(); //if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); } double a[][]=Text.readDoubleT(s1); x=a[0]; y=a[1]; int n=x.length; double S[][]=cubic_spline(x,y,2.0,2.0); System.out.println(dfSpline(S,2.5)); } }

Output 7.9-2 data derivative with cubic spline interpolation ---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA81 5.005944788930356 > Terminated with exit code 0. 7.10 PROBLEMS PROBLEM 1 Calculate integral of y=f(x)=1/(1 + x2) fonction between limits 1 ile 1 with 3 points GaussLegendre integration Formula Note: Required roots and coefficients : coefficients c0 0.555555555555553 c1 0.888888888888889 c2 0.555555555555553 PROBLEM 2

Roots x0 0.774596669241483 x1 0.000000000000000 x2 0.774596669241483

1 + 2x

dx solve with bole rule. Use n=2

Note: Bole rule for n=1 I=(b-a)*[7*f(x0)+32*f(x1)+12*f(x2)+32*f(x3)+7*f(x4)]/90 Step size h=(b-a)/n PROBLEM 3
T

f 0 =

dx c x =0 5 x exp( 2 ) 1 x

c1

Function is known as black body radiation shape function. The values of coefficients in the equation are: c1 =3.743 x108 W.m4/m2, c2 =1.4387x104 m.K, =5.67x10-8 W/(m2.K4) dr. Use Gauss-Legensre integration and for a giving T calculate black body radiation shape factor. Create a table for the values of blackbody radiation shape factor from 5000 to infinity(?) PROBLEM 4

I=

dy dx 2 x =0 y =0 (1 + y )
1 1

Solve the above integration by using a) monte-carlo method. b) Gauss-Legendre integration c) Simpson 1/3 integration d) Develop a program for 2 dimensional 4. Order Newton Cotes method(booole) PROBLEM 5 Solve the integration

I=

1 dx 2 x =0 1 + x
1

a) by analithical methods b) by Trapezoidal rule with n=2(divide region into 2 parts) and calculate % error c) Trapezoidal rule with n=2(divide region into 2 parts) and calculate % error PROBLEM 6 Solve the integration

I=

1 1+ x2

dx

x =0

By using Gauss-Legendre formulation PROBLEM 7 For x=1

1 f ( x) = ln 2 1+ x
a) b) c) d) Calculate analytical derivative Calculate derivative with 3 points central difference formula with h=0.1 Calculate derivative with 3 points central difference formula with h=0.05 Calculate derivative with Richardson extrapolation formula with h1=0.5 and h2=0.25

PROBLEM 8

f ( x) = x. exp( x) dx
1

Calculate the integration by a) Trapezoidal rule integrali b) Simpson 3/8 integration c) Gauss-Legendre integration

d) Romberg integration PROBLEM 9

f ( x) = sin(1 x + x 2 ) dx
0

Calculate the integration by e) Trapezoidal rule integrali f) Simpson 3/8 integration g) Gauss-Legendre integration h) Romberg integration PROBLEM 10

f ( x) =

ln(1 + x 2 ) 1 x2

dx

Calculate the integration by i) Trapezoidal rule integrali j) Simpson 3/8 integration k) Gauss-Legendre integration l) Romberg integration PROBLEM 11 X 0.99 2.10 3.22 4.40 5.70 7.12 8.01 8.37 9.32 9.98 f(x) 4.90 5.70 4.20 7.04 8.31 7.82 5.97 7.01 6.68 4.79 Data given in the above table. Find the integration by using cubic spline integration method. PROBLEM 12 Find the derivative of data given in problem 11 at point x=5 PROBLEM 13 Depth(H) and velocity(U) profile of a channel is given in below table x, m H, m U m/s 0 0 0 2 1.8 0.03 4 2 0.045 6 4 0.055 8 4 0.065 10 6 0.12 12 4 0.07 14 3.6 0.06 16 3.4 0.05 18 2.8 0.04 20 0 0

Cross sectiaonal area of the channel can be calculated as :

Ac = H ( x)dx
0

Flow rate of the channel will be

Q = U ( x) H ( x ) dx In this equations U is the velocity of the water measured at point x. Calculate


0

cross sectional area and flow rate of the channel.

0 0 5 10 15 20 25

-1

-2

derinlik m

-3 Seri 2 -4

-5

-6

-7 sol kydan uzaklk m

PROBLEM 14 Temperature profle of a solid body can be given as

dT = k (T Ta ) dt

In this equation T is the temperature, Ta is constant surface temperature.

A solid body with temperatures initially at T=90 C and, Ta =20 C put into a water container at temperature of Ta = 20 C. The temperature profile as a function of time is given at the table. Zaman , t, s Scaklk T C 0 90 5 55 10 40 15 28 20 24 25 22 30 21.5 35 20.6

Calculate k thermal conductivity coefficient. PROBLEM 15 Coordinate of a hard shape is given in the table. Find the area of the hard. Xi 241 258 283 307 322 333 334 Yi 127 107 96 97 106 120 138 xi 331 323 312 298 283 267 241 yi 157 178 197 217 232 246 258 xi 215 199 184 170 159 151 148 yi 246 232 217 197 178 157 138 xi 149 160 175 199 224 241 Yi 120 106 97 96 107 127

PROBLEM 16
0 .5 x 2

I=

0 .1 x

e
3

y/x

dydx Solve the integral by Gauss-Legendre method.

PROBLEM 17

I = ( x 2 + y 3 )dydx
0 x

1 2x

Solve the equation with a) 2 dimensional Gauss-Legendre integration b) 2 dimensional simpson 1/3 integration c) Develop an equation for 2 dimensional simpson. 3/8 integration and solve the integral PROBLEM 18

I=

dy dx 2 x =0 y =0 (1 + y )
1 1

Solve the integral with Monte carlo method and check the results with Gauss-Legendre method. PROBLEM 19 Show that equation below is correct

= (4 x 2 )1/ 2 dx
0

PROBLEM 20

I = e x dx
0

Solve the integral for m=25,50,100,500,1000,10000,100000 with monte-carlo method and compare the result with the analytical solution. PROBLEM 21 An angine of a vehicle with mass of 53400 kg and a velocity of v=30 m/is suddenly stopped at t=0. In this instant equation of the movement can be given as

5400 * v * (dv / dx) = 8,276 * v 2 2000


In the equation v(m/sn), is the velocity of the vehicle at time t. When velocity is 15 m/s calculate the total distance vehicle moved by using 4 point Gauss integration Formula . PROBLEM 22 A vehicle is at complete stop. When it is started velocity of the vehicle is recorded fort he first 5 minutes. Calculate the total distance vehicle moved by using trapezoidal rule as a direct data integration method.

t(minutes) v (km/h) Note : v =

0 0

0,5 4
t

1,0 7

1,5 11

2,0 15

2,5 21

3,0 25

3,5 30

4,0 32

4,5 35

5,0 40

dx dt

x x0 = v(t )dt
t0

PROBLEM 23 Upward velocity of a rocket can be calculated by using the formula


V= dz = U * ln [mo / (mo q *t )] g * t dt

In this equation V is velocity, U is the exit velocity of burning gases from the rocket nozzle, mo is the mass of the rocket at time t = 0 sn, q is fuel spending rate , g is garavitational acceleration ( assume constant as, 9,8 m2/s). If U = 2000 m/s, mo = 150 000 kg, q = 2600 kg/s, Calculate the height rocket will climb in 30 seconds by using Romberg inntegration with k=4 [O(h8)] order of error.

dH Note : V = dt

t = 30

H=

t 0=0

V (t )dt

PROBLEM 24 Calculate velocity and acceleration by using below data with c) cental difference method [with O(h4) error level], d) forward difference method [with O(h2) error level], e) backward difference method [with O(h2) error level], Time, t (s) distance, x (m) 0 0 1 0.7 2 1.8 3 3.4 4 5.1
2

5 6.5

6 7.3

7 8.0

8 8.4

d x dx Note : Velocity v = Acceleration : a = dt dt 2


PROBLEM 25 Integrate data by using trapezoidal rule x -3 -1 -4 1 -9 3 2 5 4 7 2 9 6 11 -3 f(x) 1

PROBLEM 26 In a river water height is measured in every 12.5 m distance. Water flow rate can be calculated by Q = V*dA integration. In this equation Q: water flow rate (m3/s), V: water flow speed (m/s), A: cross-sectional area (m2). If avarage water velocity is 0.4 m/s. Calculate water flow rate by using Simpson 1/3 rule X(m) 0 12,5 25 37,5 50 62,5 75 87,5 100 f(X) m 11 7,5 5,5 0 1,5 4 5,5 8 11 PROBLEM 27

10

a)

f ( x)dx calculate the integral value from the data given below
0

X f(x)

b) calculate the derivative of the function at x=3,4,5,6,7,8 0.99 2.10 3.22 4.40 5.70 7.12 4.90 5.70 4.20 7.04 8.31 7.82 PROBLEM 28 By using Gauss-Legendre integral formulation calculate I = Compare the result with analytical solution. PROBLEM 29 By using Adaptive simpson 1/3 solve I = PROBLEM 30
4
4

8.01 5.97

8.37 7.01

9.32 6.68

9.98 4.79

dx integral for n=2,3,4,5 . 2 41 + x


4

dx integral with an error level of 1.0e-5 1 + x2


1

By using Gauss-Chebychev integration equation solve the integral I = compare the result with the exact value. PROBLEM 31 By using trapezoidal rule calculate the integral I =
0 1

cos( x)dx (1 x 2 )

= 2.40394 and

2dx = 1.15470054 for n=2,4,8 ve 16 2 + sin(10x)

And then improve the result by using Richardson extrapolation Formula (Romberg integration). PROBLEM 32 If velocity profile in a pipe is known, flow rate can be calculated as Q=vdA In this equation v i the velocity of the fluid A is the cross-sectional area of the pipe. In a circular pipe A=(2 r) and dA = (2 r dr). If it is substituted into flow rate equation Q = v(2r) dr is obtained. Where r is the radius. If the velocity distribution is given as a function of r as v = 3[1-(r/r0)](1/7) and if the radius of the pipe r0= 0.1 m calculate mass flow rate. Select your own method. PROBLEM 33 In the integratls below exact solutions are given. By using an adaptive integration method. Chck out the results. a) I1 =

1 + 256( x 0.375)
0

4dx

1 = [tan 1 (10) + tan 1 (6)] = 0.7191938309210011 4

b) I 2 = x 2 x dx =
0

2 7

c) I 3 =

x dx =

2 3

d) I 4 = log( x)dx = 1
0

e) I 5 = log(| x 0.7 |)dx = 0.3 * log(0.3) + 0.7 * log(0.7) 1.0


0 2

f) I 6 = g) I 6 =

e
0

sin(50 x)dx =
dx x = 206

50 (1 e 2 ) 2501

10000

PROBLEM 34

Wind pressure in a flat wall is measures as in the graphic. It is desired to find pressure effect center of the wall. It is calculated by using :
112

h=

0 112 0

hp (h)dh p(h)dh

Calculate the value. PROBLEM 35 For the analytically solved integration that result is given below: Solve with with a) Adaptive Simpson 1/3 method b) Adaptive Gauss-Kronrod integral formula.

PROBLEM 36 v(m/s) 0 P(kW) 0

1 4.7

1.8 12.2

2.5 19.0

3.5 31.8

4.4 40.1

5.1 43.8

6 43.2

Power transmitted to the wheels of a vehicle is given in the table as a function of velocty. If the mass of the ar is m=2000 kg, Calculate the required time to accelrate the car from 0 m/s2 to 6 m/s2 Note : t = m (v / P )dv
0s 6s

PROBLEM 37

I = e x + y + z dzdydx Calculate the integral by using 3 dimensional Gauss-Legendre integration


0 1 0

1 2 y

PROBLEM 38 x xy 1 z I = sin( )dzdydx Calculate the integral by using 3 dimensional Gauss-Legendre integration 0 0 0 y y PROBLEM 39 Blackbody radiation function F(0- ),

C2 14387.69 mK = T T x 15 x 3 F (0 T ) = 1 4 x dx 0 e 1 x=

(m) T(degree Kelvin) can be calculated as

It can also be calculated from the series solution

F (0 T ) =

15

n =1

e nx 3 3 x 2 6 x 6 n ( x + n + n 2 + n3 )

By creating a program that calculates the result with using series solution and integration. Compare the results. Resulting tables are given as a reference

T mK

F(0-T)

T T

F(0-T)

T mK

F(0-T)

T mK

F(0-T)

T mK

F(0-T)

T mK

F(0-T)

1000 1050 1100 1150 1200 1250 1300 1350 1400 1450 1500 1550 1600 1650 1700 1750 1800 1850 1900 1950 2000 2050 2100 2150 2200 2250 2300 2350 2400 2450 2500 2550 2600 2650 2700 2750 2800 2850

0.00032 0.00056 0.00091 0.00142 0.00213 0.00308 0.00432 0.00587 0.00779 0.01011 0.01285 0.01605 0.01972 0.02388 0.02854 0.03369 0.03934 0.04549 0.05211 0.0592 0.06673 0.07469 0.08306 0.0918 0.10089 0.11031 0.12003 0.13002 0.14026 0.15071 0.16136 0.17217 0.18312 0.19419 0.20536 0.2166 0.22789 0.23922

2900 2950 3000 3050 3100 3150 3200 3250 3300 3350 3400 3450 3500 3550 3600 3650 3700 3750 3800 3850 3900 3950 4000 4050 4100 4150 4200 4250 4300 4350 4400 4450 4500 4550 4600 4650 4700 4750

0.25056 0.26191 0.27323 0.28453 0.29578 0.30697 0.3181 0.32915 0.34011 0.35097 0.36173 0.37238 0.38291 0.39332 0.4036 0.41375 0.42377 0.43364 0.44338 0.45297 0.46241 0.47172 0.48087 0.48987 0.49873 0.50744 0.516 0.52442 0.53269 0.54081 0.54878 0.55662 0.56431 0.57186 0.57927 0.58654 0.59367 0.60067

4800 4850 4900 4950 5000 5050 5100 5150 5200 5250 5300 5350 5400 5450 5500 5550 5600 5650 5700 5750 5800 5850 5900 5950 6000 6050 6100 6150 6200 6250 6300 6350 6400 6450 6500 6600 6700 6800

0.60754 0.61428 0.62089 0.62737 0.63373 0.63996 0.64608 0.65207 0.65795 0.66371 0.66937 0.67491 0.68034 0.68566 0.69089 0.696 0.70102 0.70594 0.71077 0.7155 0.72013 0.72468 0.72914 0.73351 0.73779 0.74199 0.74611 0.75015 0.75411 0.758 0.76181 0.76554 0.76921 0.7728 0.77632 0.78317 0.78976 0.7961

6900 7000 7100 7200 7300 7400 7500 7600 7700 7800 7900 8000 8100 8200 8300 8400 8500 8600 8700 8800 8900 9000 9100 9200 9300 9400 9500 9600 9700 9800 9900 10000 10100 10200 10300 10400 10500 10600

0.8022 0.80808 0.81374 0.81918 0.82443 0.82949 0.83437 0.83907 0.8436 0.84797 0.85219 0.85625 0.86018 0.86397 0.86763 0.87116 0.87457 0.87787 0.88105 0.88413 0.88711 0.88999 0.89278 0.89547 0.89808 0.9006 0.90305 0.90541 0.9077 0.90992 0.91207 0.91416 0.91618 0.91814 0.92004 0.92188 0.92367 0.9254

10700 10800 10900 11000 11200 11400 11600 11800 12000 12200 12400 12600 12800 13000 13200 13400 13600 13800 14000 14200 14400 14600 14800 15000 15200 15400 15600 15800 16000 16200 16400 16600 16800 17000 17200 17400 17600 17800

0.92709 0.92872 0.93031 0.93185 0.9348 0.93758 0.94021 0.9427 0.94505 0.94728 0.94939 0.95139 0.95329 0.95509 0.95681 0.95843 0.95998 0.96145 0.96285 0.96419 0.96546 0.96667 0.96783 0.96893 0.96999 0.971 0.97196 0.97289 0.97377 0.97461 0.97542 0.9762 0.97694 0.97765 0.97834 0.97899 0.97962 0.98023

18000 18200 18400 18600 18800 19000 19200 19400 19600 19800 20000 20500 21000 21500 22000 22500 23000 23500 24000 24500 25000 26000 27000 28000 29000 30000 32000 34000 36000 38000 40000 45000 50000 55000 60000

0.98081 0.98137 0.98191 0.98243 0.98293 0.98341 0.98387 0.98431 0.98474 0.98516 0.98555 0.98649 0.98735 0.98814 0.98886 0.98952 0.99014 0.9907 0.99123 0.99172 0.99217 0.99297 0.99368 0.99429 0.99482 0.99529 0.99607 0.99669 0.99719 0.99759 0.99792 0.99851 0.9989 0.99917 0.99935

8.0 DIFFERENTIAL EQUATIONS


8.1 EULER EQUATION It is desired the find solution of (y values) differential equation dy = f ( x, y ) dx The first simplest method to solve differential equation numerically is the euler equation. Solution set in euler equation can be written as: yi+1 = yi +f(xi,yi)h In the equation h is step size. H can be calculated as h = x = xi+1 - xi Our example problem is solved for different step sizes as h=1, h=0.5 and h=0.1 The results clearly show that when h is getting smaller, euler solution and exact solution is getting closer to each other. Program 8.1-1 Euler equation
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== // Basit diferansiyel denklemler dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy extends f_xy { //diferansiyeli alnan fonksiyon double func(double x,double y) {return -2*x*x*x+12*x*x-20*x+8.5;} } class fxyKC extends f_x { //Gerek zm fonksiyonu double func(double x) {return -0.5*x*x*x*x+4.0*x*x*x-10.0*x*x+8.5*x+1.0;} } class diferansiyel { public static double[][] euler(f_xy f,double x0,double y0,double xn,double h) { int n=(int)((xn-x0)/h); System.out.println("n="+n); double a[][]=new double[2][n+1]; int i=0; a[0][0]=x0; a[1][0]=y0; for(double xx=x0;xx<xn;xx+=h) {a[0][i+1]=xx+h; a[1][i+1]=a[1][i]+f.func(a[0][i],a[1][i])*h; i++; } return a; } public static void main(String args[]) throws IOException

{ double x0=0; double y0=1.0; fxy f1=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=euler(f1,x0,y0,4.0,0.1); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); p1.addSpline(a,5); p1.addFunction(fKC,0.0,4.0,200); p1.plot(); } }

Output 8.1-1 Euler equation h=1

0.0 1.0 1.0 9.5 2.0 8.0 3.0 8.5 4.0 11.0
Output 8.1-2 Euler equation h=1

Output 8.1-3 Euler equation h=0.5

Output 8.1-4 Euler equation h=0.1

8.2 HEUN EQUATION Heun equation is similar to euler equation. It is added a correction term into the euler equation y0i+1=yi +f(xi,yi)h Corection term
y ik++11 = y i + f ( xi , y ik ) + f ( xi +1 , y ik+1 )

, k = 0...n

The term yki+1 can be calculated iteratively. Number of iteration can be changed.
Program 8.1-1 Heun equation
//====================================================== // Numerical Analysis package in java

// example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== // Basit diferansiyel denklemler dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); }

class fxy extends f_xy { double func(double x,double y) {return -2*x*x*x+12*x*x-20*x+8.5;} } class fxyKC extends f_x { double func(double x) {return -0.5*x*x*x*x+4.0*x*x*x-10.0*x*x+8.5*x+1.0;} } class fxy1 extends f_xy { double func(double x,double y) {return 4.0*Math.exp(0.8*x)-0.5*y;} } class diferansiyelA { public static double[][] heun(f_xy f,double x0,double y0,double xn,double h) { int n=(int)((xn-x0)/h); System.out.println("n="+n); double a[][]=new double[2][n+1]; int i=0; a[0][0]=x0; a[1][0]=y0; double yi0=0; for(double xx=x0;xx<xn;xx+=h) {a[0][i+1]=xx+h; yi0=a[1][i]+f.func(a[0][i],a[1][i])*h; for(int j=0;j<5;j++) {yi0=a[1][i]+(f.func(a[0][i],a[1][i])+f.func(a[0][i+1],yi0) )/2.0; } a[1][i+1]=a[1][i]+(f.func(a[0][i],a[1][i])+f.func(a[0][i+1],yi0) )/2.0; i++; } return a; } public static void main(String args[]) throws IOException { double x0=0; double y0=1.0; fxy f1=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=heun(f1,x0,y0,4.0,0.1); Text.print(Text.T(a)); Plot p1=new Plot(a);

p1.setPlotType(0,24); p1.addSpline(a,8); p1.addFunction(fKC,0.0,4.0,200); p1.plot(); } }

Output 8.1-1 Heun equation, step h=1 created data

0.0 1.0 1.0 4.5 2.0 4.0 3.0 5.5 4.0 3.0
Output 8.1-2 Graphic output for Heun equation, step h=1

8.3 MIDPOINT METHOD

In this method a midpoint is calculated first, then by using derivative at the mid point the real point is calculated. yi+1/2 = yi + f( xi , yi )h/2 yi+1 = yi + f( xi+1/2 , yi+1/2 )h
Program 8.3-1 Mid Point equation
/====================================================== // Numerical Analysis package in java // example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== // Basit diferansiyel denklemler dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); }

class fxy extends f_xy { double func(double x,double y) {return -2*x*x*x+12*x*x-20*x+8.5;} }

class NA84 { public static double[][] polygon(f_xy f,double x0,double y0,double xn,double h) { int n=(int)((xn-x0)/h); System.out.println("n="+n); double a[][]=new double[2][n+1]; int i=0; a[0][0]=x0; a[1][0]=y0; double yi0=0; double xi0; for(double xx=x0;xx<xn;xx+=h) {a[0][i+1]=xx+h; xi0=xx+h/2.0; yi0=a[1][i]+f.func(a[0][i],a[1][i])*h/2.0; a[1][i+1]=a[1][i]+f.func(xi0,yi0)*h; i++; } return a; } public static void main(String args[]) throws IOException { double x0=0; double y0=1.0; fxy f1=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=polygon(f1,x0,y0,4.0,1.0); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); p1.addSpline(a,8); p1.addFunction(fKC,0.0,4.0,200); p1.plot(); } }

Output 8.3-1 Midpoint method h=1

Output 8.3-2 Midpoint method, h=0.5

Output 8.3-3 Orta nokta ve gelitirilmi polinom equation, h=0.1

8.4 RUNGE KUTTA METHODS

Runge-Kutta method as in most general form can be given as: yi+1 = yi + (xi,yi,h)h

( x1 , y1 , h) = ai k i
i =1

In this equation a is constant values depends on polynamial degree of Runge-Kutta equation and coefficients k, can be defined as: k1=f(xi,yi) k2=f(xi+p1h,yi+q11k1h) k3=f(xi+p2h,yi+q21k1h+ q22k2h) . kn=f(xi+pn-1h,yi+qn-1,1k1h+ + qn-1,n-1kn-1h ) As it is seen from the equation the definition is very general. Depends on polynamial degree of Runge-Kutta equation accuracy will be improve. For example Runge-Kutta equation derived by using a second degree polynomial(RK2) yi+1 = yi + ( 0.5k1 + 0.5k2 )h

k1=f(xi,yi) k2=f(xi+h,yi+k1h)
Second degree Ralston method: yi+1 = yi + (1.0/3.0k1 + 2.0/3.0k2 )h k1=f(xi,yi) k2=f(xi+(3.0/4.0)h,yi+(3.0/4.0)k1h) Runge-Kutta with third degree polynomial solution RK3: yi+1 = yi + (1/6)*( k1 + 4k2 +k3)h k1=f(xi,yi) k2=f(xi+0.5h,yi+0.5k1h) k3=f(xi+h,yi - k1h +2k2h) Runge-Kutta with fourth degree polynomial solution RK4: yi+1 = yi + (1/6)*( k1 + 2k2 +2k3+k4)h k1=f(xi,yi) k2=f(xi+0.5h,yi+0.5k1h) k3=f(xi+0.5h,yi+0.5k2h) k4=f(xi+h,yi +k3h) Runge-Kutta with sixth degree polynomial solution RK6: yi+1 = yi + (1/90)*( 7k1 + 32k3 +12k4+32k5+7k6)h k1=f(xi,yi) k2=f(xi+0.25h , yi+0.25k1h) k3=f(xi+0.25h , yi+0.125k1h+0.125k2h) k4=f(xi+0.5h , yi 0.5k2h+k3h) k5=f(xi+0.75h , yi + (3/16)k1h+(9/16)k4h) k6=f(xi+h , yi - (3/7)k1h+(2/7)k2h+(12/7)k3h - (12/7)k4h+(8/7)k5h)

Some of these methods are listed as programs.


Program 8.4-1 second degree Runge-Kutta (Ralston) equation
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // A second degree Runge-Kutta RK2 (Ralston equation) // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy extends f_xy { double func(double x,double y) {return -2*x*x*x+12*x*x-20*x+8.5;} } class fxyKC extends f_x

{ double func(double x) {return -0.5*x*x*x*x+4.0*x*x*x-10.0*x*x+8.5*x+1.0;} } class NA85 {

public static double[][] RK2(f_xy f,double x0,double y0,double xn,double h) { //Ralston 2/3 int n=(int)((xn-x0)/h); System.out.println("n="+n); double a[][]=new double[2][n+1]; int i=0; a[0][0]=x0; a[1][0]=y0; double k1,k2; for(double xx=x0;xx<xn;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+3.0/4.0*h,a[1][i]+3.0/4.0*k1*h); a[1][i+1]=a[1][i]+(1.0/3.0*k1+2.0/3.0*k2)*h; i++; } return a; } public static void main(String args[]) throws IOException {double x0=0; double y0=1.0; fxy f1=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=RK2(f1,x0,y0,4.0,1.0); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); p1.addSpline(a,8); p1.addFunction(fKC,0.0,4.0,200); p1.plot(); } }

Output 8.4-1 ikinci dereceden Runge-Kutta (Ralston) equation, h=1

Program 8.4-2 Third degree Runge-Kutta Equation


//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 3rd order solution RK3 // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy extends f_xy { double func(double x,double y) {return -2*x*x*x+12*x*x-20*x+8.5;} } class fxyKC extends f_x { double func(double x) {return -0.5*x*x*x*x+4.0*x*x*x-10.0*x*x+8.5*x+1.0;} } class NA86 { public static double[][] RK3(f_xy f,double x0,double y0,double xn,double h) { //Runge-Kutta 3 int n=(int)((xn-x0)/h); double a[][]=new double[2][n+1]; int i=0; a[0][0]=x0; a[1][0]=y0; double k1,k2,k3; for(double xx=x0;xx<xn;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+0.5*h,a[1][i]+0.5*k1*h); k3=f.func(a[0][i]+h,a[1][i]-k1*h+2.0*k2*h); a[1][i+1]=a[1][i]+(k1+4.0*k2+k3)/6.0*h; i++; } return a; } public static void main(String args[]) throws IOException {double x0=0; double y0=1.0; fxy f2=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=RK3(f2,x0,y0,4.0,1.0); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); //p1.addSpline(a,8); p1.addFunction(fKC,0.0,4.0,200); p1.plot();

} }

Output 8.4-2 nc dereceden Runge-Kutta equation, h=1

This result is clearly shows the superiority of Runge-Kutta methods compare to the previous ones.
Program 8.4-3 Fourth order Runge-Kutta (Ralston) equation
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 4rd order solution RK4 // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy extends f_xy { double func(double x,double y) {return -2*x*x*x+12*x*x-20*x+8.5;} }

class fxyKC extends f_x { double func(double x) {return -0.5*x*x*x*x+4.0*x*x*x-10.0*x*x+8.5*x+1.0;} } class NA87 {

public static double[][] RK4(f_xy f,double x0,double y0,double xn,double h) { // Runge-Kutta 4 int n=(int)((xn-x0)/h); double a[][]=new double[2][n+1]; int i=0;

a[0][0]=x0; a[1][0]=y0; double k1,k2,k3,k4; for(double xx=x0;xx<xn;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+0.5*h,a[1][i]+0.5*k1*h); k3=f.func(a[0][i]+0.5*h,a[1][i]+0.5*k2*h); k4=f.func(a[0][i]+h,a[1][i]+k3*h); a[1][i+1]=a[1][i]+(k1+2.0*k2+2.0*k3+k4)/6.0*h; i++; } return a; } public static void main(String args[]) throws IOException {double x0=0; double y0=1.0; fxy f2=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=RK4(f2,x0,y0,4.0,1.0); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); //p1.addSpline(a,8); p1.addFunction(fKC,0.0,4.0,200); p1.plot(); } }

Output 8.4-3 Fourth order Runge-Kutta (Ralston) equation, h=0.5

Program 8.4-4 Sixth order Runge-Kutta (RK6) equation


//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 6th order solution RK6 // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y)

import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy extends f_xy { double func(double x,double y) {return -2*x*x*x+12*x*x-20*x+8.5;} } class fxyKC extends f_x { double func(double x) {return -0.5*x*x*x*x+4.0*x*x*x-10.0*x*x+8.5*x+1.0;} } class RK6 { public static double[][] RK6(f_xy f,double x0,double y0,double xn,double h) { // Runge-Kutta 6 int n=(int)((xn-x0)/h); double a[][]=new double[2][n+1]; int i=0; a[0][0]=x0; a[1][0]=y0; double k1,k2,k3,k4,k5,k6; for(double xx=x0;xx<xn;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+0.25*h,a[1][i]+0.25*k1*h); k3=f.func(a[0][i]+0.25*h,a[1][i]+1.0/8.0*k1*h+1.0/8.0*k2*h); k4=f.func(a[0][i]+0.5*h,a[1][i]-0.5*k2*h+k3*h); k5=f.func(a[0][i]+3.0/4.0*h,a[1][i]+3.0/16.0*k1*h+9.0/16.0*k4*h); k6=f.func(a[0][i]+h,a[1][i]-3.0/7.0*k1*h+2.0/7.0*k2*h+12.0/7.0*k3*h-12.0/7.0*k4*h+8.0/7.0*k5*h); a[1][i+1]=a[1][i]+(7.0*k1+32.0*k3+12.0*k4+32.0*k5+7*k6)/90.0*h; i++; } return a; }

public static void main(String args[]) throws IOException {double x0=0; double y0=1.0; double yn=4.0; double h=1.0; fxy f2=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=RK6(f2,x0,y0,yn,h); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); //p1.addSpline(a,10); p1.addFunction(fKC,0.0,4.0,200); p1.plot(); } }

Program 8.4-4 Altnc dereceden Runge-Kutta equation, h=0.5

8.5 MULTISTEP (OPEN) METHODS

If more than one step of equations are used for calculation of one step solution, it is called multistep method. Some people claims that this type of equations gives more stable solutions. Milne Formula is the fist multistep method to be investigated.
Milne 1. Step formula (first prediction)

yip 1 = yi 3 + +

4h [2 f ( xi , yi ) f ( xi1 , yi 1 ) + 2 f ( xi 2 , yi2 )] 3 28 yi yip 29

Milne 2. Step Formula (first correction)


0 yi(+1) = y jp+1 +

]
]
k=0,1,.

Milne 3. Step formula (second correction)


k yi(+1+1) = y j 1 +

h k f ( xi +1 , yi(+1) ) + 4 f ( xi , yi ) + f ( xi 1 , yi 1 ) 3

Due to using a first step to predict a guess and then corrected it with one or multistep correctors, Multistep methods are also called Predictor-Corrector methods. In order to apply this method a previously calculated points are needed. Single step methods like Runge-Kutta methods can serve us for calculating this first points. As an example problem

y' =

1 2 y 2 diferential equation with the initial conditions y(0)=0 will be solved. Exact analytical 2 1+ x x 1+ x2

solution of this diferential equation

y=

Program 8.5-1 Milne Predictor-corrector formula


//====================================================== // Numerical Analysis package in java // example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban

// ===================================================== // Basit diferansiyel denklemler dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy extends f_xy { double func(double x,double y) {return 1.0/(1.0+x*x)-2.0*y*y;} } class fxyKC extends f_x { double func(double x) {return x/(1+x*x);} } class diferansiyelN { public static double[][] RK6M(f_xy f,double x0,double y0,double xn,double h) { // Milne forml int n=(int)((xn-x0)/h); double a[][]=new double[2][n+1]; double yp[]=new double[n+1]; double y[][]=new double[6][n+1]; // Runge-Kutta 6 int i=0; a[0][0]=x0; a[1][0]=y0; double x2=x0+3.0*h; double k0,k1,k2,k3,k4,k5,k6; for(double xx=x0;xx<x2;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+0.25*h,a[1][i]+0.25*k1*h); k3=f.func(a[0][i]+0.25*h,a[1][i]+1.0/8.0*k1*h+1.0/8.0*k2*h); k4=f.func(a[0][i]+0.5*h,a[1][i]-0.5*k2*h+k3*h); k5=f.func(a[0][i]+3.0/4.0*h,a[1][i]+3.0/16.0*k1*h+9.0/16.0*k4*h); k6=f.func(a[0][i]+h,a[1][i]-3.0/7.0*k1*h+2.0/7.0*k2*h+12.0/7.0*k3*h-12.0/7.0*k4*h+8.0/7.0*k5*h); a[1][i+1]=a[1][i]+(7.0*k1+32.0*k3+12.0*k4+32.0*k5+7*k6)/90.0*h; i++; } for(double xx=x2;xx<xn;xx+=h) { a[0][i+1]=xx+h; //Milne forml k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i-1],a[1][i-1]); k3=f.func(a[0][i-2],a[1][i-2]); yp[i+1]=a[1][i-3]+4.0*h/3.0*(2.0*k1-k2+2.0*k3); y[0][i+1]=yp[i+1]+28.0/29.0*(a[1][i]- yp[i]); int k; for(k=0;k<5;k++) { k0=f.func(a[0][i+1],y[k][i+1]); y[k+1][i+1]=a[1][i-1]+h/3.0*(k0+4.0*k1+k2); } a[1][i+1]=y[k][i+1]; i++;

} return a; } public static void main(String args[]) throws IOException {double x0=0; double y0=0.0; double yn=10.0; double h=0.0625; fxy f2=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=RK6M(f2,x0,y0,yn,h); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); p1.addFunction(fKC,0.0,10.0,50); p1.plot(); } }

Output 8.5-1 Milne Predictor-Corrector method table output with error term
0 0.0625 0.125 0.1875 0.25 0.3125 0.375 0.4375 0.5 0.5625 0.625 0.6875 0.75 0.8125 0.875 0.9375 1 1.0625 1.125 1.1875 1.25 1.3125 1.375 1.4375 1.5 1.5625 1.625 1.6875 1.75 1.8125 1.875 1.9375 2 2.0625 2.125 2.1875 2.25 2.3125 2.375 2.4375

Milne solution 0 0.062256809 0.123076922 0.181132074 0.235294614 0.284697577 0.328767367 0.367212702 0.399999711 0.427298769 0.449437527 0.466842287 0.479999216 0.489410522 0.495574537 0.498959376 0.499999514 0.499081613 0.496551452 0.492705844 0.487804796 0.482065316 0.475675749 0.468789209 0.461538657 0.454028961 0.446352222 0.438578152 0.430769597 0.422971217 0.415225342 0.40755904 0.400000481 0.392564504 0.38526965 0.378122313 0.371134592 0.364307086 0.357647672 0.351152991

Exact solution 0 0.062256809 0.123076923 0.181132075 0.235294118 0.284697509 0.328767123 0.367213115 0.4 0.427299703 0.449438202 0.466843501 0.48 0.489411765 0.495575221 0.498960499 0.5 0.499082569 0.496551724 0.492706645 0.487804878 0.482065997 0.475675676 0.468789809 0.461538462 0.454029512 0.446351931 0.43857868 0.430769231 0.422971741 0.415224913 0.407559573 0.4 0.392565056 0.385269122 0.37812289 0.371134021 0.364307692 0.357647059 0.35115363

Error 0 1.29392E-10 6.07481E-10 1.36894E-09 -4.9588E-07 -6.78819E-08 -2.43534E-07 4.1304E-07 2.8879E-07 9.34563E-07 6.75003E-07 1.2147E-06 7.8413E-07 1.24273E-06 6.83803E-07 1.1231E-06 4.86142E-07 9.56231E-07 2.72191E-07 8.01101E-07 8.16887E-08 6.81141E-07 -7.3537E-08 5.99558E-07 -1.95371E-07 5.51033E-07 -2.9049E-07 5.28107E-07 -3.6604E-07 5.23921E-07 -4.28026E-07 5.33046E-07 -4.8098E-07 5.51496E-07 -5.28128E-07 5.76463E-07 -5.7169E-07 6.06014E-07 -6.13158E-07 6.38839E-07

2.5 2.5625 2.625 2.6875 2.75 2.8125 2.875 2.9375 3 3.0625 3.125 3.1875 3.25 3.3125 3.375 3.4375 3.5 3.5625 3.625 3.6875 3.75 3.8125 3.875 3.9375 4 4.0625 4.125 4.1875 4.25 4.3125 4.375 4.4375 4.5 4.5625 4.625 4.6875 4.75 4.8125 4.875 4.9375 5 5.0625 5.125 5.1875 5.25 5.3125 5.375 5.4375 5.5 5.5625 5.625 5.6875 5.75 5.8125 5.875 5.9375 6 6.0625 6.125 6.1875 6.25

0.34482824 0.338667369 0.332673961 0.326840144 0.321168616 0.315650281 0.310287451 0.305070205 0.300000814 0.295068798 0.290276617 0.28561341 0.281081977 0.276671192 0.272384293 0.268209956 0.264151924 0.260198716 0.256354615 0.252608002 0.248963723 0.245410027 0.241952332 0.23857875 0.235295275 0.232089874 0.228969112 0.2259208 0.222952068 0.220050554 0.217223948 0.214459699 0.211766048 0.209130238 0.206561054 0.204045518 0.201592949 0.199190132 0.196846912 0.194549822 0.192309227 0.190111394 0.1879672 0.185862632 0.183809074 0.181792219 0.179823953 0.177889665 0.176001735 0.174145238 0.172333045 0.170549905 0.168809177 0.167095274 0.165422039 0.16377354 0.162164103 0.160577443 0.159028363 0.157500217 0.156008287

0.344827586 0.338668043 0.332673267 0.326840855 0.321167883 0.31565103 0.310286678 0.305070994 0.3 0.295069627 0.290275762 0.285614281 0.281081081 0.276672104 0.272383354 0.268210911 0.264150943 0.260199715 0.256353591 0.252609045 0.248962656 0.245411114 0.24195122 0.238579882 0.235294118 0.232091051 0.22896791 0.225922023 0.22295082 0.220051824 0.217222653 0.214461016 0.211764706 0.209131603 0.206559665 0.204046931 0.201591512 0.199191593 0.196845426 0.194551331 0.192307692 0.190112953 0.187965616 0.185864241 0.18380744 0.181793878 0.179822269 0.177891374 0.176 0.174146998 0.172331259 0.170551716 0.168807339 0.167097136 0.16542015 0.163775455 0.162162162 0.16057941 0.159026369 0.157502237 0.15600624

-6.53519E-07 6.74055E-07 -6.93416E-07 7.11066E-07 -7.33265E-07 7.49474E-07 -7.73331E-07 7.89007E-07 -8.13778E-07 8.29482E-07 -8.54704E-07 8.70771E-07 -8.96169E-07 9.12786E-07 -9.38201E-07 9.55464E-07 -9.80814E-07 9.98759E-07 -1.02401E-06 1.04264E-06 -1.06778E-06 1.08707E-06 -1.11212E-06 1.13204E-06 -1.15702E-06 1.17754E-06 -1.20245E-06 1.22353E-06 -1.24842E-06 1.27002E-06 -1.29491E-06 1.31699E-06 -1.3419E-06 1.36443E-06 -1.38939E-06 1.41233E-06 -1.43735E-06 1.46069E-06 -1.48579E-06 1.50949E-06 -1.53468E-06 1.55873E-06 -1.58403E-06 1.6084E-06 -1.63382E-06 1.65849E-06 -1.68403E-06 1.70899E-06 -1.73467E-06 1.7599E-06 -1.78573E-06 1.81122E-06 -1.83719E-06 1.86293E-06 -1.88905E-06 1.91503E-06 -1.94131E-06 1.96752E-06 -1.99395E-06 2.02038E-06 -2.04697E-06

6.3125 6.375 6.4375 6.5 6.5625 6.625 6.6875 6.75 6.8125 6.875 6.9375 7 7.0625 7.125 7.1875 7.25 7.3125 7.375 7.4375 7.5 7.5625 7.625 7.6875 7.75 7.8125 7.875 7.9375 8 8.0625 8.125 8.1875 8.25 8.3125 8.375 8.4375 8.5 8.5625 8.625 8.6875 8.75 8.8125 8.875 8.9375 9 9.0625 9.125 9.1875 9.25 9.3125 9.375 9.4375 9.5 9.5625 9.625 9.6875 9.75 9.8125 9.875 9.9375 10

0.154535557 0.153097785 0.151677578 0.150291171 0.148920787 0.147583134 0.146260046 0.144968706 0.14369055 0.142443237 0.1412078 0.140002373 0.13880758 0.13764203 0.136485935 0.135358377 0.134239151 0.133147815 0.132063742 0.131006964 0.129956427 0.128932641 0.12791412 0.126921851 0.125933914 0.124971774 0.124013071 0.123079749 0.122149006 0.121243267 0.120339281 0.119459956 0.118581592 0.117727579 0.116873761 0.116044016 0.11521373 0.114407265 0.113599547 0.112815428 0.112029367 0.111266707 0.110501439 0.109759397 0.109014103 0.108291882 0.107565784 0.106862627 0.106154984 0.105470172 0.104780284 0.104113133 0.10344033 0.102790189 0.102133838 0.101500089 0.100859582 0.100241636 0.099616398 0.099013693

0.15453763 0.153095685 0.151679705 0.150289017 0.148922968 0.147580926 0.146262281 0.144966443 0.14369284 0.142440919 0.141210146 0.14 0.138809981 0.137639602 0.136488391 0.135355893 0.134241664 0.133145275 0.132066311 0.131004367 0.129959052 0.128929987 0.127916802 0.12691914 0.125936654 0.124969006 0.124015868 0.123076923 0.122151861 0.121240382 0.120342194 0.119457014 0.118584564 0.117724577 0.116876792 0.116040956 0.11521682 0.114404145 0.113602697 0.112812248 0.112032577 0.111263467 0.110504709 0.109756098 0.109017433 0.108288522 0.107569174 0.106859206 0.106158436 0.10546669 0.104783797 0.104109589 0.103443905 0.102786584 0.102137474 0.101496422 0.10086328 0.100237906 0.099620159 0.099009901

2.07362E-06 -2.10037E-06 2.12722E-06 -2.15413E-06 2.18119E-06 -2.20826E-06 2.23552E-06 -2.26275E-06 2.29019E-06 -2.31759E-06 2.34522E-06 -2.37278E-06 2.40059E-06 -2.42831E-06 2.4563E-06 -2.48418E-06 2.51235E-06 -2.54039E-06 2.56873E-06 -2.59693E-06 2.62544E-06 -2.65379E-06 2.68247E-06 -2.71098E-06 2.73981E-06 -2.76848E-06 2.79748E-06 -2.8263E-06 2.85546E-06 -2.88443E-06 2.91375E-06 -2.94287E-06 2.97234E-06 -3.00161E-06 3.03124E-06 -3.06066E-06 3.09043E-06 -3.12E-06 3.14993E-06 -3.17964E-06 3.20971E-06 -3.23957E-06 3.26979E-06 -3.29979E-06 3.33015E-06 -3.3603E-06 3.3908E-06 -3.42109E-06 3.45173E-06 -3.48216E-06 3.51294E-06 -3.5435E-06 3.57443E-06 -3.60513E-06 3.63619E-06 -3.66703E-06 3.69823E-06 -3.72919E-06 3.76053E-06 -3.79163E-06

Output 8.5-2 Milne Predictor-Corrector method graphic output

As a second method, Hamming Formula will be investigated. This method is very close to Milne method. As amound of error it is a little worst than Milne method, but its stability is much better.
Hamming 1. step formula (first prediction)

yip 1 = yi 3 + +
0 yi(+1) = y jp+1 +

4h [2 f ( xi , yi ) f ( xi1 , yi 1 ) + 2 f ( xi 2 , yi2 )] 3 112 yi yip 121

Hamming 2. Step formula (first correction)

] [ ]
k=0,1,.

Hamming 3. Step formula (second correction)


k yi(+1+1) =

1 (9 yi yi 2 ) + 3h f ( xi +1 , yi(+k1) ) + 2 f ( xi , yi ) f ( xi1 , yi1 ) 8 8

Program 8.5-2 Hamming Predictor-corrector Formula


//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Hamming Predictor-Corrector method // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); }

class fxy extends f_xy { double func(double x,double y) {return 1.0/(1.0+x*x)-2.0*y*y;} } class fxyKC extends f_x { double func(double x) {return x/(1+x*x);} } class NA90 { public static double[][] RK6M(f_xy f,double x0,double y0,double xn,double h) { // Hamming forml int n=(int)((xn-x0)/h); double a[][]=new double[2][n+1]; double yp[]=new double[n+1]; double y[][]=new double[6][n+1]; // Runge-Kutta 6 int i=0; a[0][0]=x0; a[1][0]=y0; double x2=x0+3.0*h; double k0,k1,k2,k3,k4,k5,k6; for(double xx=x0;xx<x2;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+0.25*h,a[1][i]+0.25*k1*h); k3=f.func(a[0][i]+0.25*h,a[1][i]+1.0/8.0*k1*h+1.0/8.0*k2*h); k4=f.func(a[0][i]+0.5*h,a[1][i]-0.5*k2*h+k3*h); k5=f.func(a[0][i]+3.0/4.0*h,a[1][i]+3.0/16.0*k1*h+9.0/16.0*k4*h); k6=f.func(a[0][i]+h,a[1][i]-3.0/7.0*k1*h+2.0/7.0*k2*h+12.0/7.0*k3*h-12.0/7.0*k4*h+8.0/7.0*k5*h); a[1][i+1]=a[1][i]+(7.0*k1+32.0*k3+12.0*k4+32.0*k5+7*k6)/90.0*h; i++; } for(double xx=x2;xx<xn;xx+=h) { a[0][i+1]=xx+h; //Milne forml k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i-1],a[1][i-1]); k3=f.func(a[0][i-2],a[1][i-2]); yp[i+1]=a[1][i-3]+4.0*h/3.0*(2.0*k1-k2+2.0*k3); y[0][i+1]=yp[i+1]+112.0/121.0*(a[1][i]- yp[i]); int k; for(k=0;k<5;k++) { k0=f.func(a[0][i+1],y[k][i+1]); y[k+1][i+1]=1.0/8.0*(9.0*a[1][i]-a[1][i-2])+3.0*h/8.0*(k0+2.0*k1-k2); } a[1][i+1]=y[k][i+1]; i++; } return a; } public static void main(String args[]) throws IOException {double x0=0; double y0=0.0; double yn=10.0; double h=0.0625; fxy f2=new fxy(); fxyKC fKC=new fxyKC();

double a[][]=RK6M(f2,x0,y0,yn,h); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); p1.addFunction(fKC,0.0,10.0,50); p1.plot(); } }

Output 8.5-2 Hamming Predictor-corrector Formula calculated values


0 0.0625 0.125 0.1875 0.25 0.3125 0.375 0.4375 0.5 0.5625 0.625 0.6875 0.75 0.8125 0.875 0.9375 1 1.0625 1.125 1.1875 1.25 1.3125 1.375 1.4375 1.5 1.5625 1.625 1.6875 1.75 1.8125 1.875 1.9375 2 2.0625 2.125 2.1875 2.25 2.3125 2.375 2.4375 2.5 2.5625 2.625 2.6875 2.75 2.8125 2.875 2.9375 3 3.0625 3.125 3.1875 3.25 3.3125 3.375 3.4375 3.5 3.5625 Hamming solution 0 0.062256809 0.123076922 0.181132074 0.235295447 0.284699363 0.328768754 0.367213781 0.399999285 0.427297497 0.449434654 0.466838916 0.479994747 0.489406203 0.495569657 0.498955165 0.499995055 0.499078103 0.496547778 0.492703217 0.487801944 0.482063515 0.475673598 0.468788085 0.461537041 0.45402835 0.446350987 0.438577917 0.430768617 0.422971251 0.415224525 0.407559266 0.399999761 0.392564871 0.38526898 0.378122784 0.371133942 0.364307636 0.357647021 0.351153605 0.344827573 0.338668039 0.332673269 0.326840862 0.321167894 0.315651043 0.310286693 0.30507101 0.300000017 0.295069645 0.290275779 0.285614298 0.281081098 0.276672121 0.27238337 0.268210926 0.264150958 0.260199728 Exact solution 0 0.062256809 0.123076923 0.181132075 0.235294118 0.284697509 0.328767123 0.367213115 0.4 0.427299703 0.449438202 0.466843501 0.48 0.489411765 0.495575221 0.498960499 0.5 0.499082569 0.496551724 0.492706645 0.487804878 0.482065997 0.475675676 0.468789809 0.461538462 0.454029512 0.446351931 0.43857868 0.430769231 0.422971741 0.415224913 0.407559573 0.4 0.392565056 0.385269122 0.37812289 0.371134021 0.364307692 0.357647059 0.35115363 0.344827586 0.338668043 0.332673267 0.326840855 0.321167883 0.31565103 0.310286678 0.305070994 0.3 0.295069627 0.290275762 0.285614281 0.281081081 0.276672104 0.272383354 0.268210911 0.264150943 0.260199715 Error 0 1.29392E-10 6.07481E-10 1.36894E-09 -1.32917E-06 -1.85455E-06 -1.63052E-06 -6.6612E-07 7.14995E-07 2.20588E-06 3.5483E-06 4.58534E-06 5.25346E-06 5.56192E-06 5.56428E-06 5.33375E-06 4.94536E-06 4.46539E-06 3.94663E-06 3.42766E-06 2.93423E-06 2.48172E-06 2.07773E-06 1.7244E-06 1.42031E-06 1.16193E-06 9.44654E-07 7.63473E-07 6.13446E-07 4.89946E-07 3.88796E-07 3.06316E-07 2.3933E-07 1.85132E-07 1.41441E-07 1.06351E-07 7.82796E-08 5.59194E-08 3.81956E-08 2.42269E-08 1.3293E-08 4.80608E-09 -1.71236E-09 -6.65133E-09 -1.03267E-08 -1.29944E-08 -1.4862E-08 -1.6097E-08 -1.68349E-08 -1.7185E-08 -1.72351E-08 -1.70561E-08 -1.67044E-08 -1.62255E-08 -1.56554E-08 -1.50227E-08 -1.435E-08 -1.36552E-08

3.625 3.6875 3.75 3.8125 3.875 3.9375 4 4.0625 4.125 4.1875 4.25 4.3125 4.375 4.4375 4.5 4.5625 4.625 4.6875 4.75 4.8125 4.875 4.9375 5 5.0625 5.125 5.1875 5.25 5.3125 5.375 5.4375 5.5 5.5625 5.625 5.6875 5.75 5.8125 5.875 5.9375 6 6.0625 6.125 6.1875 6.25 6.3125 6.375 6.4375 6.5 6.5625 6.625 6.6875 6.75 6.8125 6.875 6.9375 7 7.0625 7.125 7.1875 7.25 7.3125 7.375 7.4375 7.5 7.5625 7.625 7.6875 7.75 7.8125 7.875 7.9375

0.256353604 0.252609057 0.248962667 0.245411125 0.24195123 0.238579891 0.235294127 0.23209106 0.228967918 0.225922031 0.222950827 0.22005183 0.217222659 0.214461021 0.211764711 0.209131607 0.206559669 0.204046935 0.201591516 0.199191596 0.196845429 0.194551334 0.192307695 0.190112956 0.187965618 0.185864243 0.183807442 0.18179388 0.17982227 0.177891375 0.176000001 0.174146999 0.17233126 0.170551717 0.16880734 0.167097137 0.16542015 0.163775456 0.162162163 0.160579411 0.15902637 0.157502238 0.156006241 0.154537631 0.153095685 0.151679706 0.150289018 0.148922968 0.147580926 0.146262281 0.144966443 0.14369284 0.142440919 0.141210146 0.14 0.138809981 0.137639602 0.136488391 0.135355893 0.134241664 0.133145275 0.132066311 0.131004367 0.129959052 0.128929987 0.127916802 0.12691914 0.125936654 0.124969006 0.124015868

0.256353591 0.252609045 0.248962656 0.245411114 0.24195122 0.238579882 0.235294118 0.232091051 0.22896791 0.225922023 0.22295082 0.220051824 0.217222653 0.214461016 0.211764706 0.209131603 0.206559665 0.204046931 0.201591512 0.199191593 0.196845426 0.194551331 0.192307692 0.190112953 0.187965616 0.185864241 0.18380744 0.181793878 0.179822269 0.177891374 0.176 0.174146998 0.172331259 0.170551716 0.168807339 0.167097136 0.16542015 0.163775455 0.162162162 0.16057941 0.159026369 0.157502237 0.15600624 0.15453763 0.153095685 0.151679705 0.150289017 0.148922968 0.147580926 0.146262281 0.144966443 0.14369284 0.142440919 0.141210146 0.14 0.138809981 0.137639602 0.136488391 0.135355893 0.134241664 0.133145275 0.132066311 0.131004367 0.129959052 0.128929987 0.127916802 0.12691914 0.125936654 0.124969006 0.124015868

-1.29519E-08 -1.22508E-08 -1.15601E-08 -1.08858E-08 -1.02323E-08 -9.60278E-09 -8.99929E-09 -8.42314E-09 -7.87496E-09 -7.35492E-09 -6.86278E-09 -6.39803E-09 -5.95992E-09 -5.54758E-09 -5.16002E-09 -4.79616E-09 -4.4549E-09 -4.13513E-09 -3.83572E-09 -3.55556E-09 -3.29356E-09 -3.04869E-09 -2.81991E-09 -2.60627E-09 -2.40683E-09 -2.22071E-09 -2.04707E-09 -1.88511E-09 -1.73409E-09 -1.59329E-09 -1.46205E-09 -1.33975E-09 -1.22579E-09 -1.11963E-09 -1.02075E-09 -9.28673E-10 -8.42937E-10 -7.63125E-10 -6.88839E-10 -6.19713E-10 -5.55399E-10 -4.95579E-10 -4.39949E-10 -3.88231E-10 -3.40162E-10 -2.95498E-10 -2.54013E-10 -2.15491E-10 -1.79737E-10 -1.46565E-10 -1.15802E-10 -8.72877E-11 -6.08722E-11 -3.64138E-11 -1.3784E-11 7.14004E-12 2.64724E-11 4.43198E-11 6.07798E-11 7.59459E-11 8.99049E-11 1.02736E-10 1.14516E-10 1.25316E-10 1.35198E-10 1.44227E-10 1.52459E-10 1.59948E-10 1.66743E-10 1.72892E-10

8 8.0625 8.125 8.1875 8.25 8.3125 8.375 8.4375 8.5 8.5625 8.625 8.6875 8.75 8.8125 8.875 8.9375 9 9.0625 9.125 9.1875 9.25 9.3125 9.375 9.4375 9.5 9.5625 9.625 9.6875 9.75 9.8125 9.875 9.9375 10

0.123076923 0.122151861 0.121240382 0.120342194 0.119457013 0.118584564 0.117724577 0.116876792 0.116040955 0.11521682 0.114404145 0.113602697 0.112812248 0.112032577 0.111263467 0.110504709 0.109756097 0.109017433 0.108288522 0.107569174 0.106859206 0.106158436 0.10546669 0.104783796 0.104109589 0.103443904 0.102786584 0.102137474 0.101496421 0.10086328 0.100237906 0.099620159 0.099009901

0.123076923 0.122151861 0.121240382 0.120342194 0.119457014 0.118584564 0.117724577 0.116876792 0.116040956 0.11521682 0.114404145 0.113602697 0.112812248 0.112032577 0.111263467 0.110504709 0.109756098 0.109017433 0.108288522 0.107569174 0.106859206 0.106158436 0.10546669 0.104783797 0.104109589 0.103443905 0.102786584 0.102137474 0.101496422 0.10086328 0.100237906 0.099620159 0.099009901

1.78437E-10 1.83421E-10 1.8788E-10 1.91852E-10 1.95369E-10 1.98462E-10 2.0116E-10 2.03492E-10 2.05481E-10 2.07154E-10 2.0853E-10 2.09633E-10 2.10481E-10 2.11091E-10 2.11482E-10 2.1167E-10 2.1167E-10 2.11494E-10 2.11156E-10 2.1067E-10 2.10047E-10 2.09296E-10 2.08428E-10 2.07452E-10 2.06378E-10 2.05214E-10 2.03965E-10 2.02642E-10 2.01251E-10 1.99795E-10 1.98283E-10 1.96719E-10 1.9511E-10

First points calculated by Runge Kutta -6 method is given with bold font. As the third method AdamsBashford-Moulton formulas. This formulas like Runge-Kutta sets can be given in different orders. They are given as two sets. The first set (Adams-Bashford) is used as a predictor, and the second set is used as a corrector step. Predictor Formula : Adams-Bashford 1 step formula

y i +1 = yi + hf ( xi , yi )
Adams-Bashford 2 step formula

y i +1 = yi + y i +1 = yi + y i +1 = yi + y i +1 = yi +

h (3 f ( xi , yi ) f ( xi 1 , yi 1 ) ) 2 h (23 f ( xi , yi ) 16 f ( xi 1 , yi 1 ) + 5 f ( xi 2 , yi 2 ) ) 12 h (55 f ( xi , yi ) 59 f ( xi 1 , yi 1 ) + 37 f ( xi 2 , yi 2 ) 9 f ( xi 3 , yi 3 ) ) 24 h (1901 f ( xi , yi ) 2774 f ( xi1 , yi1 ) + 2616 f ( xi 2 , yi 2 ) 1274 f ( xi3 , yi 3 ) + 251 f ( xi 4 , yi 4 ) ) 720

Adams-Bashford 3 step formula

Adams-Bashford 4 step formula

Adams-Bashford 5 step formula

Corrector Formula : Adams-Moulton 0step formula

y i +1 = yi + hf ( xi +1 , yi +1 )
Adams-Moulton 1step formula

y i +1 = yi + y i +1 = yi + y i +1 = yi + y i +1 = yi +

h ( f ( xi+1 , yi +1 ) + f ( xi , yi ) ) 2 h (5 f ( xi+1 , yi +! ) + 8 f ( xi , yi ) f ( xi1 , yi ! ) ) 12 h (9 f ( xi+1 , yi +1 ) + 19 f ( xi , yi ) 5 f ( xi 1 , yi 1 ) + f ( xi 2 , yi 2 ) ) 24 h (251 f ( xi+1 , yi +1 ) + 646 f ( xi , yi ) 264 f ( xi 1 , yi1 ) + 106 f ( xi 2 , yi 2 ) 19 f ( xi3 , yi 3 ) ) 720

Adams-Moulton 2step formula

Adams-Moulton 3step formula

Adams-Moulton 4step formula

Program list is given as the following program. In this program two method is defined. In the first method Adams-Bashford 4 step Formula is used together with Adams-Milton 3 step Formula. In the second method Adams-Bashford 5 step Formula is used together with Adams-Milton 4 step Formula.
Program 8.5-3 Adams-Bashford-Milton Predictor-corrector method.
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Adams-BAshford-Milton Predictor Corrector formula // with Adams-Bashford 4 step predictor and Adams-Milford 3 step corrector // also with Adams-Bashford 5 step predictor and Adams-Milford 4 step corrector // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) import java.io.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy extends f_xy { double func(double x,double y) {return 1.0/(1.0+x*x)-2.0*y*y;} } class fxyKC extends f_x { double func(double x) {return x/(1+x*x);} } class NA91 { public static double[][] RK6AB4AM3(f_xy f,double x0,double y0,double xn,double h) { // Adams-Bashford (4 adm forml) Adams-Moulton (3 adm forml) predictor corrector method //with RK6 initial step int n=(int)((xn-x0)/h); double a[][]=new double[2][n+1]; // Runge-Kutta 6 int i=0; a[0][0]=x0; a[1][0]=y0;

double x3=x0+3.0*h; double k0,k1,k2,k3,k4,k5,k6; for(double xx=x0;xx<x3;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+0.25*h,a[1][i]+0.25*k1*h); k3=f.func(a[0][i]+0.25*h,a[1][i]+1.0/8.0*k1*h+1.0/8.0*k2*h); k4=f.func(a[0][i]+0.5*h,a[1][i]-0.5*k2*h+k3*h); k5=f.func(a[0][i]+3.0/4.0*h,a[1][i]+3.0/16.0*k1*h+9.0/16.0*k4*h); k6=f.func(a[0][i]+h,a[1][i]-3.0/7.0*k1*h+2.0/7.0*k2*h+12.0/7.0*k3*h-12.0/7.0*k4*h+8.0/7.0*k5*h); a[1][i+1]=a[1][i]+(7.0*k1+32.0*k3+12.0*k4+32.0*k5+7*k6)/90.0*h; i++; } for(double xx=x3;xx<xn;xx+=h) {a[0][i+1]=xx+h; //Adams-Bashford k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i-1],a[1][i-1]); k3=f.func(a[0][i-2],a[1][i-2]); k4=f.func(a[0][i-3],a[1][i-3]); a[1][i+1]=a[1][i]+h/24.0*(55.0*k1-59.0*k2+37.0*k3-9.0*k4); //Adams-Moulton j 1 den byk olabilir for(int j=0;j<5;j++) {k0=f.func(a[0][i+1],a[1][i+1]); a[1][i+1]=a[1][i]+h/24.0*(9.0*k0+19.0*k1-5.0*k2+k3); } i++; } return a; } public static double[][] RK6AB5AM4(f_xy f,double x0,double y0,double xn,double h) { // Adams-Bashford (5 adm forml) Adams-Moulton (4 adm forml) predictor corrector method //with RK6 initial step int n=(int)((xn-x0)/h); double a[][]=new double[2][n+1]; // Runge-Kutta 6 int i=0; a[0][0]=x0; a[1][0]=y0; double x4=x0+4.0*h; double k0,k1,k2,k3,k4,k5,k6; for(double xx=x0;xx<x4;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+0.25*h,a[1][i]+0.25*k1*h); k3=f.func(a[0][i]+0.25*h,a[1][i]+1.0/8.0*k1*h+1.0/8.0*k2*h); k4=f.func(a[0][i]+0.5*h,a[1][i]-0.5*k2*h+k3*h); k5=f.func(a[0][i]+3.0/4.0*h,a[1][i]+3.0/16.0*k1*h+9.0/16.0*k4*h); k6=f.func(a[0][i]+h,a[1][i]-3.0/7.0*k1*h+2.0/7.0*k2*h+12.0/7.0*k3*h-12.0/7.0*k4*h+8.0/7.0*k5*h); a[1][i+1]=a[1][i]+(7.0*k1+32.0*k3+12.0*k4+32.0*k5+7*k6)/90.0*h; i++; } for(double xx=x4;xx<xn;xx+=h) {a[0][i+1]=xx+h; //Adams-Bashford k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i-1],a[1][i-1]); k3=f.func(a[0][i-2],a[1][i-2]); k4=f.func(a[0][i-3],a[1][i-3]); k5=f.func(a[0][i-4],a[1][i-4]); a[1][i+1]=a[1][i]+h/720.0*(1901.0*k1-2774.0*k2+2616.0*k3-1274.0*k4+251*k5); //Adams-Moulton j 1 den byk olabilir for(int j=0;j<5;j++) {k0=f.func(a[0][i+1],a[1][i+1]);

a[1][i+1]=a[1][i]+h/720.0*(251.0*k0+646.0*k1-264.0*k2+106.0*k3-19*k4); } i++; } return a; } public static void main(String args[]) throws IOException {double x0=0; double y0=0.0; double yn=10.0; double h=0.0625; fxy f2=new fxy(); fxyKC fKC=new fxyKC(); double a[][]=RK6AB4AM3(f2,x0,y0,yn,h); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); //p1.addSpline(a,10); p1.addFunction(fKC,0.0,4.0,200); p1.plot(); } }

Output 8.5-4 Adams-Bashford 4 step Adams-Milton 3 step Predictor-corrector formla graphic output

In order to check the error created solution and exact solution is given as a table. The first steps calculated with Runge Kutta 6 method is given as bold font. Output 8.5-5 Adams-Bashford 4 step Adams-Milton 3 step Predictor-corrector formla calculated output with error
x 0 0.0625 0.125 0.1875 0.25 0.3125 0.375 0.4375 0.5 0.5625 0.625 0.6875 0.75 0.8125 0.875 0.9375 1 1.0625 1.125 y calculated 0 0.062256809 0.123076922 0.181132074 0.235295557 0.284699363 0.328768525 0.367213479 0.399999069 0.427297499 0.449434941 0.466839495 0.479995578 0.489407221 0.495570788 0.498956338 0.499996212 0.499079203 0.496548791 y exact 0 0.062256809 0.123076923 0.181132075 0.235294118 0.284697509 0.328767123 0.367213115 0.4 0.427299703 0.449438202 0.466843501 0.48 0.489411765 0.495575221 0.498960499 0.5 0.499082569 0.496551724 Error 0 1.29392E-10 6.07481E-10 1.36894E-09 -1.43921E-06 -1.8537E-06 -1.40145E-06 -3.64545E-07 9.31283E-07 2.20423E-06 3.26136E-06 4.0062E-06 4.42175E-06 4.54356E-06 4.43364E-06 4.16066E-06 3.78762E-06 3.36586E-06 2.93349E-06

1.1875 1.25 1.3125 1.375 1.4375 1.5 1.5625 1.625 1.6875 1.75 1.8125 1.875 1.9375 2

0.492704129 0.487802747 0.482064213 0.475674195 0.468788591 0.461537466 0.454028704 0.44635128 0.438578158 0.430768815 0.422971412 0.415224655 0.407559372 0.399999845

0.492706645 0.487804878 0.482065997 0.475675676 0.468789809 0.461538462 0.454029512 0.446351931 0.43857868 0.430769231 0.422971741 0.415224913 0.407559573 0.4

2.51644E-06 2.13058E-06 1.78422E-06 1.48028E-06 1.21822E-06 9.95355E-07 8.0791E-07 6.51661E-07 5.22374E-07 4.16056E-07 3.29088E-07 2.58278E-07 2.00868E-07 1.54505E-07

In order to see the error comparisons, the same problem is also solved with Adam Bashford 5 step predictor and Adams-Milton 4 step corrector
Output 8.5-3 Adams-Bashford 5 step predictor and Adams-Milton 4 step corrector and the error created compare to exact solution.
x y_claculated y_exact solution Error 0.00000000000000000000 0.00000000000000000000 0.00000000000000000000 0.00000000000000000000E+00 0.06250000000000000000 0.06225680920912990000 0.06225680933852140000 1.29391504455345000000E-10 0.12500000000000000000 0.12307692246944200000 0.12307692307692300000 6.07481079328842000000E-10 0.18750000000000000000 0.18113207410275700000 0.18113207547169800000 1.36894109936669000000E-09 0.25000000000000000000 0.23529411533906500000 0.23529411764705900000 2.30799382383573000000E-09 0.31250000000000000000 0.28469685475736000000 0.28469750889679700000 6.54139437172852000000E-07 0.37500000000000000000 0.32876594499773400000 0.32876712328767100000 1.17828993723146000000E-06 0.43750000000000000000 0.36721161302808700000 0.36721311475409800000 1.50172601132148000000E-06 0.50000000000000000000 0.39999839935601100000 0.40000000000000000000 1.60064398901927000000E-06 0.56250000000000000000 0.42729819196822300000 0.42729970326409500000 1.51129587194765000000E-06 0.62500000000000000000 0.44943690583998300000 0.44943820224719100000 1.29640720797353000000E-06 0.68750000000000000000 0.46684247928276000000 0.46684350132626000000 1.02204349994306000000E-06 0.75000000000000000000 0.47999925820049200000 0.48000000000000000000 7.41799507986141000000E-07 0.81250000000000000000 0.48941127391795700000 0.48941176470588200000 4.90787925355729000000E-07 0.87500000000000000000 0.49557493472326900000 0.49557522123893800000 2.86515669023490000000E-07 0.93750000000000000000 0.49896036576082800000 0.49896049896049900000 1.33199671015216000000E-07 1.00000000000000000000 0.49999997320325300000 0.50000000000000000000 2.67967469880226000000E-08 1.06250000000000000000 0.49908260956179300000 0.49908256880733900000 -4.07544535341309000000E-08 1.12500000000000000000 0.49655180272928400000 0.49655172413793100000 -7.85913529810500000000E-08 1.18750000000000000000 0.49270674027182300000 0.49270664505672600000 -9.52150969180110000000E-08 1.25000000000000000000 0.48780497575024000000 0.48780487804878000000 -9.77014595338055000000E-08 1.31250000000000000000 0.48206608863152900000 0.48206599713056000000 -9.15009694679725000000E-08 1.37500000000000000000 0.47567575623482700000 0.47567567567567600000 -8.05591512942350000000E-08 1.43750000000000000000 0.46878987649136200000 0.46878980891719700000 -6.75741645417283000000E-08 1.50000000000000000000 0.46153851582225100000 0.46153846153846200000 -5.42837894634829000000E-08 1.56250000000000000000 0.45402955364521000000 0.45402951191827500000 -4.17269353514271000000E-08 1.62500000000000000000 0.44635196178861500000 0.44635193133047200000 -3.04581428967055000000E-08 1.68750000000000000000 0.43857870091491100000 0.43857868020304600000 -2.07118653339222000000E-08 1.75000000000000000000 0.43076924329127700000 0.43076923076923100000 -1.25220462332543000000E-08 1.81250000000000000000 0.42297174691765600000 0.42297174111212400000 -5.80553199958089000000E-09 1.87500000000000000000 0.41522491391281700000 0.41522491349481000000 -4.18007350955207000000E-10 1.93750000000000000000 0.40755956890978500000 0.40755957271980300000 3.81001780125345000000E-09 2.00000000000000000000 0.39999999294883700000 0.40000000000000000000 7.05116304233755000000E-09

8.6 METHODS WITH VARIABLE STEP SIZES

One way of reducing the error, is changing step sizes by using error estimation methods. In order to achive this, two equations with different error level is required. For example 4th order Runge-Kutta-Fehlberg set is ideal for this purpose, because with only one set of k values is required for two different equation. 37 250 125 512 (4. order Runge-Kutta- Fehlberg) y i +1 = y i + ( k1 + k3 + k4 + k 6 )h 378 621 591 1771
y i +1 = y i + (

2825 18575 13525 277 1 k1 + k3 + k4 + k 5 + k 6 )h 27648 48384 55296 14336 4

(5. order Runge-Kutta- Fehlberg)

k1=f(xi,yi) k2=f(xi+0.2h , yi+0.2k1h) k3=f(xi+0.3h , yi+(3/40)k1h+(9/40)k2h) k4=f(xi+(3/5)h , yi + 0.3k1h - (9/10)k2h +(6/5)k3h) k5=f(xi+h , yi - (11/54)k1h+0.25k2h-(70/27)k3h+(35/27)k4h) k6=f(xi+(7/8)h , yi + (1631/55296)k1h+(175/512)k2h+(575/13824)k3h + (44275/110592)k4h+(253/4096)k5h) The error term

hyeni = hmevcut

new previous

new = ymeasure
is a general tolerance level. ymeasure indicates how error will be measured. For example ymeasure = y can be choosen. In our program, ymeasure will be taken as

ymeasure = y + y ( previous

dy . The value of can be taken as 0.2 if ( previous new ) ; as 0.25 if dx new ) .

As an example differential equation the following eqaution will be taken


2 2 dy = 0.6 y + 10e ( x 2 ) /[ 2 ( 0.075) ] dx

Solution of this differential equation has a jump point around x=2 . Because of this, solving it with constant step size is relatively difficult.
Program 8.6-1 Runge-Kutta-Fehlberg variable step method with variable step size
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta-Fehlberg variable step method // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) import java.io.*; import java.util.*; abstract class f_xy { //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy2 extends f_xy { double func(double x,double y) {return -0.6*y+10.0*Math.exp(-(x-2)*(x-2)/(2*0.075*0.075));} } class NA92 { public static double[][] RKF6(f_xy f,double x0,double y0,double xn,double h,double eps) { // Runge-Kutta Fehlberg 6 deiken stepli //eps bal hata double alfa; //double a[][]=new double[2][n+1]; int i=0; //a[0][0]=x0; //a[1][0]=y0;

double delta=h; double deltayeni=eps*y0; double k1,k2,k3,k4,k5,k6; Vector<Double> xi=new Vector<Double>(); Vector<Double> yi=new Vector<Double>(); double x,y,yp1_5,yp1_6,xeski,yeski; x=x0; y=y0; xeski=x; yeski=y; Double Xi=new Double(x); Double Yi=new Double(y); xi.add(Xi); yi.add(Yi); while((x+h)<=xn) { xeski=x; yeski=y; k1=f.func(x,y); k2=f.func(x+1.0/5.0*h,y+1.0/5.0*k1*h); k3=f.func(x+3.0/10.0*h,y+3.0/40.0*k1*h+9.0/40.0*k2*h); k4=f.func(x+3.0/5.0*h,y+3.0/10.0*k1*h-9.0/10.0*k2*h+6.0/5.0*k3*h); k5=f.func(x+h,y-11.0/54.0*k1*h+5.0/2.0*k2*h-70.0/27.0*k3*h+35/27.0*k4*h); k6=f.func(x+7.0/8.0*h,y+1631.0/55296.0*k1*h+175.0/512.0*k2*h+575.0/13824.0*k3*h+44275.0/110592.0*k4*h+253.0/4096.0*k5*h); yp1_5=y+(37.0/378.0*k1+250.0/621.0*k3+125.0/594.0*k4+512.0/1771.0*k6)*h; yp1_6=y+(2825.0/27648.0*k1+18575.0/48384.0*k3+13525.0/55296.0*k4+277.0/14336.0*k5+1.0/4.0*k6)*h; y=yp1_6; x=x+h; Xi=new Double(x); Yi=new Double(y); xi.add(Xi); yi.add(Yi); delta=Math.abs(yp1_6-yp1_5); deltayeni=Math.abs(eps*yp1_6); if(delta<=deltayeni) alfa=0.2; else alfa=0.25; h=h*Math.pow((deltayeni/delta),alfa); i++; } if(x<xn)//son bir step daha { h=xn-x; k1=f.func(x,y); k2=f.func(x+1.0/5.0*h,y+1.0/5.0*k1*h); k3=f.func(x+3.0/10.0*h,y+3.0/40.0*k1*h+9.0/40.0*k2*h); k4=f.func(x+3.0/5.0*h,y+3.0/10.0*k1*h-9.0/10.0*k2*h+6.0/5.0*k3*h); k5=f.func(x+h,y-11.0/54.0*k1*h+5.0/2.0*k2*h-70.0/27.0*k3*h+35/27.0*k4*h); k6=f.func(x+7.0/8.0*h,y+1631.0/55296.0*k1*h+175.0/512.0*k2*h+575.0/13824.0*k3*h+44275.0/110592.0*k4*h+253.0/4096.0*k5*h); yp1_5=y+(37.0/378.0*k1+250.0/621.0*k3+125.0/594.0*k4+512.0/1771.0*k6)*h; yp1_6=y+(2825.0/27648.0*k1+18575.0/48384.0*k3+13525.0/55296.0*k4+277.0/14336.0*k5+1.0/4.0*k6)*h; y=yp1_6; //System.out.println("x="+x+"h="+h+"y="+yp1_6); x=xn; Xi=new Double(x); Yi=new Double(y); xi.add(Xi); yi.add(Yi); i++; } //vektre aktarlan deerleri double matrise aktar int n=i; double aa[][]=new double[2][n+1]; Enumeration nx=xi.elements(); Enumeration ny=yi.elements(); for(i=0;i<=n;i++) {

//String ss1=(String)nx.nextElement(); //String ss2=(String)ny.nextElement(); Double ax=(Double)nx.nextElement(); Double ay=(Double)ny.nextElement(); aa[0][i]=ax.doubleValue(); aa[1][i]=ay.doubleValue(); } return aa; } public static void main(String args[]) throws IOException { double x0=0; double y0=0.5; fxy2 f1=new fxy2(); double a[][]=RKF6(f1,x0,y0,4.0,0.01,1e-7); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); p1.addSpline(a,8); //p1.addData(a); p1.plot(); } }

Output 8.6-1 Runge-Kutta-Fehlberg variable step method with variable step size

8.7 DIFFERENTIAL EQUATIONS WITH MORE THEN ONE SET OF EQUATION Consider the soluton of differential equation set: dy1 = f1 ( x, y1 , y 2 ,..., y n ) dx dy 2 = f 2 ( x, y1 , y 2 ,..., y n ) dx

.
dy n = f n ( x, y1 , y 2 ,..., y n ) dx

This set requires n initial conditions to solve it. For example consider differential equation set
dy1 = 0.5 y1 dx

dy 2 = 4 0.3 y 2 0.1y1 dx If for x=0, y1=4 and y2=6 is given. If this set is solved with 6th order Runge_Kutta method:

Program 8.7-1 2 variable differential equation set with Runge-Kutta6 method


//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 6 method with multible differential equations // Dr. Turhan Coban // ===================================================== // dy1/dx=f(x,y1,y2,..,yn) // dy2/dx=f(x,y1,y2,..,yn) // ...... // dyn/dx=f(x,y1,y2,..,yn) import java.io.*; abstract class f_xi { // multifunction multi independent variable // a single value is returned indiced to equation_ref // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[],int equation_ref); } class fm1 extends f_xi { //multivariable function double func(double x[],int x_ref) { //x[0] is x, x[1] is y if(x_ref==0) return -0.5*x[1]; if(x_ref==1) return 4-0.3*x[2]-0.1*x[1]; else return 0; } }

class NA93 { public static double[][] RK6(f_xi fp,double x0,double xn,double f0[],int N) { //6th order Runge Kutta Method //fp : given set of derivative functions dfj/dxi(fj,x) // xo : initial value of the independent variable // xn : final value of the independent variable // f0 : initial value of the dependent variable // N : number of dependent variable to be calculated // fi : dependent variable double h=(xn-x0)/N; int M=f0.length; double fi[][]; fi=new double[M][N+1]; double xi[]=new double[M+1]; double k[]=new double[6]; int i,j; double x; for(j=0;j<M;j++) {

fi[j][0]=f0[j]; xi[j+1]=f0[j]; } for(x=x0,i=0;i<N;x+=h,i++) { for(j=1;j<=M;j++) { xi[0]=x; xi[j]=fi[j-1][i]; k[0]=h*fp.func(xi,j-1); xi[0]=x+h/2.0; xi[j]=fi[j-1][i]+k[0]/2; k[1]=h*fp.func(xi,j-1); xi[0]=x+h/2.0; xi[j]=fi[j-1][i]+k[0]/4.0+k[1]/4.0; k[2]=h*fp.func(xi,j-1); xi[0]=x+h; xi[j]=fi[j-1][i]-k[1]+2.0*k[2]; k[3]=h*fp.func(xi,j-1); xi[0]=x+2.0/3.0*h; xi[j]=fi[j-1][i]+7.0/27.0*k[0]+10.0/27.0*k[1]+1.0/27.0*k[3]; k[4]=h*fp.func(xi,j-1); xi[0]=x+1.0/5.0*h; xi[j]=fi[j-1][i]+28.0/625.0*k[0]-1.0/5.0*k[1]+546.0/625.0*k[2]+54.0/625.0*k[3]-378/625.0*k[4]; k[5]=h*fp.func(xi,j-1); fi[j-1][i+1]=fi[j-1][i]+k[0]/24.0+5.0*k[3]/48.0+27.0*k[4]/56.0+125.0*k[5]/336.0; xi[j]=fi[j-1][i]; } } double a[][]=new double[M+1][N+1]; for(x=x0,i=0;i<=N;x+=h,i++) { a[0][i]=x; for(j=1;j<=M;j++) { a[j][i]=fi[j-1][i]; } } return a; } public static void main(String args[]) throws IOException { fm1 b3=new fm1(); double y[]; y=new double[2]; y[0]=4; y[1]=6; double a[][]=RK6(b3,0.0,2.0,y,4); Text.print(Text.T(a)); Plot pp=new Plot(a); pp.plot(); } }

Output 8.7-1 Solution of 2 equation set of differential equation by using RK6

Output 8.7-2 Output 8.7-1 Solution of 2 equation set of differential equation by using RK6
0.0 4.0 6.0 0.5 3.115199788411459 6.835752376757813 1.0 2.426117430429699 7.596172963183511 1.5 1.889460126483987 8.282667565228241 2.0 1.4715114465587011 8.898456285684427

The same equation solved by Runge-Kutta-Fehlberg method:


//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta-Fehlberg variable step method // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) // Runge_Kutta-Fehlberg method // Multiequation method with error correction import java.io.*; import java.util.*; abstract class f_xi { // multifunction multi independent variable // a single value is returned indiced to equation_ref // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[],int equation_ref); } class fm1 extends f_xi { //multivariable function double func(double x[],int x_ref) { //x[0] is x, x[1] is y if(x_ref==0) return -0.5*x[1]; if(x_ref==1) return 4-0.3*x[2]-0.1*x[1];

else return 0; } } class fxy2 extends f_xy { double func(double x,double y) {return -0.6*y+10.0*Math.exp(-(x-2)*(x-2)/(2*0.075*0.075));} } class NA92A { public static double[] doublearray(String s) { StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static double[][] RKF6(f_xi fp,double x0,double xn,double f0[],double h,double eps) { // Runge-Kutta Fehlberg with 6 variable step int M=f0.length; int N=0; //number of data points String s=""; Vector<String> vi=new Vector<String>(); double hi=h; double hh[]=new double[M]; double deltai=1; double alfa=1.0; double delta=1; double deltanew=1; double deltamin=9.99e60; double deltanewmin=9.99e60; double hmin=h; double xi[]=new double[M+1]; double xxi[]=new double[M+1]; double k[]=new double[6]; double fi1,fi2,fi; int i,j; double x; xi[0]=x0; x=x0; s=xi[0]+" "; for(j=0;j<M;j++) { xi[j+1]=f0[j]; s+=xi[j+1]+" "; } vi.add(s);

while((x+h)<=xn) { xi[0]=x+h; for(j=1;j<=M;j++) { xxi[0]=x; xxi[j]=xi[j]; k[0]=fp.func(xxi,j-1); xxi[0]=x+h/5.0; xxi[j]=xi[j]+k[0]*h/5.0; k[1]=fp.func(xxi,j-1); xxi[0]=x+h*3.0/10.0; xxi[j]=xi[j]+k[0]*3.0/40.0+9.0/40.0*k[1]*h; k[2]=fp.func(xxi,j-1); xxi[0]=x+3.0/5.0*h; xxi[j]=xi[j]+3.0/10.0*k[0]*h-9.0/10.0*k[1]*h+6.0/5.0*k[2]*h; k[3]=fp.func(xxi,j-1); xxi[0]=x+h; xxi[j]=xi[j]-11.0/54.0*k[0]*h+5.0/2.0*k[1]*h+70.0/27.0*k[2]*h+35.0/27.0*k[3]*h; k[4]=fp.func(xxi,j-1); xxi[0]=x+7.0/8.0*h; xxi[j]=xi[j]+1631.0/55296.0*k[0]*h-175.0/512.0*k[1]*h+575.0/13824.0*k[2]*h+44275.0/110592.0*k[3]*h+253.0/4096.0*k[4]*h; k[5]=h*fp.func(xxi,j-1); fi1=xi[j]+(37.0/378.0*k[0]+250.0/621.0*k[2]+125.0/594.0*k[3]+512.0/1771.0*k[5])*h; fi2=xi[j]+(2825.0/27648.0*k[0]+18575.0/48384.0*k[2]+13525.0/55296.0*k[3]+277.0/14336.0*k[4]+1.0/4.0*k[5])*h; fi=fi2; xi[j]=fi2; delta=Math.abs(fi1-fi2); deltanew=Math.abs(eps*fi2); if(delta<=deltanew) alfa=0.2; else alfa=0.25; hh[j-1]=h*Math.pow((deltanew/delta),alfa); } //end of for(j for(j=1;j<=M;j++) {if(hh[j-1]<h) {h=hh[j-1];}} s=xi[0]+" "; for(j=0;j<M;j++) { s+=xi[j+1]+" "; } vi.add(s); x+=h; N++; } //end of while x-=h; if(x<xn)//one last additional step { h=xn-x; xi[0]=xn; for(j=1;j<=M;j++) { xxi[0]=x; xxi[j]=xi[j]; //k1=f.func(x,y); k[0]=fp.func(xxi,j-1); //k2=f.func(x+1.0/5.0*h,y+1.0/5.0*k1*h); xxi[0]=x+h/5.0; xxi[j]=xi[j]+k[0]*h/5.0; k[1]=fp.func(xxi,j-1);

//k3=f.func(x+3.0/10.0*h,y+3.0/40.0*k1*h+9.0/40.0*k2*h); xxi[0]=x+h*3.0/10.0; xxi[j]=xi[j]+k[0]*3.0/40.0+9.0/40.0*k[1]*h; k[2]=fp.func(xxi,j-1); //k4=f.func(x+3.0/5.0*h,y+3.0/10.0*k1*h-9.0/10.0*k2*h+6.0/5.0*k3*h); xxi[0]=x+3.0/5.0*h; xxi[j]=xi[j]+3.0/10.0*k[0]*h-9.0/10.0*k[1]*h+6.0/5.0*k[2]*h; k[3]=fp.func(xxi,j-1); //k5=f.func(x+h,y-11.0/54.0*k1*h+5.0/2.0*k2*h-70.0/27.0*k3*h+35/27.0*k4*h); xxi[0]=x+h; xxi[j]=xi[j]-11.0/54.0*k[0]*h+5.0/2.0*k[1]*h+70.0/27.0*k[2]*h+35.0/27.0*k[3]*h; k[4]=fp.func(xxi,j-1); //k6=f.func(x+7.0/8.0*h,y+1631.0/55296.0*k1*h+175.0/512.0*k2*h+575.0/13824.0*k3*h+44275.0/110592.0*k4*h+253.0/4096.0*k5*h); xxi[0]=x+7.0/8.0*h; xxi[j]=xi[j]+1631.0/55296.0*k[0]*h-175.0/512.0*k[1]*h+575.0/13824.0*k[2]*h+44275.0/110592.0*k[3]*h+253.0/4096.0*k[4]*h; k[5]=h*fp.func(xxi,j-1); //yp1_5=y+(37.0/378.0*k1+250.0/621.0*k3+125.0/594.0*k4+512.0/1771.0*k6)*h; //yp1_6=y+(2825.0/27648.0*k1+18575.0/48384.0*k3+13525.0/55296.0*k4+277.0/14336.0*k5+1.0/4.0*k6)*h; //y=yp1_6; fi1=xi[j]+(37.0/378.0*k[0]+250.0/621.0*k[2]+125.0/594.0*k[3]+512.0/1771.0*k[5])*h; fi2=xi[j]+(2825.0/27648.0*k[0]+18575.0/48384.0*k[2]+13525.0/55296.0*k[3]+277.0/14336.0*k[4]+1.0/4.0*k[5])*h; fi=fi2; xi[j]=fi; delta=Math.abs(fi1-fi2); deltanew=Math.abs(eps*fi2); if(delta<=deltanew) alfa=0.2; else alfa=0.25; hh[j-1]=h*Math.pow((deltanew/delta),alfa); } for(j=1;j<=M;j++) {if(hh[j-1]<h) {h=hh[j-1];}} s=xi[0]+" "; for(j=0;j<M;j++) { s+=xi[j+1]+" "; } vi.add(s); N++; x+=h; } //end of if(x<xn) //convert vector values back to the array Iterator nx=vi.iterator(); String s1=""; double aa[][]=new double[M+1][N+1]; i=0; while(nx.hasNext()) {s1=(String)nx.next(); xi=doublearray(s1); for(j=0;j<=M;j++) { aa[j][i]=xi[j];} i++; } return aa; } public static void main(String args[]) throws IOException {

fm1 b3=new fm1(); double y[]; y=new double[2]; y[0]=4; y[1]=6; double a[][]=RKF6(b3,0.0,2.0,y,0.01,1e-5); Text.print(Text.T(a)); Plot pp=new Plot(a); pp.plot(); } }

Runge-Kutta-Verner Method (8 steps)


//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta-Verner variable step method // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) // Multiequation method with error correction import java.io.*; import java.util.*; abstract class f_xi { // multifunction multi independent variable // a single value is returned indiced to equation_ref // example f[0]=x[0]+sin(x[1]) // f[1]=x[0]*x[0]-x[1] // func(x,1) returns the value of f[1] // func(x,0) returns the value of f[0] abstract double func(double x[],int equation_ref); } class fm1 extends f_xi { //multivariable function double func(double x[],int x_ref) { //x[0] is x, x[1] is y if(x_ref==0) return -0.5*x[1]; if(x_ref==1) return 4-0.3*x[2]-0.1*x[1]; else return 0; } } class fxy2 extends f_xy { double func(double x,double y) {return -0.6*y+10.0*Math.exp(-(x-2)*(x-2)/(2*0.075*0.075));} } class NA92C { public static double[] doublearray(String s) {

StringTokenizer token=new StringTokenizer(s); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } return a; } public static double[][] RKV8(f_xi fp,double x0,double xn,double f0[],int N) { // Runge-Kutta Verner with 8 variable step double h=(xn-x0)/N; int M=f0.length; String s=""; Vector<String> vi=new Vector<String>(); double hi=h; double hh[]=new double[M]; double deltai=1; double alfa=1.0; double delta=1; double deltanew=1; double deltamin=9.99e60; double deltanewmin=9.99e60; double hmin=h; double xi[]=new double[M+1]; double xxi[]=new double[M+1]; double k[]=new double[8]; double fi1=0,fi2=0,fi=0; int i,j; double x; xi[0]=x0; x=x0; s=xi[0]+" "; for(j=0;j<M;j++) { xi[j+1]=f0[j]; s+=xi[j+1]+" "; } vi.add(s); for(i=0;i<N;i++) { x=h*i; xi[0]=x; for(j=1;j<=M;j++) { xxi[0]=x; xxi[j]=xi[j]; k[0]=h*fp.func(xxi,j-1); xxi[0]=x+h/6.0; xxi[j]=xi[j]+k[0]/6.0; k[1]=h*fp.func(xxi,j-1); xxi[0]=x+h*4.0/15.0;

xxi[j]=xi[j]+k[0]*4.0/75.0+16.0/75.0*k[1]; k[2]=h*fp.func(xxi,j-1); xxi[0]=x+h*2.0/3.0; xxi[j]=xi[j]+k[0]*5.0/6.0-8.0/3.0*k[1]+5.0/2.0*k[2]; k[3]=h*fp.func(xxi,j-1); xxi[0]=x+5.0/6.0*h; xxi[j]=xi[j]-165.0/64.0*k[0]+55.0/6.0*k[1]-425.0/64.0*k[2]+85.0/96.0*k[3]; k[4]=h*fp.func(xxi,j-1); xxi[0]=x+h; xxi[j]=xi[j]+12.0/5.0*k[0]*h-8.0*k[1]+4015.0/612.0*k[2]-11.0/36.0*k[3]+88.0/255.0*k[4]; k[5]=h*fp.func(xxi,j-1); xxi[0]=x+1.0/15.0*h; xxi[j]=xi[j]-8263.0/15000.0*k[0]+124.0/75.0*k[1]-643.0/680.0*k[2]-81.0/250.0*k[3]+2484.0/10625.0*k[4]; k[6]=h*fp.func(xxi,j-1); xxi[0]=x+h; xxi[j]=xi[j]+3501.0/1720.0*k[0]-300.0/43.0*k[1]+297275.0/52632.0*k[2]319.0/2322.0*k[3]+24068.0/84065.0*k[4]+3850/26703*k[6]; k[7]=h*fp.func(xxi,j-1); fi1=xi[j]+13.0/160.0*k[0]+2375.0/5984.0*k[2]+5.0/16.0*k[3]+12.0/85.0*k[4]+3.0/44.0*k[5]; fi2=xi[j]+3.0/40.0*k[0]+875.0/2244.0*k[2]+23.0/72.0*k[3]+264.0/1955.0*k[4]+125.0/11592.0*k[6]+43.0/616.0*k[7]; fi=fi2; xi[j]=fi2; } s=xi[0]+" "; for(j=0;j<M;j++) { s+=xi[j+1]+" "; } vi.add(s); x+=h; } //end of while x-=h; if(x<xn)//one last additional step { h=xn-x; xi[0]=xn; for(j=1;j<=M;j++) { xxi[0]=x; xxi[j]=xi[j]; k[0]=h*fp.func(xxi,j-1); xxi[0]=x+h/6.0; xxi[j]=xi[j]+k[0]/6.0; k[1]=h*fp.func(xxi,j-1); xxi[0]=x+h*4.0/15.0; xxi[j]=xi[j]+k[0]*4.0/75.0+16.0/75.0*k[1]; k[2]=h*fp.func(xxi,j-1); xxi[0]=x+h*2.0/3.0; xxi[j]=xi[j]+k[0]*5.0/6.0-8.0/3.0*k[1]+5.0/2.0*k[2]; k[3]=h*fp.func(xxi,j-1);

xxi[0]=x+5.0/6.0*h; xxi[j]=xi[j]-165.0/64.0*k[0]+55.0/6.0*k[1]-425.0/64.0*k[2]+85.0/96.0*k[3]; k[4]=h*fp.func(xxi,j-1); xxi[0]=x+h; xxi[j]=xi[j]+12.0/5.0*k[0]*h-8.0*k[1]+4015.0/612.0*k[2]-11.0/36.0*k[3]+88.0/255.0*k[4]; k[5]=h*fp.func(xxi,j-1); xxi[0]=x+1.0/15.0*h; xxi[j]=xi[j]-8263.0/15000.0*k[0]+124.0/75.0*k[1]-643.0/680.0*k[2]-81.0/250.0*k[3]+2484.0/10625.0*k[4]; k[6]=h*fp.func(xxi,j-1); xxi[0]=x+h; xxi[j]=xi[j]+3501.0/1720.0*k[0]-300.0/43.0*k[1]+297275.0/52632.0*k[2]319.0/2322.0*k[3]+24068.0/84065.0*k[4]+3850/26703*k[6]; k[7]=h*fp.func(xxi,j-1); fi1=xi[j]+13.0/160.0*k[0]+2375.0/5984.0*k[2]+5.0/16.0*k[3]+12.0/85.0*k[4]+3.0/44.0*k[5]; fi2=xi[j]+3.0/40.0*k[0]+875.0/2244.0*k[2]+23.0/72.0*k[3]+264.0/1955.0*k[4]+125.0/11592.0*k[6]+43.0/616.0*k[7]; fi=fi2; xi[j]=fi; } s=xi[0]+" "; for(j=0;j<M;j++) { s+=xi[j+1]+" "; } vi.add(s); N++; x+=h; } //end of if(x<xn) //convert vector values back to the array Iterator nx=vi.iterator(); String s1=""; double aa[][]=new double[M+1][N+1]; i=0; while(nx.hasNext()) {s1=(String)nx.next(); xi=doublearray(s1); for(j=0;j<=M;j++) { aa[j][i]=xi[j];} i++; } return aa; } public static void main(String args[]) throws IOException { fm1 b3=new fm1(); double y[]; y=new double[2]; y[0]=4; y[1]=6; double a[][]=RKV8(b3,0.0,2.0,y,1000); Text.print(Text.T(a)); Plot pp=new Plot(a); pp.plot();

} }

Set of differential equation solution concept can be used to solve nth degree diferental equations as well.
d n y1 dy d 2 y d n 1 y = g ( x, y , , 2 ,..., n 1 ) n dx dx dx dx

This equation can be cange to take form of x=x0 y=x1 dy = x2 dx d2y = x3 dx 2 .. d n 1 y = xn 1 dx n 1 Our high order differential equation is converted to a set of single order diferential equations
dxk d k y = k = xk +1 , 1 k < n dx0 dx 0 dxn d n y = n = g ( x0 , x1 , x2 ,..., xn ) dx0 dx 0 If the initial conditions are given, this type of set of equations can be solved easily. For example consider the diferential equation d2y dy + 11 + 10 y = 10u (t ) dt 2 dt With u(t)=1,and the initial conditions at t=0 y(t)=0 and dy(t)/dt=0 . Exact solution of this differential equation is : y(t)=1-(10/9)exp(-t)+(1/9)exp(-10t) . To solve the same equation numerically, it can be divided into two equations as dx1 = x2 dx0
dx 2 =10u 10 x1 11x 2 dx0

x1(0)=0 x2(0)=0 THis form can be easily solved byusing the previous RK6 program(NA93)
Program 8.7-2 diferansiyel denklem setinin 6 derece Runge-Kutta metodu ile zlmesi
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 6 method with multible differential equations // Dr. Turhan Coban // ===================================================== // dy1/dx=f(x,y1,y2,..,yn) // dy2/dx=f(x,y1,y2,..,yn) // ...... // dyn/dx=f(x,y1,y2,..,yn) import java.io.*;

class fm1 extends f_xi { //multivariable function double func(double x[],int x_ref) { //x[0] is x, x[1] is y if(x_ref==0) return x[2]; if(x_ref==1) return 10-10.0*x[1]-11*x[2]; else return 0; } } class fm2 extends f_x {// gerek zm public double func(double t) {return 1.0-10.0/9.0*Math.exp(-t)+1.0/9.0*Math.exp(-10.0*t);} } class NA94 { public static void main(String args[]) throws IOException { fm1 b1=new fm1(); fm2 b2=new fm2(); double y[]; y=new double[2]; y[0]=0; y[1]=0; double a[][]=NA93.RK6(b1,0.0,0.2,y,200); Text.print(Text.T(a)); Plot pp=new Plot(a); pp.addFunction(b2,0.0,0.2,20); pp.addSpline(2,100); pp.setPlotType(2,33); pp.plot(); } }

Program 8.7-2 diferansiyel equationn 6 derece Runge-Kutta metodu ile zlmesi

Few more example will be given this chapter to understand nature of the real diferential equation solutions. Fluid mechanics is a topic mechaniacla engineering, chemical engineering meterology is closely interested. Turbulent flow is one of the important sub topic. In the classical approach to the turbulance random movements are assumed. When it is investigated further it is shown that some diferential equations can be given chaotic results. This created a new science of chaotic systems. Edward Lorents, an athmospheric scientist developed Lorents set of diferential equations to investigate athmospheric turbulance effects. The equation set: dx1 = ( x2 x2 ) dt
dx2 = (1 + x3 ) x1 x2 dt dx3 = x1 x 2 x2 dt

In this equations , , and are pozitif constants. Equation behave chaotically when > + 1 and
>
( + 1)( + + 1) . For example with = 10, = 24 ve = 2 system behaves chaotically. If we solve the 1

Lorents equation set with 0 s t 30 s time range, x1(0) = 1.0; x2(0) = 0.0; x3(0) = 20.0 conditions
Program 8.7-3 Solution of Lorentz equation with RK6
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== import java.io.*;

class fm1 extends f_xi { //multivariable function double sigma=10.0;

double lambda=28.0; double gamma=8.0/3.0; double func(double x[],int x_ref) { //x[0] is x, x[1] is y if(x_ref==0) return sigma*(x[2]-x[1]); if(x_ref==1) return (1+lambda-x[3])*x[1]-x[2]; else return x[1]*x[2]-gamma*x[3]; } } class diferansiyel3B { public static void main(String args[]) throws IOException { fm1 b1=new fm1(); double y[]; y=new double[3]; y[0]=-0.6; y[1]=0.0; y[2]=60.0; double a[][]=diferansiyel3.RK6(b1,0.0,40.0,y,10000); Plot p1=new Plot(a[1],a[3]); p1.setXlabel("x1"); p1.setYlabel("x3"); p1.setPlabel("Lorentz attractor"); p1.plot(); Plot p2=new Plot(a[0],a[1]); p2.setXlabel("x0"); p2.setYlabel("x3"); p2.setPlabel("Lorentz equation"); p2.plot(); } }

Output 8.7-3 Lorentz denklem setinin 6 derece Runge-Kutta metodu ile zlmesi = 10, =24 and =2

Parametreleri biraz deitirirsek denklemlerin deiik bir davran gsterdiini gzleyebiliriz.


Output 8.7-4 Lorentz denklem setinin 6 derece Runge-Kutta metodu ile zlmesi = 10, =90 ve =5.5

Another interesting example in chaotic behavior Predator-Prey model. When the population of Predators are increased, the population of the Prey animal will decreased and it wil caused population deacrease of the Predator population as well. This relation is explained and put into a matematical model by italian mathematician Vito Voltera and American Biolog Alfret J. Lotka. Their equation is: dy1 = ay1 by1 y 2 dt dy 2 = cy2 + dy1 y 2 dt In this equation y1 is Prey animal population, y2 is Predator population, a is Prey animal birth rate, b is Predator dead rate, c is Predator-Prey relation ratio in Predator dead rate and d is Predator-Prey relation ratio in Predator population increase.
Program 8.7-4 Predator-Prey model (Lotka-Volterra equation)
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 6 method with multible differential equations // Dr. Turhan Coban // ===================================================== // dy1/dx=f(x,y1,y2,..,yn) // dy2/dx=f(x,y1,y2,..,yn) // ...... // dyn/dx=f(x,y1,y2,..,yn) // Lotka - Volterra equation // Predator-Prey model import java.io.*; class fm1 extends f_xi { //av avc modeli // Lotka-Volterra equation double a=1.2; //av oalma oran double b=0.6; //avc lm oran double c=0.8; //avn lmndeki av avc ilikisinin etkisi double d=0.3; //avcnn yetimesindeki av avc ilikisinin etkisi double func(double x[],int x_ref) { //x[0] is x, x[1] is y if(x_ref==0) return a*x[1]-b*x[1]*x[2]; else return -c*x[2]+d*x[1]*x[2];

} } class NA96 { public static void main(String args[]) throws IOException { fm1 b1=new fm1(); double y[]; y=new double[2]; y[0]=1.0; y[1]=2.0; double a[][]=NA93.RK6(b1,0.0,40.0,y,10000); Plot p1=new Plot(a[0],a[1]); p1.addData(a[0],a[2]); p1.setXlabel("zaman"); p1.setYlabel("av ve avc"); p1.setPlabel("Av - Avc modeli"); p1.plot(); Plot p2=new Plot(a[1],a[2]); p2.setXlabel("hunted"); p2.setYlabel("hunter"); p2.setPlabel("Hunted-Hunter model"); p2.plot(); } }

Output 8.7-5 Predator-Prey model (Lotka-Volterra equation) a=1.2 b= 0.6 c=0.8 d=0.3, Predator-pray population change with respect to each other

Output 8.7-6 Predator-Prey model (Lotka-Volterra equation) a=1.2 b= 0.6 c=0.8 d=0.3, Predator-pray population change with time

While investigating hard beats, Van der Pol developed the set of differential equation to represent hard beats. This equations are d2y dy + ( y 2 1) + y = 0 If this equation are converted into a system of equations with dt 2 dt y=y1 and

dy = y2 dt
dy1 = y2 dt dy2 = y1 ( y12 1) y2 = 0 dt If this equation is solved for =1 and y1(0)=0, y2(0)=0.5 :

Program 8.7-5 Van der Pol differential equation


//====================================================== // Numerical Analysis package in java // example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== import java.io.*; class fm1 extends f_xi { //Van der Pol salnm deklemi //kalp at simulasyonu double mu=1.0; // d^2y/dt^2+mu(y^2-1)dy/dt=0 double func(double x[],int x_ref) { //x[0] is x, x[1] is y if(x_ref==0) return x[2]; else return -x[1]-mu*(x[1]*x[1]-1.0)*x[2]; }} class diferansiyel3D { public static void main(String args[]) throws IOException { fm1 b1=new fm1(); double y[];

y=new double[3]; y[0]=0.0; y[1]=0.5; double a[][]=diferansiyel3.RK6(b1,0.0,40.0,y,10000); Plot p1=new Plot(a[1],a[2]); p1.setXlabel("y"); p1.setYlabel("dy/dt"); p1.setPlabel("van der Pol equation"); p1.plot(); Plot p2=new Plot(a[0],a[1]); p2.setXlabel("t"); p2.setYlabel("y"); p2.setPlabel("van der Pol equation"); p2.plot(); } }

Output 8.7-7 Van der Pol denklem setinin 6 derece Runge-Kutta metodu ile zlmesi

Output 8.7-8 Van der Pol denklem setinin 6 derece Runge-Kutta metodu ile zlmesi

The next example is the harmonic motion with damping. The diferential equation is:

d2y dy + b + 2 y = 0 2 dt dt

The equation will be solved with initial conditions y(0)=1

dy (0) = 1 and =1 ve b=0.1. When b=0, dt

it is called simple harmonic motion. a periodic motion that is neither driven nor damped. When b=0.1, it is a damped motion. Oscilations will get smaller and stop after a certain time. Converted form of the equation:

dy1 = y2 dt dy2 = by2 2 y dt


y1(0)=1 y2(0)=1
Program 8.7-6 Harmonic motion with damping
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== import java.io.*; class fm1 extends f_xi { //Snml harmonik hareket double b=0.1; double w=1.0; // d^2y/dt^2+b*dy/dt+w^2y=0 double func(double x[],int x_ref) { //x[0] is x, x[1] is y if(x_ref==0) return x[2]; else return -b*x[2]-w*w*x[1]; } } class diferansiyel3E { public static void main(String args[]) throws IOException { fm1 b1=new fm1(); double y[]; y=new double[3]; y[0]=0.0; y[1]=1.0; double a[][]=diferansiyel3.RK6(b1,0.0,40.0,y,10000); Plot p1=new Plot(a[1],a[2]); p1.setXlabel("y"); p1.setYlabel("dy/dt"); p1.setPlabel("Snml harmonik hareket"); p1.plot(); Plot p2=new Plot(a[0],a[1]); p2.setXlabel("t"); p2.setYlabel("y"); p2.setPlabel("Snml harmonik hareket"); p2.plot(); } }

Output 8.7-9 Harmonic motion with damping

The next example is the harmonic motion with damping in an electrical circuit. This kind of behavior can be seen frequently in circuits when current is not constant and changed as a function of time. Such a circuit is shown in the figure below.

The differential equation of the system:

d 2q dq q +R + E (t ) = 0 In this equation q is capacitor energy storage capacity (C, Coulomb). 2 dt dt C

L enductance (H, Henry), R res,stance (, Ohm), C capacitance (F), and E(t)=E0sin(wt) is time dependent voltage, w frequency and t is time (s) . Current density is the derivative of capacitor energy storage capacity.

i=

dq (Ampere). An analytical solution of this equation for R=0 ve E(t)=E0sin(wt) : dt

q (t ) =

E0 w E0 sin( pt ) + sin( wt ) . And the current density 2 2 2 L( p w 2 ) L( p w ) p

i=

E0 w dq(t ) E0 w sin( pt ) + sin( wt ) . In this equation = 2 2 L( p 2 w 2 ) dt L( p w ) 1 dir. LC dq(0) = 0 and dt

p=

Now this equation will be solved numerically for initial conditions q(0)=0 ve i (0) =

physical conditions w2=3.5 1/s2 p=2, L=1 H, E0=1 V, C=0.25 F. Since analytical solution is known, two solutions can be compared. It is a good exampel to observe the numerical solution error generation due to very complex behaviour of the function. Converted equation :

dy1 = y2 dt y1 Ry2 C + E0 sin( wt ) dy2 = dt L


y1(0)=0 y2(0)=0
Program 8.7-7 Harmonic motion electrical circuit (Changing voltage problem)
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 6 method with multible differential equations // Dr. Turhan Coban // ===================================================== // dy1/dx=f(x,y1,y2,..,yn) // dy2/dx=f(x,y1,y2,..,yn) // ...... // dyn/dx=f(x,y1,y2,..,yn) // Changing voltage problem import java.io.*;

class fm1 extends f_xi { double E0=1.0; //Volt double L=1.0; // Henry double C=0.25; // Coulomb double w2=3.5; // s^2 double R=0.0; // Ohm double w=Math.sqrt(w2); double p=1.0/Math.sqrt(L*C); // Ld^2q/dt^2+R*dq/dt+q/C-E(t)=0 double func(double x[],int x_ref) { //x[0] is t, x[1] is q x[2] is i

if(x_ref==0) return x[2]; else return (-R*x[2]-x[1]/C+E0*Math.sin(w*x[0]))/L; } double i(double t) { return -E0/L/(p*p-w2)*w*Math.cos(p*t)+E0/L/(p*p-w2)*w*Math.cos(w*t); } double q(double t) { return -E0/L/(p*p-w2)*w/p*Math.sin(p*t)+E0/L/(p*p-w2)*Math.sin(w*t); } } class NA98 { public static void main(String args[]) throws IOException { fm1 b1=new fm1(); double y[]; y=new double[3]; y[0]=0.0; y[1]=0.0; int n=800000; double a[][]=NA93.RK6(b1,0.0,100.0,y,n); Plot p1=new Plot(a[0],a[2]); double t=0; double dt=100.0/n; for(int i=0;i<(n-1);i++) {a[1][i]=b1.i(t);t+=dt;} p1.addData(a[0],a[1]); p1.setXlabel("t time second"); p1.setYlabel("i=dq/dt Current density Ampere"); p1.setPlabel("Changing Voltage problem"); p1.plot(); } }

To calculate current both numerical and analytical solutions are calculated and plotted. As number of points N=800000 points are used for the numerical calculations. If N=50000, error generation in numerical solution can be observed.

For N=25000 level of numerical error even gets bigger by time

Another example. Assume that a bullet is thrown from the gun into an athmosphere. Due to high velocity of the bullet, friction will effect the movement of the bullet. The diferential equation of the motion in cartesian coordinate system can be given as:

d 2x 2 2 = ( v x + v y )v x 2 dt d2y 2 2 = g ( v x + v y )v y 2 dt k In this equation = ,where k friciton coefficient between the bullet and air, vx is x direction component of m
the velocity, vy is y direction component of the velocity, m mass, g gravitational acceleration, and t is time. With t=x[0] , x=x[1], y=x[2], vx=x[3], vy=x[4] variable convertion the diferential equation becomes

dx[1] = x[3] dx[0] dx[2] = x[4] dx[0] dx[3] = ( x 2 [3] + x 2 [4] ) x[3] dx[0] dx[4] = g ( x 2 [3] + x 2 [4] ) x[4] dx[0]
Initial conditions x[0]=0 da x[1]=0 x[2]=0, V0=250 m/s x[3]= V0 sin() m/s, x[4]= V0 cos() m/s, g=9.806 m/s2, =0.01 , =30,45,60 degree
Program 8.7-7 gun bullet trajectory with air friction
//====================================================== // Numerical Analysis package in java

// example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== import java.io.*;

class fm1 extends f_xi { double gamma; public fm1(double gammai) {gamma=gammai;} // srtnmeli eik at problemi double func(double x[],int x_ref) { //x[0] is x, x[1] is y double g=9.806; if(x_ref==0) return x[3]; else if(x_ref==1) return x[4]; else if(x_ref==2) return -gamma*Math.sqrt(x[3]*x[3]+x[4]*x[4])*x[3]; else return -g - gamma*Math.sqrt(x[3]*x[3]+x[4]*x[4])*x[4]; } } class diferansiyel3F { public static void main(String args[]) throws IOException { fm1 b1=new fm1(0.01); double y[]; y=new double[4]; double V0=250.0;// m/s double alfa=Math.PI/3; //60 derece y[0]=0.0; y[1]=0.0; y[2]=V0*Math.sin(alfa); y[3]=V0*Math.cos(alfa); double a[][]=diferansiyel3.RK6(b1,0.0,20.0,y,1000); Plot p1=new Plot(a[1],a[2]); alfa=Math.PI/4; //45 derece y[2]=V0*Math.sin(alfa); y[3]=V0*Math.cos(alfa); double b[][]=diferansiyel3.RK6(b1,0.0,20.0,y,1000); p1.addData(b[1],b[2]); alfa=Math.PI/6; //30 derece y[2]=V0*Math.sin(alfa); y[3]=V0*Math.cos(alfa); double c[][]=diferansiyel3.RK6(b1,0.0,20.0,y,1000); p1.addData(c[1],c[2]); double xa[]=new double[2]; double ya[]=new double[2]; xa[0]=0;xa[1]=12.0; ya[0]=0;ya[1]=0.0; p1.addData(xa,ya); p1.setXlabel("x"); p1.setYlabel("y"); p1.setPlabel("srtnmeli top at 60,45 ve 30 derece a ve 250 m/s mermi k hz"); p1.setMinMax(0,300,0,200); p1.plot(); } }

Output 8.7-10 gun bullet trajectory with air friction

=30, 45, 60 derece, bullet gun exit speed 250 m/s, gravitationa accaleration 8.806 m/s2, Friction coefficient = (k / m )= 0.01

Pendulum is another interesting problem. Pendulum is an old mechanical problem. A pendulum is a weight suspended from a pivot, length l, so it can swing freely. When a pendulum is displaced from its resting equilibrium position with a displacement angle , it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position (=0). When released, the restoring force combined with the pendulum's mass causes it to oscillate about the equilibrium position, swinging back and forth. The time for one complete cycle, a left swing and a right swing, is called the period. From its discovery around 1602 by Galileo Galilei the regular motion of pendulums was used for timekeeping, and was the world's most accurate timekeeping technology until the 1930s.

The diferential equation of the pendulum motion is d 2 g = sin( ) 2 l dt

In this equation , swing angle, t time, and g is gravitational acceleration coefficient(m/s2) . This is a non-linear differential equation. For very small swing angles, from sin() Taylor series opening

sin( ) =

3
3!

5
5!

7
7!

9
9!

+ .....

Diferential equation can be approximated as

d 2 g = This is a simle linear equation that can be solved by analytical methods easily. The solution is 2 l dt

(t ) = 0 cos(

g t) l

An analytical solution of the non-linear equation is not existed, but it can be solved by using numerical methods. Both solution is calculated and plotted by using the following program. As it is seen from the plot outputs, for small angles (/16 radian) both solutions are quite similar, but when the angle grows to bigger values(/2 radian), solutions differs.
Program 8.7-8 Pendulum problem //====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 6 method with multible differential equations // Dr. Turhan Coban // ===================================================== // dy1/dx=f(x,y1,y2,..,yn) // dy2/dx=f(x,y1,y2,..,yn) // ...... // dyn/dx=f(x,y1,y2,..,yn) // Pendulum problem

import java.io.*; class fm1 extends f_xi { // Pendulum equation double g=9.806; //gravitational acceleration m/s2 double l=0.6; //pendulum length m double func(double x[],int x_ref) { //x[0] is t, x[1] is teta(pendulum swing angle) x[2] v velocity if(x_ref==0) return x[2]; else return -g/l*Math.sin(x[1]); } } class NA101 { public static void main(String args[]) throws IOException { fm1 b1=new fm1();

double y[]; y=new double[2]; y[0]=Math.PI/16.0; //11.25 degree swing angle y[1]=0.0; double a[][]=NA93.RK6(b1,0.0,10.0,y,100000); int n=a[0].length; double teta[]=new double[n]; for(int i=0;i<n;i++) {teta[i]=y[0]*Math.cos(Math.sqrt(b1.g/b1.l)*a[0][i]);} Plot p1=new Plot(a[0],a[1]); p1.addData(a[0],teta); p1.setXlabel("time"); p1.setYlabel("pendulum angle"); p1.setPlabel("Pendulum model"); p1.plot(); } }
Output 8.7-11 Pendulum problem =/16 radian, gravitational accaleration g=9.806 m/s2, Pendulum length l= 0.6 m

Output 8.7-12 Pendulum problem =/16 radian, gravitational accaleration g=9.806 m/s2, Pendulum length l= 0.6 m

Even with small angle two solutions differs by long period of time, but the wrong one could be the numerical solution here due to added error.
8.8 BOUNDARY PROBLEM AND SHOOTING METHODS IN DIFFERENTIAL EQUATIONS

In all differential equations solutions so far, it is asumed that initial condition (value of dependent value at satrting independent value). In the real system given boundary condition is not necassarily initial condition. Numerical differential equation solution methods requires an initial condition to be given. When a different type of boundary value is defined, following steps can be taken to solve the problem Step 1 : Guess a value for the initial condition and solve the diferential equation fort his initial condition Step 2 : Repeat step 1 3 to 5 times. Find the value correspond to the given boundary condition (for example at the other end) Step 3 : Make a curve fitting or interpolation between initial conditions and corrsponding end result Step 4 : By using the the curve fitting find initial condition corresponds to the given boundary condition As an example problem Blassius flat plate problem from fluid mechanics is considered. This problem is one of the first numerically solved diferential equation. (An analytical solution is not available for this problem) . Diferential equation:

df''/d +0.5*f*f'' = 0 When the equation written in seperated form f'''=-0.5*x[1]*x[2] f''=x[2] f'=x[1] The boundary conditions to the equaton is given as x[0]=0 da x[2]=0 x[0]= da x[2]=1 x[0]=0 da x[3]=0 Initial condition for x[1] is not specified. Therefore a guess value is taken (in the example code the initial value is taken as 0.3). Instead of infinity value of x[0] the value of x[0]=6.8 is taken . The cubic spline interpoation applied to find actual initial value (value at x[0]=0) as x[1]=0.3267065268657032. At x[0]=6.8 x[2] = 0.9992172933326781 is found.
Program 8.8-1 Blassius flat plate problem
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // and differentiation (derivative) functions // Dr. Turhan Coban // ===================================================== //Blasius flat plate problem import java.io.*; class fm1 extends f_xi { //multivariable function double func(double x[],int x_ref) { //Blasius flat plate diferential equation //f'''+0.5*ff'' = 0 //f'''=-0.5*x[1]*x[2] f''=x[1] f'=x[2] double a=0; if(x_ref==0) a= -0.5*x[1]*x[3]; if(x_ref==1) a= x[1]; if(x_ref==2) a= x[2]; return a; } } class diferansiyel1 { public static double[][] RK4(f_xi fp,double x0,double xn,double f0[],int N) { //4th order Runge Kutta Method //fp : given set of derivative functions dfj/dxi(fj,x) // xo : initial value of the independent variable // xn : final value of the independent variable // f0 : initial value of the dependent variable // N : number of dependent variable to be calculated // fi : dependent variable double h=(xn-x0)/N; int M=f0.length; double fi[][]; fi=new double[M][N+1]; double xi[]=new double[M+1]; double k[]=new double[4]; int i,j; double x;

for(j=0;j<M;j++) { fi[j][0]=f0[j]; xi[j+1]=f0[j]; } for(x=x0,i=0;i<N;x+=h,i++) { for(j=1;j<=M;j++) { xi[0]=x; xi[j]=fi[j-1][i]; k[0]=h*fp.func(xi,j-1); xi[0]=x+h/2.0; xi[j]=fi[j-1][i]+k[0]/2; k[1]=h*fp.func(xi,j-1); xi[j]=fi[j-1][i]+k[1]/2.0; k[2]=h*fp.func(xi,j-1); xi[0]=x+h; xi[j]=fi[j-1][i]+k[2]; k[3]=h*fp.func(xi,j-1); fi[j-1][i+1]=fi[j-1][i]+k[0]/6.0+k[1]/3.0+k[2]/3.0+k[3]/6.0; xi[j]=fi[j-1][i]; } } double a[][]=new double[M+1][N+1]; for(x=x0,i=0;i<=N;x+=h,i++) { a[0][i]=x; for(j=1;j<=M;j++) { a[j][i]=fi[j-1][i]; } } return a; }

public static double[][] RK6(f_xi fp,double x0,double xn,double f0[],int N) { //6th order Runge Kutta Method //fp : given set of derivative functions dfj/dxi(fj,x) // xo : initial value of the independent variable // xn : final value of the independent variable // f0 : initial value of the dependent variable // N : number of dependent variable to be calculated // fi : dependent variable double h=(xn-x0)/N; int M=f0.length; double fi[][]; fi=new double[M][N+1]; double xi[]=new double[M+1]; double k[]=new double[6]; int i,j; double x; for(j=0;j<M;j++) { fi[j][0]=f0[j]; xi[j+1]=f0[j]; } for(x=x0,i=0;i<N;x+=h,i++) { for(j=1;j<=M;j++) { xi[0]=x; xi[j]=fi[j-1][i];

k[0]=h*fp.func(xi,j-1); xi[0]=x+h/2.0; xi[j]=fi[j-1][i]+k[0]/2; k[1]=h*fp.func(xi,j-1); xi[0]=x+h/2.0; xi[j]=fi[j-1][i]+k[0]/4.0+k[1]/4.0; k[2]=h*fp.func(xi,j-1); xi[0]=x+h; xi[j]=fi[j-1][i]-k[1]+2.0*k[2]; k[3]=h*fp.func(xi,j-1); xi[0]=x+2.0/3.0*h; xi[j]=fi[j-1][i]+7.0/27.0*k[0]+10.0/27.0*k[1]+1.0/27.0*k[3]; k[4]=h*fp.func(xi,j-1); xi[0]=x+1.0/5.0*h; xi[j]=fi[j-1][i]+28.0/625.0*k[0]-1.0/5.0*k[1]+546.0/625.0*k[2]+54.0/625.0*k[3]-378/625.0*k[4]; k[5]=h*fp.func(xi,j-1); fi[j-1][i+1]=fi[j-1][i]+k[0]/24.0+5.0*k[3]/48.0+27.0*k[4]/56.0+125.0*k[5]/336.0; xi[j]=fi[j-1][i]; } } double a[][]=new double[M+1][N+1]; for(x=x0,i=0;i<=N;x+=h,i++) { a[0][i]=x; for(j=1;j<=M;j++) { a[j][i]=fi[j-1][i]; } } return a; }

public static double[][] RKF45(f_xi fp,double x0,double xn,double f0,int N) throws IOException { // Runge Kutta - Fehlberg Method // fp : derivative function df/dx(f,x) (defined as a class) // Tol :Tolerance //Hmax : maximum possible step size //Hmin : minimum possible step size //k : Runge kutta coefficients //Err : error //x[i] : input variable to fp = df/dx(f,x) = df/dx(x[i]) //j : actual step size //fi[][]:solution matrix //fj[][]:solution matrix in exact size(j) (return function) double h=(xn-x0)/N; double fi[][]=new double[2][1000]; double Tol=2.0e-3; double Hmin=h/64.0; double Hmax=h*64.0; double k[]=new double[6]; double Err; double s; double xi[]=new double[2]; int j=0; fi[0][j]=x0; fi[1][j]=f0; while(fi[0][j] < xn ) { if(fi[0][j]+h > xn) h=xn-fi[0][j]; xi[0]=fi[0][j]; xi[1]=fi[1][j]; k[0]=h*fp.func(xi,0);

xi[0]=fi[0][j]+h/4.0; xi[1]=fi[1][j]+k[0]/4.0; k[1]=h*fp.func(xi,0); xi[0]=fi[0][j]+3.0/8.0*h; xi[1]=fi[1][j]+3.0/32.0*k[0]+9.0/32.0*k[1]; k[2]=h*fp.func(xi,0); xi[0]=fi[0][j]+12.0/13.0*h; xi[1]=fi[1][j]+1932.0/2197.0*k[0]-7200.0/2197.0*k[1]+7296.0/2197.0*k[2]; k[3]=h*fp.func(xi,0); xi[0]=fi[0][j]+h; xi[1]=fi[1][j]+439.0/216.0*k[0]-8.0*k[1]+3680.0/513.0*k[2]-845.0/4104.0*k[3]; k[4]=h*fp.func(xi,0); xi[0]=fi[0][j]+0.5*h; xi[1]=fi[1][j]-8.0/27.0*k[0]+2.0*k[1] -3544/2565*k[2]+1859.0/4104.0*k[3]-11.0/40.0*k[4]; k[5]=h*fp.func(xi,0); Err=Math.abs(1.0/360.0*k[0]-128/4275*k[2] -2197.0/75240*k[3]+1.0/5.0*k[4]+2.0/55.0*k[5]); if(Err<Tol || h<2*Hmin) { fi[0][j+1]=fi[0][j]+h; fi[1][j+1]=fi[1][j]+16.0/135.0*k[0]+6656.0/12825.0*k[2]+ 28561.0/56430.0*k[3]-9.0/50.0*k[4]+2.0/55.0*k[5]; j++; } else { if(Err==0) s=0; else s=0.84*(Math.pow(Tol*h/Err,0.25)); if(s<0.75 && h>(2*Hmin) ) h/=2.0; if(s>1.5 && Hmax >(2*h) ) h*=2.0; } } double fj[][]=new double[2][j+1]; for(int i=0;i<=j;i++) { fj[0][i]=fi[0][i]; fj[1][i]=fi[1][i]; } return fj; } public static double [] thomas(double a[][],double r[]) { // int n=a.length; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double x[]=new double[n]; for(int i=0;i<n;i++) {f[i]=a[i][i];} for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];} for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];} for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; }

public static double [] thomas(double f[],double e[],double g[],double r[]) { int n=f.length; double x[]=new double[n]; for(int k=1;k<n;k++) {e[k]=e[k]/f[k-1]; f[k]=f[k]-e[k]*g[k-1]; } for(int k=1;k<n;k++) {r[k]=r[k]-e[k]*r[k-1]; } x[n-1]=r[n-1]/f[n-1]; for(int k=(n-2);k>=0;k--) {x[k]=(r[k]-g[k]*x[k+1])/f[k];} return x; }

public static double [][] cubic_spline(double xi[],double yi[],double c0,double cn) { int n=xi.length; double h[]=new double[n]; double w[]=new double[n]; double f[]=new double[n]; double e[]=new double[n]; double g[]=new double[n]; double d[]=new double[n]; double x[]=new double[n]; double S[][]=new double[4][n]; int k; for(k=0;k<(n-1);k++) {h[k]=xi[k+1]-xi[k]; w[k]=(yi[k+1]-yi[k])/h[k]; } d[0]=c0; d[n-1]=cn; for(k=1;k<(n-1);k++) {d[k]=6.0*(w[k]-w[k-1]);} f[0]=1.0; f[n-1]=1.0; g[0]=0.0; g[n-1]=0.0; e[0]=0.0; e[n-1]=0.0; for(k=1;k<(n-1);k++) {f[k]=2.0*(h[k]+h[k-1]);e[k]=h[k-1];g[k]=h[k];} S[2]=thomas(f,e,g,d); S[3]=xi; for(k=0;k<(n-1);k++) {S[0][k]=(6.*yi[k+1]-h[k]*h[k]*S[2][k+1])/(6.0*h[k]); S[1][k]=(6.*yi[k]-h[k]*h[k]*S[2][k])/(6.0*h[k]); } return S; } public static double funcSpline(double S[][],double x) { int n=S[0].length; double xx1=0; double xx2=0; double y=0; double hk=0; for(int k=0;k<(n-1);k++) {if(S[3][k]<=x && x<=S[3][k+1]) {hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]);

xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); break; } } if(y==0 && S[3][n-2]<=x ) { int k=n-2; hk=(S[3][k+1]-S[3][k]); xx1=(x-S[3][k]); xx2=(S[3][k+1]-x); y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk); } return y; } public static double[][] funcSpline(double xi[],double yi[],int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double S[][]=cubic_spline(xi,yi,0,0); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) { z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcSpline(S,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]); return z; } public static void main(String args[]) throws IOException { fm1 b3=new fm1(); double y[]=new double[3]; y[0]=0.3; //y0 first guess for f'' y[1]=0.0; y[2]=0.0; double a[][]=new double[3][501]; //solution of the differential system : double xi[]=new double[20]; double fi[]=new double[20]; //shooting method for(int i=1;i<20;i++) { a=RK6(b3,0.0,6.0,y,500); xi[i]=a[2][500]; fi[i]=y[0]; y[0]+=0.1; //change the boundary value } //curve fit to find actual boundary value double S[][]=cubic_spline(xi,fi,0,0); y[0]=funcSpline(S,1.0); //y[0]=Numeric.f_least_square(Numeric.poly_least_square(xi,fi,4),1.0);//curve fit value for f'' // calculate final solution a=RK6(b3,0.0,6.0,y,500); Text.print(Text.T(a)); //System.out.println(Matrix.toString(Matrix.T(a))); }

8.9 PROBLEMS
PROBLEM 1 dy/dx = y*sin(x)* sin(x) diferansiyel equation is given. Solution is desired in the x=0 to x=1 range for h=dx=0.2 with the initial conditions of (x=0) y=2. Use fourth order Runge-Kutte method. PROBLEM 2 d2y/dx2+6 dy/dx= sin(x) diferansiyel equation at x=0 to x=1 range with the boundary conditions of x=0 y=2 and x=1 de y=-1 Solve diferential equation for h=0.02 step size. PROBLEM 3 dy/dx= sin(x) diferansiyel equation at x=0 to x=1 range with the boundary conditions of x=0 y=2 and Solve diferential equation for h=0.02 step size. PROBLEM 4 Falkner-Scan flow equation is given as f''' + f*f'' + (1-f2) = 0 eklinde verilmitir. Boundary conditions of the equation f(0)=0 f(0)=1 f( )=1 = 0.3 ve = 1.0 Instead of infinity( ) value ytake value of 8. PROBLEM 5 Solve diferential equation y = x2 - y2 - 3y with initial conditions y(0)=0, y(0)=0 PROBLEM 6 Solve diferential equation y = y2 - 4y with initial conditions y(0)=0, y(0)=1, y(0)=0 . PROBLEM 7 Solve diferential equation

x = 3x-y y=x+y with initial conditions x(0)=0.2, ve y(0)=0.5 .


PROBLEM 8 Solve diferential equation 2x(t)-5x(t)-3x(t)=45e2t with initial conditions x(0)=0 ve x(0)=1 . PROBLEM 9 Solve diferential equation x(t)=x(t)-x(t)y(t) y(t)=-y(t)+x(t)y(t) with initial conditions x(0)=4 ve y(0)=1 at t=[0,8] range. PROBLEM 10 Solve diferential equation

x' (t ) =

2 2t x' (t ) x(t ) + 1 with initial conditions x(0)=1.25 ve x(4)=-0.95 at range t=[0,4]. 2 1+ t2 1+ t

PROBLEM 11 Solve diferential equation y = -(y)2 y +lnx with initial conditions: y(1)=0 PROBLEM 12 For diferential equation

y(2)=ln2 at the range 1<=x<=2

y 2(1 + y 2 ) = 0 with , y(0)=0 starting value and, h=0,25 step size find y(1) deerini with Heun method
PROBLEM 13 Solve diferential equation dy = yx 2 y at the range x=0 ve x=2 , with initial condition y(0)=1 . dx

a) b) c) d)

solve analytically from x=0 to x=2. solve numerically by hand with h=0.5 step size with 4th order Runge_Kutte and calculate y(2) solve numerically by computer with h=0.1 step size with 4th order Runge-Kutta Plot to results and compare with analytical solution

PROBLEM 14 Solve diferential equation

dy = 5 y + e 2 x dx
1/3 rule.

with initial conditions : x0=0 y0=1 . y(1) , and with step size h=0.2 by using Simpson

PROBLEM 15 Solve diferential equation

dy 1 = dx x + y

with initial conditions x0=0 , y0=2 , at the interval 0-0.6 with step size h=0.2,

a-) with 4th order Runge-Kutta method b-) with Euler method
PROBLEM 16 Cooling rate of a substance are given with the following equation

dT = k (T Ta ) dt

In the equation T indicates the temperature of the substance in (oC), Ta is environmental temperature (oC) ,k is a constant (minute-1) dir. Therefore cooling rate is function of temperature difference between body and environment. If a metal ball initially at 90 oC drop into a water bath at Ta=20 oC and if k=0.1 minute-1, Find the time period required ball to reach 30 oC temperature a) By analytical methods b) By a numerical method
PROBLEM 17 Solution of diferential equation dy = yx 2 y are investigated with range x=0 ve x=2. Initial condition is given as y(0)=1 dx a) Solve it anaytically Plot the solution. b) Solve it with Heun method with step size h=0.5 adm c) Plot the numerical results also and compare it with analytical results PROBLEM 18 In a chemical reaction a chemical A and a chemical B reacts to give chemical C. Concentration of chemical C as a function of time, y(t) is given with the following equation y = k(a y) (b y) y(0)=0 In this equation k is a positive constant, a and b is the initial concentration of chemicals A and B. Assume that k=0.01 , a= 70 milimole/liter , b= 50 milimole/liter . By using 4th order Runge-Kutta method with step size h=0.5 estimate concentration at t=1 second. PROBLEM 19 Solve differential equation dy = yx 2 y dx With range of x=0 ve x=2. Initial value is given as y(0)=1 . a) Solve the problem analytically b) Solve it with Heun method, step size h=0.5 c) Plot both results and compare relative error. PROBLEM 20

In the spring-mass system shown in the figure force P(t) is equal to 10t N t < 2 s P (t ) = 20 N t 2 s Diferential equation of the movement d 2 y P (t ) k with m=2.5 kg, k=75 N/m. Determine amplitude (maximum amount of movement) of = y dt 2 m m the movement.
PROBLEM 21

A cone buoy can slide freely on a bar is floating on the surface. If cone is disturbed by any effect, it will oscilate. The diferential equation of the oscilation is given as: d2y = g (1 ay 3 ) If a=16 m-3, g=9.80665 m/s2 and cone position initially moved upward to y=0.1 m and dt 2 released, find the period(s) and amplitude(m) of the oscilation.
PROBLEM 23 Gun problem with air friction was given in the examples. Solve the same problem with 6th order RungeKutta method: Nozle exit velocity 250 m/s, gravitational acceleration 9.806 m/s2, friction coefficient = (k / m )= 0.01. A target at 750 m distance will be hit. Determine the angle . PROBLEM 24 In cars and other machines for vibration control mechanical system with a spring and a shock absorber is used. The differential equation of such a system is given as:

d 2x dx + kd + ks x = 0 2 dt dt

In this equation m is mass, kd shock absorber coefficient, ks is spring coefficient. Solve the diferential equation for m=5 kg, ks =500 N/m, kd =33 N/m values.

PROBLEM 25 Hopf Bifurcations equation is given by

dx = ( g + x 2 + y 2 ) x wy dt dy = ( g + x 2 + y 2 ) y + wx dt
Solve the set of differential equations with g=-0.5,w=4, with the initial conditions x=0,y=0.01 at t=0. Tfinal=20 s

PROBLEM 26 Hopf Bifurcations equation is given by

dx = ( g + x 2 + y 2 ) x wy dt dy = ( g + x 2 + y 2 ) y + wx dt dg = 0.0001 + 0.1 tanh(10 g 2 ) dt


Solve the set of differential equations with,w=4, with the initial conditions x=0,y=0.01,g=-1 at t=0. Tfinal=20
PROBLEM 27 The Duffing Oscilator differential equations are given as

dx =v dt d 2 x dv = = 0.2v + x 0.1x 3 + cos(t ) 2 dx dt


Solve the differential equation set with the initial conditions of x=v=0 at t=0 for 0 to t=200 s period.

PROBLEM 27 The Mathieu equation is given as

d 2x + ( + 2 cos(2t )) x = 0 dt 2
Solve the differential equation with initial conditions =8, =2.5, x=0,x=0 for the time period of 0<=t<=100 s.

PROBLEM 28 Henon-Heiles equation is given as

dx = Px dt dy = Py dt dPx = x 2 xy dt dPy = y x2 + y2 dt
Solve the differential equation with initial conditions x=0,y=0.1 and Px=0.49057, Py=0 for the time period of 0<=t<=200 s.

PROBLEM 29 Henon-Heiles equation is described in Problem 28. Solve the equation with the initial conditions of x=0,y=0 and Px= 0.35355, Py= 0.35355 for the time period of 0<=t<=200 s.

PROBLEM 30

Intermittency in Josephson Junctions: The study of the chaotic behavior of (quantum) Josephson junctions is of much fundamental and even practical interest. Written in dimensionless form, the differential equation for the quantum phase difference, , across the junction is given by

d 2 1 d + + sin = A sin t 2 dt c dt
where c is the so-called McCumber parameter and is the (normalized) angular frequency of the driving current. u = v = and v =

d , this can be written as dt

For differential equation solution parametric values will be taken as A=0.9045, r=0.5, =0.47. Solve the differential equation for the initial conditions u=v=0 for 0<=t<=1200 s

du =v dt dv = Asin(t) - rv - sin(u) dt

PROBLEM 31 Planar motion of a three body celestial system can be expressed by the following equations:

dx = p+ y dt dy =qx dt dp m( x + n) = q dt ( x + n) 2 + y 2

3/ 2

[( x + n)

n( x m)
2

+ y2 ny
2

3/ 2

my dq = p dt ( x + n) 2 + y 2

3/ 2

[( x + n)

+ y2

3/ 2

where the p and q are the canonical momenta of the coordinates x and y, respectively. There are five equilibrium positions, called Lagrangian positions. One of the Lagrangian position is , L4, located at (xaverage,yaverage) = (0.48, 0.866). In order to solve the diferential equation of the motion initial starting point will be taken as: (x,y,p,q)={0.44,0.866,-0.866,0.48} as initial conditions. Solve the differential equations for 0<t<200. Take m=0.98 n=0.02

EIGENVALUE AND EIGENVECTOR CALCULATIONS

9.1 INTRODUCTION TO EIGENVALUES Non-zero solution of [A]{X} = 0 equation is existed, but not necessarily a single solution. A set of {X} vectors can be existed for any given matrix [A]. The equation can also be written as follows for the
unit matrix I

[A]{X} = [I]{} {X} = 0 [A- ]{X}= 0 In this equation is called eigen-value and {X} is called eigen-vector. If the equation is written in the
open format:

a11 a 11 a11 ... a1n

a12 a 22 a11 ... a2n

a1n X 1 a 23 a2 n X 2 ... a33 ... a3 n X 3 = 0 ... .... .... ... a3 n ... a nn X 5 a13 ...

In this chapter, solution methods of eigenvalues and eigenvectors will be investigated. It should be note that eigenvalues could be complex numbers as well.
8.1 DOMINANT EIGENVALUE CALCULATION (POWER METHOD)

Assume that A is n x n matrix with eigenvalues 1, 2, 3,.., n with an associated collection of lineerly independent eigenvectors x1, x2, x3,, xn . It is also assumed that A has one eigenvalue 1 which is largest in magitude so that |1| > |2| > |3| > |4| > |n-1| > |n| It shoud be noted that a matrix A does not have to have linealy independent eigenvectors. When it does not, convergence of the power method may fail. Eigen value with the maximum absolute value is called dominant eigen value. If an eigenvalue is dominant it should be either a real value or it should have an eigen vector consist of real values. If xk is the eigenvector corresponds to eigenvalue k , The following equations can be written. A xk= k xk , 1 <= k < n. Where { x1 , x2 , x3 , x4 ,, xn } is eigenvector with n components. If A is a symmetric matrix, with n linearly independent eigenvalues. If y0 is any vector the fact that { x1 , x2 , x3 , x4 ,, xn } is linearly independent implies that constants ck exist with

y 0 = ck x k
k =1

If a series of yk vectors ae constructed yk+1=A yk, k 0


n n n y 1 = A c k x k = c k A x k = c k k x k k =1 k =1 k =1

Generalizing the equation will yield

y = ck ik x k , i 0
i k =1

remembering that 1 is the eigenvalue with the largest magnitude. If equation becomes
i n k k 1 y = c1 x + ck x , i 0 k =2 1 i i 1

i 1

is taken out of the paranthesis, the

It is known tht

k / 1 < 1 . Therefore when i k / 1 i 0 .

i y i 1c1 x1 , i >> 1

If vector y is normalised : Ay k y k +1 = Ay k Vector y became the eigenvector of the dominant eigenvalue. When the eigenvector is known, eigenvalue can be calculated easily. Ax=x By multilying both side of te equation with the transpose matrix xT xT A x = xT x and xT A x = T x x When k is is large, it is known that yk ia approximating to the dominant eigenvalue .In this case

( y k )T A y k , ( y k )T y k

k 0

Can be written with interpolatie process approximate to the dominant eigenvalue. Deviation of the from the eigen value is

r k ( k I A) y k
In thisequation when rk = 0

k becomes eigenvalue and

yk becomes eigenvector. In other words if

rk

is

small iteration process can be finilize. In the following program computer code fro the power method is given Program 9.2-1 Power method to calculate the dominant eigenvalue
import java.io.*; import javax.swing.*; class NA131 { //===========Eigen value calculations ============= //*************norm methods definition**************** public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); } public static double norm(double v[],int p)

{ //p vector norm if(p==0) p=1; double total=0; double x,y; for(int i=0;i<v.length;i++) { x=Math.abs(v[i]); total+=Math.pow(x,p); } return Math.pow(total,(1.0/p)); } public static double norminf(double v[]) { //infinite vector norm double x; double max=0; for(int i=0;i<v.length;i++) { x=Math.abs(v[i]); if(x>max) max=x; } return max; } public static double norminf(double a[][]) { //infinite matrix norm double x; double max = 0; double total; int i,j; int n=a.length; for(i=0;i<n;i++) { total=0; for(j=0;j<n;j++) {total+=Math.abs(a[i][j]);} x=total; if(x>max) max=x; } return max; } public static double norm(double a[][]) { //matrix norm double x; double max = 0; double total; int i,j; int n=a.length; for(j=0;j<n;i++) { total=0; for(i=0;i<n;j++) {total+=Math.abs(a[i][j]);} x=total; if(x>max) max=x; } return max; } public static double normE(double a[][]) { //Euclidian matrix norm double x; double total;

int i,j; total=0; int n=a.length; for(j=0;j<n;i++) {for(i=0;i<n;j++) {total+=a[i][j]*a[i][j];}} return Math.sqrt(total); } public static double[] multiply(double[][] left,double[] right) { //multiplication of one matrix with one vector int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right.length; double[] b; b=new double[m2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) {b[ii]=0;} return b; } for(i=0;i<m1;i++) { b[i]=0; for(k=0;k<n1;k++) b[i]+=left[i][k]*right[k];} return b; //end of multiply of a matrix and a vector } public static double YT_X(double [] left,double [] right) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double eigen_power(double A[][]) { double eps=1e-15; double err=1; int m=100; int n=A.length; double y[]=new double[n]; double r[]=new double[n]; double xnorm; double mu=1.0; for(int i=0;i<n;i++) y[i]=1.0; int k=0; double mueski; double x[]=new double[n]; do{ mueski=mu; x=multiply(A,y); xnorm=norminf(x); for(int i=0;i<n;i++) {y[i]=x[i]/xnorm;} mu=YT_X(y,x)/YT_X(y,y); for(int i=0;i<n;i++) {r[i]=mu*y[i]-x[i];} k++; err=Math.abs((mu-mueski)/mu); }while(err>eps); return mu;

public static void main(String args[]) throws IOException { //Eigen Value calculations using method //double a[][]={{3.556,-1.778,0},{-1.778,3.556,-1.778},{0,-1.778,3.556}}; //double a[][]={{-3.9,-0.6,-3.9,0.4},{1,0,0,0},{0,1,0,0},{0,0,1,0}}; double a[][]={{2,-3,6},{0,3,-4},{0,2,-3}}; String s="Orijinal Matrix : \n"; s+=Matrix.toString(a); s+="Dominant eigenvalue = "+eigen_power(a); JOptionPane.showMessageDialog(null,s, "Power method to calculate dominant eigenvalue : ",JOptionPane.PLAIN_MESSAGE); } }

Output 9.2-1 Power method to calculate dominant eigenvalue

9.3 SMALLEST MAGNITUDE EIGENVALUE( INVERSE POWER METHOD )

If Power method for dominant eigenvalue problem applied in inverse way, smallest magnitude eigenvalue can be obtained.

A 1 y k 1 y = 1 k 1 A y
k

k =

( y k ) T A 1 y k ( y k )T y k

Program 9.3-1 Power method to calculate smallest magnitude eigenvalue


import java.io.*; import javax.swing.*;

class NA132 { //===========Eigen value calculations ============== public static double norm(double v[]) { // vector norm double total=0;; for(int i=0;i<v.length;i++) {total+=v[i]*v[i];} return Math.sqrt(total); }

public static double norm(double v[],int p) { //p vector norm if(p==0) p=1; double total=0; double x,y; for(int i=0;i<v.length;i++) { x=Math.abs(v[i]); total+=Math.pow(x,p); } return Math.pow(total,(1.0/p)); } public static double norminf(double v[]) { //infinite vector norm double x; double max=0; for(int i=0;i<v.length;i++) { x=Math.abs(v[i]); if(x>max) max=x; } return max; } public static double norminf(double a[][]) { //infinite matrix norm double x; double max = 0; double total; int i,j; int n=a.length; for(i=0;i<n;i++) { total=0; for(j=0;j<n;j++) {total+=Math.abs(a[i][j]);} x=total; if(x>max) max=x; } return max; } public static double norm(double a[][]) { //matrix norm double x; double max = 0; double total; int i,j; int n=a.length; for(j=0;j<n;i++) { total=0; for(i=0;i<n;j++) {total+=Math.abs(a[i][j]);} x=total; if(x>max) max=x; } return max; } public static double normE(double a[][]) { //Euclidian matrix norm double x;

double total; int i,j; total=0; int n=a.length; for(j=0;j<n;i++) {for(i=0;i<n;j++) {total+=a[i][j]*a[i][j];}} return Math.sqrt(total); } public static double[] multiply(double[][] left,double[] right) { //multiplication of one matrix with one vector int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right.length; double[] b; b=new double[m2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) {b[ii]=0;} return b; } for(i=0;i<m1;i++) { b[i]=0; for(k=0;k<n1;k++) b[i]+=left[i][k]*right[k];} return b; //end of multiply of a matrix and a vector } public static double YT_X(double [] left,double [] right) { //multiplys a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*right[i]; } return tot; } public static double eigen_power(double A[][]) { double eps=1e-15; double err=1; int m=100; int n=A.length; double y[]=new double[n]; double r[]=new double[n]; double xnorm; double mu=1.0; for(int i=0;i<n;i++) y[i]=1.0; int k=0; double mueski; double x[]=new double[n]; do{ mueski=mu; x=multiply(A,y); xnorm=norminf(x); for(int i=0;i<n;i++) {y[i]=x[i]/xnorm;} mu=YT_X(y,x)/YT_X(y,y); for(int i=0;i<n;i++) {r[i]=mu*y[i]-x[i];} k++; err=Math.abs((mu-mueski)/mu); }while(err>eps);

return mu; } public static void pivot(double a[][],int o[],double s[],int k) {// GaussLU iin pivot metodu int n=a.length; int dummy1=0; double buyuk; double dummy=0; //pivotlama int p=k; buyuk=Math.abs(a[o[k]][k]); for(int ii=k+1;ii<n;ii++) { dummy=Math.abs(a[o[ii]][k]/s[o[ii]]); if(dummy > buyuk) {buyuk=dummy;p=ii;} } dummy1=o[p]; o[p]=o[k]; o[k]=dummy1; } public static int[] sirali_indeks(int n) { // sral rakamlar tayan bir vektr oluturur int o[]=new int[n]; for(int i=0;i<n;i++) {o[i]=i;} return o; } public static double[][] gaussLU(double ai[][],int o[]) { //ksmi pivotlu gauss eleme yntemi int n=ai.length; double a[][]=new double[n][n]; double s[]=new double[n]; double carpan=0; double toplam=0; double absaij; //gauss eleme int i,j,k,p,ii,jj; //System.out.println("Orijinal Matris :\n"+Matrix.toString(ai)); for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];}} for(i=0;i<n;i++) { o[i]=i;s[i]=Math.abs(a[i][0]); for(j=1;j<n;j++) { absaij=Math.abs(a[i][j]); if(absaij>s[i]) {s[i]=absaij;} } } for(k=0;k<(n-1);k++) { pivot(a,o,s,k); //pivot referansn deitir //System.out.println("pivotlu Matris : k="+k+"\n"+Matrix.toString(a)); //gauss elemeyi zme for(i=k+1;i<n;i++) { carpan=a[o[i]][k]/a[o[k]][k]; a[o[i]][k]=carpan; for(j=k+1;j<n;j++) { a[o[i]][j]-=carpan*a[o[k]][j]; } } } return a; } public static double[] yerine_koyma(double ai[][],double bi[]) { int n=ai.length; return yerine_koyma(ai,bi,sirali_indeks(n));

} public static double[][] yerine_koyma(double ai[][],double bi[][]) { int n=ai.length; return yerine_koyma(ai,bi,sirali_indeks(n)); } public static double[] yerine_koyma(double ai[][],double bi[],int o[]) { //gauss LU zmyle LU indirgenmesi yaplm Matris kullanarak lineer //denklem sistemi kkleri int n=ai.length; double toplam; double x[]=new double[n]; double a[][]=new double[n][n]; double b[]=new double[n]; int i,j; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};b[i]=bi[i];} //System.out.println("a=\n "+Matrix.toString(a)); //System.out.println("b=\n "+Matrix.toStringT(b)); for(i=1;i<n;i++) {toplam=b[o[i]];for(j=0;j<i;j++){toplam-=a[o[i]][j]*b[o[j]];};b[o[i]]=toplam;} //System.out.println("d=\n "+Matrix.toStringT(b)); x[n-1]=b[o[n-1]]/a[o[n-1]][n-1]; for(i=(n-2);i>=0;i--) {toplam=0; for(j=i+1;j<n;j++) {toplam+=a[o[i]][j]*x[j];}; x[i]=(b[o[i]]-toplam)/a[o[i]][i]; } //System.out.println("d=\n "+Matrix.toStringT(x)); return x; } public static double[][] yerine_koyma(double ai[][],double bi[][],int o[]) { int n=ai.length; int m=bi[0].length; double toplam; double x[][]=new double[n][m]; double a[][]=new double[n][n]; double b[][]=new double[n][m]; int i,j,k; for(i=0;i<n;i++) {for(j=0;j<n;j++){a[i][j]=ai[i][j];};for(k=0;k<m;k++) {b[i][k]=bi[i][k];}} //System.out.println("a=\n "+Matrix.toString(a)); //System.out.println("b=\n "+Matrix.toStringT(b)); for(k=0;k<m;k++) { for(i=1;i<n;i++) {toplam=b[o[i]][k];for(j=0;j<i;j++){toplam-=a[o[i]][j]*b[o[j]][k];};b[o[i]][k]=toplam;} //System.out.println("d=\n "+Matrix.toStringT(b)); x[n-1][k]=b[o[n-1]][k]/a[o[n-1]][n-1]; for(i=(n-2);i>=0;i--) {toplam=0; for(j=i+1;j<n;j++) {toplam+=a[o[i]][j]*x[j][k];}; x[i][k]=(b[o[i]][k]-toplam)/a[o[i]][i]; } }//kin sonu //System.out.println("d=\n "+Matrix.toStringT(x)); return x; } public static double [] AXB(double a[][],double b[]) {

//denklem sistemi zm b vektr iin int n=a.length; int q[]=new int[n]; double c[][]=gaussLU(a,q); double s[]=yerine_koyma(c,b,q); return s; } public static double [][] AXB(double a[][],double b[][]) {//denklem sistemi zm b matrisi iin int n=a.length; int q[]=new int[n]; double c[][]=gaussLU(a,q); double s[][]=yerine_koyma(c,b,q); return s; } public static double eigen_inversepower(double A[][]) { double eps=1e-15; double err=1; int m=100; int n=A.length; double y[]=new double[n]; double r[]=new double[n]; int o[]=new int[n]; double B[][]=new double[n][n]; double xnorm; double mu=1.0; for(int i=0;i<n;i++) y[i]=1.0; int k=0; double mueski; double x[]=new double[n]; do{ mueski=mu; B=gaussLU(A,o); x=yerine_koyma(B,y,o); xnorm=norminf(x); for(int i=0;i<n;i++) {y[i]=x[i]/xnorm;} x=yerine_koyma(B,y,o); mu=YT_X(y,x)/YT_X(y,y); for(int i=0;i<n;i++) {r[i]=mu*y[i]-x[i];} k++; err=Math.abs((mu-mueski)/mu); }while(err>eps); return 1.0/mu; } public static void main(String args[]) throws IOException { //Eigen Value calculations using method //double a[][]={{3.556,-1.778,0},{-1.778,3.556,-1.778},{0,-1.778,3.556}}; //double a[][]={{-3.9,-0.6,-3.9,0.4},{1,0,0,0},{0,1,0,0},{0,0,1,0}}; //double a[][]={{-1,2},{2,2}}; double a[][]={{1,-2,3},{-2,4,1},{3,1,2}}; String s="Original Matrix : \n"; s+=Matrix.toString(a); s+="Smallest magnitude = "+eigen_inversepower(a); JOptionPane.showMessageDialog(null,s, "Power method for the smallest magnitude eigenvalue : ",JOptionPane.PLAIN_MESSAGE); } }

Output 9.3-1 Power method for the smallest magnitude eigevalue

9.4 JACOB METHOD AND HOUSEHOLDER TRANSFORMATION

Jacobi method is used to calculate eigen value problems for the symmetric matices. For a symmetric matrix, transpose of the matrix is the same as the matrix itself. {A}T={A} By using this property of the symmetric matrices a transformation called similarity tranformation can be applied. This trnsformation converts the matrix into diagonal form. Eigenvalues can be calculated by using this form. Assume matrix P be a nonsingular matrix similarity transformation : {B}={P}-1{A}{ P} After applying similarity transformation eigen values rmain the same, but eigenvectors change. Therefore eigenvalues of matrix B is the same as eigenvalues of the matrix A. eklinde yazlabilir. Benzerlik dnm sonucu snr deerler deimez, fakat snr deer vektrleri deiir. Now assume that vector [y]k is the eigenvector of matrix {B} corresponding eigenvalue k . In this case : {B}[y]k = k [y]k {B}={P}-1{A}{ P} I both side of the equaion is multiplied with { P}: { P} {B}={ P} {P}-1{A}{ P}={A}{ P} and {A}{ P}[y]k = { P} {B}[y]k= k{ P} [y]k . If [y]k is eigenvector of {B} , eigenvector of {A} took a velue like [x]k . The relation between [y]k and [x]k [x]k={ P}[y]k . Eigenvector of {A} cb be obtained from multiplicaion of eigenvector {B} by {P} Symmetric matrices can be converted to the diagonal form by applying orthogonal symmetry .Ortagonal ( {P}-1 ={P}T) In order to obtained this convertion angular convertion matrix, R(), can be applied Matrix R() is a unit matrix except row p and column q. Rpp=Rqq = cos()=c Rpq= - Rqp= - sin()=-s For example for n=8 p=3 q=5

1 0 0 0 R ( ) = 0 0 0 0

0 1 0 0 0 0 0

0 0 0 Rqp 0 0 0

0 0 1 0 0 0 0

0 0 0 Rqq 0 0 0

0 R pp

0 R pq

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 = 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0

0 1 0 0 0 0 0

0 0 0 sin( ) 0 0 0

0 0 1 0 0 0 0

0 cos( ) 0

0 0 0 0 0 0 sin( ) 0 0 0 0 0 0 0 cos( ) 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0

In this equation is the rotaion. Rotation matrix R has interesting properties, one of these is multipliction of rotation matrix and transpose value will be given a unit matrix. {R()}T{R()}=I Inversion of the rotation matrix is also very simple. {R()}-1 ={R()}T For the rotation matrix following property is also valid. {R()}T ={R(- )} If similarity transformation is applied by using R matrix {B}={R()}T{A}{R()} In this transform angle can be selected such that Bqp=0 . Bqp=(Aqq - App) sin()cos()+Apq [cos2() sin2()] = 0 If sin(2)=2 sin()cos() And remembering that cos(2)=cos2() sin2() Bqp=(Aqq - App) sin(2)/2+Apq cos(2) = 0 becomes

= arctan

1 2

2 Apq App Aqq

By using thiangle and R matrix similarity transformation is carried out. Aplication of Jacobi method is as follows P=I is selected. In upper trianglar part of the matrix the value with the biggest magnitude Apq value is found. By using p and q values (indices of th element) angle is calculated. {P} ={P}{R} tranformation and ve {A} ={R}T{A} {R} tranformation is applied. And biggest magnitude sonra Diagonalin st genin den blgede mutlak deeri en byk olan Apq Apq value is found in the upper triangular part of the matrix is found again and iteration continued until absoute value Apq is close to zero (equal to ) . Eigenvlues is taken as k=Akk , and eigen vector becomes P=[x1,x2,,xn]. One of the problem of Jacobi iteration is the excessive number of iteration steps. If A matrix is pre-transformed before the process above by using Householder transformation, total number of iteration reduces significantly. Householder transformation converts the matrix into upper Hessenberg matrix.

B11 B 21 0 B= 0 ... 0

B12 B22 B32 0 ... 0

B13

... B1,n1

B23 ... B2,n1 B33 ... B3,n1 B43 ... B4,n1 ... 0 ... ... ... Bn ,n1

B1,n B2,n B3,n B4,n ... Bn ,n

As it is seen from the matrix itself, upper triangular part plus one below the diagonal is existed in this form, rest of the matrix elements are zero. Householder transformations is as follows: 1. Q=I is taken 2. = sign( Ak +1,k )

j = k +1

2 j ,k

In this equation sign( Ak +1,k ), is the sign of Ak +1,k . sign(x)= x / |x|

2. uk=[0,,0,Ak+1,k + 3. P = I

, Ak+2,k ,.., An,k ]T .

2uu T uT u

4. Q = Q P is applied. 5. A=PAPT . 6. B=A B is the upper Hessenberg matrix. An example problem: Upper Hessenberg transformation of Matrix

4 2 6 4 2 2 1 3 [A] = 6 1 22 13 3 13 46 4 And then find the eigenvalues:


Program 9.4-1 Jacobi method to calculate eigenvalues of the symmetric matrices.
import java.io.*; import javax.swing.*; class NA133 { // Jacobi eigenvalue calculation method public static double[][] I(int n) { //unit matrix double b[][]; b=new double[n][n]; for(int i=0;i<n;i++) b[i][i]=1.0; return b; } public static double[][] multiply(double[][] left,double right) { //multiplying a matrix with a constant int i,j; int n=left.length; int m=left[0].length;

double b[][]; b=new double[n][m]; for(i=0;i<n;i++) { for(j=0;j<m;j++) b[i][j]=left[i][j]*right; } return b; //end of multiplying a matrix with a constant double } public static double[][] multiply(double[][] left,double[][] right) { //multiplication of two matrices int ii,jj,i,j,k; int m1=left[0].length; int n1=left.length; int m2=right[0].length; int n2=right.length; double[][] b; b=new double[m1][n2]; if(n1 != m2) { System.out.println("inner matrix dimensions must agree"); for(ii=0;ii<n1;ii++) { for(jj=0;jj<m2;jj++) b[ii][jj]=0; } return b; } for(i=0;i<m1;i++) { for(j=0;j<n2;j++) { for(k=0;k<n1;k++) b[i][j]+=left[i][k]*right[k][j]; } } return b; //end of multiply of two matrices } public static double[][] substract(double[][] left,double[][] right) { //addition of two matrices int n1=left.length; int m1=left[0].length; int n2=right.length; int m2=right[0].length; int nMax,mMax; int i,j; if(m1>=m2) mMax=m1; else mMax=m2; if(n1>=n2) nMax=n1; else nMax=n2; double b[][]; b=new double[nMax][mMax]; for(i=0;i<n1;i++) { for(j=0;j<m1;j++) { b[i][j]=b[i][j]+left[i][j]; } } for(i=0;i<n2;i++)

{ for(j=0;j<m2;j++) { b[i][j]=b[i][j]-right[i][j]; } } return b; //end of matrix substraction method } public static double[][] T(double [][] left) { //transpose matrix (if A=a(i,j) T(A)=a(j,i) int i,j; int n=left.length; int m=left[0].length; double b[][]; b=new double[m][n]; for(i=0;i<n;i++) { for(j=0;j<m;j++) { b[j][i]=left[i][j]; } } return b; } public static double VT_V(double [] left) { //multiply a vector transpose with a vector int n=left.length; double tot=0; for(int i=0;i<n;i++) { tot+=left[i]*left[i]; } return tot; } public static double[][] V_VT(double [] left) { //multiplys a vector transpose with a vector int n=left.length; double aa[][]=new double[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { aa[i][j]=left[i]*left[j]; } } return aa; } public static double[] eigen_jacobi(double A[][]) { A=Householder(A); double eps=1e-15; double err=1; int m=100; int n=A.length; double P[][]=new double[n][n]; double R[][]=new double[n][n]; int p=n,q=n; double max=-9.99e100;

double fi; P=I(n); for(int i=0;i<n;i++) {for(int j=i+1;j<n;j++) {if(Math.abs(A[i][j]) > max) {max=Math.abs(A[i][j]);p=i;q=j;}} } while(Math.abs(A[p][q]) > eps ) { fi=0.5*Math.atan(2.0*A[p][q]/(A[p][p]-A[q][q])); R=I(n); R[p][p]=Math.cos(fi); R[q][q]=Math.cos(fi); R[p][q]=-Math.sin(fi); R[q][p]=Math.sin(fi); P=multiply(P,R); A=multiply(T(R),multiply(A,R)); max=-9.99e100; for(int i=0;i<n;i++) {for(int j=i+1;j<n;j++){if(Math.abs(A[i][j])>max) {max=Math.abs(A[i][j]);p=i;q=j;}}} } double lambda[]=new double[n]; for(int i=0;i<n;i++) {lambda[i]=A[i][i];} return lambda; } public static double sgn(double x) {return x/Math.abs(x);} public static double[][] Householder(double A[][]) { //Householder transformation int n=A.length; double Q[][]=I(n); double alpha; double toplam; double u[]=new double[n]; double C[][]=new double[n][n]; double P[][]=new double[n][n]; double B[][]=new double[n][n]; double cc; for(int k=0;k<(n-2);k++) { toplam=0; for(int j=k+1;j<n;j++) {toplam+=A[j][k]*A[j][k];} toplam=Math.sqrt(toplam); alpha=sgn(A[k+1][k])*toplam; for(int j=0;j<n;j++) { if(j<=k) u[j]=0.0; else if(j==k+1) u[j]=A[j][k]+alpha; else u[j]=A[j][k]; } C=V_VT(u); cc=2.0/VT_V(u); C=multiply(C,cc); P=substract(I(n),C); Q=multiply(Q,P); A=multiply(P,multiply(A,P)); } return A; }

public static void main(String args[]) throws IOException { //Snr deer hesaplar double c[]; c=new double[2];

//double a[][]={{12,6,-6},{6,16,2},{-6,2,16}}; double a[][]={{4,-2,6,4},{-2,2,-1,3},{6,-1,22,13},{4,3,13,46}}; String s="Original matrix : \n"; s+=Matrix.toString(a); //c=eigen_jacobi(a); s+="Matrix with Householder transformation \n"; s+=Matrix.toStringT(Householder(a)); c=eigen_jacobi(a); s+="eigen vector \n"; s+=Matrix.toStringT(c); JOptionPane.showMessageDialog(null,s, "Jacobi method with householder transformation : ",JOptionPane.PLAIN_MESSAGE); } }

Output 9.4-1 Jacobi method with Householder transformation to solve eigenvalues of the symmetric matrices.

9.5 QL ALGORITHM

If a symmetric matrix A is considered, This matrix can e devided into two submatrix as Q orthogonal matrix and L lower triangular matrix. A=Q L . Since Q is an orthogonal matrix QT = Q-1 . If both side of the eqaution is multiplied by QT , it becomes QT A = L . A new B matrix can be created as: B=L Q = QT A Q . Matrix Bis a similarity transformed form of matrix A, therefore eigenvalues of both matrices are the same. QL algorithm is based on a series of othogonal transformations. The recurrence relations used is Am=Qm Lm and Am+1= Lm Qm = QmT Am Qm Because A is a symmetric matrix, eigenvalue will be the real numbers

Program 9.5-1 QL algorithm to calculate eigenvalues of the symmetric matrices.


import java.io.*; import java.io.*; import javax.swing.*; class NA134 { public static double[][] tridiagonal(double b[][], double d[], double e[]) { //reduces matrix to tridiaonal form by using HOUSEHOLDER TRANSFORMATION //this method is used by QL method to calculate eigen values //and eigen vectors of a symmetric matrix int l,k,j,i; int n=b.length; double scale,hh,h,g,f; double a[][]=new double[n+1][n+1]; double c[][]=new double[n][n]; for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j]=b[i][j]; for (i=n;i>=2;i--) { l=i-1; h=scale=0.0; if (l > 1) { for (k=1;k<=l;k++) scale += Math.abs(a[i-1][k-1]); if (scale == 0.0) e[i-1]=a[i-1][l-1]; else { for (k=1;k<=l;k++) { a[i-1][k-1] /= scale; h += a[i-1][k-1]*a[i-1][k-1]; } f=a[i-1][l-1]; g=(f >= 0.0 ? -Math.sqrt(h) : Math.sqrt(h)); e[i-1]=scale*g; h -= f*g; a[i-1][l-1]=f-g; f=0.0; for (j=1;j<=l;j++) { a[j-1][i-1]=a[i-1][j-1]/h; g=0.0; for (k=1;k<=j;k++) g += a[j-1][k-1]*a[i-1][k-1]; for (k=j+1;k<=l;k++) g += a[k-1][j-1]*a[i-1][k-1]; e[j-1]=g/h; f += e[j-1]*a[i-1][j-1]; } hh=f/(h+h); for (j=1;j<=l;j++) { f=a[i-1][j-1]; e[j-1]=g=e[j-1]-hh*f; for (k=1;k<=j;k++) a[j-1][k-1] -= (f*e[k-1]+g*a[i-1][k-1]); } } } else e[i-1]=a[i-1][l-1]; d[i-1]=h; } d[1-1]=0.0; e[1-1]=0.0; /* Contents of this loop can be omitted if eigenvectors not wanted except for statement d[i-1]=a[i-1][i-1]; */

for (i=1;i<=n;i++) { l=i-1; if (d[i-1] != 0) { for (j=1;j<=l;j++) { g=0.0; for (k=1;k<=l;k++) g += a[i-1][k-1]*a[k-1][j-1]; for (k=1;k<=l;k++) a[k-1][j-1] -= g*a[k-1][i-1]; } } d[i-1]=a[i-1][i-1]; a[i-1][i-1]=1.0; for (j=1;j<=l;j++) a[j-1][i-1]=a[i-1][j-1]=0.0; } return a; } public static double pythag(double a, double b) { //this method is used by QL method double absa,absb; absa=Math.abs(a); absb=Math.abs(b); if (absa > absb) return absa*Math.sqrt(1.0+(absb/absa)*(absb/absa)); else return (absb==0.0 ? 0.0 : absb*Math.sqrt(1.0+(absa/absb)*(absa/absb))); } public static double[][] QL(double d[], double e[], double a[][]) { // QL algorithm : eigenvalues of a symmetric matrix reduced to tridiagonal // form by using method tridiagonal int n=d.length; int m,l,iter,i,j,k; double s,r,p,g,f,dd,c,b; for (i=2;i<=n;i++) e[i-2]=e[i-1]; e[n-1]=0.0; double z[][]=new double[n][n]; for(i=0;i<n;i++) for(j=0;j<n;j++) z[i][j]=a[i][j]; for (l=1;l<=n;l++) { iter=0; do { for (m=l;m<=n-1;m++) { dd=Math.abs(d[m-1])+Math.abs(d[m]); if ((double)(Math.abs(e[m-1])+dd) == dd) break; } if (m != l) { if (iter++ == 30) System.out.println("Too many iterations in QL"); g=(d[l]-d[l-1])/(2.0*e[l-1]); r=Matrix.pythag(g,1.0); g=d[m-1]-d[l-1]+e[l-1]/(g+Matrix.SIGN(r,g)); s=c=1.0; p=0.0; for (i=m-1;i>=l;i--) { f=s*e[i-1]; b=c*e[i-1]; e[i]=(r=Matrix.pythag(f,g)); if (r == 0.0) { d[i] -= p; e[m-1]=0.0; break; } s=f/r; c=g/r;

g=d[i ]-p; r=(d[i-1]-g)*s+2.0*c*b; d[i ]=g+(p=s*r); g=c*r-b; for (k=1;k<=n;k++) { f=z[k-1][i ]; z[k-1][i ]=s*z[k-1][i-1]+c*f; z[k-1][i-1]=c*z[k-1][i-1]-s*f; } } if (r == 0.0 && i >= l) continue; d[l-1] -= p; e[l-1]=g; e[m-1]=0.0; } } while (m != l); } return z; } public static double[][] eigenQL(double a[][]) { // QL algoritm to solve eigen value problems // symmetric matrices only (real eigen values) // first column of the matrix returns eigen values // second..n+1 column returns eigen vectors. // Note : If matrix is not symmetric DO NOT use // this method use eigenValue method (a QR algorithm) int i,j; int n=a.length; double sum[]=new double[n];; double d[]=new double[n]; double b[][]=new double[n][n]; double e[]=new double[n]; double z[][]=new double[n+1][n]; b=tridiagonal(a,d,e); b=QL(d,e,b); for(j=0;j<n;j++) { z[0][j]=d[j]; for(i=0;i<n;i++) { z[i+1][j]=b[i][j]/b[0][j]; if(z[i+1][j]<1e-13) z[i+1][i]=0; } } // result: first row is eigenvalues, the rest is eigenvector return z; } public static void main(String args[]) throws IOException { //Eigen Value calculations double c[][]; c=new double[3][3]; //double a[][]={{-19,-4.7,6.45},{-4.7,4.6,11.8}, {6.45,11.8,-8.3}}; double a[][]={{3,-1,0},{-1,2,-1},{0,-1,3}}; String s="Original matrix : \n"; s+=Matrix.toString(a); c=new double[3][3]; c=eigenQL(a); s+="eigen matrix (first colmn is the eigenvalues) \n"; s+=Matrix.toString(c); JOptionPane.showMessageDialog(null,s, "QL eigenvalue method : ",JOptionPane.PLAIN_MESSAGE); }

9.6 QR ALGORTHM By using a similarity transformation matrix A can be subdivided into a orthogonal Q and an upper triangular matrix R A=Q R Because Q is an orthogonal matrix QT = Q-1 can be written. If equation is multiplied with QT QT A = R, a new matrix B can be created as B=R Q = QT A Q B and A is realated to each other by similarity transformation, i.e. they have the same eigenvalues. Similarity transforms applied to B sevral times. When B is converted into an upper triangualr form, eigenvalues can be obtained from this upper triangular form. Converting matrix to upper triangular form could be a very long process. In order to shorten the process a householder tranformation can be applied. Imatrix does not have to be symmetric in QR algorithm, therfore it can be applied into non-symmetric matrices. Please note that eige values of the non-symmetric matrices can be complex number, theefore the proces steps should be trhough complex number algorithms. Program 9.6-1 QR algorithm
import java.io.*; import javax.swing.*;

class NA135 { //===========Eigen value calculations ============== public static double[][] balance(double b[][]) { // balance of a matrix for more accurate eigenvalue // calculations double radix=2.0; double sqrdx=radix*radix; double c,r,f,s,g; int m,j,i,last; int n=b.length; last=0; double a[][]; a=new double[n][n]; f=1;

s=1; for(i=1;i<=n;i++) for(j=1;j<=n;j++) a[i-1][j-1]=b[i-1][j-1]; while(last==0) { last=1; for(i=1;i<=n;i++) { c=0;r=0; for(j=1;j<=n;j++) { if(j != i) { c+=Math.abs(a[j-1][i-1]); r+=Math.abs(a[i-1][j-1]); } //end of if(j!=.. } //end of for(j=1... if(c != 0 && r != 0 ) { g=r/radix; f=1.0; s=c+r; while(c<g) { f*=radix; c*=sqrdx; } g=r*radix; while(c>g) { f/=radix; c/=sqrdx; } } //end of if(c != 0 && .... if( (c+r)/f < 0.95*s ) { last=0; g=1.0/f; for(j=1;j<=n;j++) { a[i-1][j-1]*=g; } for(j=1;j<=n;j++) { a[j-1][i-1]*=f; } } //end of if( ((c+r.. }//end of for(i=1;i<=n.... } //end of while last==0 return a; } public static double[][] Hessenberg(double b[][]) { // Calculates the hessenberg matrix // it is used in QR method to calculate eigenvalues // of a matrix(symmetric or non-symmetric) int m,j,i; int n=b.length; double a[][]; a=new double[n][n]; for(i=0;i<n;i++) for(j=0;j<n;j++)

a[i][j]=b[i][j]; double x,y; if(n>2) { for(m=2;m<=(n-1);m++) { x=0.0; i=m; for(j=m;j<=n;j++) { if(Math.abs(a[j-1][m-2]) > Math.abs(x)) { x=a[j-1][m-2]; i=j; } //end of if(Math.abs(.. }//end of for(j=m,j<=n... if(i!=m) { for(j=(m-1);j<=n;j++) { y=a[i-1][j-1]; a[i-1][j-1]=a[m-1][j-1]; a[m-1][j-1]=y; }//end of for(j=(m-1).. for(j=1;j<=n;j++) { y=a[j-1][i-1]; a[j-1][i-1]=a[j-1][m-1]; a[j-1][m-1]=y; }//end of for(j=1;j<=n.... } //end of if(i!=m) if(x != 0.0) { for(i=(m+1);i<=n;i++) { y=a[i-1][m-2]; if(y!=0.0) { y=y/x; a[i-1][m-2]=y; for(j=m;j<=n;j++) { a[i-1][j-1]-=y*a[m-1][j-1]; } for(j=1;j<=n;j++) { a[j-1][m-1]+=y*a[j-1][i-1]; } }//end of if(y!=0.. }//end of for(i=(m+1)... } //end of if(x != 0.0... }//end of for(m=2;m<=(n-1).. }//end of Hessenberg for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(i>(j+1)) a[i-1][j-1]=0; }

return a; } public static double[][] QR(double b[][]) { //calculates eigenvalues of a Hessenberg matrix int n=b.length; double rm[][]=new double[2][n]; double a[][]=new double[n+1][n+1]; double wr[]=new double[n+1]; double wi[]=new double[n+1]; int nn,m,l,k,j,its,i,mmin; double z,y,x,w,v,u,t,s,r=0,q=0,p=0,anorm; for(i=0;i<n;i++) for(j=0;j<n;j++) a[i+1][j+1]=b[i][j]; anorm=Math.abs(a[1][1]); for (i=2;i<=n;i++) for (j=(i-1);j<=n;j++) anorm += Math.abs(a[i][j]); nn=n; t=0.0; while (nn >= 1) { its=0; do { for (l=nn;l>=2;l--) { s=Math.abs(a[l-1][l-1])+Math.abs(a[l][l]); if (s == 0.0) s=anorm; if ((double)(Math.abs(a[l][l-1]) + s) == s) break; } x=a[nn][nn]; if (l == nn) { wr[nn]=x+t; wi[nn--]=0.0; } else { y=a[nn-1][nn-1]; w=a[nn][nn-1]*a[nn-1][nn]; if (l == (nn-1)) { p=0.5*(y-x); q=p*p+w; z=Math.sqrt(Math.abs(q)); x += t; if (q >= 0.0) { z=p+Matrix.SIGN(z,p); wr[nn-1]=wr[nn]=x+z; if (z!=0) wr[nn]=x-w/z; wi[nn-1]=wi[nn]=0.0; } else { wr[nn-1]=wr[nn]=x+p; wi[nn-1]= -(wi[nn]=z); } nn -= 2; } else { if (its == 30) System.out.println("Matrix.java, module QR, Too many iterations in hqr"); if (its == 10 || its == 20) {

t += x; for (i=1;i<=nn;i++) a[i][i] -= x; s=Math.abs(a[nn][nn-1])+Math.abs(a[nn-1][nn-2]); y=x=0.75*s; w = -0.4375*s*s; } ++its; for (m=(nn-2);m>=l;m--) { z=a[m][m]; r=x-z; s=y-z; p=(r*s-w)/a[m+1][m]+a[m][m+1]; q=a[m+1][m+1]-z-r-s; r=a[m+2][m+1]; s=Math.abs(p)+Math.abs(q)+Math.abs(r); p /= s; q /= s; r /= s; if (m == l) break; u=Math.abs(a[m][m-1])*(Math.abs(q)+Math.abs(r)); v=Math.abs(p)*(Math.abs(a[m-1][m-1])+ Math.abs(z)+Math.abs(a[m+1][m+1])); if ((double)(u+v) == v) break; } for (i=m+2;i<=nn;i++) { a[i][i-2]=0.0; if (i != (m+2)) a[i][i-3]=0.0; } for (k=m;k<=nn-1;k++) { if (k != m) { p=a[k][k-1]; q=a[k+1][k-1]; r=0.0; if (k != (nn-1)) r=a[k+2][k-1]; if ((x=Math.abs(p)+Math.abs(q)+Math.abs(r)) != 0.0) { p /= x; q /= x; r /= x; } if ((s=Matrix.SIGN(Math.sqrt(p*p+q*q+r*r),p)) != 0.0) { if (k == m) { if (l != m) a[k][k-1] = -a[k][k-1]; } else a[k][k-1] = -s*x; p += s; x=p/s; y=q/s; z=r/s; q /= p; r /= p; for (j=k;j<=nn;j++) { p=a[k][j]+q*a[k+1][j]; if (k != (nn-1)) { p += r*a[k+2][j]; a[k+2][j] -= p*z; }

a[k+1][j] -= p*y; a[k][j] -= p*x; } mmin = nn<k+3 ? nn : k+3; for (i=l;i<=mmin;i++) { p=x*a[i][k]+y*a[i][k+1]; if (k != (nn-1)) { p += z*a[i][k+2]; a[i][k+2] -= p*r; } a[i][k+1] -= p*q; a[i][k] -= p; } } } } } } while (l < nn-1); } for(i=0;i<n;i++) { rm[0][i]=wr[i+1]; rm[1][i]=wi[i+1]; } return rm; } //end of QR public static double[][] eigenValue(double b[][]) { // this routine input a matrix (non symetric or symmetric) // and calculate eigen values // method balance can be used prior to this method to balance // the input matrix int n=b.length; double d[][]=new double[2][n]; d=Matrix.QR(Matrix.Hessenberg(b)); return d; } public static complex[] eigenValueC(double b[][]) { // this routine input a matrix (non symetric or symmetric) // and calculate eigen values // method balance can be used prior to this method to balance // the input matrix //output eigenvalues will be in a vector of complex form int n=b.length; double d[][]=new double[2][n]; d=Matrix.QR(Matrix.Hessenberg(b)); complex c[]=new complex[n]; for(int i=0;i<n;i++) { c[i]=new complex(d[0][i],d[1][i]); } return c; } public static void main(String args[]) throws IOException {

//Eigen Value calculations using method double a[][]={{3,-1,0},{-1,2,-1},{0,-1,3}}; //double a[][]={{1,-2,3},{-2,4,1},{3,1,2}}; String s="Original matrix : "; s+=Matrix.toString(a); s+="QL solution matrix \n"; s+=Matrix.toString(SCO13A.eigenQL(a)); //Eigenvlue calculation complex c1[]; int n=a.length; c1=new complex[n]; c1=eigenValueC(a); s+="QR method eigenvalues \n"; s+=Matrix.toStringT(c1); JOptionPane.showMessageDialog(null,s, "QR - QL eigenvalue method : ",JOptionPane.PLAIN_MESSAGE); } }

Output 8.2-1 QR algorithm

Program 9.6.2- complex class


import java.io.*; import java.text.*; import java.util.Locale; import java.util.*; import javax.swing.*; // giri k // class complex // complex number abstraction // class complex { // constructors public complex() { nreal=0; nimag=0; } public complex(double nre,double nim) { nreal=nre; nimag=nim;

} public complex(int nre,int nim) { nreal=(double)nre; nimag=(double)nim; } public complex(double numer) { nreal=numer; nimag=0; } public complex(complex value ) { nreal=value.real(); nimag=value.imaginary(); } // accessor functions public double real() { //return real part of the complex number return nreal; } public double imaginary() { //return imaginary part of the complex number return nimag; } public double R() { // Return radius of polar coordinate equivalent of complex number return Math.sqrt(nreal*nreal+nimag*nimag); } public double theta() { // Return angle of polar coordinate equivalent of complex number in radian return Math.atan2(nimag,nreal); } public double dtheta() { // Return angle of polar coordinate equivalent of complex number in degree return Math.atan2(nimag,nreal)*180.0/Math.PI; } // assignments public void assign(complex right) { nreal=right.real(); nimag=right.imaginary(); }

public void add(complex right) { nimag = nimag + right.imaginary(); nreal = nreal + right.real(); } public void add(double nr1) { nreal = nreal+nr1; } public void add(double nr1,double ni1) { nreal = nreal+nr1; nimag = nimag + ni1; } public void substract(complex right) { nimag = nimag - right.imaginary(); nreal = nreal - right.real(); } public void multiply(complex right ) { nreal = nreal*right.real() - nimag*right.imaginary(); nimag = nreal*right.imaginary() + nimag*right.real(); } public void divide(complex right ) { double a=nreal*nreal+nimag*nimag; nreal = ( nreal*right.real() + nimag*right.imaginary())/a; nimag = (-nreal*right.imaginary() + nimag*right.real())/a; } public static complex add(complex left, complex right) { // return sum of two complex numbers double r1=(left.real() + right.real()); double i1=(left.imaginary() + right.imaginary()); complex result; result=new complex(r1,i1); return result; } public static complex add(complex left, double right) { // return sum of two complex numbers double r1=(left.real() + right); double i1=left.imaginary(); complex result; result=new complex(r1,i1); return result; }

public static complex conjugate(complex z)

{complex z1=new complex(z.real(),-z.imaginary()); return z1; } public static complex substract(complex left, complex right) { // return substraction of two complex numbers complex result; result=new complex((left.real() - right.real()), (left.imaginary() - right.imaginary())); return result; } public static complex multiply(complex left, complex right) { // return multiplication of two complex numbers complex result; result=new complex ((left.real()*right.real() - left.imaginary()*right.imaginary()), (left.real()*right.imaginary() + left.imaginary()*right.real())); return result; } public static complex multiply(complex left, double right) {complex result; result=new complex((left.real()*right),(left.imaginary()*right)); return result; } public static complex divide(complex left, complex right) { // return division of two complex numbers double a=right.real()*right.real()+right.imaginary()*right.imaginary(); complex result; result=new complex ((left.real()*right.real() + left.imaginary()*right.imaginary())/a, (-left.real()*right.imaginary() + left.imaginary()*right.real())/a); return result; } public static complex divide(complex left, double right) { // return division of a complex number to a real number complex result; result=new complex((left.real()/right),(left.imaginary()/right)); return result; } public static complex pow(complex left, double right) { // return sum of two complex numbers double Rad,th; Rad=Math.pow(left.R(),right); th=right*left.theta(); complex result; result =new complex((Rad*Math.cos(th) ), (Rad*Math.sin(th) ) ); return result; } public static complex exp(complex left) { // exp(x+i*y) complex result; result =new complex((Math.exp(left.real())*Math.cos(left.imaginary()) ), (Math.exp(left.real())*Math.sin(left.imaginary()) ));

return result; } public static complex exp(double left) { // exp(i*left) imaginary exponent complex result; result =new complex(Math.cos(left), Math.sin(left) ); return result; } public static complex sqrt(complex left) { return pow(left,0.5); } public static double abs(complex left) { return left.R(); } public boolean smaller(complex left,complex right) { // less then comparison of two complex numbers return (left.R() < right.R()); } public boolean smaller_equal(complex left,complex right) { // less then and equal comparison of two complex numbers return (left.R() <= right.R()); } public boolean greater(complex left,complex right) { // greater then comparison of two complex numbers return left.R() > right.R(); } public boolean greater_equal(complex left,complex right) { // greater then and equal comparison of two complex numbers return left.R() >= right.R(); }

public boolean equal(complex left,complex right) { // equal comparison of two complex numbers return left.R() == right.R(); } public boolean not_equal(complex left,complex right) { // not equal comparison of two complex numbers return left.R() != right.R(); }

public static String toString(double left, int w, int d) // converts a double to a string with given width and decimals. { NumberFormat df=NumberFormat.getInstance(Locale.US); df.setMaximumFractionDigits(d); df.setMinimumFractionDigits(d); df.setGroupingUsed(false); String s = df.format(left); while (s.length() < w) s = " " + s; if (s.length() > w) { s = ""; for (int i=0; i<w; i++) s = s + "-"; } return s; } public static String toString(double left) {// converts a double to a string with a constant width and constant decimals. return toString(left,15,10);} public static String toString(complex value) { String b=""; if(Math.abs(value.imaginary())!=1) { if(value.imaginary() >= 0) b=b+"("+toString(value.real())+" + "+toString(value.imaginary())+"i )"; else b=b+"("+toString(value.real())+" - "+toString(-value.imaginary())+"i )"; } else { if(value.imaginary() >= 0) b=b+"("+toString(value.real())+" + i )"; else b=b+"("+toString(value.real())+" - i )"; } return b; } public String toString() { String b=""; if(Math.abs(imaginary())!=1) { if(imaginary() >= 0) b=b+"("+toString(real())+" + "+toString(imaginary())+"i )"; else b=b+"("+toString(real())+" - "+toString(-imaginary())+"i )"; } else { if(imaginary() >= 0) b=b+"("+toString(real())+" + i )"; else

b=b+"("+toString(real())+" - i )"; } return b; } public static complex toComplex(String s) { //bu metod compleks sayy ekrandan okur. //StringTokanizer ktphane snf bir stringi cmlelere ayrr String s1=JOptionPane.showInputDialog(s); StringTokenizer token=new StringTokenizer(s1); int n=token.countTokens()-1; int m=n+1; double a[]=new double[m]; int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } complex b=new complex(a[0],a[1]); return b; } // data areas public double nreal; public double nimag; };

9.7 ROOTS OF THE POLYNOMIALS AND COMPANION MATRIX

Consider a nth degree polynomial f(x)= anxn + +a4x4 + a3x3 + a2x2 + a1x +a0 f the coefficients of he polynomial are divided by an , it becomes f(x)= xn + bn-1x4 + +b4x4 + b3x3 + b2x2 + b1x +b0 The roots of this polynomial is the same as the eigenvalues of the Matrix
bn 1 1 A= 0 ... bn 2 0 1 ... ... b1 ... ... ... ... 0 0 ... 1 b0 0 0 ... 0

This matrix is called a companion matrix. Since the matrixis not symmetric, eigen values or root of the polynomials can be complex values.
Program 9.7-1 QR method to solve companion matrix (root of the nth degree polynomials)
import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*;

class NA136 extends JFrame implements ActionListener { public static double[][] balancedEigenValue(double b[][]) {

// this routine input a matrix (non symetric or symmetric) // and calculates eigen values // method balance is used to balance the matrix previous to // actual calculations int n=b.length; double d[][]=new double[2][n]; d=Matrix.QR(Matrix.Hessenberg(Matrix.balance(b))); return d; } public static complex[] poly_rootsC(double c[]) { // roots of a degree n polynomial // P(x)=c[n]*x^n+c[n-1]*x^(n-1)+....+c[1]*x+c[0]=0; // roots are returned as complex variables int n=c.length-1; double a[][]=new double[n][n]; a=characteristic_matrix(c); double d[][]=new double[2][n]; d=balancedEigenValue(a); complex e[]=new complex[n]; for(int i=0;i<n;i++) e[i]=new complex(d[0][i],d[1][i]); return e; } public static double SIGN(double a,double b) { //returns the value of double a with sign of double b; //if a=-2, b= 3 SIGN(a,b) returns 2 //if a=-2, b=-3 SIGN(a,b) returns -2 //if a= 2, b=-3 SIGN(a,b) returns -2 //if a= 2, b= 3 SIGN(a,b) returns 2 if(b!=0) return Math.abs(a)*b/Math.abs(b); else return Math.abs(a); } public static double[][] characteristic_matrix(double c[]) { //this routine converts polynomial coefficients to a matrix //with the same eigenvalues (roots) int n=c.length-1; int i; double a[][]=new double[n][n]; for(i=0;i<n;i++) { a[0][i]=-c[i+1]/c[0]; } for(i=0;i<n-1;i++) { a[i+1][i]=1; } return a; } public static double[][] tridiagonal(double b[][], double d[], double e[]) { //reduces matrix to tridiaonal form by using HOUSEHOLDER TRANSFORMATION //this method is used by QL method to calculate eigen values //and eigen vectors of a symmetric matrix int l,k,j,i; int n=b.length; double scale,hh,h,g,f; double a[][]=new double[n+1][n+1]; double c[][]=new double[n][n]; for(i=0;i<n;i++)

for(j=0;j<n;j++) a[i][j]=b[i][j]; for (i=n;i>=2;i--) { l=i-1; h=scale=0.0; if (l > 1) { for (k=1;k<=l;k++) scale += Math.abs(a[i-1][k-1]); if (scale == 0.0) e[i-1]=a[i-1][l-1]; else { for (k=1;k<=l;k++) { a[i-1][k-1] /= scale; h += a[i-1][k-1]*a[i-1][k-1]; } f=a[i-1][l-1]; g=(f >= 0.0 ? -Math.sqrt(h) : Math.sqrt(h)); e[i-1]=scale*g; h -= f*g; a[i-1][l-1]=f-g; f=0.0; for (j=1;j<=l;j++) { a[j-1][i-1]=a[i-1][j-1]/h; g=0.0; for (k=1;k<=j;k++) g += a[j-1][k-1]*a[i-1][k-1]; for (k=j+1;k<=l;k++) g += a[k-1][j-1]*a[i-1][k-1]; e[j-1]=g/h; f += e[j-1]*a[i-1][j-1]; } hh=f/(h+h); for (j=1;j<=l;j++) { f=a[i-1][j-1]; e[j-1]=g=e[j-1]-hh*f; for (k=1;k<=j;k++) a[j-1][k-1] -= (f*e[k-1]+g*a[i-1][k-1]); } } } else e[i-1]=a[i-1][l-1]; d[i-1]=h; } d[1-1]=0.0; e[1-1]=0.0; /* Contents of this loop can be omitted if eigenvectors not wanted except for statement d[i-1]=a[i-1][i-1]; */ for (i=1;i<=n;i++) { l=i-1; if (d[i-1] != 0) { for (j=1;j<=l;j++) { g=0.0; for (k=1;k<=l;k++) g += a[i-1][k-1]*a[k-1][j-1]; for (k=1;k<=l;k++) a[k-1][j-1] -= g*a[k-1][i-1]; } } d[i-1]=a[i-1][i-1]; a[i-1][i-1]=1.0; for (j=1;j<=l;j++) a[j-1][i-1]=a[i-1][j-1]=0.0; } return a; } public static double pythag(double a, double b)

{ //this method is used by QL method double absa,absb; absa=Math.abs(a); absb=Math.abs(b); if (absa > absb) return absa*Math.sqrt(1.0+(absb/absa)*(absb/absa)); else return (absb==0.0 ? 0.0 : absb*Math.sqrt(1.0+(absa/absb)*(absa/absb))); } public static double[][] QL(double d[], double e[], double a[][]) { // QL algorithm : eigenvalues of a symmetric matrix reduced to tridiagonal // form by using method tridiagonal int n=d.length; int m,l,iter,i,j,k; double s,r,p,g,f,dd,c,b; for (i=2;i<=n;i++) e[i-2]=e[i-1]; e[n-1]=0.0; double z[][]=new double[n][n]; for(i=0;i<n;i++) for(j=0;j<n;j++) z[i][j]=a[i][j]; for (l=1;l<=n;l++) { iter=0; do { for (m=l;m<=n-1;m++) { dd=Math.abs(d[m-1])+Math.abs(d[m]); if ((double)(Math.abs(e[m-1])+dd) == dd) break; } if (m != l) { if (iter++ == 30) System.out.println("Too many iterations in QL"); g=(d[l]-d[l-1])/(2.0*e[l-1]); r=pythag(g,1.0); g=d[m-1]-d[l-1]+e[l-1]/(g+SIGN(r,g)); s=c=1.0; p=0.0; for (i=m-1;i>=l;i--) { f=s*e[i-1]; b=c*e[i-1]; e[i]=(r=pythag(f,g)); if (r == 0.0) { d[i] -= p; e[m-1]=0.0; break; } s=f/r; c=g/r; g=d[i ]-p; r=(d[i-1]-g)*s+2.0*c*b; d[i ]=g+(p=s*r); g=c*r-b; for (k=1;k<=n;k++) { f=z[k-1][i ]; z[k-1][i ]=s*z[k-1][i-1]+c*f; z[k-1][i-1]=c*z[k-1][i-1]-s*f; } } if (r == 0.0 && i >= l) continue; d[l-1] -= p; e[l-1]=g; e[m-1]=0.0; } } while (m != l); } return z; }

public static double[][] eigenQL(double a[][]) { // QL algoritm to solve eigen value problems // symmetric matrices only (real eigen values) // first column of the matrix returns eigen values // second..n+1 column returns eigen vectors. // Note : If matrix is not symmetric DO NOT use // this method use eigenValue method (a QR algorithm) int i,j; int n=a.length; double sum[]=new double[n];; double d[]=new double[n]; double b[][]=new double[n][n]; double e[]=new double[n]; double z[][]=new double[n+1][n]; b=tridiagonal(a,d,e); b=QL(d,e,b); for(j=0;j<n;j++) { z[0][j]=d[j]; for(i=0;i<n;i++) { z[i+1][j]=b[i][j]/b[0][j]; if(z[i+1][j]<1e-13) z[i+1][i]=0; } } // result: first row is eigenvalues, the rest is eigenvector return z; } private JLabel prompt1,prompt2; private JTextField input; JTextArea t; JPanel YaziPaneli; int n; String s; Container c; public NA136() { super("Roots of the nth degree polynomials"); c=getContentPane(); c.setLayout(new FlowLayout()); YaziPaneli=new JPanel(); YaziPaneli.setFont(new Font("Serif",Font.BOLD,12)); YaziPaneli.setLayout( new GridLayout(3,1) ); t=new JTextArea(); t.setBackground(c.getBackground()); prompt1= new JLabel("a[0]+a[1]*x+...+a[n]*x^n=0"); prompt2= new JLabel("enter coefficients of nth degree polynomial : "); input = new JTextField(30); input.setBackground(c.getBackground()); YaziPaneli.add(prompt1); YaziPaneli.add(prompt2); YaziPaneli.add(input); c.add(YaziPaneli); c.add(t); input.addActionListener(this); } public void actionPerformed(ActionEvent e) { s=input.getText(); StringTokenizer token=new StringTokenizer(s); t.setText(""); n=token.countTokens()-1;

int m=n+1; double a[]=new double[m]; complex z[]=new complex[n]; for(int i=0;i<n;i++) { z[i]=new complex(); } int j=0; while(token.hasMoreTokens()) { Double ax=new Double(token.nextToken()); a[j++]=ax.doubleValue(); } z=poly_rootsC(a); int i=0; t.setText(Matrix.toStringT(z)); input.setText(""); } public static void main(String[] args) { NA136 pencere= new NA136(); pencere.addWindowListener(new BasicWindowMonitor()); pencere.setSize(350,300); pencere.setVisible(true); } }

Details of the class complex was given above.


Output 9.7-1 Roots of the nth degree polynomial by using QR algorithm kts

9.8 PROBLEMS PROBLEM 1 A steel shaft is placed inside of a iron base as shown in the figure . T torsion and M beding moment is applied to the shaft. Under this forces stress matrix at point Q is as follows (Mpa) -19

-4.7 4.6 11.8

6.45 11.8 -8.3

-4.7 6.45

Calculate the principle stresses at point Q Note : Principle stresses re the eigenvalues of the matrix

PROBLEM 2 1 2 A= Calculate the eigenvalues of the matrix 3 5 PROBLEM 3 0 1 1 1 1 0 Calculate the eigenvalues of the matrix A= 0 1 1 PROBLEM 4 3 2 0 A = 2 1 4 Calculate the eigenvalues of the matrix 1 1 2 PROBLEM 5
9 5 4 1 3 1 2 7 12 7 A= 3 7 0 11 8 Calculate the eigenvalues of the matrix 2 4 14 1 5 11 8 6 2 13

PROBLEM 6 8 1 5 A = 4 4 2 calculate eigenvalues and eigenvectors of he mtrix 18 5 7 PROBLEM 7


x xy xz 10 4 6 A = yx y yz = 4 6 8 MPa Normal and shar stresses are given. Find the principle stresses zx zy z 6 8 14

10.0 HOMEWORK PROBLEMS AND EXAMS NUMERICAL ANALYSIS 10.5.2010 Midterm Examination QUESTION 1 Determine the highest real root of f(x)=0.95x3 - 5.9x2 +10.9x-6 using the Newton-Raphson method with x0=3.5. Carry out 3 iterations. QUESTION 2 Enthapy of a saturated water will be approximated by the following equation

h f = a0 + a1t + a2t 2
Following data is taken from the thermodynamic table. Find the polynomial coefficient by using polinomial least square by hand calculations 10 30 50 70 t, oC hf , kJ/kg 41.99 125.66 209.26 292.97 QUESTION 3 Perform first two iteration of the steepest descent method to find minimum of

z = f ( x, y ) = 8 x + x 2 + 12 y + 4 y 2 2 xy Using initial guesses x=0 and y=0


NUMERICAL ANALYSIS FINAL EXAM 2009-2010 Spring Semester, 9/6/2010 Time : 2 hours PROBLEM 1 Solve diferential equation

dy 1 = dx x + y

with initial conditions x0=0 , y0=2 , at the interval 0-0.4 with step size h=0.2,

with 4th order Runge-Kutta method


---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NA87 k1=0.5 k2=0.46511627906976744 k3=0.465872156013001 k4=0.43607672682604176 i=0ai=2.0932684532330526 k1=0.43605884805601314 k2=0.41036174265959957 k3=0.4107949303893789 k4=0.38828506085481335 i=1ai=2.1754903617333454
//====================================================== // Numerical Analysis package in java // example to show differential equation solution // Runge-Kutta 4rd order solution RK4 // Dr. Turhan Coban // ===================================================== // dy/dx=f(x,y) import java.io.*; abstract class f_xy

{ //single function single independent variable // example f=x*x abstract double func(double x,double y); } class fxy extends f_xy { double func(double x,double y) {return 1.0/(x+y);} } class NAFP1 { public static double[][] RK4(f_xy f,double x0,double y0,double xn,double h) { // Runge-Kutta 4 int n=(int)((xn-x0)/h); double a[][]=new double[2][n+1]; int i=0; a[0][0]=x0; a[1][0]=y0; double k1,k2,k3,k4; for(double xx=x0;xx<xn;xx+=h) {a[0][i+1]=xx+h; k1=f.func(a[0][i],a[1][i]); k2=f.func(a[0][i]+0.5*h,a[1][i]+0.5*k1*h); k3=f.func(a[0][i]+0.5*h,a[1][i]+0.5*k2*h); k4=f.func(a[0][i]+h,a[1][i]+k3*h); a[1][i+1]=a[1][i]+(k1+2.0*k2+2.0*k3+k4)/6.0*h; System.out.println("k1="+k1); System.out.println("k2="+k2); System.out.println("k3="+k3); System.out.println("k4="+k4); System.out.println("i="+i+"ai="+a[1][i+1]); i++; } return a; } public static void main(String args[]) throws IOException {double x0=0; double y0=2.0; fxy f2=new fxy(); double a[][]=RK4(f2,x0,y0,0.4,0.2); Text.print(Text.T(a)); Plot p1=new Plot(a); p1.setPlotType(0,24); p1.plot(); } } 0. 2.0 0 0. 2.0932684532330526 2 0. 2.1754903617333454 4

PROBLEM 2 Solve the following integral by n=4 Gauss-Legendre integral

4dx 1 + 256( x 0.375) 2 0 Coefficients: I =


N 4 xk -0.861136311594052 -0.339981043584856 0.339981043584856 0.861136311594052 ck 0.347854845137447 0.652145154862546 0.652145154862546 0.347854845137447

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NAFP2 k1=0.5k2=0.5x=0.06943184420297399y=0.16062189039772795z=0.02793655140499282 k1=0.5k2=0.5x=0.330009478207572y=2.634730839304772z=0.887050027014761 k1=0.5k2=0.5x=0.669990521792428y=0.1718436836502664z=0.9430835398578876 k1=0.5k2=0.5x=0.930568155797026y=0.049990045864377546z=0.9517781796891711 integral of class f1 : 0.9517781796891711 > Terminated with exit code 0.
//====================================================== // Numerical Analysis package in java // example to show utilisation of integration (integral) // and differentiation (derivative) functions // Gauss-Legendre 10 points integration formula // Dr. Turhan Coban // ===================================================== import java.io.*; class f1 extends f_x { public double func(double x) { double ff=4.0/(1+256*(x-0.375)*(x-0.375)); return ff; } } class NAFP2 { public static double integral(f_x f_xnt,double a,double b) { double r[],c[]; int n=4; r=new double[n]; c=new double[n]; r[ 0] = -0.861136311594052; r[ 1] = -0.339981043584856; r[ 2] = 0.339981043584856; r[ 3] = 0.861136311594052; c[ 0] = 0.347854845137447; c[ 1] = 0.652145154862546; c[ 2] = 0.652145154862546; c[ 3] = 0.347854845137447; double z=0; double x,y; double k1=(b-a)/2.0; double k2=(b+a)/2.0; for(int i=0;i<n;i++) { x=k2+k1*r[i]; y=f_xnt.func(x); z+=k1*c[i]*y;

System.out.println("k1="+k1+"k2="+k2+"x="+x+"y="+y+"z="+z); } return z; } public static void main(String args[]) throws IOException { //Gauss-Legendre integration formula with n=10 f1 b1=new f1(); System.out.println("integral of class f1 : "+integral(b1,0.0,1.0)); } }

PROBLEM 3 Enthapy of a saturated water will be approximated by the following equation

h f = A + Bt + Ct 2
Following data is taken from the thermodynamic table. Find the polynomial coefficient by using polinomial least square t, oC 10 30 50 hf , kJ/kg 41.99 125.66 209.26

---------- Capture Output ---------> "C:\turhan\java\bin\javaw.exe" NAFP3 katsaylar : 0.128749999999834 4.187000000000012 -0.000087500000000 Polynomial Least Square Curve fitting 10.0000000000 41.9900000000 16.6666666667 69.8877777778 23.3333333333 97.7777777778 30.0000000000 125.6600000000 36.6666666667 153.5344444444 43.3333333333 181.4011111111 50.0000000000 209.2600000000
import java.io.*; import javax.swing.*; class NAFP3 { // Polynomial least square public static double[] gausswithpartialpivot(double a[][],double b[]) { //Gauss elimination with partial pivoting int n=b.length; double x[]=new double[n]; double carpan=0; double toplam=0; double buyuk; double dummy=0; //gauss elimination int i,j,k,p,ii,jj; for(k=0;k<(n-1);k++) { //partial pivoting p=k; buyuk=Math.abs(a[k][k]); for(ii=k+1;ii<n;ii++) { dummy=Math.abs(a[ii][k]); if(dummy > buyuk) {buyuk=dummy;p=ii;}

} if(p!=k) { for(jj=k;jj<n;jj++) { dummy=a[p][jj]; a[p][jj]=a[k][jj]; a[k][jj]=dummy; } dummy=b[p]; b[p]=b[k]; b[k]=dummy; } // for(i=k+1;i<n;i++) { carpan=a[i][k]/a[k][k]; a[i][k]=0; for(j=k+1;j<n;j++) { a[i][j]-=carpan*a[k][j]; } b[i] =b[i] -carpan*b[k]; } } //backward substitution x[n-1]=b[n-1]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { toplam=0; for(j=i+1;j<n;j++) { toplam+=a[i][j]*x[j];} x[i]=(b[i]-toplam)/a[i][i]; } return x; } public static double[] PolynomialLSQ(double xi[],double yi[],int n) { //Polynomial least square int l=xi.length; int i,j,k; int np1=n+1; double A[][]; A=new double[np1][np1]; double B[]; B=new double[np1]; double X[]; X=new double[np1]; for(i=0;i<n+1;i++) { for(j=0;j<n+1;j++) {if(i==0 && j==0) A[i][j]=l; else for(k=0;k<l;k++) A[i][j] += Math.pow(xi[k],(i+j)); } for(k=0;k<l;k++) { if(i==0) B[i]+= yi[k]; else B[i] += Math.pow(xi[k],i)*yi[k];} } X=gausswithpartialpivot(A,B); double max=0; for(i=0;i<n+1;i++) if(Math.abs(X[i]) > max) max = Math.abs(X[i]); for(i=0;i<n+1;i++) if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0; return X; } public static double funcPolynomialLSQ(double e[],double x) { // this function calculates the value of // least square curve fitting function int n=e.length; double ff; if(n!=0.0)

{ ff=e[n-1]; for(int i=n-2;i>=0;i--) { ff=ff*x+e[i]; } } else ff=0; return ff; } public static double error(double x[],double y[],double e[]) { //calculates absolute square root error of a least square approach double n=x.length; int k; double total=0; for(k=0;k<n;k++) { total+=(y[k]-funcPolynomialLSQ(e,x[k]))*(y[k]-funcPolynomialLSQ(e,x[k])); } total=Math.sqrt(total); return total; } public static double[][] funcPolynomialLSQ(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi) { //aradegersayisi: x--o--o--x--o--o--x zincirinde x deneysel noktalar ise // ara deer says 2 dir int n=xi.length; int nn=(n-1)*(aradegersayisi+1)+1; double z[][]=new double[2][nn]; double E[]=PolynomialLSQ(xi,yi,polinomkatsayisi); System.out.println("katsaylar :\n"+Matrix.toStringT(E)); double dx=0; int k=0; int i; for(i=0;i<(n-1);i++) {z[0][k]=xi[i];z[1][k]=funcPolynomialLSQ(E,z[0][k]);k++; for(int j=0;j<aradegersayisi;j++) {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0); z[0][k]=z[0][k-1]+dx;z[1][k]=funcPolynomialLSQ(E,z[0][k]);k++;} } z[0][k]=xi[i];z[1][k]=funcPolynomialLSQ(E,z[0][k]); return z; } public static void main(String args[]) throws IOException { double x[]; double y[]; String s1=JOptionPane.showInputDialog("Data file name (input1.txt)"); // JFileChooser fc=new JFileChooser(); // if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); } double a[][]=Text.readDoubleT(s1); x=a[0]; y=a[1]; double z[][]=funcPolynomialLSQ(x,y,2,2); System.out.println("Polynomial Least Square Curve fitting\n"+Matrix.toStringT(z)); Plot pp=new Plot(a[0],a[1]); pp.setPlabel("Polynomial Least Square Curve fitting"); pp.setXlabel("x"); pp.setYlabel("y=f(x)"); pp.setPlotType(0,0); pp.addData(z[0],z[1]); pp.setPlotType(6,22); pp.setGrid(1,1);

pp.plot(); } }

PROBLEM 4

Find minimum of function by using Fibonnachi search(Golden search) method in the region

1 x 4

f ( x) =

4.1xe x x2 +1

x=0.5 -0.9947102819287187 x=1.5 -0.422230918434721


// OPTIMIZATION Golden search or fibonnachi method import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; class fb extends f_x { public double func(double x) { //zm istenen fonksiyon double ff; ff=-4.1*x*Math.exp(-x)/(x*x+1); return ff; //maksimum testi } } public class NAFP4 { public static double golden(f_x f,double a,double b,double epsilon) {return golden(f,a,b,epsilon,1.0e-10,0);} public static double golden(f_x f,double a,double b) {return golden(f,a,b,1.0e-10,1.0e-10,0);} public static double golden(f_x f,double a,double b,double epsilon,double delta,int print) { // find the minimum of the function // note maximum f(x) = minimum (-f(x)) double r1 = (Math.sqrt(5.0)-1.0)/2.0; // golden ratio double r2 = r1*r1; double h = b - a; double ya = f.func(a);

double yb = f.func(b); double c = a + r2*h; double d = a + r1*h; double yc = f.func(c); double yd = f.func(d); int k = 1; double dp,dy,p,yp; while ((Math.abs(yb-ya)>epsilon) || (h>delta)) { k++; if (yc<yd) { b = d; yb = yd; d = c; yd = yc; h = b - a; c = a + r2 * h; yc = f.func(c); } else { a = c; ya = yc; c = d; yc = yd; h = b - a; d = a + r1 * h; yd = f.func(d); }//end of if }//end of while dp = Math.abs(b-a); dy = Math.abs(yb-ya); p = a; yp = ya; if (yb<ya) { p = b; yp = yb; } if(print==1) {System.out.println("x min = "+p+"ymin = "+yp+"error of x ="+dp+"error of y"+dy); } return p; } public static void main (String args[]) { fb f=new fb(); double a,b; a=Double.parseDouble(JOptionPane.showInputDialog(" enter lower limit of the function a : ")); b=Double.parseDouble(JOptionPane.showInputDialog(" enter upper limit of the function b : ")); double r; System.out.println("x=0.5"+f.func(0.5)); System.out.println("x=1.5"+f.func(1.5)); r= golden(f,a,b,1.0e-10,1.0e-5,0); JOptionPane.showMessageDialog(null," optimization value : "+r+"\nFunction value : "+f.func(r), "Fibonacchi (Golden search) optimization : ",JOptionPane.PLAIN_MESSAGE); System.exit(0);

} }

WEEKLY EXERCISES AND HOMEWORKS WEEK 1 EXERCISES WK1 EX1 Solve root of function f(x)=13*(x - x*x)*exp(-3*x/2) by using bisection method. Carry out 3 step hand calsulation. Also solve by uding computer algorithm. EX2 Find the root of function f(x)=x10-1. Use false position and modified false position methods to find the root of this function. Are the any difficulty to find it? Explain why? Not that normal false position method can not solve this function. EX 3 Find the root of f(x)= x

10 byusingFixed point iteration x+4

HOMEWORKS WK1 HW1 : Write a program with a method sqrt(x) to calculate square root of a given number x, use bisection method to write it. HW2 : Problem 5.3(Page131) Determine the real root of f(x)=-12-21x+18x2-2.75x3 a) graphically b) using bi-section. Employ initial gueses of xl=0.5 and xu=1.0 HW3 : Problem 5.10(Page 131) Find the positive real root of f(x)=x4-8x3-35x2+450x-1001 using the false position method. Use initial gueses of xl=4.5 and xu=6 HW3: Find the roots of f(x)=tan(x)*2x by using brent method. WEEK 2 EXERCISES WK2 EX1 Re-write the program NA5(Newton Raphson method) to find root of f(x)=x2-2 function. Note that you should also define the derivative EX2 Re-write the program NA6 and NA7 (Secant method) to find root of f(x)=x2-2 function. EX3 Solve root of function f(x)=13*(x - x*x)*exp(-3*x/2) by using a) Inverse lagrange interpolation method b) Newton-bisection combined method c) Brent method HOMEWORKS WK2 WK2_HW1 : In round smooth tubes for turbulent flow region Fanning friction factor is given as:

1/

= 1.737 * ln (Re *
3

f ) 0 .4

This equation is valid for 4 * 10 < Re < 3 * 106 By using one of the root finding methods, calculate friction factor for Re=5000. You can utilize any root finding method you learned so far. WK2_HW2: Determine the real root of f(x)=-12-21x+18x2-2.75x3 by using a) Newton-Raphson Method b) Brent method c) Muller method d) Secant method WK2_HW3 : Find all real roots of function f ( x) = x 4 7.223x 3 + 13.447 x 2 0.672 x 10.223

WEEK 3
LAB EXERCISES WK3 PROBLEM 1

7 1 2 X 0 47 1 4 1 X = 19 1 3 15 20 X 2 87
a) b) c) d) Solve the given system of equation by using Gauss elimination method by hand By using computer code NA14 (Gauss elimination) Solve the given system of equation by using Gauss elimination method with partial pivoting By using computer code NA15 (Gauss elimination with partial pivoting)

PROBLEM 2

4 3 1 X 0 6 7 2 3 X = 9 1 5 18 13 X 2 3
a) Solve the given system of equation by using Gauss-Jordan elimination method by hand b) By using computer code NA16 (Gauss elimination) c) By using computer code NA17 (Gauss elimination with partial pivoting)
PROBLEM 3

8 2 3 X 0 30 1 9 2 X = 1 1 2 3 6 X 2 31
LU Decompose the given system of equation by using Gauss(Dolittle) decomposition method by hand and solve it b) LU Decompose the given system of equation by using Gauss(Dolittle) decomposition method by using program NA18 and solve it c) LU Decompose the given system of equation by using pivoted Gauss(Dolittle) decomposition method by using program NA19 and solve it d) LU Decompose the given system of equation by using Craut decomposition method by using program NA20 and solve it HOMEWORK PROBLEMS WK 3 PROBLEM 1 a)

1.1348 0.5301 3.4129 1.2371

3.4017 X 0 9.5342 1.7875 2.5330 1.5435 X 1 6.3941 = 4.9317 8.7643 1.3142 X 2 18.4231 4.9998 10.6721 0.0147 X 3 16.9237 3.8326 1.1651

X 0 1 X 1 1 Exact solution : = X 2 1 X 3 1
a) b) c) d) Solve the given system of equation by using Gauss elimination method by hand By using computer code NA14 (Gauss elimination) Solve the given system of equation by using Gauss elimination method with partial pivoting By using computer code NA15 (Gauss elimination with partial pivoting)

PROBLEM 2

20 X 0 18 3 5 47 11 16 17 10 X 1 26 = 56 22 11 18 X 2 34 17 66 12 7 X 3 82
a) Solve the given system of equation by using Gauss-Jordan elimination method by hand b) By using computer code NA16 (Gauss elimination) c) By using computer code NA17 (Gauss elimination with partial pivoting)
PROBLEM 3 3x1 + 5x2 + 4x3 + 2x4 = 11 6x1 +14x2 + 11x3 + 6x4 = 26 9x1 + 11x2 +16x3 + 5x4 = 68 3x1 + 13x2 +17x3 + 12x4 = 43

LU Decompose the given system of equation by using Gauss(Dolittle) decomposition method by hand and solve it b) LU Decompose the given system of equation by using Gauss(Dolittle) decomposition method by using program NA18 and solve it c) LU Decompose the given system of equation by using pivoted Gauss(Dolittle) decomposition method by using program NA19 and solve it d) LU Decompose the given system of equation by using Craut decomposition method by using program NA20 and solve it PROBLEM 4

a)

Load balance of the system given in the figure can be formed into a system of equations as follows

where c=cos() s=sin(). For =60o find the load distribution. Use any method (computer program) you desired for the solution

WEEK 4
LAB EXERCISES WK4 PROBLEM 1

0 X 0 41 0 .8 0 .4 0.4 0.8 0.4 X = 25 1 0 0.4 0.8 X 2 105

e) f)

Solve the given system of equation by using Gauss-seidel method by using computer code NA21 Solve the given system of equation by using Thomas algorithm (3 band matrix) by using computer code NA23 g) Solve the given system of equation by using Thomas algorithm (3 band matrix) by hand

PROBLEM 2

1 4 3 7 2 3 5 18 1
d) Find the Euclidian norm A e) f)
e

a
i =1 j =1

2 ij

by hand

Find the inverse matrix by using gauss elimination by hand Find the inverse matrix by using computer code NA28

PROBLEM 3

8 20 15 X 0 50 20 80 50 X = 250 1 15 50 60 X 2 100
Solve the given system of equation by using cholesky decomposition by computer code NA22 HOMEWORK PROBLEMS WK4 PROBLEM 1

2 1 X 0 0. 5 1 2 1 X 1 .5 1 X 2 1 .5 1 2 1 ... = ... X 7 1 .5 1 2 1 1 2 1 X 8 1 .5 1 2 X 9 0 .5
Solve the given system of equation by using Thomas algorithm (3 band matrix) computer code NA21 PROBLEM 2

10 2 1 X 0 27 3 6 2 X = 61.5 1 1 1 5 X 2 21.5
d) Solve the given system of equation by using Gauss-Seidel method by hand until the percent relative error falls below 5% e) Solve the given system of equation by using Gauss-Seidel method by computer code PROBLEM 3

6 15 55 X 0 152.6 15 55 225 X = 585.6 1 55 225 979 X 2 2488.8


f) Solve the given system of equation by using cholesky decomposition method by hand g) Solve the given system of equation by using cholesky decomposition by computer code NA22 PROBLEM 4

For the circuit shown in the diagram following equations can be written

For the electrical resistance R=10 calculate i current densities in Amper. Use any method you like (solving by hand or by computer code is left to you) WEEK 5 LAB EXERCISES WK5 EX 1 x1(1- x1) + 4x3 12 =0 (x1-2)2+(2x2-3)2-25 =0 Solve the roots of nonlineer system of equation a) by using Newton Raphson method by hand b) by using Newton Raphson method bycomputer program
EX 2 Find the minimum of function f(x)=-(1.0/((x-0.3)*(x-0.3)+0.01)+1.0/((x-0.9)*(x-0.9)+0.04))

a) By using golden search method with computer b) By using second degree polynomial with computer c) By using Newton-Raphson method by computer
EX 3 Find the minimum of function f(x) = 2500x6-15000x5+14875x4+40500x3-97124x2+73248x-19008 between x0=0.8, x1=1.2

a) By using golden search method with computer b) By using second degree polynomial with computer c) By using Newton-Raphson method by computer
HOMEWORKS WK5 PROBLEM HW5.1 10x1-2x22+x2-2x3-5=0 8x22+4x32-9=0 8x2x3+4=0

Solve the roots of nonlineer system of equation c) by using Newton Raphson method by hand d) by using Newton Raphson method bycomputer program
PROBLEM HW5.2

One of the basic optimization problem is to manufacture a box with a certain volume with minimum cost. The cost of a box is basically depend on the surface area. Therefore for a given volume surface area should be minimised. As an example if box volume, V is taken as 0.5 liters=0.5x10-3 m3 Volume V =

D 2
4

h and the height will be h =

4V

4 2 This minimisation problem can be solved analytically dA 4V = D 2 = 0 dD D And D diameter is found as: D=3 4V

Surface area of the cylinder : A = 2

D 2

+ Dh =

D 2 D 2

.
+ 4V . D

D=3

4 * 0.5 x10 3

=0.086025401 m .

If this value is substituted into h, 4 * 0.5 x10 3 = 0.086025401 m . h= D 2 Solve the same problem with computer for : 0.01<=D<=0.2 arasnda

j) k) l) m) n) o)

Graphic method Golden ratio method Newton-Raphson method Quadratic(Second degree) polynomial Cubic polynomial Brent optimization method

PROBLEM HW5.3

Find the minimum of function f(x)=-(1.0/((x-0.3)*(x-0.3)+0.01)+1.0/((x-0.9)*(x-0.9)+0.04))

a) By using golden search method by hand b) By using second degree polynomial by hand c) By using Newton-Raphson method by hand
WEEK 6 LAB EXERCISES PROBLEM 1 In a channel polutant concentration is given as C(x,y)=7.9+0.13x+0.21y-0.05x2-0.016y2-0.007xy Maximum concentration is in between -10 <= x <= 10 ve 0 <= y <= 20 limits. Find the maximum concentration b) By using Newton raphson method c) Nelder-Mead method d) Steepest descent method (use computer codes)

PROBLEM 2 Cities, A,B,C, and D are located at points (0,0),(1,5),(5,2) and (7,3) respectively. Roads from this four city is crossed at a point P. Find the point p so that total road distance will become minimum. (use computer codes)

D = ( x xi ) 2 + ( y yi ) 2
i

D = ( x 0) 2 + ( y 0) 2 + ( x 1) 2 + ( y 5) 2 + ( x 5) 2 + ( y 2) 2 + ( x 7) 2 + ( y 3) 2

HOMEWORKS PROBLEM 1 Minimum of function f(x) is located at point(1,2) f(x0, x1) = 3(x0 - 1)2 +2(x1-1) (x0-1)(x1-2)+(x1- 2)4 Starting from point (0,0) find the minimum by a) Newton raphson metod calculated by hand b) Newton-raphson metod by computer code

PROBLEM 2 Find the minimum of function f(x0, x1, x2, x3) = (x0+10 x1)2 +5(x2-10 x3)2+(x1-2 x2)4+10(x1- x4)4 by starting from point (1,-1,0,1) .

a) By using Newton raphson method b) Nelder-Mead method c) Steepest descent method (use computer codes) PROBLEM 3 A petrochemical company is developing a new fuel additive for commercial airplanes. Additive has three different chemicals X,Y and Z. For a good performance additive amount in a liter of fuel should be 6 mL/L. Because of security concerns total of very flammable X and Y components should be less than 3 mL/L. In addition component X should be equal or greater than Y and Z component should be greater than half of Y. The cost of X,Y and Z components are 0.15,0.025 ve 0.05 TL respectively. Calculate the component percentages for the minimum fuel additive cost. a) Solve by hand b) Solve by computer code
WEEK 7 WEEK 7 LAB EXERCISES PROBLEM 1 Use linear least square method (x=ao+a1*x) by hand to fit data to the following equation

X Y

2 9

3 6

4 5

7 10

PROBLEM 2 Fit cubic spline interpolation Formula to the following equation by computer
X 0 1 2 3 Y 0 1 4 8

PROBLEM 3 Enthapy of a saturated water will be approximated by the equation


h f = A + Bt + Ct 2 + Dt 3

Following data is taken from the thermodynamic table. Find the polynomial coefficient by using least square computer program t, oC hf , kJ/kg 10 41.99 30 125.66 50 209.26 70 292.97 90 376.94

PROBLEM 4

Force aplied to a linear spring is given with the Formula F=kx according to Hooke law. In this equation k is the spring constant and x is the displacement. Experiment is being carried out and found the following results between displacement and Force. Estimate spring constant k (use computer programs) a) By using Newton Interpolation Formula

b) By using Lagrange interpolation Formula c) By using least square curve fitting 0.02 3.1 0.04 6.1 0.06 9.2 0.08 12 0.1 15.1

X, m F, kN

WEEK 7 HOMEWORKS PROBLEM 1

Depth (H) and velocity (U) profile of a channel is given below x, m 0 2 4 6 8 10 12 H, m 0 1.8 2 4 4 6 4 U m/s 0 0.03 0.045 0.055 0.065 0.12 0.07

14 3.6 0.06

16 3.4 0.05

18 2.8 0.04

20 0 0

0 0 5 10 15 20 25

-1

-2

derinlik m

-3 Seri 2 -4

-5

-6

-7 sol kydan uzaklk m

Curve fit to H(x) and U(x) functions by using (Computer programming) d) Cubic spline interpolation formula e) Polinomial least sqare formula f) Lagrange interpolation formula
PROBLEM 2 Thermal conductivity coefficient of water for different temperature values are given in the below table

Temperature
k. [W/(m.K)]

T . [K]

275 0.574

290 0.598

305 0.620

320 0.640

340 0.660

360 0.674

Thermal conductivity coefficient

a-) curvefit the following equation to thermal conductivity-temperature relation by using hand calculations:

k = AT +B

b-) Calculate thermal conduvtivity for T=300 K .


PROBLEM 3 Enthapy of a saturated water will be approximated by the following equation
h f = A + Bt + Ct 2 + Dt 3

Following data is taken from the thermodynamic table. Find the polynomial coefficient by using polinomial least square by hand calculations t, oC 10 30 50 70 90 hf , kJ/kg 41.99 125.66 209.26 292.97 376.94
PROBLEM 4 Force aplied to a linear spring is given with the Formula F=kx according to Hooke law. In this equation k is the spring constant and x is the displacement. Experiment is being carried out and found the following results between displacement and Force. Estimate spring constant k (by hand calculations) d) By using Newton Interpolation Formula e) By using Lagrange interpolation Formula f) By using least square curve fitting X, m 0.02 0.04 0.06 0.08 0.1 F, kN 3.1 6.1 9.2 12 15.1

WEEK 8 WEEK 8 LAB EXERCISES PROBLEM 1

Calculate integral of y=f(x)=1/(1 + x2) fonction between limits 1 ile 1 with 3 points Gauss-Legendre integration formula by hand and by computer program Note: Required roots and coefficients : coefficients c0 0.555555555555553 c1 0.888888888888889 c2 0.555555555555553 PROBLEM 2

Roots x0 0.774596669241483 x1 0.000000000000000 x2 0.774596669241483

1 + 2x
b

dx solve with bole rule. Use n=2

Note: Bole rule for n=1

I = a f ( x)dx =

(b a) [7 f ( x0 ) + 32 f ( x1 ) + 12 f ( x2 ) + 32 f ( x3 ) + 7 f ( x4 )] 90

Step size h=(b-a)/n PROBLEM 3


I =
0 1

2dx = 1.15470054 2 + sin(10x)

Solve the above integration by using e) Gauss-Legendre integration by computer f) Simpson 1/3 integration by computer g) Romberg integration WEEK 8 HOMEWORKS PROBLEM 1 Coordinate of a hard shape is given in the table. Find the area of the hard. Xi Yi xi yi xi yi xi 241 127 331 157 215 246 149 258 107 323 178 199 232 160 283 96 312 197 184 217 175 307 97 298 217 170 197 199 322 106 283 232 159 178 224 333 120 267 246 151 157 241 334 138 241 258 148 138

Yi 120 106 97 96 107 127

PROBLEM 2 Depth(H) and velocity(U) profile of a channel is given in below table

x, m H, m U m/s

0 0 0

2 1.8 0.03

4 2 0.045

6 4 0.055

8 4 0.065

10 6 0.12

12 4 0.07

14 3.6 0.06

16 3.4 0.05

18 2.8 0.04

20 0 0

Cross sectional area of the channel can be calculated as :

Ac = H ( x ) dx
0

Flow rate of the channel will be

Q = U ( x) H ( x)dx In this equations U is the velocity of the water measured at point x. Calculate
0

cross sectional area and flow rate of the channel.

PROBLEM 3 Blackbody radiation function F(0- ),

C2 14387.69 mK = T T x 15 x 3 F (0 T ) = 1 4 x dx 0 e 1 x=

(m) T(degree Kelvin) can be calculated as

It can also be calculated from the series solution

F (0 T ) =

15

n =1

e nx 3 3x 2 6 x 6 n ( x + n + n 2 + n3 )

By creating a program that calculates the result with using series solution and integration. Compare the results. Resulting tables are given as a reference F(0T) F(0T) F(0T) F(0T) F(0T) F(0T)

T mK
1000 1050

T T

T mK

T mK

T mK

T mK

0.000322900 0.000562950

0.250564800 0.261914850

0.607546900 0.614287000

0.8022 10700 0.9270918000 0.98081 0.8080810800 0.9287218200 0.98137

1100 1150 1200 1250 1300 1350 1400 1450 1500 1550 1600 1650 1700 1750 1800 1850 1900 1950 2000 2050 2100 2150 2200 2250 2300 2350 2400 2450 2500 2550 2600 2650 2700 2750 2800

0.000913000 0.001423050 0.002133100 0.003083150 0.004323200 0.005873250 0.007793300 0.010113350 0.012853400 0.016053450 0.019723500 0.023883550 0.028543600 0.033693650 0.039343700 0.045493750 0.052113800 0.0592 3850 0.066733900 0.074693950 0.083064000 0.0918 4050 0.100894100 0.110314150 0.120034200 0.130024250 0.140264300 0.150714350 0.161364400 0.172174450 0.183124500 0.194194550 0.205364600 0.2166 4650 0.227894700

0.273234900 0.284534950 0.295785000 0.306975050 0.3181 5100 0.329155150 0.340115200 0.350975250 0.361735300 0.372385350 0.382915400 0.393325450 0.4036 5500 0.413755550 0.423775600 0.433645650 0.443385700 0.452975750 0.462415800 0.471725850 0.480875900 0.489875950 0.498736000 0.507446050 0.516 6100 0.524426150 0.532696200 0.540816250 0.548786300 0.556626350 0.564316400 0.571866450 0.579276500 0.586546600 0.593676700

0.620897100 0.627377200 0.633737300 0.639967400 0.646087500 0.652077600 0.657957700 0.663717800 0.669377900 0.674918000 0.680348100 0.685668200 0.690898300 0.696 8400 0.701028500 0.705948600 0.710778700 0.7155 8800 0.720138900 0.724689000 0.729149100 0.733519200 0.737799300 0.741999400 0.746119500 0.750159600 0.754119700 0.758 9800 0.761819900 0.7655410000 0.7692110100 0.7728 10200 0.7763210300 0.7831710400 0.7897610500

0.8137410900 0.8191811000 0.8244311200 0.8294911400 0.8343711600 0.8390711800 0.8436 12000 0.8479712200 0.8521912400 0.8562512600 0.8601812800 0.8639713000 0.8676313200 0.8711613400 0.8745713600 0.8778713800 0.8810514000 0.8841314200 0.8871114400 0.8899914600 0.8927814800 0.8954715000 0.8980815200 0.9006 15400 0.9030515600 0.9054115800 0.9077 16000 0.9099216200 0.9120716400 0.9141616600 0.9161816800 0.9181417000 0.9200417200 0.9218817400 0.9236717600

0.9303118400 0.9318518600 0.9348 18800 0.9375819000 0.9402119200 0.9427 19400 0.9450519600 0.9472819800 0.9493920000 0.9513920500 0.9532921000 0.9550921500 0.9568122000 0.9584322500 0.9599823000 0.9614523500 0.9628524000 0.9641924500 0.9654625000 0.9666726000 0.9678327000 0.9689328000 0.9699929000 0.971 30000 0.9719632000 0.9728934000 0.9737736000 0.9746138000 0.9754240000 0.9762 45000 0.9769450000 0.9776555000 0.9783460000 0.97899 0.97962

0.98191 0.98243 0.98293 0.98341 0.98387 0.98431 0.98474 0.98516 0.98555 0.98649 0.98735 0.98814 0.98886 0.98952 0.99014 0.9907 0.99123 0.99172 0.99217 0.99297 0.99368 0.99429 0.99482 0.99529 0.99607 0.99669 0.99719 0.99759 0.99792 0.99851 0.9989 0.99917 0.99935

WEEK 9 WEEK 9 LAB EXERCISES PROBLEM 1 Calculate velocity and acceleration by using below data and creating excell tables with f) cental difference method [with O(h4) error level], g) forward difference method [with O(h2) error level], h) backward difference method [with O(h2) error level], At t=1.5,2.5 and 3.5 seconds Time, t (s) 0 1 2 3 4 5 6 7 8 distance, x (m) 0 0.7 1.8 3.4 5.1 6.5 7.3 8.0 8.4

dx Note : Velocity v = Acceleration : dt

d 2x a= 2 dt

PROBLEM 2 Calculate velocity (first derivative) and acceleration (second derivative) by using function x=0.038073038073421 -0.005445480447162*t+0.645464257965281*t2 -0.100887075887276*t3+0.004574592074604 At t=1.5,2.5 and 3.5 seconds by using analytical hand derivative and by using related java program WEEK 9 HOMEWORKS PROBLEM 1 Calculate velocity and acceleration by using below data directly and creating your own java program with a) cental difference method [with O(h4) error level], b) forward difference method [with O(h2) error level], c) backward difference method [with O(h2) error level], Time, t (s) distance, x (m) 0 0 1 0.7 2 1.8 3 3.4 4 5.1 5 6.5 6 7.3 7 8.0 8 8.4

dx Note : Velocity v = Acceleration : dt


At t=1.5,2.5 and 3.5

d 2x a= 2 dt

PROBLEM 2 Calculate velocity and acceleration by using below data and creating your own java program to curve-fit a 4th degree polynamial first and then apply a) cental difference method [with O(h4) error level], b) forward difference method [with O(h2) error level], c) backward difference method [with O(h2) error level], to the given polynomial function Time, t (s) 0 1 2 3 4 5 6 7 8 distance, x (m) 0 0.7 1.8 3.4 5.1 6.5 7.3 8.0 8.4

dx Note : Velocity v = Acceleration : dt


At t=1.5,2.5 and 3.5 seconds

d 2x a= 2 dt

Note : Due to holiday of last week you are responsible both week 8 and week 9 exercises and homeworks, any exercises cannot be finished in class add them into the homework please WEEK 10 WEEK 10 LAB EXERCISES PROBLEM 1
Solve diferential equation dy = yx 2 y at the range x=0 ve x=2 , with initial condition y(0)=1 . dx

c) d) e) f)

solve analytically from x=0 to x=2. solve numerically by hand with h=0.5 step size with 4th order Runge_Kutte and calculate y(2) solve numerically by computer with h=0.1 step size with 4th order Runge-Kutta Plot to results and compare with analytical solution

PROBLEM 2
For diferential equation

y 2(1 + y 2 ) = 0 with initial condition, y(0)=0 and


a) h=0.25 step size find y(1) with Heun method by hand b) h=0.05 step size find y(1) with 6th order Runge Kutta by computer

PROBLEM 3
Solve diferential equation

dy = 5 y + e 2 x dx

with initial conditions : x0=0 y0=1 . y(1) , by using Simpson 1/3 rule.

a) By hand with step size h=0.2 b) By computer with step size h=0.02

WEEK 10 HOMEWORKS PROBLEM 1


Cooling rate of a substance are given with the following equation dT = k (T Ta ) dt In the equation T indicates the temperature of the substance in (oC), Ta is environmental temperature (oC) ,k is a constant (minute-1) dir. Therefore cooling rate is function of temperature difference between body and environment. If a metal ball initially at 90 oC drop into a water bath at Ta=20 oC and if k=0.1 minute-1, Find the time period required ball to reach 30 oC temperature c) By analytical methods d) By a numerical method

PROBLEM 2
In a chemical reaction a chemical A and a chemical B reacts to give chemical C. Concentration of chemical C as a function of time, y(t) is given with the following equation y = k(a y) (b y) y(0)=0 In this equation k is a positive constant, a and b is the initial concentration of chemicals A and B. Assume that k=0.01 , a= 70 milimole/liter , b= 50 milimole/liter . a) By using 4th order Runge-Kutta method as hand calculation with step size h=0.5 estimate concentration at t=1 second. b) By using 4th order Runge-Kutta method as computer calculation with step size h=0.05 estimate concentration at t=1 second.

PROBLEM 3
Solve diferential equation

dy 1 = dx x + y

with initial conditions x0=0 , y0=2 , at the interval 0-0.6 with step size h=0.2,

a-) with 4th order Runge-Kutta method by hand b-) with Euler method by hand

11.0 REFERENCES
1. Chapra, Steven C.; Canale, Raymod P., Numerical Methods for Engineers, fifth Edition, 2006,McGraw-Hill, ISBN 007-124429-8

2. 3. 4. 5. 6. 7. 8. 9.

Pres, W. H.,Teulkolsky, S. A.,Vetterling, W. T., Flannery B. P.; Numerical Recipes in C, 1992, Cambridge University Pres, ISBN 0-521-43108-5 Borse, G.J.;Numerical Methods with MATLAB 1997,PWS Publishing company, ISBN 0-53493822-1 Ralston, A.;Rabinowitz, P;A First Course in Numerical Analysis, second Edition, 1978, McGrawHill, ISBN 007-051158-6 Atkinson, K.E.;An Introduction toNumerical Analysis, Second edition, 1989, John Wiley & Sons, ISBN 0-471-62489-6 Burden, R.L.,Faires,D.F.;Numerical Analysis, Eight Edition, 2005,Thomson Brooks/Cole, ISBN 0534-39200-8 Mathews,J.H.,Fink,K.D.;Numerical Methods Using MATLAB,1999,Third Edition, Prentice Hall, ISBN 0-13-270042-5 Schilling,R.J.,Haris, S. L.Applied Numerical Methods for Engineers,2000, Brooks/Cole, ISBN 0-53437014-4 Dahlquist, G.,Anderson N.;Numerical Methods,1974,Prentice Hall, ISBN 0-13-627315-7

10. Mathews J. H.,Numerical Methods for Mathematics, Science and Engineering, Second Edition, 1992, Prentice-Hall, ISBN 0-13-625047-5 11. Michalewicz, Z., Genetic Algorithms+Data Structures = Evaluation Programs, second extended edition, 1992, Springer-Verlag, ISBN 3-540-58090-5

Karaolu, B.,Saysal Fizik,2004, Seyir Yaynclk, ISBN 975-92544-9-2 Bayram, M,Nmerik Analiz, 2002, Aktif Yaynevi, ISBN 975-6755-50-4

12. Tapramaz, R.,Saysal zmleme, 2002, Literatr Yaynlar, ISBN 975-8431-82-X 13. Scheid, F.,SCHAUMS OUTLINES Nmerik Analiz, 1988,Nobel Yaym Datm, ISBN 975-591078-6 14. Fox, L., Mayers D. F., Computing Methods for Scientist and Engineers, 1968, Oxford University Press 15. Johnson, R. L.,Numerical Methods A Software Aproach, 1982, John Willey & Sons 16. Mitchell, M, An Introduction to Genetic Algorithms1999, A Bradford Book The MIT Pres, ISBN 0262-13316-4 17. Arnold, D. N. , A Concise Introduction to Numerical Analysis, 1991 18. Nash, J.C. ,A Compact Methods for Computers Lineer Algebra and Function Minimisation1990, Adam Hilger, ISBN 0-85274-318-1 19. Chenney, W.,Kinkaid, D.;Numerical Mathematics and Computing, Third Edition, Brooks/Cole publishing 20. oban, M. Turhan, Java Programlama Klavuzu, Herkes iin , 2000, Alfa yaynevi, ISBN 975-316631-1 21. Deitel & Deitel, Java How to Program, Second Edition, Prentice Hall, http://www.deitel.com, second edition,1998, ISBN 0-13-906249-1

22. Naughton, Patrick, t Schildt, Herbert ;Java 1.1 The Complete Reference, Second Adition, 1998, Mc Graw Hill, ISBN 0-07-882436-2

23. Zukowski, John; Mastering Java 1.2 (Renamed Java 2 by sun microsystems),1998, Sybex publishing, ISBN 0-7821-2180-2

24. Learning Jave, Covers Java 1.3, Patrick Niemeyer & Jonathan Knudsen, OReilly , 2000, ISBN 156592-718-4 25. Java Swing, Robert Eckstein, Marc Loy & Dave Wood, OReilly , 1998, ISBN 1-56592-455-X 26. JDBC Database Access with Java, The Java Series, Graham Hamilton, Rick Cattell, Maydene Fisher, Addison Wesley, 1997, ISBN 0-201-30995-5 27. Java Algorithms, Scott Robert Ladd, Mc Graw-Hill, 1998, ISBN 0-07-913696-6 28. Fundamentals of Database Systems, Elmasr & Navathe, Benjamin/Cummings, 1994, ISBN 0-80531753-8 29. Thinking in Java, Bruce Eckel, Prentice Hall, 2000, ISBN 013-659723-8 30. VisAD grafik paketi , http://www.ssec.wisc.edu/~billh/visad.html 31. Java Development kit ve 3D graphic package, http://java.sun.com 32. ALGLIN-NET internet site, (Gauss- Hermit integration) http://www.alglib.net/integral/gq/ghermite.php 33. ALGLIN-NET internet site, (Gauss-Laguerre integration) http://www.alglib.net/integral/gq/glaguerre.php
34. Gander, W. ,W. Gautschi, "Adaptive Quadrature - Revisited", BIT, Vol. 40, 2000, page. 84-101. http:// www.inf.ethz.ch/personal/gander

35. H.J. Korsch H.-J. Jodl T. Hartmann,Chaos, A Program Collection for the PC, Springer publishers, 2008, ISBN 978-3-540-74866-3 36. Forman S. Acton, Numerical Methods that work, Mathematical Association of America, 1990, ISBN 0-88385-450-3 37. Alfio Quarteroni,,Riccardo Sacco, Fausto Saleri, Numerical Mathematics, Springer and Verlag, 1991, ISBN 0-387-98959-5 38. Perviz Moin, Fundamentals of Enginering Numerical Analysis, Cambridge publications, 2010, ISBN
978-0-521-88432-7

S-ar putea să vă placă și