非因变量图的光滑图 [英] Smooth plot of non-dependent variable graph
问题描述
假设我们有以下数据:
A1= [41.3251
18.2350
9.9891
36.1722
50.8702
32.1519
44.6284
60.0892
58.1297
34.7482
34.6447
6.7361
1.2960
1.9778
2.0422];
A2=[86.3924
86.4882
86.1717
85.8506
85.8634
86.1267
86.4304
86.6406
86.5022
86.1384
86.5500
86.2765
86.7044
86.8075
86.9007];
当我使用plot(A1,A2);
绘制上述数据时,得到以下图表:
When I plot the above data using plot(A1,A2);
, I get this graph:
有什么方法可以使图形看起来像三次方图一样平滑吗?
Is there any way to make the graph look smooth like a cubic plot?
推荐答案
可以.您可以在关键点之间进行插值.但是,这将需要一些技巧.盲目地将插值与MATLAB的任何命令结合使用是行不通的,因为它们要求增加独立轴(在您的情况下为x
轴).您目前无法使用您的数据执行此操作……至少是开箱即用的.因此,您必须创建一个虚拟的值列表,该值的范围从1到与A1
(或A2
,因为它们的大小均相等)中的元素一样多,以创建独立的轴并内插<通过在解析度上指定更小的间距来指定虚拟列表,从而独立地实现strong> both 阵列.较细的间距由要在绘图中引入的新点的总数控制.这些点将在虚拟列表的范围内定义,但是随着您增加新点的总数,每个点之间的间隔将减小.通常,添加的点越多,间距就越小,因此绘图应更平滑.完成此操作后,将最终值绘制在一起.
Yes you can. You can interpolate in between the keypoints. This will require a bit of trickery though. Blindly using interpolation with any of MATLAB's commands won't work because they require that the independent axes (the x
-axis in your case) to increase. You can't do this with your data currently... at least out of the box. Therefore you'll have to create a dummy list of values that span from 1 up to as many elements as there are in A1
(or A2
as they're both equal in size) to create an independent axis and interpolate both arrays independently by specifying the dummy list with a finer spacing in resolution. This finer spacing is controlled by the total number of new points you want to introduce in the plot. These points will be defined within the range of the dummy list but the spacing in between each point will decrease as you increase the total number of new points. As a general rule, the more points you add the less spacing there will be and so the plot should be more smooth. Once you do that, plot the final values together.
这里有一些代码供您运行.我们将使用 interp1
为我们和大部分工作.函数 linspace
在虚拟对象中创建更精细的点网格列表以方便插值. N
将是您要绘制的所需点的总数.我现在将其设为500,这意味着将使用您的原始数据进行500点插值.通过增加(或减少)点总数进行实验,看看这对数据的平滑度有什么影响.
Here's some code for you to run. We will be using interp1
to perform the interpolation for us and most of the work. The function linspace
creates the finer grid of points in the dummy list to facilitate the interpolation. N
would be the total number of desired points you want to plot. I've made it 500 for now meaning that 500 points will be used for interpolation using your original data. Experiment by increasing (or decreasing) the total number of points and seeing what effect this has in the smoothness of your data.
我还将使用 Piecewise三次Hermite插值多项式或pchip
作为插值方法,如果您想获取技术知识,这基本上是三次样条插值.假设已经创建了A1
和A2
:
I'll also be using the Piecewise Cubic Hermite Interpolating Polynomial or pchip
as the method of interpolation, which is basically cubic spline interpolation if you want to get technical. Assuming that A1
and A2
are already created:
%// Specify number of interpolating points
N = 500;
%// Specify dummy list of points
D = 1 : numel(A1);
%// Generate finer grid of points
NN = linspace(1, numel(A1), N);
%// Interpolate each set of points independently
A1interp = interp1(D, A1, NN, 'pchip');
A2interp = interp1(D, A2, NN, 'pchip');
%// Plot the data
plot(A1interp, A2interp);
我现在得到以下信息:
这篇关于非因变量图的光滑图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!