Source Code Cross Referenced for SecantSolver.java in  » Science » Apache-commons-math-1.1 » org » apache » commons » math » analysis » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Apache commons math 1.1 » org.apache.commons.math.analysis 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003-2004 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.math.analysis;
017:
018:        import java.io.Serializable;
019:
020:        import org.apache.commons.math.ConvergenceException;
021:        import org.apache.commons.math.FunctionEvaluationException;
022:
023:        /**
024:         * Implements a modified version of the 
025:         * <a href="http://mathworld.wolfram.com/SecantMethod.html">secant method</a>
026:         * for approximating a zero of a real univariate function.  
027:         * <p>
028:         * The algorithm is modified to maintain bracketing of a root by successive
029:         * approximations. Because of forced bracketing, convergence may be slower than
030:         * the unrestricted secant algorithm. However, this implementation should in
031:         * general outperform the 
032:         * <a href="http://mathworld.wolfram.com/MethodofFalsePosition.html">
033:         * regula falsi method.</a>
034:         * <p>
035:         * The function is assumed to be continuous but not necessarily smooth.
036:         *  
037:         * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
038:         */
039:        public class SecantSolver extends UnivariateRealSolverImpl implements 
040:                Serializable {
041:
042:            /** Serializable version identifier */
043:            private static final long serialVersionUID = 1984971194738974867L;
044:
045:            /**
046:             * Construct a solver for the given function.
047:             * @param f function to solve.
048:             */
049:            public SecantSolver(UnivariateRealFunction f) {
050:                super (f, 100, 1E-6);
051:            }
052:
053:            /**
054:             * Find a zero in the given interval.
055:             * 
056:             * @param min the lower bound for the interval
057:             * @param max the upper bound for the interval
058:             * @param initial the start value to use (ignored)
059:             * @return the value where the function is zero
060:             * @throws ConvergenceException if the maximum iteration count is exceeded
061:             * @throws FunctionEvaluationException if an error occurs evaluating the
062:             * function 
063:             * @throws IllegalArgumentException if min is not less than max or the
064:             * signs of the values of the function at the endpoints are not opposites
065:             */
066:            public double solve(double min, double max, double initial)
067:                    throws ConvergenceException, FunctionEvaluationException {
068:
069:                return solve(min, max);
070:            }
071:
072:            /**
073:             * Find a zero in the given interval.
074:             * @param min the lower bound for the interval.
075:             * @param max the upper bound for the interval.
076:             * @return the value where the function is zero
077:             * @throws ConvergenceException  if the maximum iteration count is exceeded
078:             * @throws FunctionEvaluationException if an error occurs evaluating the
079:             * function 
080:             * @throws IllegalArgumentException if min is not less than max or the
081:             * signs of the values of the function at the endpoints are not opposites
082:             */
083:            public double solve(double min, double max)
084:                    throws ConvergenceException, FunctionEvaluationException {
085:
086:                clearResult();
087:                verifyInterval(min, max);
088:
089:                // Index 0 is the old approximation for the root.
090:                // Index 1 is the last calculated approximation  for the root.
091:                // Index 2 is a bracket for the root with respect to x0.
092:                // OldDelta is the length of the bracketing interval of the last
093:                // iteration.
094:                double x0 = min;
095:                double x1 = max;
096:                double y0 = f.value(x0);
097:                double y1 = f.value(x1);
098:
099:                // Verify bracketing
100:                if (y0 * y1 >= 0) {
101:                    throw new IllegalArgumentException(
102:                            "Function values at endpoints do not have different signs."
103:                                    + "  Endpoints: [" + min + "," + max + "]"
104:                                    + "  Values: [" + y0 + "," + y1 + "]");
105:                }
106:
107:                double x2 = x0;
108:                double y2 = y0;
109:                double oldDelta = x2 - x1;
110:                int i = 0;
111:                while (i < maximalIterationCount) {
112:                    if (Math.abs(y2) < Math.abs(y1)) {
113:                        x0 = x1;
114:                        x1 = x2;
115:                        x2 = x0;
116:                        y0 = y1;
117:                        y1 = y2;
118:                        y2 = y0;
119:                    }
120:                    if (Math.abs(y1) <= functionValueAccuracy) {
121:                        setResult(x1, i);
122:                        return result;
123:                    }
124:                    if (Math.abs(oldDelta) < Math.max(relativeAccuracy
125:                            * Math.abs(x1), absoluteAccuracy)) {
126:                        setResult(x1, i);
127:                        return result;
128:                    }
129:                    double delta;
130:                    if (Math.abs(y1) > Math.abs(y0)) {
131:                        // Function value increased in last iteration. Force bisection.
132:                        delta = 0.5 * oldDelta;
133:                    } else {
134:                        delta = (x0 - x1) / (1 - y0 / y1);
135:                        if (delta / oldDelta > 1) {
136:                            // New approximation falls outside bracket.
137:                            // Fall back to bisection.
138:                            delta = 0.5 * oldDelta;
139:                        }
140:                    }
141:                    x0 = x1;
142:                    y0 = y1;
143:                    x1 = x1 + delta;
144:                    y1 = f.value(x1);
145:                    if ((y1 > 0) == (y2 > 0)) {
146:                        // New bracket is (x0,x1).                    
147:                        x2 = x0;
148:                        y2 = y0;
149:                    }
150:                    oldDelta = x2 - x1;
151:                    i++;
152:                }
153:                throw new ConvergenceException(
154:                        "Maximal iteration number exceeded" + i);
155:            }
156:
157:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.