实验三.docx
《实验三.docx》由会员分享,可在线阅读,更多相关《实验三.docx(12页珍藏版)》请在冰豆网上搜索。
实验三
实验报告
实验序号:
实验三实验名称:
Lagrange插值法
学号:
20141103261姓名:
杨慧芳
任课教师:
马季骕专业班级:
14级计算机科学与技术师范汉班
一.实验目的
用Lagrange插值法求f(x)的逼近函数
(x)并画出图像
二.实验内容
1.算法分析
插值节点取的足够多时逼近函数
(x)能够很好的逼近被逼近函数f(x)。
插值函数
(x)的次数相应升高,不一定收敛到相应的被逼近函数,从而会产生Runge现象。
要想解决Lagrange插值,先研究线形插值和二次插值。
在线性插值中,若逼近函数用两点式表示出来,则有F(x)=y0*((x-x1)/(x0-x1))+y1*((x-x0)/(x1-x0)),过三个点的二次插值可以确定一条抛物线,经化简,被逼近函数可表示为F(x)=y0*((x-x1)*(x-x2)/(x0-x1)/(x0-x2))+y1*((x-x0)*(x-x2)/(x1-x0)/(x1-x2))+y2*((x-x0)*(x-x1)/(x2-x0)/(x2-x1)),分析可得对于具有n+1个节点的函数,都可以表示成该节点的函数值与其对应基函数的乘积的线形组合。
要求逼近函数是n次多项式,并且逼近函数过所有的型值点。
2.源代码
//LDlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"L.h"
#include"LDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CLDlgdialog
CLDlg:
:
CLDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CLDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CLDlg)
//NOTE:
theClassWizardwilladdmemberinitializationhere
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCLDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLDlg)
//NOTE:
theClassWizardwilladdDDXandDDVcallshere
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLDlg,CDialog)
//{{AFX_MSG_MAP(CLDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_LARGRI,OnLargri)
ON_BN_CLICKED(IDC_BUTTON2,OnButton2)
ON_BN_CLICKED(IDC_HERMITE,OnHermite)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CLDlgmessagehandlers
BOOLCLDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCLDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCLDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCLDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
voidCLDlg:
:
OnOK()
{
intx00=300,y00=350,i,j;
doublex;
CDC*pDC=GetDC();
pDC->SetMapMode(MM_LOMETRIC);
pDC->SetViewportOrg(x00,y00);
//画坐标轴与原函数
for(i=-700;i<=700;i++)
{
pDC->SetPixel(i,0,RGB(0,0,0));
pDC->SetPixel(0,i,RGB(0,0,0));
}
for(x=-1;x<=1;x+=0.001)
{
doublej=400.0/(1+25*x*x);
pDC->SetPixel(x*500,j,RGB(255,0,0));
}
pDC->TextOut(-30,-10,"0");
pDC->TextOut(-30,430,"1");
pDC->TextOut(490,-10,"1");
pDC->TextOut(-490,-10,"-1");
pDC->MoveTo(-10,680);//x箭头
pDC->LineTo(0,700);
pDC->MoveTo(0,700);
pDC->LineTo(10,680);
pDC->MoveTo(680,10);//y箭头
pDC->LineTo(700,0);
pDC->MoveTo(700,0);
pDC->LineTo(680,-10);
pDC->TextOut(-30,700,"y");
pDC->TextOut(700,-10,"x");
}
voidCLDlg:
:
OnLargri()
{
intx00=300,y00=350,i,j;
doublex;
CDC*pDC=GetDC();
pDC->SetMapMode(MM_LOMETRIC);
pDC->SetViewportOrg(x00,y00);
//画坐标轴
for(i=-700;i<=700;i++)
{
pDC->SetPixel(i,0,RGB(0,0,0));
pDC->SetPixel(0,i,RGB(0,0,0));
}
doubleyx[]={-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1};
pDC->TextOut(-30,-10,"0");
pDC->TextOut(-30,430,"1");
pDC->TextOut(490,-10,"1");
pDC->TextOut(-490,-10,"-1");
pDC->MoveTo(-10,680);//x箭头
pDC->LineTo(0,700);
pDC->MoveTo(0,700);
pDC->LineTo(10,680);
pDC->MoveTo(680,10);//y箭头
pDC->LineTo(700,0);
pDC->MoveTo(700,0);
pDC->LineTo(680,-10);
pDC->TextOut(-30,700,"y");
pDC->TextOut(700,-10,"x");
//拉格朗日差值的函数
doubleyy[12],lx[12],ly[12];
doublel_fenzi[12],l_fenmu[12];
doublel_x,l_y;
for(i=0;i<=10;i++)
{
yy[i]=1.0/(1+25*yx[i]*yx[i]);
}
for(i=0;i<=10;i++)
{
l_fenmu[i]=1.0;
for(j=0;j<=10;j++)
{
if(i!
=j)
l_fenmu[i]=l_fenmu[i]*(yx[i]-yx[j]);
}
}
doubleqq,pp;
for(qq=-1;qq<=1;qq+=0.0003)
{
for(i=0;i<=10;i++)
{
l_fenzi[i]=1.0;
for(j=0;j<=10;j++)
{
if(i!
=j)
l_fenzi[i]=l_fenzi[i]*(qq-yx[j]);
}
}
pp=0;
for(i=0;i<=11;i++)
{
pp=pp+1.0/(1+25*yx[i]*yx[i])*l_fenzi[i]/l_fenmu[i];
}
pDC->SetPixel(qq*500,pp*390+5,RGB(132,112,225));
}
}
三.实验截图
四.结果分析
本次实验使我对用Lagrange插值法求f(x)的逼近函数
(x)有了更深的理解,知道了如何用程序来求解Lagrange插值基函数
(j=0,1,…n),求出逼近函数
(x)并绘制出其图像,且通过图像看到了Lagrange插值法的缺点,即当Lagrange插值函数次数很高时出现了Runge现象。