分段线性插值法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

数值分析实验报告1《数值分析》实验报告实验序号:实验五实验名称:分段线性插值法1、实验目的:随着插值节点的增加,插值多项式的插值多项式的次数也增加,而对于高次的插值容易带来剧烈的震荡,带来数值的不稳定(Runge现象)。为了既要增加插值的节点,减小插值的区间,以便更好的逼近插值函数,又要不增加插值多项式的次数以减少误差,可采用分段线性插值。2、实验内容:求一个函数(x)用来近似函数f(x),用分段线性插值的方法来求解近似函数(x)并画出近似函数图像及原函数图像。设在区间[a,b]上,给定n+1个插值节点bxxxxan...210和相应的函数值nyyy,...,,10,求一个插值函数)(x,满足以下条件:(1)),...,2,1,0()(njyxjj;(2))(x在每一个小区间[1,jjxx]上是线性函数。对于给定函数11-,2511)(2xxxf。在区间11-,上画出f(x)和分段线性插值函数)(x的函数图像。1.分段线性插值的算法思想:分段线性插值需要在每个插值节点上构造分段线性插值基函数)(xlj,然后再作它们的线性组合。分段线性插值基函数的特点是在对应的插值节点上函数值取1,其它节点上函数值取0。插值基函数如下:数值分析实验报告2其它,0,)(101010xxxxxxxxl其它,0,,)(111111jjjjjjjjjjjxxxxxxxxxxxxxxxl其它,0,)(111nnnnnnxxxxxxxxl设在节点a≤x0x1…≤b=f(xi),(i=0,1,2,…,n)求折线函数L(x)满足:(1)L(x)∈C[a,b](2)L(x[i]=y[i])(3)L(x)在每个小区间(x[i],x[i+1])上是线性插值函数¢(x)叫做区间[a,b]上对数据(x[j],y[j])(j=0,1,2,…,n)的分段区间函数。利用一介拉格朗日函数,直接得到线性插值函数为:L(x0)=(x-x[1])/x[0]-x[1];(x[0]≤x≤x[1])L(x0)=0(x[1]≤x≤x[n])分段线性方程的表达式:¢(x)=∑(j=0,..,n)y[j]*L[j](x);3、实验代码://LDlg.cpp:implementationfile//#includestdafx.h#includeL.h#includeLDlg.h#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif数值分析实验报告3///////////////////////////////////////////////////////////////////////////////CAboutDlgdialogusedforAppAboutclassCAboutDlg: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//Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)数值分析实验报告4//}}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_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CLDlgdialogCLDlg::CLDlg(CWnd*pParent/*=NULL*/):CDialog(CLDlg::IDD,pParent){//{{AFX_DATA_INIT(CLDlg)//NOTE:theClassWizardwilladdmemberinitializationhere//}}AFX_DATA_INIT//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32m_hIcon=AfxGetApp()-LoadIcon(IDR_MAINFRAME);}voidCLDlg::DoDataExchange(CDataExchange*pDX)数值分析实验报告5{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_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CLDlgmessagehandlersBOOLCLDlg::OnInitDialog(){CDialog::OnInitDialog();//AddAbout...menuitemtosystemmenu.//IDM_ABOUTBOXmustbeinthesystemcommandrange.ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX0xF000);数值分析实验报告6CMenu*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'smainwindowisnotadialogSetIcon(m_hIcon,TRUE);//SetbigiconSetIcon(m_hIcon,FALSE);//Setsmallicon//TODO:AddextrainitializationherereturnTRUE;//returnTRUEunlessyousetthefocustoacontrol}voidCLDlg::OnSysCommand(UINTnID,LPARAMlParam){if((nID&0xFFF0)==IDM_ABOUTBOX){CAboutDlgdlgAbout;dlgAbout.DoModal();}数值分析实验报告7else{CDialog::OnSysCommand(nID,lParam);}}//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,//thisisautomaticallydoneforyoubytheframework.voidCLDlg::OnPaint(){if(IsIconic()){CPaintDCdc(this);//devicecontextforpaintingSendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);//CentericoninclientrectangleintcxIcon=GetSystemMetrics(SM_CXICON);intcyIcon=GetSystemMetrics(SM_CYICON);CRectrect;GetClientRect(&rect);intx=(rect.Width()-cxIcon+1)/2;inty=(rect.Height()-cyIcon+1)/2;//Drawtheicondc.DrawIcon(x,y,m_hIcon);}else数值分析实验报告8{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)数值分析实验报告9{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-

1 / 16
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功