📄 equation.java
字号:
package numericalAnalysis.equation;
import numericalAnalysis.function.Function;
/**
* 本类包含了几个解方程的迭代方法.对照书:数值分析第二版,史万明等编,北京理工大学出版社
*
* @author 山
*
*/
public class Equation {
private Function function;
/**
* 新建一个方程.外部类中必须定义一个类实现Function接口,这个类的对象的f方法即方程f(x)=0的f(x).
*
* @param function function的f(x)方法即方程f(x)=0的f(x)
*/
public Equation(Function function){
this.function=function;
}
/**
* 用双点弦截法解方程.leftEndpoint相当于左端点a,rightEndpoint相当于右端点b.方程f(x)=0必须满足:
* 1.f(leftEndpoint)*f(rightEndpoint)<0
* 2.f'(x)不等于0,当x属于[leftEndpoint,rightEndpoint]时.
*
* @param leftEndpoint 相当于左端点a
* @param rightEndpoint 相当于右端点b
* @return 方程的根
*/
public double getSolution(double leftEndpoint,double rightEndpoint){
double x0 = leftEndpoint, x1 = rightEndpoint;
do {
x0 = (x0 * function.f(x1) - x1 * function.f(x0)) / (function.f(x1) - function.f(x0));
x1 = (x1 * function.f(x0) - x0 * function.f(x1)) / (function.f(x0) - function.f(x1));
} while (Math.abs(x1 - x0) > 1e-10);
return x1;
}
/**
* 用双点弦截法解方程.leftEndpoint相当于左端点a,rightEndpoint相当于右端点b.方程f(x)=0必须满足:
* 1.f(leftEndpoint)*f(rightEndpoint)<0
* 2.f'(x)不等于0,当x属于[leftEndpoint,rightEndpoint]时.
*
* @param leftEndpoint 相当于左端点a
* @param rightEndpoint 相当于右端点b
* @return 计算过程
*/
public String doubleChordSection(double leftEndpoint,double rightEndpoint){
String output="\n";
int count = 0;
double x0 = leftEndpoint, x1 = rightEndpoint;
do {
output+="x" + count + "=" + x0 + ", f(x" + (count++)
+ ")=" + function.f(x0)+"\n";
output+="x" + count + "=" + x1 + ", f(x" + (count++)
+ ")=" + function.f(x1)+"\n";
x0 = (x0 * function.f(x1) - x1 * function.f(x0)) / (function.f(x1) - function.f(x0));
x1 = (x1 * function.f(x0) - x0 * function.f(x1)) / (function.f(x0) - function.f(x1));
} while (Math.abs(x1 - x0) > 1e-10);
return output;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -