this solution code is jxw for his classmates including two ways. one is GAOSI
the other is YAKEBI
//souce code 1.cpp#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<memory.h>#define MAX_ROW 8void print(double solution[],int num)
{
for( int i=0;i<num;i++)
{
printf("X%d=%lf\n",i+1,solution[i]);
}
printf("\n");
}double caculate(double * OneRow,int NumOfCol,double * solution,int iUnknown)
{
double sum=OneRow[NumOfCol-1];
for(int i=0;i<NumOfCol-1;i++)
{
if(i==iUnknown)
{
continue;
}
sum-=OneRow[i]*solution[i];
}
sum=sum/OneRow[iUnknown];
return sum;
}
double GetPrecision(double *OneRow,int NumOfCol,double * solution)
{
double sum=OneRow[NumOfCol-1]; for(int i=0;i<NumOfCol-1;i++)
{
sum-=OneRow[i]*solution[i];
}
if(sum<0.0) sum=-sum; return sum;
}//#define GAOSI
#define YAKEBI
#define PRICISION 0.0005int main(int argc,char** argv)
{
FILE * fp;
double array[MAX_ROW][MAX_ROW+1];
double xn[MAX_ROW]={0.0,0.0};
int row;
double Precision=0.0; int counter=0; if(argc<2)
return 0; fp=fopen(argv[1],"r"); fscanf(fp,"%d",&row);
if(row>8)
{
printf("the param input error\n");
return 0;
}
printf("\n the array input is:\n"); for(int i=0;i<row;i++)
{
for(int j=0;j<row+1;j++)
{
fscanf(fp,"%lf",&array[i][j]);
printf("%lf",array[i][j]);
}
printf("\n");
}
getchar();
print(xn,row);#ifdef GAOSI // --------------way 1
double xn1[MAX_ROW];
do{
Precision=0.0;
for(int i=0;i<row;i++)
{
xn1[i]=caculate(array[i],row+1,xn,i);
double tempPrecision=GetPrecision(array[i],row+1,xn);
if(tempPrecision>Precision)
{
Precision=tempPrecision;
}
}
memcpy(xn,xn1,MAX_ROW*sizeof(double));// update new solution
print(xn,row); }while(counter++<1000&&Precision>PRICISION);#endif#ifdef YAKEBI
do{
Precision=0.0;
for(int i=0;i<row;i++)
{
xn[i]=caculate(array[i],row+1,xn,i);
double tempPrecision=GetPrecision(array[(i+2)%3],row+1,xn);// for precision can
not use itself .else it alway 0
if(tempPrecision<0.0)
printf("Error Happened");
if(tempPrecision>Precision)
{
Precision=tempPrecision;
}
// printf("\n%d,%f\n",counter,Precision);
}
print(xn,row);
}while(counter++<1000&&Precision>PRICISION);#endif
return 0;
}// input data-----file param1.txt
3
5.0 2.0 1.0 -12.0
-1.0 4.0 2.0 20.0
2.0 -3.0 10.0 3.0
//解非线性方程,包括迭代法,弦截法,和二分法搜索
#include<stdio.h>
#include<stdlib.h>
#include<math.h>#define F(x) (x)*(x)*(x)+4*(x)*(x)-10//change the option here. the first way is two divide if you want to compile in
this way.remove "//"//#define TWO_DIVIDE
//#define DIE_DAI
#define XANJIE#ifdef TWO_DIVIDEint main(void)
{
double f;
double x;
double dlHigh=2.0; //Xhide
double dlLow=1.0; //Xlow
double dlValue; //Function value
// it easily can see that F(1)<0, F2)>0 and F'(x)[0-1]>0. the solution must
be existed.
double dlTemp; //middle Point
do{
dlTemp= (dlHigh+dlLow)/2.0; // Get middle point of high and low
dlValue=F(dlTemp); // Caculate the value of the mid point;
if(dlValue>0) // if F(mid)>0,the solution is between low
and mid
dlHigh=dlTemp;
else // else the solution is between mid and Hi
gh
dlLow=dlTemp;
printf("Verify: F(%lf)=%lf\n",dlTemp,dlValue);
}while(dlValue>0.05||dlValue<-0.05);//if the value of fution F(x) !=0.(just ne
ar to 0 ).go on! printf("The solution is %lf\n.the fuction return is %lf\n",dlTemp,F(dlTemp));/
/ print the result to screen return 0;
}
#endif#ifdef DIE_DAI#define D(x) sqrt((10.0-(x)*(x)*(x))/4.0)
// there may be existed a question .can you prove it?
// sqrt---KaiFang
#define ABS(a) ((a)<0.0? -(a):(a))
int main()
{
double dlValue;
double dlValueBefore=1.3;// start from 1.3 default
printf("please Guess a solution:");
scanf("%lf",&dlValueBefore);
int count=0;
do
{
dlValue=D(dlValueBefore); //Caculate the New Value printf("%f is Test...\n",dlValue);
if(ABS(dlValue-dlValueBefore)<0.0005) //how near the new value and t
he old value are!.if near enough break out this loop
break; dlValueBefore=dlValue; //take the the new value to dl
ValueBefore }while(count++<1000); //'count' is a counter.for ther
e is a possibilty .you may cannot find the solution for ever.to avoid a
//invalid dead loop.Set a count
er here. if counter>1000) exit loop if(count==1001)
printf("Function D(x) cannot work\n");
else
printf("The solution is %lf\n.result=%lf\n",dlValue,F(dlValue)); return 0;
}
#endififdef XANJIE //(y-y0)=k(x-x0) let y=0 then you can get a midx on X zhou. the from(midX,0)
draw a vetical line. it will cross F(midX).
// if F(midX)>0 let (x1,y1)=(midX,midY) else let (x0,y0) = (midX,midY)
// y-y0=k*(x-x0);
// | |/
// | /|
// | / |
// | / /
// | / /
//--------------------------
// | / /
// | / _
// | _ /
// | /
//x=x0-(y0)/(y1-y0) void main()
{
double x0,y0;
double x1,y1;
double midX=0.0;
double midY=0.0;
x0=1.0; //(y-y0)=k(x-x0) let y=0 then you can get a midx on X zhou
. the from(midX,0) draw a vetical line. it will cross F(midX).
x1=2.0; // if F(midX)>0 let (x1,y1)=(midX,midY) else let (x0,y0) =
(midX,midY)
y0=F(x0);
y1=F(x1); do{
midX=x0-y0*(x1-x0)/(y1-y0);// get the point which y-y0=k(x-x0); cross X dire
ction
midY=F(midX); //caculate F(x0);
if(midY>0.0){ //update upper point
x1=midX;
y1=midY;
}
else if(midY<0.0) //update low point
{
x0=midX;
y0=midY;
}
else
break; printf("midX:%lf, midY:%lf\n",midX,midY);
}while(x1-x0>0.005); printf("The solution is %lf,The precision is %lf\n",midX,midY);
}
#endif//////
公主是谁?
the other is YAKEBI
//souce code 1.cpp#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<memory.h>#define MAX_ROW 8void print(double solution[],int num)
{
for( int i=0;i<num;i++)
{
printf("X%d=%lf\n",i+1,solution[i]);
}
printf("\n");
}double caculate(double * OneRow,int NumOfCol,double * solution,int iUnknown)
{
double sum=OneRow[NumOfCol-1];
for(int i=0;i<NumOfCol-1;i++)
{
if(i==iUnknown)
{
continue;
}
sum-=OneRow[i]*solution[i];
}
sum=sum/OneRow[iUnknown];
return sum;
}
double GetPrecision(double *OneRow,int NumOfCol,double * solution)
{
double sum=OneRow[NumOfCol-1]; for(int i=0;i<NumOfCol-1;i++)
{
sum-=OneRow[i]*solution[i];
}
if(sum<0.0) sum=-sum; return sum;
}//#define GAOSI
#define YAKEBI
#define PRICISION 0.0005int main(int argc,char** argv)
{
FILE * fp;
double array[MAX_ROW][MAX_ROW+1];
double xn[MAX_ROW]={0.0,0.0};
int row;
double Precision=0.0; int counter=0; if(argc<2)
return 0; fp=fopen(argv[1],"r"); fscanf(fp,"%d",&row);
if(row>8)
{
printf("the param input error\n");
return 0;
}
printf("\n the array input is:\n"); for(int i=0;i<row;i++)
{
for(int j=0;j<row+1;j++)
{
fscanf(fp,"%lf",&array[i][j]);
printf("%lf",array[i][j]);
}
printf("\n");
}
getchar();
print(xn,row);#ifdef GAOSI // --------------way 1
double xn1[MAX_ROW];
do{
Precision=0.0;
for(int i=0;i<row;i++)
{
xn1[i]=caculate(array[i],row+1,xn,i);
double tempPrecision=GetPrecision(array[i],row+1,xn);
if(tempPrecision>Precision)
{
Precision=tempPrecision;
}
}
memcpy(xn,xn1,MAX_ROW*sizeof(double));// update new solution
print(xn,row); }while(counter++<1000&&Precision>PRICISION);#endif#ifdef YAKEBI
do{
Precision=0.0;
for(int i=0;i<row;i++)
{
xn[i]=caculate(array[i],row+1,xn,i);
double tempPrecision=GetPrecision(array[(i+2)%3],row+1,xn);// for precision can
not use itself .else it alway 0
if(tempPrecision<0.0)
printf("Error Happened");
if(tempPrecision>Precision)
{
Precision=tempPrecision;
}
// printf("\n%d,%f\n",counter,Precision);
}
print(xn,row);
}while(counter++<1000&&Precision>PRICISION);#endif
return 0;
}// input data-----file param1.txt
3
5.0 2.0 1.0 -12.0
-1.0 4.0 2.0 20.0
2.0 -3.0 10.0 3.0
//解非线性方程,包括迭代法,弦截法,和二分法搜索
#include<stdio.h>
#include<stdlib.h>
#include<math.h>#define F(x) (x)*(x)*(x)+4*(x)*(x)-10//change the option here. the first way is two divide if you want to compile in
this way.remove "//"//#define TWO_DIVIDE
//#define DIE_DAI
#define XANJIE#ifdef TWO_DIVIDEint main(void)
{
double f;
double x;
double dlHigh=2.0; //Xhide
double dlLow=1.0; //Xlow
double dlValue; //Function value
// it easily can see that F(1)<0, F2)>0 and F'(x)[0-1]>0. the solution must
be existed.
double dlTemp; //middle Point
do{
dlTemp= (dlHigh+dlLow)/2.0; // Get middle point of high and low
dlValue=F(dlTemp); // Caculate the value of the mid point;
if(dlValue>0) // if F(mid)>0,the solution is between low
and mid
dlHigh=dlTemp;
else // else the solution is between mid and Hi
gh
dlLow=dlTemp;
printf("Verify: F(%lf)=%lf\n",dlTemp,dlValue);
}while(dlValue>0.05||dlValue<-0.05);//if the value of fution F(x) !=0.(just ne
ar to 0 ).go on! printf("The solution is %lf\n.the fuction return is %lf\n",dlTemp,F(dlTemp));/
/ print the result to screen return 0;
}
#endif#ifdef DIE_DAI#define D(x) sqrt((10.0-(x)*(x)*(x))/4.0)
// there may be existed a question .can you prove it?
// sqrt---KaiFang
#define ABS(a) ((a)<0.0? -(a):(a))
int main()
{
double dlValue;
double dlValueBefore=1.3;// start from 1.3 default
printf("please Guess a solution:");
scanf("%lf",&dlValueBefore);
int count=0;
do
{
dlValue=D(dlValueBefore); //Caculate the New Value printf("%f is Test...\n",dlValue);
if(ABS(dlValue-dlValueBefore)<0.0005) //how near the new value and t
he old value are!.if near enough break out this loop
break; dlValueBefore=dlValue; //take the the new value to dl
ValueBefore }while(count++<1000); //'count' is a counter.for ther
e is a possibilty .you may cannot find the solution for ever.to avoid a
//invalid dead loop.Set a count
er here. if counter>1000) exit loop if(count==1001)
printf("Function D(x) cannot work\n");
else
printf("The solution is %lf\n.result=%lf\n",dlValue,F(dlValue)); return 0;
}
#endififdef XANJIE //(y-y0)=k(x-x0) let y=0 then you can get a midx on X zhou. the from(midX,0)
draw a vetical line. it will cross F(midX).
// if F(midX)>0 let (x1,y1)=(midX,midY) else let (x0,y0) = (midX,midY)
// y-y0=k*(x-x0);
// | |/
// | /|
// | / |
// | / /
// | / /
//--------------------------
// | / /
// | / _
// | _ /
// | /
//x=x0-(y0)/(y1-y0) void main()
{
double x0,y0;
double x1,y1;
double midX=0.0;
double midY=0.0;
x0=1.0; //(y-y0)=k(x-x0) let y=0 then you can get a midx on X zhou
. the from(midX,0) draw a vetical line. it will cross F(midX).
x1=2.0; // if F(midX)>0 let (x1,y1)=(midX,midY) else let (x0,y0) =
(midX,midY)
y0=F(x0);
y1=F(x1); do{
midX=x0-y0*(x1-x0)/(y1-y0);// get the point which y-y0=k(x-x0); cross X dire
ction
midY=F(midX); //caculate F(x0);
if(midY>0.0){ //update upper point
x1=midX;
y1=midY;
}
else if(midY<0.0) //update low point
{
x0=midX;
y0=midY;
}
else
break; printf("midX:%lf, midY:%lf\n",midX,midY);
}while(x1-x0>0.005); printf("The solution is %lf,The precision is %lf\n",midX,midY);
}
#endif//////
公主是谁?
欢迎大家,为人类造福:)呵呵