Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

matlab - Smooth plot of non-dependent variable graph

Let's say we have the following data:

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];

When I plot the above data using plot(A1,A2);, I get this graph:

enter image description here

Is there any way to make the graph look smooth like a cubic plot?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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);

I now get the following:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...