How to Connect Data Points As If Hand-Drawn in Matlab

162 views Asked by At

I am trying to connect six Data points in a way as I would do it intuitively by hand. The regular plot command gives me a very jagged line because it's only six data points. The spline command gets close to were I want to be. However it continues to fit the line before and after. I however would like for it to start with the first data point and to end with the last. Further I would like to avoid the bump shown in the picture attached. For this I would need a higher order polynomial I'd assume.

Can someone help me to connect those data points as one would do it manually? It does not need to be done via the spline command.

Minimal Example:

clear all, close all, clc
%% Minimal Example

%% Data
x = 2:7;
y = [69, 27, 3, 0.5, 0, 0]

%% Using Regular Plot
plot(x,y)

%% Using Spline
hold on
xx = 0:.25:10;
yy = spline(x,y,xx);
plot(x,y,'o',xx,yy)

Data points connected via plot and spline

Bump

1

There are 1 answers

1
Timon On

For stopping the spline at the desired beginning and ending I had edit xx = 2:.25:7 to the desired boundaries 2 to 7. I oversaw that option.

In order to get rid of the bump I edited yy with the following for-loop. This does the job for me right now.

I'd still be thankful if someone knows how to just connect data points with a more curvy line. Thank you!

clear all, close all, clc
%% Minimal Example

%% Data
x = 2:7;
y = [69, 27, 3, 0.5, 0, 0]

%% Using Regular Plot
plot(x,y)

%% Using Spline
hold on
xx = 0:.25:10;
yy = spline(x,y,xx);
i = 1;
xx = 2:.05:7;
yy = spline(x,y,xx);
for i = 1:length(yy)
    if yy(i) < 0.3
        idx = i
        for idx = idx:length(yy)
            yy(idx) = 0.3
            idx = idx + 1
        end
    end
end
plot(xx,yy,'LineWidth',3)