Solving nonlinear equations by gradient descent method

Source: Internet
Author: User
#include <math.h> #include <iostream> using namespace std;
void Newton (int n,double x[],double y[],double eps);
DOUBLE fn (int n,double x[],double y[]);
Const double PI = 3.14159265358979323846;
	int main () {int i,n=3;
	Double y[3],x[3]={0.5,0.5,0.5};
	Double eps=1.e-08;	
	Newton (N,X,Y,EPS);
	for (i=0;i<n;i++) {cout<<i<< "" <<x[i]<< "" <<y[i]<<endl;
} return 0;
	} double fn (int n,double x[],double y[]) {double s2=0.0;
	Y[0]=3.0*x[0]-cos (x[1]*x[2])-1.5;
	Y[1]=4*x[0]*x[0]-625*x[1]*x[1]+2*x[2]-1;
	Y[2]=exp (-x[0]*x[1]) +20*x[2]+ (10*pi-3)/3.0;
	for (int i=0;i<n;i++) {s2+=y[i]*y[i];
} return s2;
	} void Newton (int n,double x[],double y[],double eps) {double s[3],s0,s1,s2,t,alpha,h=1.e-05;
		while (1) {S2=FN (n,x,y);
		S0=S2;
		if (s0<eps) break;
		s1=0.0;
			for (int i=0;i<n;i++) {t=x[i];
			x[i]= (1.0+h) *t;
			S2=FN (N,x,y);
			s[i]= (S2-S0)/(H*T)///???
			S1+=s[i]*s[i];
		x[i]=t;
		} alpha=s0/s1; for (int i=0;i<n;i++) {x[i]-=alpha*s[i];
		cout<<i<< "" <<x[i];
	} cout<<endl; }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.