⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svmview.cpp

📁 支持向量基预测的小数据量的预测方法。包括数据拟合
💻 CPP
📖 第 1 页 / 共 2 页
字号:

         m = 6;

         b = 0;

         /*分别为x,y,a分配存贮空间*/

         x = (double *)calloc(n,sizeof(double));

         if(x == NULL)

         {
            AfxMessageBox("内存分配失败\n");

            exit (0);
         }

         y = (double *)calloc(n,sizeof(double));

         if(y == NULL)

         {
              AfxMessageBox("内存分配失败\n");

             exit (0);
         }

         a = (double *)calloc(n,sizeof(double));

         if(a == NULL)

         {
              AfxMessageBox("内存分配失败\n");

             exit (0);
         }

         for(i=1;i<=n;i++)

         {
             x[i-1]=b+(i-1)*0.1;

             /*每隔0.1取一个点,这样连续取n个点*/

             y[i-1]=x[i-1]-exp(-x[i-1]);

             /*计算x[i-1]点对应的y值作为拟合已知值*/
         }

         Smooth(x,y,a,n,m,&dt1,&dt2,&dt3);   /*调用拟合函数*/

         for(i=1;i<=m;i++)

                 // printf("a[%d] = %.10f\n",(i-1),a[i-1]);
				 X1=a[0];
                 X2=a[1];
				 X3=a[2];
                 X4=a[3];
				 X5=a[4];
                 X6=a[5];

		 CString str11,str22,str33,str44,str55,str66;
	     str11.Format("拟合多项式系数第一项a[0]=%.10e",a[0]);
	     AfxMessageBox(str11);

		 str22.Format("拟合多项式系数第二项a[1]=%.10e",a[1]);
	     AfxMessageBox(str22);

		 str33.Format("拟合多项式系数第三项a[2]=%.10e",a[2]);
	     AfxMessageBox(str33);

		 str44.Format("拟合多项式系数第四项a[3]=%.10e",a[3]);
	     AfxMessageBox(str44);

		 str55.Format("拟合多项式系数第五项a[4]=%.10e",a[4]);
	     AfxMessageBox(str55);

		 str66.Format("拟合多项式系数第六项a[5]=%.10e",a[5]);
	     AfxMessageBox(str66);

		 //X1=(double)str11;
//                 X2=a[1];
//				 X3=a[2];
//                 X4=a[3];
//				 X5=a[4];
//                 X6=a[5];
//		 double value = total_entroy(root);
//	
//	     CString str;
//	     str.Format("你选择的最优小波基代价函数的总和=%lf",value);
//	     AfxMessageBox(str);

//          AfxMessageBox("拟合多项式与数据点偏差的平方和为:\n");
//
//          AfxMessageBox("%.10e\n",dt1);

//	     CString str1,str2,str3;
//	     str1.Format("拟合多项式与数据点偏差的平方和为:",dt1);
//	     AfxMessageBox(str1);
//
//		  str2.Format("拟合多项式与数据点偏差的绝对值之和为:",dt2);
//	     AfxMessageBox(str2);
//
//		  str3.Format("拟合多项式与数据点偏差的绝对值最大值为:",dt3);
//	     AfxMessageBox(str3);



//          AfxMessageBox("拟合多项式与数据点偏差的绝对值之和为:\n");
//
//          AfxMessageBox("%.10e\n",dt2);

//          AfxMessageBox("拟合多项式与数据点偏差的绝对值最大值为:\n");
//
//         AfxMessageBox("%.10e\n",dt3);


		 CString str1,str2,str3;
	     str1.Format("拟合多项式与数据点偏差的平方和=%.10e",dt1);
	     AfxMessageBox(str1);

		 str2.Format("拟合多项式与数据点偏差的绝对值之和为=%.10e",dt2);
	     AfxMessageBox(str2);

		 str3.Format("拟合多项式与数据点偏差的绝对值最大值为=%.10e",dt3);
	     AfxMessageBox(str3);

         free(x);        /*释放存储空间*/

         free(y);        /*释放存储空间*/

         free(a);        /*释放存储空间*/	
}

 

void CSvmView::OnREGRESSParemter() 
{
	// TODO: Add your command handler code here
	CDialog_Congress_Paramter Congress_Paramter;
    if(	Congress_Paramter.DoModal()/*IDCANCEL)//*/==IDOK)
	{   
		Congress_Paramter.m_X1=X1;
        Congress_Paramter.m_X2=X2;
		Congress_Paramter.m_X3=X3;
        Congress_Paramter.m_X4=X4;
		Congress_Paramter.m_X5=X5;
        Congress_Paramter.m_X6=X6;
	}		
}
//坐标曲线的编程实现
//
//由于统计图的直观显示,在实际应用中经常会要画各种统计图。坐标曲线图是其中比较常用的一种。
//而坐标曲线在各种统计图中应该算比较难画的,主要是很难将坐标值与图中的位置对应起来。
//我在做一个asp网站的项目中曾遇到过要画坐标曲线,由于没有易用免费的控件可用,只好自己来实现。
//因此对画坐标曲线有了一点心得。
//
//画坐标曲线的主要思路是:先找到坐标值中x的最小值minX,最大值maxX。同样取到y的最小值minY,
//最大值maxY。设图形宽度为width,高度为height,于是对于坐标(x,y)对应到图上的位置是 :
//
//((x-minX)*width/(maxX-minX),(y-minY)*height/(maxY-minY))
//
//下面结合具体代码给以详细解释。这段代码基本适合画各种坐标曲线,既可以把原点画在坐标轴上,
//也可以不画在坐标轴上。为了方便大家调试运行,我全部改成了VC代码,只要在VC中新建一个project,
//把下面的代码拷贝到OnDraw()函数中即可实现。各位只要稍微修改一下,就可以在其他各种编程语言中实
//现。


void CSvmView::OnGressDraw() 
{
	// TODO: Add your command handler code here
	CSvmDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CDC*pDC=GetDC();

       // TOD add draw code for native data here

       //初始化坐标值,一般从数据库取得。这里为了示例方便取得比较简单

       const int num=10;

       float initX[num]={1,2,3,4,5,6,7,8,9,10};

       float initY[num]={0.5,2,2.8,4,5.6,6,7,9.4,13.8,23.4};

       //分别取得X和Y最大值,最小值

       float maxX=initX[0];

       float minX=initX[0];

       float maxY=initY[0];

       float minY=initY[0];

       for(int i=0;i<num;i++)

       {

              if(initX[i]>maxX)

                     maxX=initX[i];

              if(initX[i]<minX)

                     minX=initX[i];

              if(initY[i]>maxY)

                     maxY=initY[i];

              if(initY[i]<minY)

                     minY=initY[i];

       }

  
       //如果原点必须在X轴上,加上下面2行,否则注释掉

       if(minX>0)

              minX=0;


       //如果原点必须在Y轴上,加上下面2行,否则注释掉

       if(minY>0)

              minY=0;

       //确定图象显示大小

       int width=500;

       int height=300;


       //确定坐标图四周预留的空白大小

    const int mytop=10;

    const int mybottom=40;

    const int myleft=80;

    const int myright=50;


       //确定X,Y轴每单位显示宽度

       float intervalX=(width-myleft-myright)/(maxX-minX);

       float intervalY=(height-mybottom-mytop)/(maxY-minY);


       //绘制曲线。由于绘图坐标的Y轴是向下延升,所以这里每个点的Y值是用

       //图象高度减去y值大小。

       pDC->MoveTo(int(myleft+(initX[0]-minX)*intervalX), 

                     int(height-(mybottom+(initY[0]-minY)*intervalY)));

       for(i=0;i<num;i++)

       {
              pDC->LineTo(int(myleft+(initX[i]-minX)*intervalX), 

                     int(height-(mybottom+(initY[i]-minY)*intervalY)));
       }
 

       //绘制X,Y轴

       //X轴从图形区域最左端到最右端

       float bottomY=0;

       float leftX=0;

       //bottomY表示X轴的y值,leftX表示Y轴的x值

       if(minY>0)

              bottomY=minY;

       if(minX>0)

              leftX=minX;


       pDC->MoveTo(int(myleft),int(height-(mybottom+(bottomY-minY)*intervalY)));

       pDC->LineTo(int(width-myright),int(height-(mybottom+(bottomY-minY)*intervalY)));

       //Y轴从图形区域最底端到最顶端

       pDC->MoveTo(int(myleft+(leftX-minX)*intervalX),int(height-mybottom));

       pDC->LineTo(int(myleft+(leftX-minX)*intervalX),int(mytop));

       //确定显示刻度个数

       const int count=5;
 
       //确定每个显示刻度之间的宽度

       float spaceX=(width-myleft-myright)/count;

       float spaceY=(height-mybottom-mytop)/count;

       //绘制刻度和刻度值

       CString str;

       //X轴

       for(i=0;i<=count;i++)

       {

              str.Format("%.1f",minX+i*(maxX-minX)/count);

              pDC->MoveTo(int(myleft+spaceX*i),int(height-(mybottom+(bottomY-minY)*intervalY)));

              pDC->LineTo(int(myleft+spaceX*i),int(height-(mybottom+(bottomY-minY)*intervalY+5)));

              pDC->TextOut(int(myleft+spaceX*i-10),

                     int(height-(mybottom+(bottomY-minY)*intervalY-5)),str);

       }

       //Y轴

       for(i=0;i<=count;i++)

       {
             str.Format("%.1f",minY+i*(maxY-minY)/count);

              pDC->MoveTo(int(myleft+(leftX-minX)*intervalX),int(height-(mybottom+spaceY*i)));

              pDC->LineTo(int(myleft+(leftX-minX)*intervalX+5),int(height-(mybottom+spaceY*i)));

              pDC->TextOut(int(myleft+(leftX-minX)*intervalX-30),

                     int(height-(mybottom+spaceY*i+8)),str);
       }
 
       //绘制X,Y轴的变量名

       pDC->TextOut(width/2,height-20,"时间(h)");

       pDC->TextOut(0,height/2,"产量(kg)");

       //Invalidate();  //重绘整个窗口	 
	   ReleaseDC(pDC); //一定不能忘记,释放设备环境
	   UpdateWindow(); // 更新窗口
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -