Last active
August 27, 2016 21:09
-
-
Save residentkrm/711b81fef5dab33c59a6cd1a6aeb4866 to your computer and use it in GitHub Desktop.
MMIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <iostream> | |
#include <math.h> | |
using namespace std; | |
double func(double x) | |
{ | |
return pow(x,3.0)-0.2*x*x+0.5*x-1; | |
} | |
int main() | |
{ | |
setlocale(LC_ALL,"Russian"); | |
const double eps = 0.000001; | |
double a,b,t,f1,f2,x; | |
cout <<"Введите отрезок где предположительно расположен корень уравнения!"; | |
cout <<"\nВведите a= ";cin >> a; | |
cout <<"Введите b= ";cin >> b; | |
do | |
{ | |
f1=func(a); | |
t=(a+b)/2.0; | |
f2=func(t); | |
if (f1*f2<=0) b=t; | |
else a=t; | |
} | |
while (fabs(b-a)>eps); | |
x=(a+b)/2.0; | |
f1=func(x); | |
if (fabs(f1)<=0.000001) | |
{ | |
cout <<"\nКорень уравнения с погрешностью ";cout<<fixed<<eps;cout<<", X= ";cout<<x; | |
cout <<"\nЗначение функции F(X)= "<< f1; | |
} | |
else cout <<"На данном отрезке уравнение корней не имеет!"; | |
getchar(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
const double epsilon = 1e-2; | |
double f(double x) | |
{ | |
return 4- exp(x) - 2*x^2; | |
} | |
int main() | |
{ | |
double a, b, c; | |
a = 0; | |
b = 2; | |
while (b - a > epsilon) | |
{ | |
c = (a + b) / 2; | |
if(f(b) * f(c) < 0) | |
a = c; | |
else | |
b = c; | |
} | |
cout << (a + b) / 2 << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
#include <locale.h> | |
using namespace std; | |
double f(double x) | |
{ | |
return pow(x,3)-0.2*pow(x,2)+0.2*x-1.2; | |
} | |
double findRoot(double a, double b, double e) | |
{ | |
while(fabs(f(b)) > e) | |
{ | |
a = b - ((b - a) * f(b))/(f(b) - f(a)); | |
b = a - ((a - b) * f(a))/(f(a) - f(b)); | |
} | |
return b; | |
} | |
int main() | |
{ | |
setlocale(LC_ALL,"Russian"); | |
double a, b, e; | |
a=0.5; b=1.5; e=0.0001; | |
cout<< "Корень уравнения x=" << findRoot(a, b, e) << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int main() | |
{ | |
//Порядок полинома: | |
const int N=3; | |
//Индексные переменные и число итераций: | |
int i,k,n=10; | |
//Аргумент, функция и производная: | |
double x,f,df; | |
//Массив коэффициентов полинома-функции: | |
double a[N+1]; | |
//Массив коэффициентов полинома-производной: | |
double b[N]; | |
//Ввод коэффициентов функции-полинома: | |
cout << "Function: "; | |
cin >> a[0]; | |
for(i=1; i < N+1 ;i++) | |
{ | |
cin >> a[i]; | |
//Вычисление коэффициентов для производной: | |
b[i-1]=i*a[i]; | |
} | |
//Начальное приближение: | |
cout << "Enter x0 = "; | |
cin >> x; | |
//Последовательные итерации: | |
for(k=1; k <= n; k++) | |
{ | |
f=a[0]; | |
df=0; | |
for(i=1; i < N+1; i++) | |
{ | |
f+=a[i]*pow(x,i); | |
df+=b[i-1]*pow(x,i-1); | |
} | |
x-=f/df; | |
} | |
cout << "x = " << x << endl; | |
cout << "f(" << x << ") = " << f << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream.h> | |
#include <math.h> | |
double f(double x); | |
void main() | |
{ | |
double tau=(sqrt(5.)-1.)/2.; | |
double eps=1e-7; | |
double a=-0.5 | |
double b=0.5; | |
double x1,x2; | |
while (b-a>eps) | |
{ | |
x1=b-tau*(b-a); | |
x2=a+tau*(b-a); | |
if (f(x1)>f(x2)) | |
b=x2; | |
else | |
a=x1; | |
} | |
cout<< x1; | |
} | |
double f(double x) | |
{ | |
return 1/3*x*x*x+(1+x)*(ln(1+x)+1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Программа для поиска минимума функции методом координатного спуска | |
#include<iostream> | |
#include<vector> | |
#include<math.h> | |
//максимально возможное число итераций | |
#define NUMBER_OF_ITERATIONS (1000) | |
//eps для критерия останова | |
#define EPS (1e-5) | |
//критерий останова | |
#define OSTANOV (2) | |
using namespace std; | |
vector<double> goldensectionoptimize(double(*f)(vector<double>),vector<double> x,int p,double a, double b, int n); | |
double f(vector<double> x); | |
vector<double> CoordinateDescent(double(*f)(vector<double>),int N,vector<double> x0); | |
double f(vector<double> x) | |
{ | |
//int l=x.size(); | |
return (x[0]-1)*(x[0]-1)+(x[1]-1)*(x[1]-1)-x[0]*x[1]; | |
} | |
vector<double> CoordinateDescent(double(*f)(vector<double>),int N,vector<double> x0,int&Iterations) | |
//minimizes N-dimensional function f; x0 - start point | |
{ | |
vector <double> tmp,cur_x=x0,old; | |
double s; | |
int i,j; | |
for (Iterations=0;Iterations<NUMBER_OF_ITERATIONS;Iterations++) | |
{ | |
old=cur_x; | |
for(i=0;i<N;i++) | |
{ | |
//ищем минимум вдоль i-й координаты | |
cur_x=goldensectionoptimize(f,cur_x,i,-10,10,100); | |
} | |
//выбор критерия останова | |
if(OSTANOV==1) | |
{ | |
//условие останова 1 | |
s=0; | |
for(j=0;j<old.size();j++) | |
s+=(old[j]-cur_x[j])*(old[j]-cur_x[j]); | |
s=sqrt(s); | |
if (s<EPS) | |
return cur_x; | |
} | |
if(OSTANOV==2) | |
{ | |
//условие останова 2 | |
s=fabs(f(cur_x)-f(old)); | |
if (s<EPS) | |
return cur_x; | |
} | |
} | |
return cur_x; | |
} | |
int main() | |
{ | |
vector<double> x; | |
x.push_back(10); | |
x.push_back(10); | |
int i,Iteration; | |
vector<double> ans=CoordinateDescent(&f,2,x,Iteration); | |
cout<<"Value: "<<f(ans)<<endl; | |
cout<<"Point: "; | |
for(i=0;i<ans.size();i++) | |
cout<<ans[i]<<' '; | |
cout<<endl<<"Number of iterations:"<<Iteration<<endl; | |
return 0; | |
} | |
vector<double> goldensectionoptimize(double (*f) (vector<double>),vector<double> x,int p,double a, double b, int n) | |
{ | |
vector<double> tmp=x; | |
int i; | |
double s1; | |
double s2; | |
double u1; | |
double u2; | |
double fu1; | |
double fu2; | |
s1 = (3-sqrt(double(5)))/2; | |
s2 = (sqrt(double(5))-1)/2; | |
u1 = a+s1*(b-a); | |
u2 = a+s2*(b-a); | |
tmp[p]=u1; | |
i=tmp.size(); | |
fu1 = (*f)(tmp); | |
tmp[p]=u2; | |
fu2 = (*f)(tmp); | |
for(i = 1; i <= n; i++) | |
{ | |
if( fu1<=fu2 ) | |
{ | |
b = u2; | |
u2 = u1; | |
fu2 = fu1; | |
u1 = a+s1*(b-a); | |
tmp[p]=u1; | |
fu1 = (*f)(tmp); | |
} | |
else | |
{ | |
a = u1; | |
u1 = u2; | |
fu1 = fu2; | |
u2 = a+s2*(b-a); | |
tmp[p]=u2; | |
fu2 = (*f)(tmp); | |
} | |
} | |
tmp[p]=u1; | |
fu1 = (*f)(tmp); | |
tmp[p]=u2; | |
fu2 = (*f)(tmp); | |
if (fu1<fu2) | |
tmp[p]=u1; | |
else | |
tmp[p]=u2; | |
return tmp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/anonymous/969b5af7e7475f3dbda7df5949fff998