-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteepest.cpp
More file actions
92 lines (84 loc) · 1.98 KB
/
steepest.cpp
File metadata and controls
92 lines (84 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Cauchy's Steepest Descent Method
//Sudharsan Neelamegam
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
//Calculate the Gradient as soon as question is given and put gradient in terms of x1 and x2 in two functions below
float f1(float x1,float x2)
{ float r;
r=(1+4*x1+2*x2); //Gradient First term
return r;
}
float f2(float x1,float x2)
{ float r1;
r1=(-1+2*x1+2*x2); //Gradient Second term
return r1;
}
float gf(float p,float q)
{
float h;
h=p-q+2*pow(p,2)+(2*p*q)+pow(q,2); // Function given in the question
return h;
}
float f1();
float f2();
float gf();
float univariate(float x0[2],float s[2])
{ float k;
if (gf(x0[0]+0.01*s[0],x0[1]+0.01*s[1])<gf(x0[0],x0[1]))
{float t1,t2;
while (k<100)
{
t1=gf(x0[0]+k*s[0],x0[1]+k*s[1]);
t2=gf(x0[0]+(k+0.001)*s[0],x0[1]+(k+0.001)*s[1]);
if(t1<t2)
{break;}
else
{k=k+0.001;}
}
}
else
{float k=0;
while (k>-100)
{ float t1,t2;
t1=gf(x0[0]+k*s[0],x0[1]+k*s[1]);
t2=gf(x0[0]+(k-0.001)*s[0],x0[1]+(k-0.001)*s[1]);
if(t1<t2)
{break;}
else
{k=k-0.001;}
}
}
return k;
}
float univariate();
int main()
{ float X1[2]={0,0},S[2]; //Trial Point X1
float gf1[2];
int i=0;
gf1[0]=f1(X1[0],X1[1]);
gf1[1]=f2(X1[0],X1[1]);
S[0]=-1*gf1[0];
S[1]=-1*gf1[1];
for(int j=0;j<10;j++) //Enter n value for n iterations
{ cout<<"Iteration No."<<(i+1)<<endl;
cout<<"Search Direc is : ("<<S[0]<<","<<S[1]<<")"<<endl;
float k1=univariate(X1,S);
float z=k1;
cout<<"K value is: "<<z<<endl;
X1[0]=X1[0]+z*S[0];
X1[1]=X1[1]+z*S[1];
cout<<"X2:"<<X1[0]<<","<<X1[1]<<endl;
float e,r;
e=f1(X1[0],X1[1]);
r=f2(X1[0],X1[1]);
S[0]=-1*e;
S[1]=-1*r;
i++;
if(e==0 && r==e)
{
break;
}
}
return (0);
}