Chap 9: Root finding with newt, Nonlinear Eqns


amirvahid
07-09-2013, 11:42 AM
Dear All,

I am using the newt method in roots_multidim.h to solve for a set of simultaneous nonlinear equations. The code loops over several calculations for different systems, after about 9 calculations I receive the malloc error and the execution stops. I have included my code. I wonder if you could help to find the source of the error.

Error:
malloc: *** error for object 0x100107ff0: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

#include "nr3.h"
#include "quadrature.h"
#include "qrdcmp.h"
#include "ludcmp.h"
#include "svd.h"
#include "roots_multidim.h"




VecDoub vecfunc(VecDoub_I & x)
{


MatInt my_beta_mat_refined(Num, counter);

VecInt my_b_mat(counter);


VecDoub my_gi_T_Int(Num);

ifstream InFileBuffer2;
InFileBuffer2.open("buffer.txt");

string read_text;

for (int ik =0 ; ik < Num ; ik ++)
{
for (int il=0; il < counter ; il ++)
{
InFileBuffer2 >> my_beta_mat_refined[ik][il] ;
}
}
getline(InFileBuffer2,read_text);

for (int il=0; il < counter ; il ++)
{
InFileBuffer2 >> my_b_mat[il];
}

getline(InFileBuffer2,read_text);

for (int ik =0 ; ik < (Num) ; ik ++)
{
InFileBuffer2 >> my_gi_T_Int[ik];
}

getline(InFileBuffer2,read_text);

InFileBuffer2.close();



int nn = counter;
int NumEqn;
NumEqn = Num + counter ;
VecDoub xx(NumEqn);


for (int ii=0; ii< Num ; ii++)
{
xx[ii] = exp(x[ii]);
}
for (int ii= Num ; ii<NumEqn ; ii++)
{
xx[ii] = x[ii];
}



VecDoub y(NumEqn);


for (int ii=0; ii<NumEqn; ii ++)
{
y[ii] =0;
}


for (int kk = 0; kk < nn; kk++){
for (int jj = 0; jj < Num; jj++){
y[kk] += my_beta_mat_refined[jj][kk] * xx[jj];
}
y[kk] -= my_b_mat[kk];
}




double x_tot = 0;
for (int kk = 0; kk < Num ; kk++) x_tot += xx[kk];
for (int kk = 0; kk < Num ; kk++)
{
y [kk+nn] = my_gi_T_Int[kk]/R/T+ log ( xx[kk] / x_tot );
for (int ll =0 ; ll< nn ; ll++ )
{
y [kk+nn] += xx [ll + Num] * my_beta_mat_refined [kk][ll];
}
}


return y;
}


int main()
{
....
newt(x, check, vecfunc);


if (check) {
cout << "Check is true (convergence to a local minimum)." << endl;
cout << "Try another initial guess." <<endl;

for (int ii=0 ; ii< (Num) ; ii ++)
{
cout << "Enter the initial guess for exp(ni)" << endl;
cin >> x[ii];
}

for (int ii= (Num) ; ii < numx ; ii++)
{
cout << "Enter the initial guess for lambda_LM" << endl;
cin >> x[ii] ;

}
newt(x, check, vecfunc);

}
else {
cout << "Check is false (a \"normal\" return)." << endl;
}
cout << endl;

cout << scientific;
cout << "Solution from newt:" << endl;
for (int i = 0; i < Num; i++)
{
cout << " x[" << i << "] = " << exp(x[i]) << endl;
}
for (int i = Num; i < x.size(); i++)
{
cout << " x[" << i << "] = " << x[i] << endl;
}
cout << endl;

VecDoub y;
//y = vecfunc(x);
y = vecfunc(x); //, Beta_mat, b_mat, NumSpeci, num_atom_types, tK, gi_T_Int);
cout << "Value of the solution vector:" << endl;
for (int i = 0; i < y.size(); i++)
{
cout << " y[" << i << "] = " << y[i] << endl;
}
cout << endl;
....
...

return 0;
}

I wonder if you could elaborate on this issue. Thanks!

Amir