gpt4 book ai didi

java - 在 Java 中返回方程的问题

转载 作者:行者123 更新时间:2023-12-01 10:51:17 25 4
gpt4 key购买 nike

我的函数调用遇到问题。我试图找到边界条件为 x(0) + x(1) 的两点边界值问题 x' = f(t,x) = x + 0.09 x 2 + cos(10 t) 的解 - 3.0 = 0 使用割线和三阶龙格-库塔方法,无论出于何种原因,我的方程方法在一行上给出了四个错误,这些错误是它不是一个陈述,缺少两个分号,并且缺少一个结束括号。

public class BoundaryValueProblem 
{
public static double f(double t, double x)
{
return x + 0.09x^2 + Math.cos(10t);
}

public static void findZero
{
double x4;
double x5 = .7;
double x6 = 1.0;
int n = 1;
double fx;
double f1;
double f2;
double Error;
BoundaryValueProblem FZ = new BoundaryValueProblem();
f1 = FZ.f(1.0, 1.0);
f2 = FZ.f(1.0, 1.0);
System.out.println("Secant Method");
System.out.println("n: \t\t x1: \t\t x2: \t\t Error:");
while(abs(x5 + x6 - 3.0) < 1e-5)
{
x4 = x6 - f2 * ((x6 - x5)/(f2 - f1));
fx = FZ.f(1.0, 1.0);
x5 = x6;
x6 = x4;
f1 = f2;
f2 = fx;
Error = x5 + x6 - 3.0;
System.out.println(n + "\t\t" + x5 + "\t\t" + x6 + "\t\t" + Error);
n++;
}
System.out.println();
}

public void rkm(double x0, double t0, double h)
{
double x1, x2, x3;
int i=0;
double a1 = 0.5;
double a2 = 0.25;
double c0 = 2.0/3.0;
double c1 = 5.0/3.0;
double c2 = -4.0/3.0;
double b21 = -.25;
double b10 = .5;
double b20 = .5;
double stepsize = .025;
System.out.println("Runge-Kutta Method:");
System.out.println("i: \t\t h: \t\t t0: \t\t x0:");
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0 );
for(i = 0; i < 40; i++)
{
x1 = x0 + h * b10 * f(t0, x0);
x2 = x0 + h * (b20 * f(t0, x0) + b21 * f(t0 + a1 * h, x1));
x3 = x0 + h * (c0 * f(t0, x0) + c1 * f(t0 + a1 * h, x1) + c2 * f(t0 + a2 * h, x2));
t0 = t0 + stepsize;
x0 = x3;
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0 );
}
System.out.println();
}

public static void main(String[] args)
{
BoundaryValueProblem FZ1 = new BoundaryValueProblem();
FZ1.findZero();
BoundaryValueProblem RKM1 = new BoundaryValueProblem();
RKM1.rkm(1.0, 0.0, 0.0);
}
}

我已经让它可以处理更简单的方程,但这个对我不起作用。我的其余代码大部分都是正确的(我相信),但我不认为这是错误的原因,因为我将其注释掉并且错误仍然存​​在,因此我不关注这一点。任何建议或帮助将不胜感激。

最佳答案

下面是固定代码。请记住,与 Octave 或 Matlab 不同,java 需要乘法运算符 (*) 并且不支持 pow 运算符 (^) - 您需要使用 Math.pow () 代替。

public class BoundaryValueProblem {
public static double f(double t, double x) {
return x + 0.09 * x * x + Math.cos(10 * t);
}

public static void findZero() {
double x4;
double x5 = .7;
double x6 = 1.0;
int n = 1;
double fx;
double f1;
double f2;
double Error;
f1 = f(1.0, 1.0);
f2 = f(1.0, 1.0);
System.out.println("Secant Method");
System.out.println("n: \t\t x1: \t\t x2: \t\t Error:");
while (Math.abs(x5 + x6 - 3.0) < 1e-5) {
x4 = x6 - f2 * ((x6 - x5) / (f2 - f1));
fx = f(1.0, 1.0);
x5 = x6;
x6 = x4;
f1 = f2;
f2 = fx;
Error = x5 + x6 - 3.0;
System.out.println(n + "\t\t" + x5 + "\t\t" + x6 + "\t\t" + Error);
n++;
}
System.out.println();
}

public void rkm(double x0, double t0, double h) {
double x1, x2, x3;
int i = 0;
double a1 = 0.5;
double a2 = 0.25;
double c0 = 2.0 / 3.0;
double c1 = 5.0 / 3.0;
double c2 = -4.0 / 3.0;
double b21 = -.25;
double b10 = .5;
double b20 = .5;
double stepsize = .025;
System.out.println("Runge-Kutta Method:");
System.out.println("i: \t\t h: \t\t t0: \t\t x0:");
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0);
for (i = 0; i < 40; i++) {
x1 = x0 + h * b10 * f(t0, x0);
x2 = x0 + h * (b20 * f(t0, x0) + b21 * f(t0 + a1 * h, x1));
x3 = x0 + h * (c0 * f(t0, x0) + c1 * f(t0 + a1 * h, x1) + c2 * f(t0 + a2 * h, x2));
t0 = t0 + stepsize;
x0 = x3;
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0);
}
System.out.println();
}

/**
* @param args
*/
public static void main(String[] args) {
BoundaryValueProblem FZ1 = new BoundaryValueProblem();
FZ1.findZero();
BoundaryValueProblem RKM1 = new BoundaryValueProblem();
RKM1.rkm(1.0, 0.0, 0.0);
}
}

关于java - 在 Java 中返回方程的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33882654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com