Java计算任意一个函数微积分的代码
时间: 2023-06-13 17:06:30 浏览: 70
以下是一个简单的 Java 代码,可以计算任意一个函数的导数:
```java
import java.util.Scanner;
import java.lang.Math;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个函数:");
String function = scanner.nextLine();
System.out.println("请输入一个数值:");
double x = scanner.nextDouble();
double delta = 1e-6; // 可以调整 delta 的值来改变精度
double y1 = eval(function, x - delta);
double y2 = eval(function, x + delta);
double dydx = (y2 - y1) / (2 * delta);
System.out.println("f(x) = " + function);
System.out.println("f'(" + x + ") = " + dydx);
}
public static double eval(String function, double x) {
function = function.replaceAll("x", "(" + x + ")");
return eval(function);
}
public static double eval(String function) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < function.length()) ? function.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < function.length()) throw new RuntimeException("Unexpected: " + (char)ch);
return x;
}
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+')) x += parseTerm(); // 加法
else if (eat('-')) x -= parseTerm(); // 减法
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*')) x *= parseFactor(); // 乘法
else if (eat('/')) x /= parseFactor(); // 除法
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // 正号
if (eat('-')) return -parseFactor(); // 负号
double x;
int startPos = this.pos;
if (eat('(')) { // 括号
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // 数字
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(function.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // 变量
while (ch >= 'a' && ch <= 'z') nextChar();
String func = function.substring(startPos, this.pos);
x = Math.sin(eval(func)); // 这里可以添加更多的函数
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // 指数
return x;
}
}.parse();
}
}
```
这个代码使用了递归下降语法分析的方法,可以解析包含加、减、乘、除、指数、正负号、括号、常数和函数的表达式。其中,支持的函数只有 sin,如果需要添加更多的函数,可以在 `parseFactor` 方法中添加相应的代码。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)