⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_interpolation.html

📁 Demo of Interpolation in MATLAB
💻 HTML
📖 第 1 页 / 共 2 页
字号:
hold <span class="string">on</span>;plot(u,p,<span class="string">':g'</span>,<span class="string">'linewidth'</span>,1.5)plot(t,v,<span class="string">'-r'</span>,<span class="string">'linewidth'</span>,1.5)hold <span class="string">off</span>;figure(h2)</pre><img vspace="5" hspace="5" src="demo_interpolation_06.png"> <h2>Piecewise Linear Interpolation<a name="7"></a></h2>         <p>The Matlab plotting routines use piecewise linear interpolation</p><pre class="codeinput">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,<span class="string">'linewidth'</span>,1.5)axis([0 7 10 25])title(<span class="string">'Piecewise Linear Interpolation'</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_07.png"> <h2>Cubic Spline Interpolation using pchip<a name="8"></a></h2>         <p>pchip is a Matlab function that implements "Piecewise Cubic Hermite Interpolating Polynomial" as described in NCM, Chapter            2.         </p><pre class="codeinput">w = pchip(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">'Cubic Interpolation with pchip'</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">':g'</span>,<span class="string">'linewidth'</span>,1.5)plot(t,v,<span class="string">':r'</span>,<span class="string">'linewidth'</span>,1.5)plot(t,w,<span class="string">'-m'</span>,<span class="string">'linewidth'</span>,1.5)hold <span class="string">off</span>;figure(h2);</pre><img vspace="5" hspace="5" src="demo_interpolation_08.png"> <h2>Cubic Spline Interpolation using spline<a name="9"></a></h2>         <p>spline is a Matlab function that implements "Piecewise Cubic Hermite Interpolating Polynomial" as described in NCM, Chapter            2. It uses a different condition to estimate slopes at the "knots".         </p><pre class="codeinput">z = spline(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">'Cubic Interpolation with spline'</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">':g'</span>,<span class="string">'linewidth'</span>,1.5)plot(t,v,<span class="string">':r'</span>,<span class="string">'linewidth'</span>,1.5)plot(t,w,<span class="string">':m'</span>,<span class="string">'linewidth'</span>,1.5)plot(t,z,<span class="string">'-k'</span>,<span class="string">'linewidth'</span>,1.5);hold <span class="string">off</span>;</pre><img vspace="5" hspace="5" src="demo_interpolation_09.png"> <p class="footer"><br>            Published with MATLAB&reg; 7.6<br></p>      </div>      <!--##### SOURCE BEGIN #####%% Demonstrations of Polynomial Interpolation% 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.%% Example 1: A small number of data points% Set up some data pointsx = (0:3)';y = [-5; -6; -1; 16];disp('Data Points')disp([x';y'])h1 = figure(1);plot(x,y,'o','markerfacecolor','b','markersize',9)title('Data Points','fontsize',20)xlabel('x','fontsize',18')ylabel('y','fontsize',18')grid on%% Linear Interpolation% The standard Matlab plot routine draws straight lines between the data% points.hold on;plot(x,y,'-o','linewidth',1.5)title('Linear Fit','fontsize',20)hold off;%% Cubic Polynomial Fit% 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.V = [x.^3 x.^2 x ones(length(x),1)]c = V\y% Specify a vector of independent valuesu = (0:.1:3)';% Construct an array of the powers of uU = [u.^3 u.^2 u ones(length(u),1)];% Calculate the polynomial values and plotp = U*c;plot(x,y,'REPLACE_WITH_DASH_DASHo','linewidth',1.5)title('Full Cubic Fit - Matrix Method','fontsize',20)xlabel('x','fontsize',18')ylabel('y','fontsize',18')grid onhold on;plot(u,p,'r','linewidth',1.5)hold off% Condition number of the matrix Xdisp('Condition Number of V')disp(cond(V,2));%% A Second Examplex = (1:6)';y = [16; 18; 21; 17; 15; 12];h2 = figure(2);plot(x,y,'o','markerfacecolor','b','markersize',9)axis([0 7 0 25])title('Data Points','fontsize',20)xlabel('x','fontsize',18')ylabel('y','fontsize',18')grid on%% Solve for the coefficients using linear equationsV = [x.^3 x.^2 x ones(length(x),1)]c = V\y% Specify a vector of independent valuesu = (0.75:.05:6.25)';% Construct an array of the powers of uU = [u.^3 u.^2 u ones(length(u),1)];% Calculate the polynomial values and plotp = U*c;hold on;plot(u,p,'g','linewidth',1.5)title('Cubic Polynomial Fit REPLACE_WITH_DASH_DASH Matrix Method','fontsize',20)hold off% Condition number of the matrix Xdisp('Condition Number of V')disp(cond(V,2));%% Lagrange form of polynomial interpolation% The function polyinterp.m does a full Lagrange form interpolation.t = (0.75:.05:6.25)';v = polyinterp(x,y,t);h2=figure(2);plot(x,y,'o','markerfacecolor','b','markersize',9)axis([0 7 0 25])title('Full Interpolation with Lagrange Form','fontsize',20)xlabel('x','fontsize',18')ylabel('y','fontsize',18')grid onhold on;plot(u,p,':g','linewidth',1.5)plot(t,v,'-r','linewidth',1.5)hold off;figure(h2)%% Piecewise Linear Interpolation% The Matlab plotting routines use piecewise linear interpolationh2=figure(2);plot(x,y,'-o','markerfacecolor','b','markersize',9,'linewidth',1.5)axis([0 7 10 25])title('Piecewise Linear Interpolation','fontsize',20)xlabel('x','fontsize',18')ylabel('y','fontsize',18')grid on%% Cubic Spline Interpolation using pchip% pchip is a Matlab function that implements "Piecewise Cubic % Hermite Interpolating Polynomial" as described in NCM, Chapter 2.w = pchip(x,y,t);h2=figure(2);plot(x,y,'o','markerfacecolor','b','markersize',9)axis([0 7 0 25])title('Cubic Interpolation with pchip','fontsize',20)xlabel('x','fontsize',18')ylabel('y','fontsize',18')grid onhold on;plot(u,p,':g','linewidth',1.5)plot(t,v,':r','linewidth',1.5)plot(t,w,'-m','linewidth',1.5)hold off;figure(h2);%% Cubic Spline Interpolation using spline% spline is a Matlab function that implements "Piecewise Cubic % Hermite Interpolating Polynomial" as described in NCM, Chapter 2. It uses% a different condition to estimate slopes at the "knots".z = spline(x,y,t);h2=figure(2);plot(x,y,'o','markerfacecolor','b','markersize',9)axis([0 7 0 25])title('Cubic Interpolation with spline','fontsize',20)xlabel('x','fontsize',18')ylabel('y','fontsize',18')grid onhold on;plot(u,p,':g','linewidth',1.5)plot(t,v,':r','linewidth',1.5)plot(t,w,':m','linewidth',1.5)plot(t,z,'-k','linewidth',1.5); hold off;##### SOURCE END #####-->   </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -