Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы 1-5 / LABA1 / LABA1
.CPP#include <math.h>
#include <iostream.h>
#include <conio.h>
double f(double x)
{
return 2*x*x+3*exp(-x);
}
void Swann1(double x1, double* a, double* b)
{
float h, x2;
h=0.001*fabs(x1);
x2=x1+h;
if (f(x2)>f(x1))
{
h=-h;
x2=x1+h;
}
do
{
x1=x2;
h=2*h;
x2=x1+h;
}
while (f(x2)<f(x1));
*a=x1-0.5*h;
*b=x2;
if (*a>*b)
{
*a=*b+*a;
*b=*a-*b;
*a=*a-*b;
}
}
double MZS1(double* a, double* b, double E)
{
double x1, x2;
int k=1;
x1=*a+0.382*fabs(*b-*a);
x2=*a+0.618*fabs(*b-*a);
while (fabs(*b-*a)>E)
{
if(f(x1)<f(x2))
{
*b=x2;
x2=x1;
x1=*a+0.382*fabs(*b-*a);
}
else
{
*a=x1;
x1=x2;
x2=*a+0.618*fabs(*b-*a);
}
k++;
}
cout<<"Kolli4estvo povtoreniy cikla v MZS: k="<<k<<'\n';
return 0.5*(*a+*b);
}
void main()
{
clrscr();
double a, b, x1, Xmin, E;
cout<<" Laboratornaya rabota 1"<<'\n';
cout<<" Metod odnomernogo poiska minimuma unimodal'nyih funkciy"<<'\n';
cout<<"Vipolnila: studentka gr.2375"<<'\n';
cout<<" Nikitina T."<<'\n';
cout<<" Variant 1"<<'\n';
cout<<" Metod Swanna 1 - Metod Zolotogo Se4eniya 1"<<'\n';
cout<<" "<<'\n';
cout<<"Vvedite na4alnuyu to4ku lokalizacii minimuma: Xo=";
cin>>x1;
Swann1(x1, &a, &b);
cout<<"Interval, polu4ennyiy metodom Swenna: ["<<a<<";"<<b<<"]"<<'\n';
cout<<"Vvedite pogrewnost' approksimacii: E=";
cin>>E;
Xmin=MZS1(&a, &b, E);
cout<<"Naydenyiy minimum: x*="<<Xmin;
getch();
}