我正在编写一个程序来计算lambda的值,除非它被卡在do循环中。它似乎没有更新n值,因为我希望通过设置nMax = 100,如果其他情况(eps> = e_allow)从未发生过,它至少会快速完成。任何人都可以发现无限循环的来源吗?
非常感谢你的帮助。我的节目是:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Solution to Linear Dispersion Relationship for a Horizontal Bottom
double f ( double, double, double, double);
double df (double, double, double);
int main(void) {
float pi = 3.1415927;
double g = 9.81; //Acceleration due to gravity
double omega; //Angular frequency of wave
double kn, dk; //Wave numbers
double e_allow, eps; //Tolerance used to find difference between kNew and kOld
int nMax, n; // Setting up counter for iterations
double lambda; //Value we need to solve for
double h, T; //Depth and Period. User needs to input these values.
double fn, dfn; //Declaring functions describing linear dispersion
printf("Enter the value of the depth (m).\n");
scanf("%lf", &h);
printf("\nEnter the value for the period (s).\n");
scanf("%lf", &T);
/*
Assuming shallow water conditions. Therefore, tanh(k*d) --> kd. kOld is calculated using
the relationship omega^2 = kold^2/(g*d)
*/
omega = (2.0 * pi)/T;
printf("Angular velocity = %.5f\n", omega);
kn = sqrt((omega * omega)/(g * h));
printf("Wave number kn = %.5f\n", kn);
e_allow = 1.0e-5;
n = 0;
nMax = 100;
//Following the Newton Raphson Method
do {
fn = f(kn, omega, g, h);
dfn = df(kn, g, h);
dk = -(fn/dfn);
kn = kn + dk;
eps = fabs(dk/kn);
n = n + 1;
} while (eps >= e_allow || n == nMax);
//Calculating value of lambda using final kn value.
lambda = (2.0 * pi)/kn;
//printf("\nfn = %.6f, dfn = %.6f, dx = %.6f, eps = %.6f\n", fn, dfn, dk, eps);
printf("\nkn = %.5lf, Wavelength = %.5lf\n", kn, lambda); */
//printf("fn = %.6lf, dfn = %.6lf, eps = %.6lf, dk = %.6lf, kn = %.6lf\n", fn, dfn, eps, dk, kn);
return 0;
}
double f (double kn, double omega, double g, double h) {
return ((omega*omega) - (g * kn * tanh(kn * h)));
}
double df (double kn, double g, double h) {
return ((( g * kn * h )/( 1 + ( h * h * kn * kn ))) - g * tanh( kn * h));
}
答案 0 :(得分:0)
您的衍生产品部分错误。不知怎的,你插入了arctan函数的派生公式。
tanh(x)的导数是1 / square(cosh(x))= 1平方(tanh(x))。
并且跟踪标志,减号在第一个任期内不会丢失。单独使用正确的符号可以使代码工作,而无需更正其余的导数。