求助!!!使用 Numerical recipes in C++求解非线性方程组
Golbally Convergent  for Nonlinear Equation
调用的newt函数。没有学过指针函数什么的,照葫芦画瓢写的很烂。编译提示错误是:
newt.cpp(22): warning #159: declaration is incompatible with previous "fmin" (declared at line 342 of "/usr/include/bits/mathcalls.h"
float fmin(float x[]);
newt.cpp(38): internal error: assertion failed at: "shared/edgcpfe/lower_c99.c", line 2870
f=fmin(x);
^
code如下,其中void *vecfunc(int n,float x[],float fvec[])是很复杂的非线性方程组,这里为了简洁易见,简单列了几个方程,可能解不出来。
Numerical recipes in C++书里格式是 void (*vecfunc)(int n,float x[],float fvec[]),但是调用的时候老出错,就把括号去了。
整个调用的函数包已经打包在附件,急用,请大神帮忙。
//////////////////////////////////////////////////
#include <math.h>
#define NRANSI
#include "nrutil.h"
#include "nrutil.cpp"
#include "newt.cpp"
#include "fdjac.cpp"
#include "fmin.cpp"
#include "lnsrch.cpp"
#include "lubksb.cpp"
#include "ludcmp.cpp"
void *vecfunc(int n, float x[], float fvec[]);
int main()
{
int n;
n=5;
float x[n];
float *fvec;
int *check;
check=ivector(1,n);
fvec=vector(1,n);
newt(x, n, check,&nbsp;&nbsp;vecfunc);
free_ivector(check,1,n);
free_vector(fvec,1,n);
return 1;
}
void *vecfunc(int n,float x[],float fvec[])
{
//float x[5];
//float fvec[5];
fvec[1]=x[1]*x[2]+x[5]-1.0;
fvec[2]=x[1]+x[2]-x[3];
fvec[3]=x[1]*x[4]+x[2]+x[3]-10.0;&nbsp; &nbsp; &nbsp; &nbsp;
fvec[4]=x[1]+x[2]+x[3]-1.0;
fvec[5]=x[4]+x[5]-1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
}
这些代码虽然文件后缀是cpp, 但是和C++一毛钱关系都没有,是彻头彻尾的C语言写的,连C++里最基本的STL模板库都不用。如果你是新手刚开始学习数值计算,还是建议使用C++语言,无论是前期编程还是后期维护都比C语言方便的多。关于你的代码, 看起来像是用牛顿迭代法求解非线性方程组的。x应该是位置向量,在迭代前应该赋予初值的。你的代码定义了数组x后没初始化就直接用了。可能有的编译器会自动把x初始化为0, 但这不是个好的变成习惯。还有,在C/C++语言里,数组的索引是从0开始的,你定义的数组大小为5,所以在fvec里只能使用0~4的下标,所以x[5]这种写法是错误的,内存越界了。
c不是很熟,就用matlab吧,fsolve函数可以解方程组.很方便.
谢谢。已经改了但是还是有问题。非常感谢。要好好学习一下编程了
,