📄 demo_interpolation.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"><html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>Demonstrations of Polynomial Interpolation</title> <meta name="generator" content="MATLAB 7.6"> <meta name="date" content="2008-12-16"> <meta name="m-file" content="demo_interpolation"><style>body { background-color: white; margin:10px;}h1 { color: #990000; font-size: x-large;}h2 { color: #990000; font-size: medium;}/* Make the text shrink to fit narrow windows, but not stretch too far in wide windows. */ p,h1,h2,div.content div { max-width: 600px; /* Hack for IE6 */ width: auto !important; width: 600px;}pre.codeinput { background: #EEEEEE; padding: 10px;}@media print { pre.codeinput {word-wrap:break-word; width:100%;}} span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.codeoutput { color: #666666; padding: 10px;}pre.error { color: red;}p.footer { text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;} </style></head> <body> <div class="content"> <h1>Demonstrations of Polynomial Interpolation</h1> <introduction> <p>Polynomial interpolation is a method for computing intermediate values between given data points. All interpolation methods make assumptions about the behavior of the function to compute values at intermediate points. The different models produce different interpolation curves. This demonstration will illustrate linear interpolation, full polynomial interpolation that constructs a polynomial with the number of coefficients equal to the number of points, and two methods of cubic spline interpolation. The development follows that in the book Numerical Computing with Matlab" by Cleve Moler, Chapter 3. </p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">Example 1: A small number of data points</a></li> <li><a href="#2">Linear Interpolation</a></li> <li><a href="#3">Cubic Polynomial Fit</a></li> <li><a href="#4">A Second Example</a></li> <li><a href="#5">Solve for the coefficients using linear equations</a></li> <li><a href="#6">Lagrange form of polynomial interpolation</a></li> <li><a href="#7">Piecewise Linear Interpolation</a></li> <li><a href="#8">Cubic Spline Interpolation using pchip</a></li> <li><a href="#9">Cubic Spline Interpolation using spline</a></li> </ul> </div> <h2>Example 1: A small number of data points<a name="1"></a></h2><pre class="codeinput"><span class="comment">% Set up some data points</span>x = (0:3)';y = [-5; -6; -1; 16];disp(<span class="string">'Data Points'</span>)disp([x';y'])h1 = figure(1);plot(x,y,<span class="string">'o'</span>,<span class="string">'markerfacecolor'</span>,<span class="string">'b'</span>,<span class="string">'markersize'</span>,9)title(<span class="string">'Data Points'</span>,<span class="string">'fontsize'</span>,20)xlabel(<span class="string">'x'</span>,<span class="string">'fontsize'</span>,18')ylabel(<span class="string">'y'</span>,<span class="string">'fontsize'</span>,18')grid <span class="string">on</span></pre><pre class="codeoutput">Data Points 0 1 2 3 -5 -6 -1 16</pre><img vspace="5" hspace="5" src="demo_interpolation_01.png"> <h2>Linear Interpolation<a name="2"></a></h2> <p>The standard Matlab plot routine draws straight lines between the data points.</p><pre class="codeinput">hold <span class="string">on</span>;plot(x,y,<span class="string">'-o'</span>,<span class="string">'linewidth'</span>,1.5)title(<span class="string">'Linear Fit'</span>,<span class="string">'fontsize'</span>,20)hold <span class="string">off</span>;</pre><img vspace="5" hspace="5" src="demo_interpolation_02.png"> <h2>Cubic Polynomial Fit<a name="3"></a></h2> <p>Here we will calculate a cubic polynomial to pass through the data points. We will do the calculation by setting up a matrix of powers of x and then inverting the X*c=y form to get the coefficients. When four data points are given, this is a full polynomial fit. The problem with this method is that X is poorly conditioned. This method is not used in practice. </p><pre class="codeinput">V = [x.^3 x.^2 x ones(length(x),1)]c = V\y<span class="comment">% Specify a vector of independent values</span>u = (0:.1:3)';<span class="comment">% Construct an array of the powers of u</span>U = [u.^3 u.^2 u ones(length(u),1)];<span class="comment">% Calculate the polynomial values and plot</span>p = U*c;plot(x,y,<span class="string">'--o'</span>,<span class="string">'linewidth'</span>,1.5)title(<span class="string">'Full Cubic Fit - Matrix Method'</span>,<span class="string">'fontsize'</span>,20)xlabel(<span class="string">'x'</span>,<span class="string">'fontsize'</span>,18')ylabel(<span class="string">'y'</span>,<span class="string">'fontsize'</span>,18')grid <span class="string">on</span>hold <span class="string">on</span>;plot(u,p,<span class="string">'r'</span>,<span class="string">'linewidth'</span>,1.5)hold <span class="string">off</span><span class="comment">% Condition number of the matrix X</span>disp(<span class="string">'Condition Number of V'</span>)disp(cond(V,2));</pre><pre class="codeoutput">V = 0 0 0 1 1 1 1 1 8 4 2 1 27 9 3 1c = 1.0000 0.0000 -2.0000 -5.0000Condition Number of V 154.4565</pre><img vspace="5" hspace="5" src="demo_interpolation_03.png"> <h2>A Second Example<a name="4"></a></h2><pre class="codeinput">x = (1:6)';y = [16; 18; 21; 17; 15; 12];h2 = figure(2);plot(x,y,<span class="string">'o'</span>,<span class="string">'markerfacecolor'</span>,<span class="string">'b'</span>,<span class="string">'markersize'</span>,9)axis([0 7 0 25])title(<span class="string">'Data Points'</span>,<span class="string">'fontsize'</span>,20)xlabel(<span class="string">'x'</span>,<span class="string">'fontsize'</span>,18')ylabel(<span class="string">'y'</span>,<span class="string">'fontsize'</span>,18')grid <span class="string">on</span></pre><img vspace="5" hspace="5" src="demo_interpolation_04.png"> <h2>Solve for the coefficients using linear equations<a name="5"></a></h2><pre class="codeinput">V = [x.^3 x.^2 x ones(length(x),1)]c = V\y<span class="comment">% Specify a vector of independent values</span>u = (0.75:.05:6.25)';<span class="comment">% Construct an array of the powers of u</span>U = [u.^3 u.^2 u ones(length(u),1)];<span class="comment">% Calculate the polynomial values and plot</span>p = U*c;hold <span class="string">on</span>;plot(u,p,<span class="string">'g'</span>,<span class="string">'linewidth'</span>,1.5)title(<span class="string">'Cubic Polynomial Fit -- Matrix Method'</span>,<span class="string">'fontsize'</span>,20)hold <span class="string">off</span><span class="comment">% Condition number of the matrix X</span>disp(<span class="string">'Condition Number of V'</span>)disp(cond(V,2));</pre><pre class="codeoutput">V = 1 1 1 1 8 4 2 1 27 9 3 1 64 16 4 1 125 25 5 1 216 36 6 1c = 0.1574 -2.4563 9.6720 8.3333Condition Number of V 1.4668e+03</pre><img vspace="5" hspace="5" src="demo_interpolation_05.png"> <h2>Lagrange form of polynomial interpolation<a name="6"></a></h2> <p>The function polyinterp.m does a full Lagrange form interpolation.</p><pre class="codeinput">t = (0.75:.05:6.25)';v = polyinterp(x,y,t);h2=figure(2);plot(x,y,<span class="string">'o'</span>,<span class="string">'markerfacecolor'</span>,<span class="string">'b'</span>,<span class="string">'markersize'</span>,9)axis([0 7 0 25])title(<span class="string">'Full Interpolation with Lagrange Form'</span>,<span class="string">'fontsize'</span>,20)xlabel(<span class="string">'x'</span>,<span class="string">'fontsize'</span>,18')ylabel(<span class="string">'y'</span>,<span class="string">'fontsize'</span>,18')grid <span class="string">on</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -