Spline interpolation plot in Matlab

huangapple go评论55阅读模式
英文:

Spline interpolation plot in Matlab

问题

我尝试在同一图中输出几个图形,使用不同的插值方法,以找到x = 5时的y,并用红色星号标记该点。我已经完成了线性插值,效果很好(意味着我得到了一个好的图形),但当我尝试输出样条插值图时,效果不太好(正如您所看到的,它只显示了一行星号)。我希望能帮助理解为什么它现在不起作用?

这是我的代码:

clc;
clear;
close all;

% 变量
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];

% 线性插值以获取v = 5 [m/s]时的压力
pressure = interp1(x, y, 5);

% 样条插值以获取v = 5 [m/s]时的压力
xx = 1.69:6.90;
yy = spline(x, y, 5);

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

% 线性图输出
hold on;
plot(x, y, 'b.-');
plot(5, pressure, '*');
grid on;

% 样条图输出
hold on;
plot(x, y, 'o');
plot(xx, yy, '*');

和图表:Spline interpolation plot in Matlab

英文:

i'm trying to output few graphs in the same plot using different interpolations in order to find the y when x = 5 and mark that point with a red asterisk. I did the linear and it's working good (means i get a good plot) but when i'm trying to output the spline plot it doesn't work very well (as you can see it only shows a line of asterisks). i would appreciate some help with understanding why it is now working?

This is my code:

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,'*')

and the plot:Spline interpolation plot in Matlab

答案1

得分: 1

你正在绘制在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];

% 线性插值以获取v = 5 [m/s]时的压力
pressure = interp1(x, y, 5);

% 样条插值以获取v = 5 [m/s]时的压力
xx = 1.69:6.90;
yy = spline(x, y, 5);

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

% 绘制原始数据
plot(x, y, 'b.-');
grid on;

% 线性插值单个点
hold on;
plot(5, pressure, '*');

% 样条插值单个点
plot(5, yy, 'square');

% 样条插值输出
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);
英文:

You’re plotting the single result of interpolating at 5, yy. You need to plot the result of the interpolation across the whole xx domain, s.

You were also plotting your original data twice. I cleaned the plotting and comments a bit, to make things more precise.

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

Note that you’re actually performing the spline interpolation twice, once for spline and once for interp1(x,y,xx,'spline'). It might be cleaner to get the entire spline structure back from spline and extract your interpolated values from it using ppval:

pp = spline(x,y);
yy = ppval(pp,5);
s = ppval(pp,xx);

huangapple
  • 本文由 发表于 2023年2月26日 20:49:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定