Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа №54 / polDlg1
.cpp// polDlg.cpp : implementation file
//
#include "stdafx.h"
#include "pol.h"
#include "polDlg.h"
#include"math.h"
#include"iostream.h"
#include"conio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPolDlg dialog
CPolDlg::CPolDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPolDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPolDlg)
m_com2 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPolDlg)
DDX_Control(pDX, IDC_EDIT5, m_ed5);
DDX_Control(pDX, IDC_EDIT4, m_ed4);
DDX_Control(pDX, IDC_EDIT3, m_ed3);
DDX_Control(pDX, IDC_EDIT2, m_ed2);
DDX_Control(pDX, IDC_EDIT1, m_ed1);
DDX_Control(pDX, IDC_COMBO1, m_com);
DDX_Control(pDX, IDC_BUTTON1, m_but);
DDX_CBString(pDX, IDC_COMBO1, m_com2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPolDlg, CDialog)
//{{AFX_MSG_MAP(CPolDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPolDlg message handlers
BOOL CPolDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CPolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CPolDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPolDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPolDlg::OnButton1()
{
class Cvector
{
public:
double x[100];
double x0[100];
double p[100];
double a;
int n,c;
double otv0[100];
double otv[100] ;
double d[100] ; //задаем вектор апроксимирующего минимума
double z,w,r;
void df(double *tochka, double *otvet)
{
double* tochka2 = new double[n];
for (int i=0; i<n; i++)
tochka2[i]=tochka[i];
double h=0.001; // приращение
for(i=0; i<n; i++)
{
tochka[i]=tochka[i]+h;
tochka2[i]=tochka2[i]-h;
otvet[i]=(f(tochka,c) - f(tochka2,c))/(2*h);//вычисляем производную
tochka[i]= tochka[i] - h;
tochka2[i]=tochka2[i] + h;
}
}
double diffp(double *tochka, double *p)
{
double* res = new double[n], otv=0;
df(tochka, res);
for(int i=0;i<n; i++)
otv=otv+res[i]*p[i];
return otv;
}
double gra(double y[], double z[])
{
double ar[2] = {0,0};
double sum = 0;
ar[0] = 400*y[0]*y[0]*y[0] - 400*y[1]*y[0] + 2*y[0] - 2 ;
ar[1] = 200*y[1] - 200*y[0]*y[0] ;
for (int i=0; i<n; i++)
sum = sum + ar[i]*z[i];
return sum;
}
double norma(double u[])
{
double su = 0;
for (int i=0; i<n; i++)
{
su = su + u[i]*u[i];
}
return fabs(sqrt(su));
}
double OtrLength(double *St, double *Fin)
{
double res=0;
for (int i=0; i<2; i++)
res = res + (Fin[i]-St[i])*(Fin[i]-St[i]);
if((Fin[0]-St[0])>0)
return sqrt(res);
else
return -sqrt(res);
}
double f(double x[],int t)
{
double otv;
switch (t)
{
case 0:
{
return pow((x[0] - 1),2) + pow((x[1] - 3),2) + 4*pow((x[2] + 5),2);break;
}
case 1:
{
return 100*(x[1] - x[0]*x[0])*(x[1] - x[0]*x[0]) + (1 - x[0])*(1 - x[0]);break;
}
case 2:{
return 4*pow((x[0] - 5),2) + pow((x[1] - 6),2);break;
}
case 3:
{
return 8*pow(x[0],2) + 4*x[0]*x[1] + 5*pow(x[1],2);break;
}
case 4:
{
return x[0]*x[0];break;
}
}
// return otv;
}
private:
double alpha;// коэффициент удлинения вектора (результат одномерной минимизации)
double b;
};
bool boo2 = false;
Cvector v;
v.c = m_com.GetCurSel();
CString sn[10];
char s3[100];
m_ed3.GetWindowText(s3,100);
int j2,j3 =0;
for(j2 = 0;j2<strlen(s3);j2++)
{
while ((s3[j2] != ',')&&(j2 <= strlen(s3)))
{
sn[j3] = sn[j3]+s3[j2];
j2++;
}
j3++;
}
for(j2 = 0;j2<j3;j2++)
v.x[j2] = atof(sn[j2]);
// INPUT N
v.n = 2;
v.df(v.x,v.p);
for(int i=0;i<v.n; i++)
v.p[i] = -v.p[i];
double norg;
char s7[100];
m_ed2.GetWindowText(s7,100);
norg = atof(s7);
if (norg == 0)
{
AfxMessageBox("You are input illegal charcters in 'Norma gradienta'",MB_ICONSTOP);
boo2 = true;
}
float cou,ogr;
char s1[100];
m_ed1.GetWindowText(s1,100);
cou = atoi(s1);
if (cou == 0)
{
AfxMessageBox("You are input illegal charcters in 'Count of Iteration'",MB_ICONSTOP);
boo2 = true;
}
ogr = 0;
while (boo2 == false)
{
ogr = ogr+1;
if (ogr>=cou)
{
boo2 = true;
}
v.a = 0.1;
for (int i= 0; i<v.n; i++)
{
v.x0[i] = v.x[i];
}
while (v.diffp(v.x0,v.p)*v.diffp(v.x,v.p)>0)
{
for(i=0; i<v.n; i++)
{
v.x0[i] = v.x[i];
}
for(int i=0; i<v.n; i++)
{
v.x[i] = v.x[i]+v.a*v.p[i];
}
v.a=2*v.a;
}
CString sr;
char s4[100];
for(i=0; i<v.n; i++)
{
gcvt(v.x[i],12, s4);
sr = sr+s4+';';
}
m_ed4.SetWindowText(sr);
// находим переменные в соответстви с методом
v.z=v.diffp(v.x0,v.p) + v.diffp(v.x,v.p) +3*(v.f(v.x0,v.c)-v.f(v.x,v.c))/v.OtrLength(v.x0,v.x);
v.w=sqrt(v.z*v.z-v.diffp(v.x0,v.p) * v.diffp(v.x,v.p));
v.r=v.a * (v.z - v.diffp(v.x0,v.p) + v.w) / (v.diffp(v.x,v.p) - v.diffp(v.x0,v.p) + 2*v.w);
for( i=0; i<v.n; i++)//находим точку апроксимирующего минимума
v.d[i]=v.x0[i]+v.r*v.p[i];
// сокращаем ТИЛ до соответствующего условия
while (fabs(v.diffp(v.d,v.p))>0.01)
{
if(v.diffp(v.d,v.p)<0) //минимум справа от апр. минимума
{
for(i=0;i<v.n;i++)
v.x0[i]=v.d[i];
v.a = v.a - v.r;
}
else //минимум слева от апр. минимума
{
v.a= v.r;
for(i=0;i<v.n;i++)
v.x[i]=v.d[i];
}
// находим переменные в соответстви с методом
v.z=v.diffp(v.x0,v.p) + v.diffp(v.x,v.p) +3*(v.f(v.x0,v.c)-v.f(v.x,v.c))/v.OtrLength(v.x0,v.x);
v.w=sqrt(v.z*v.z-v.diffp(v.x0,v.p) * v.diffp(v.x,v.p));
v.r=v.a * (v.z - v.diffp(v.x0,v.p) + v.w) / (v.diffp(v.x,v.p) - v.diffp(v.x0,v.p) + 2*v.w);
for( i=0; i<v.n; i++)//находим точку апроксимирующего минимума
v.d[i] = v.x0[i]+ v.r*v.p[i];
}
for(i=0;i<v.n;i++) //получаем конечный результат
v.x0[i]=v.d[i];
for(i=0; i<v.n; i++)
{
v.otv0[i] = v.otv[i];
}
for(i=0; i<v.n; i++)
{
v.otv[i] = v.x0[i];
}
double ak;
v.df(v.otv,v.p);
for(i=0;i<v.n; i++)
v.p[i] = -v.p[i];
for(i=0; i<v.n; i++)
{
v.x[i] = v.otv[i];
v.x0[i] = v.otv0[i];
}
if (v.norma(v.p) < norg)
boo2 = true;
}
if ((cou!= 0)&&(norg!=0))
{
CString sr2;
char s8[100];
for(i=0; i<v.n; i++)
{
gcvt(v.x[i],12, s8);
sr2 = sr2+s8+';';
}
m_ed5.SetWindowText(sr2);
}
}
Соседние файлы в папке Лабораторная работа №54