实习:Matlab作业hermite插值
实习:Matlab作业hermite插值
(总12页)
--本页仅作为文档封面,使用时请直接删除即可-- --内页可以根据需求调整合适字体及大小--
题目:利用Matlab实现数据的Hermite插值和分段三次Hermite插值
小组成员:王晓波(38) 蔡明宇(20)
一、程序实现意义:
一般的,从各种试验得来的数据总有一定的数量,而利用插值技术能够从有限的数据中获取整体的状态。而Hermite插值不仅保证了插值函数与原函数在给定数据点处得拟合,同时保证了在相应点处导数的相同,从而在很大程度上保证了曲线的“光滑性”。因此,通过Matlab实现Hermite插值具有很普遍的意义。
二、实现过程:
1、Hermite插值
由于并不是所有的Matlab版本都提供现有的Hermite插值函数包,故我们首先编写了实现给定五个观测点的Hermite插值的M程序,代码如下: function [f,f0] = Hermite1(x,y,y_1) syms t; f = ;
if(length(x) == length(y))
if(length(y) == length(y_1)) n = length(x); else
disp('y和y的导数的维数不相等'); return; end else
disp('x和y的维数不相等! '); return; end
for i=1:n h = ; a = ; for j=1:n
if( j ~= i)
h = h*(t-x(j))^2/((x(i)-x(j))^2); a = a + 1/(x(i)-x(j)); end end
2
f = f + h*((x(i)-t)*(2*a*y(i)-y_1(i))+y(i));
3
end
f0 = subs(f,'t');
其中x为给定点横坐标数组,y为给定点纵坐标数组,y_1为原函数在给定点处的导数数组。测试证明该程序可以实现,例如输入如下数组:
x=1::; y_1=[ ]; y=[1 ];
>> [f,f0] = Hermite1(x,y,y_1); 运行结果如下:
f =
(390625*((3972231*t)/35 - 28321/0000)*(t - 1)^2*(t - 7/5)^2*(t - 8/5)^2*(t - 9/5)^2)/36 - (390625*(t - 1)^2*(t - 6/5)^2*(t - 7/5)^2*(t - 9/5)^2*((28557*t)/28 - 23/2000))/36 + (390625*((64*t)/3 - 61/3)*(t - 6/5)^2*(t - 7/5)^2*(t - 8/5)^2*(t - 9/5)^2)/576 + (390625*((763*t)/1984 + 043/6240000)*(t - 1)^2*(t - 6/5)^2*(t -
8/5)^2*(t - 9/5)^2)/16 - (390625*((77623*t)/28 - 931/60000)*(t - 1)^2*(t - 6/5)^2*(t - 7/5)^2*(t - 8/5)^2)/576 f0 =
.
利用matlab绘制图像:
3
2、程序的窗口化:
利用Matlab提供的GUIDE工具以及callback函数实现相应函数的窗口化,GUI代码如下:
function varargout = untitled(varargin) % UNTITLED M-file for
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*. %
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*. %
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in with the given input arguments. %
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application
4
% stop. All inputs are passed to untitled_OpeningFcn via varargin.
5
%
% *See GUI Options on GUIDE's Tools menu. Choose \"GUI allows only one
% instance to run (singleton)\". %
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled
% Last Modified by GUIDE 15-Sep-2011 22:24:48
% Begin initialization code - DO NOT EDIT gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @untitled_OpeningFcn, ... 'gui_OutputFcn', @untitled_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) = str2func(varargin{1}); end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else
gui_mainfcn(gui_State, varargin{:}); end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled = hObject;
% Update handles structure guidata(hObject, handles);
5
% UIWAIT makes untitled wait for user response (see UIRESUME) % uiwait;
% --- Outputs from this function are returned to the command line. function varargout = untitled_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure varargout{1} = ;
function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
6
function edit2_Callback(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text % str2double(get(hObject,'String')) returns contents of edit2 as a double
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function edit3_Callback(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit3 as text % str2double(get(hObject,'String')) returns contents of edit3 as a double
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
7
% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function edit4_Callback(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit4 as text % str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties. function edit4_CreateFcn(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) x=str2num(get,'string')); y=str2num(get,'string')); y_1=str2num(get,'string')); x0=str2num(get,'string'));
8
syms t; f = ;
if(length(x) == length(y)) if(length(y) == length(y_1)) n = length(x); else
disp('yoíyμ?μ?êyμ???êy2??àμè'); return; end else
disp('xoíyμ???êy2??àμè£? '); return; end for i=1:n h = ; a = ; for j=1:n if( j ~= i)
h = h*(t-x(j))^2/((x(i)-x(j))^2); a = a + 1/(x(i)-x(j)); end end
f = f + h*((x(i)-t)*(2*a*y(i)-y_1(i))+y(i)); end
f0 = subs(f,'t',x0); plot,x,y,'*');
function edit5_Callback(hObject, eventdata, handles) % hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit5 as text % str2double(get(hObject,'String')) returns contents of edit5 as a double
guidata(hObject, handles);
9
% --- Executes during object creation, after setting all properties. function edit5_CreateFcn(hObject, eventdata, handles) % hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
程序运行结果:
其中左上方纵列的三个对话框从上到下分别输入给定点的横坐标x,纵坐标y以及导数值y_1,右侧空白框输入维数,下方坐标图显示插值函数图像,例如仍插入上面所给定的点列,得出结果:
10
从图上看拟合程度还是比较不错的。
分段三次Hermite插值与普通Hermite插值类似,但在程序实现过程中有所改良,主函数代码如下:
function [f,f0] = SubHermite(x,y,y_1) syms t; f = ; f0 = ;
if(length(x) == length(y))
if(length(y) == length(y_1)) n = length(x); else
disp('y和y的导数的维数不相等!'); return; end else
disp('x和y的维数不相等!'); return;
end %维数检查 for i=1:n
if(x(i)<=x0)&& (x(i+1)>=x0)
11
index = i; break; end
end %找到x0所在的区间 h = x(index+1) - x(index); %x0所在的区间长度
12
fl = y(index)*(1+2*(t-x(index))/h)*(t-x(index+1))^2/h/h + ...
y(index+1)*(1-2*(t-x(index+1))/h)*(t-x(index))^2/h/h;
fr = y_1(index)*(t-x(index))*(t-x(index+1))^2/h/h + ...
y_1(index+1)*(t-x(index+1))*(t-x(index))^2/h/h; f = fl + fr; %x0所在区间的插值函数 f0 = subs(f,'t'); %x0处得插值 其余部分保持不变。
12
因篇幅问题不能全部显示,请点此查看更多更全内容