Clear Filters
Clear Filters

Please, I want to get a smooth curve between two points

38 views (last 30 days)
Please, I need a smooth curve between two points. The result I get from the current algorithm is not interesting. Please, find attached picture. The curves are between two red + sign. The current code I am using is this:
x_k = [x_1 x_2]';
y_k = [y_1 y_2]';
plot( x_k, y_k, '+r' ); % plot the original points
n = numel(x_k); % number of original points
xi = interp1( 1:n, x_k, linspace(1, n, 10*n) ); % new sample points
yi = interp1( x_k, y_k, xi );
hold all;
plot( xi, yi ); % should be smooth between the original points
Please, help me take a look into my code, make necessary adjustment or give a better code to compute smooth curves. Thank you.

Answers (1)

Gautham Sholingar
Gautham Sholingar on 18 Jul 2017
The premise of your question is a little misleading. The only way to connect two points is with a straight line. Could you clarify what you mean by draw a curve between two points?
This would make sense if you had a collection of several points and you were attempting to draw a smooth curve through these points.
You can use the code you have attached to get an interpolated version.
Alternatively you can use the curve fitting toolbox to get different types of fits for your data as shown below
figure
x_k = [1:2:10]
y_k = [2,8,10,11,22]
plot( x_k, y_k, '+r' ); % plot the original points
n = numel(x_k); % number of original points
xi = interp1( 1:n, x_k, linspace(1, n, 10*n) ); % new sample points
yi = interp1( x_k, y_k, xi );
hold on
plot( xi, yi ); % should be smooth between the original points
hold off
%%Alternatively you can use the curve fitting toolbox to get different fits
figure
f = fit(x_k',y_k','smoothingspline')
plot(f,x_k,y_k)
The following documentation link is a useful starting point for this: http://www.mathworks.com/help/curvefit/fit.html
Another approach for this is to use the polyfit function to fit a polynomial for your dataset as shown in the following doc link: http://www.mathworks.com/help/matlab/ref/polyfit.html
All of these approaches should be a good starting point for you to fit curves to your data.
  1 Comment
Indrayani
Indrayani on 28 Jul 2023
I have traced some points on a png image. Those points shown by b+. I want to form a smooth cubic spline curve between these points . How to do that?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!