// // Finding roots for a system of two nonlinear equations // f(x,y) = 0 // g(x,y) = 0 // Method: call Newton method (starting search from x1,y1) // #include #include #include using namespace std; /* * Functions f(x,y), g(x,y) and their derivatives at (x,y) */ void fg(double x, double y, double& f, double& g, double& fx, double& fy, double& gx, double& gy) { f = y*y*(1.0-x)-x*x*x; g = x*x + y*y -1.0; fx = (-1.0)*(y*y + 3.0*x*x); fy = 2.0*y*(1.0-x); gx = 2.0*x; gy = 2.0*y; } void newton2(double& x1, double& y1, double eps, int& i) /* ==================================================================== Program to find solutions for nonlinear equations f(x,y) = 0 g(x,y) = 0 using Newton's method written by: Alex Godunov last revision: 29 January 2008 -------------------------------------------------------------------- input ... f(x,y), g(x,y) and their derivatives x1, y1, - initial points eps - desired uncertainity of the root output ... x1, y1 - roots Max number of allowed iterations is 99 ==================================================================== */ { double f1, g1, fx, fy, gx, gy; double del, x2, y2, dx, dy; int iter = 99; cout.setf(ios::fixed | ios::showpoint); cout.precision(5); i = 0; do { i = i + 1; fg(x1, y1, f1, g1, fx, fy, gx, gy); del = fx*gy - fy*gx; dx=(fy*g1-f1*gy)/del; dy=(f1*gx-fx*g1)/del; x2=x1+dx; y2=y1+dy; cout <= iter) break; } while (fabs(dx) >= eps && fabs(dy) >=eps); i = i+1; } int main() { double x1, y1, eps; double f1, g1, fx, fy, gx, gy; int i; cout.setf(ios::fixed | ios::showpoint); cout.precision(5); x1 = 2.0; y1 = 4.0; eps = 1.0e-6; cout << "Newtons method for two coupled nonlinear equations" <