分段线性插值法_第1页
分段线性插值法_第2页
分段线性插值法_第3页
分段线性插值法_第4页
分段线性插值法_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/《数值分析》实验报告实验序号:实验五实验名称:分段线性插值法实验目的:随着插值节点的增加,插值多项式的插值多项式的次数也增加,而对于高次的插值容易带来剧烈的震荡,带来数值的不稳定(Runge现象).为了既要增加插值的节点,减小插值的区间,以便更好的逼近插值函数,又要不增加插值多项式的次数以减少误差,可采用分段线性插值。

2、实验内容:求一个函数(x)用来近似函数f(x),用分段线性插值的方法来求解近似函数(x)并画出近似函数图像及原函数图像.设在区间[a,b]上,给定n+1个插值节点和相应的函数值,求一个插值函数,满足以下条件:;在每一个小区间[]上是线性函数.对于给定函数。在区间上画出f(x)和分段线性插值函数的函数图像。分段线性插值的算法思想:分段线性插值需要在每个插值节点上构造分段线性插值基函数,然后再作它们的线性组合。分段线性插值基函数的特点是在对应的插值节点上函数值取1,其它节点上函数值取0。插值基函数如下:设在节点a≤x0〈x1〈…≤b=f(xi),(i=0,1,2,…,n)求折线函数L(x)满足:L(x)∈C[a,b]L(x[i]=y[i])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//#include"stdafx.h"#include"L。h”#include"LDlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif///////////////////////////////////////////////////////////////////////////////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_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_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CLDlgdialogCLDlg::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_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CLDlgmessagehandlersBOOLCLDlg::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)); }}voidCLDlg::OnButton2(){ﻩ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}; doubleyy[14]; for(i=0;i<=10;i++)ﻩ{ﻩﻩyy[i]=1。0/(1+25*yx[i]*yx[i]);ﻩ} 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");//线性分段差值的图像 CPenpen; CPen*oldpen;ﻩpen。CreatePen(PS_SOLID,5,RGB(0,0,0));ﻩoldpen=pDC->SelectObject(&pen);ﻩfor(i=0;i<10;i++) {ﻩpDC->MoveTo(yx[i]*480,yy[i]*400);ﻩpDC—〉LineTo(yx[i+1]*480,yy[i+1]*400);}}voidCLDlg::OnHermite(){ﻩ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}; doubleyy[12];ﻩfor(i=0;i<=10;i++) { ﻩyy[i]=1.0/(1+25*yx[i]*yx[i]);ﻩ} 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,-1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论