/* Driver for routine fsolve: for 2 dimentional equation*/
#include
#include
#include
#define N 2
#define NP 30
void func(double x[], double f[]){
f[0]=-(x[0]*x[0]+x[1]*x[1]-2.0);
f[1]=exp(x[0]-1.0)+x[1]*x[1]*x[1]-2.0;
}
int main(){
double x[N], x0[N], f[N];
double x1[NP], x2[NP], f1[NP*NP], f2[NP*NP];
int i,j,status;
class CPlot plot;
linspace(x1, 0, 2);
linspace(x2, 0, 2);
for(i=0; i<30; i++) {
for(j=0; j<30; j++){
f1[30*i+j] = -(x1[i]*x1[i]+x2[j]*x2[j] - 2.0);
f2[30*i+j] = exp(x1[i]-1.0) + x2[j]*x2[j]*x2[j]-2.0;
}
}
plot.SetDimension(3);
plot.Label(PLOT_AXIS_X, "x");
plot.Label(PLOT_AXIS_Y, "y");
plot.Label(PLOT_AXIS_Z, "z");
plot.Title("f1 = 2.0 - x*x -y*y, f2 = exp(x-1.0)+y*y*y-2.0");
plot.Data3D(x1, x2, f1);
plot.Data3D(x1, x2, f2);
plot.Legend("f1", 0);
plot.Legend("f2", 1);
plot.TicsLevel(0);
plot.Plotting();
x0[0]=2.0;
x0[1]=0.5;
status=fsolve(x, func, x0);
func(x,f);
printf("status = %d\n",status);
if (status==-1)
fprintf(stderr, "Convergence problems.\n");
printf("%7s %3s %12s\n","Index","x","f");
for (i=0;i<2;i++)
printf("%5d %12.6f %12.6f\n",i,x[i],f[i]);
}
status = 0
Index x f
0 1.000016 -0.000020
1 0.999994 -0.000002