Matlab 中的样条插值图

问题描述 投票:0回答:1

我正在尝试使用不同的插值在同一图中输出几张图,以便在 x = 5 时找到 y 并用红色星号标记该点。我做了线性并且它工作得很好(意味着我得到了一个好的图)但是当我试图输出样条图时它不能很好地工作(你可以看到它只显示一行星号)。如果能帮助我理解它现在为什么起作用,我将不胜感激?

这是我的代码:

clc;
clear;
close all;

%variables
y = [101.3 106.6 109.4 114.7 116.9 118.2 123.7];
x = [1.69 3.67 4.37 5.45 5.84 6.05 6.90];

%Linear interpolate to get pressure at v = 5 [m/s]
pressure = interp1(x,y,5)

%Spline interpolate to get pressure at v = 5 [m/s]
xx = 1.69:6.90;
yy = spline(x,y,5)

s = interp1(x,y,xx,"spline");

%linear plot output
hold on;
plot(x,y,'b.-')
plot(5,pressure,'*')
grid on;

%spline plot output
hold on;
plot(x,y,'o')
plot(xx,yy,'*')

和情节:

matlab plot interpolation spline
1个回答
0
投票

您正在绘制在 5 处进行插值的单一结果,

yy
。您需要绘制整个
xx
域的插值结果,
s
.

您还绘制了两次原始数据。我稍微清理了绘图和评论,使事情更精确。

y = [101.3 106.6 109.4 114.7 116.9 118.2 123.7];
x = [1.69 3.67 4.37 5.45 5.84 6.05 6.90];

%Linear interpolate to get pressure at v = 5 [m/s]
pressure = interp1(x,y,5)

%Spline interpolate to get pressure at v = 5 [m/s]
xx = 1.69:6.90;
yy = spline(x,y,5)

s = interp1(x,y,xx,"spline");

%original data plot
plot(x,y,'b.-')
grid on

%linear interpolation single point
hold on
plot(5,pressure,'*')

%spline interpolation single point
plot(5,yy,'square')

%spline plot output
plot(xx,s,'+')
hold off

请注意,您实际上执行了两次样条插值,一次用于

spline
,一次用于
interp1(x,y,xx,'spline')
。从
spline
取回整个样条结构并使用
ppval
从中提取插值可能会更清晰:

pp = spline(x,y);
yy = ppval(pp,5);
s = ppval(pp,xx);
© www.soinside.com 2019 - 2024. All rights reserved.