Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //경사 하강법
- #include<bits/stdc++.h>
- using namespace std;
- double f_prime(double x) {//미분 계수 구해주기
- return -6 * pow(x, 2) + 54 * x - 84;//도함수 이용 -> -6x^2+54x-84
- }
- double new_x = 0;
- double old_x = 1e9;
- double diff = 0.001;
- double control = 0.01;//조절인자
- int main() {
- while (abs(new_x - old_x) > diff) {//x값의 변화가 거의 없다(거의 수렴했다.) -> 종료
- old_x = new_x;
- new_x = new_x - control * f_prime(new_x);//x(i+1)=x(i)-조절인자*기울기
- cout << new_x << "\n";//x값 변화 살피기
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement