// yu11View.cpp : implementation of the CYu11View class
//
#include "stdafx.h"
#include "yu11.h"
#include "yu11Doc.h"
#include "yu11View.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CYu11View
IMPLEMENT_DYNCREATE(CYu11View, CView)
BEGIN_MESSAGE_MAP(CYu11View, CView)
//{{AFX_MSG_MAP(CYu11View)
ON_COMMAND(ID_QS, OnQs)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CYu11View construction/destruction
double E=0.01,f=0.0;
double x0[3]={0.5,1,0.5};
CYu11View::CYu11View()
{
// TODO: add construction code here
}
CYu11View::~CYu11View()
{
}
BOOL CYu11View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CYu11View drawing
void CYu11View::OnDraw(CDC* pDC)
{
CYu11Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CYu11View printing
BOOL CYu11View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CYu11View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CYu11View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CYu11View diagnostics
#ifdef _DEBUG
void CYu11View::AssertValid() const
{
CView::AssertValid();
}
void CYu11View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CYu11Doc* CYu11View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CYu11Doc)));
return (CYu11Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CYu11View message handlers
double CYu11View::H(double x[3])
{
double f0;
f0=(x[1]-x[2]+x[3])*(x[1]-x[2]+x[3])+(x[2]+x[3]-x[1])*(x[2]+x[3]-x[1])+(x[1]+x[2]-x[3])*(x[1]+x[2]-x[3]);
return f0;//返回函数值
}
double CYu11View::G(double y[3],double s[3])
{
double a;
double A=-100,B=100;
double a1,a2,f1,f2,x0[3],x1[3],x2[3],C;
x0[0]=y[0];x0[1]=y[1];x0[2]=y[2];
do
{
a1=A+0.382*(B-A);
x1[0]=x0[0]+a1*s[0];x1[1]=x0[1]+a1*s[1];x1[2]=x0[2]+a1*s[2];
f1=H(x1);
a2=A+0.618*(B-A);
x2[0]=x0[0]+a2*s[0];x2[1]=x0[1]+a2*s[1];x2[2]=x0[2]+a2*s[2];
f2=H(x2);
if (f1<f2) {
B=a2;a2=a1;f2=f1;
a1=A+0.382*(B-A);
x1[0]=x0[0]+a1*s[0]; x1[1]=x0[1]+a1*s[1]; x1[2]=x0[2]+a1*s[2];
f1=H(x1);
C=B-A;
}
else {
A=a1;a1=a2;
a1=A+0.618*(B-A);//左点
x1[0]=x0[0]+a1*s[0]; x1[1]=x0[1]+a1*s[1]; x1[2]=x0[2]+a1*s[2];
f1=H(x1);
a2=A+0.618*(B-A);//左点
x2[0]=x0[0]+a1*s[0]; x2[1]=x0[1]+a1*s[1]; x2[2]=x0[2]+a1*s[2];
f2=H(x2);C=B-A;
}
}
while(C>E);
a=(A+B)/2;
return a;
}
void CYu11View::OnQs()
{
// TODO: Add your command handler code here
double s1[3]={1,0,0},s2[3]={0,1,0},s3[3]={0,0,1};
double f0,f1,f2,f3,F[10000],a[10000][3],s[10000][3],DM=0,DMF[10000],DF;
double Y3[3],x[100000][3],X[3];
double x0[3]={0.5,1,0.5};
f0=H(x0);int i,n,k,j,m,t;
for(i=1;i<10000;i++)
{
F[0]=f0;
for(j=0;j<3;j++)
{
a[i][3]=G(x[i-1],s[i]);F[i-1]=H(x[i-1]);
x[i][0]=x[i-1][0]+a[i][3]*s[i][0]; x[i][1]=x[i-1][1]+a[i][3]*s[i][1];
x[i][2]=x[i-1][2]+a[i][3]*s[i][2];
F[i]=H(x[i]);
DMF[i]=F[i-1]-F[i];
};
DF=H(x[i-3])-H(x[i]);
for(j=1;j<i;j++)
for(k=j+1;k<=j-1;k++)
{
if(DMF[j]<DMF[k])
{DM=DMF[k];t=k;}
else
{DM=DMF[j];t=j;}
};
if(DF>DM)
{DM=DF;m=t;}
else{}
Y3[0]=2*x[i][0]-x[i-3][0];
Y3[1]=2*x[i][1]-x[i-3][1];
Y3[2]=2*x[i][2]-x[i-3][2];n=i+1;
s[n][0]=x[i-1][0]-x[i-4][0];
s[n][1]=x[i-1][1]-x[i-4][1];s[i][2]=x[i-1][2]-x[i-4][2];
f3=H(Y3);f2=H(x[i]);f1=H(x[i-3]);
if((x[i][0]-x[i-3][0])*(x[i][0]-x[i-3][0])+(x[i][1]-x[i-3][1])*(x[i][1]-x[i-3][1])+(x[i][2]-x[i-3][2])*(x[i][2]-x[i-3][2])>E*E)
{
X[0]=x[i-1][0];X[1]=x[i-1][1];X[2]=x[i-1][2];f=f2;
printf("X[3]=(%5.4,%5.4,%5.4),f=%5.4",X[0],X[1],X[2],f);
break;
}
else
{
{a[n][3]=G(x[i],s[n]);f3;
x[n][0]=x[i][0]+a[n][3]*s[n][0];
x[n][1]=x[i][1]+a[n][3]*s[n][1];
x[n][2]=x[i][2]+a[n][3]*s[n][2];
F[n]=H(x[n]);
DMF[n]=F[n]-f3;
}
if((f1-2*f2+f3)<2*DMF[t])
{
if(DMF[n]>DMF[m])
{
s[i][0]=s[i][0];
s[i][1]=s[i][1];
s[i][2]=s[i][2];
}
else{
for(i=m;i<=n;i++)
{
s[i][0]=s[i+1][0];s[i][1]=s[i+1][1];s[i][2]=s[i+1][2];
x[i][0]=x[i-1][0]+a[i][3]*s[i][0];
x[i][1]=x[i-1][1]+a[i][3]*s[i][1];
x[i][2]=x[i-1][2]+a[i][3]*s[i][2];
}
i++;
a[i][3]=G(x[i-1],s[i]);
x[i][0]=x[i-1][0]+a[i][3]*s[i][0];
x[i][1]=x[i-1][1]+a[i][3]*s[i][1];
x[i][2]=x[i-1][2]+a[i][3]*s[i][2];
F[i]=H(x[i]);x[i][0]=x[i-4][0];
x[i][1]=x[i-4][1];x[i][1]=x[i-4][1];
}
}
else
{
if(f3<f2)
{
i++;f0=f3;
x[i][0]=x[i-1][0];x[i][1]=x[i-1][1];
x[i][2]=x[i-1][2];
}
else{
f0=f2;
i++;
a[i][3]=G(x[i-1],s[i]);
x[i][0]=x[i-1][0]+a[i][3]*s[i][0];
x[i][1]=x[i-1][1]+a[i][3]*s[i][1];
x[i][2]=x[i-1][2]+a[i][3]*s[i][2];
}
}
}