# How can I interpolate a graph with 3 nodes?

5 views (last 30 days)
Filipe Silva on 20 Jun 2021
Answered: Vimal Rathod on 23 Jun 2021
I have developed an RRT code that creates a simplified path from one node to another, and I need to smooth it using splines or any other way possible, the code that interpolates the nodes is:
function map_update(vertices,path)
[~, pathCount] = size(path);
for ii = 1 : pathCount - 1
plot([vertices(path(ii), 1), vertices(path(ii + 1), 1)], ...
[vertices(path(ii), 2), vertices(path(ii + 1), 2)], ...
'r', 'LineWidth', 2);
% Creation of spline
h=0:0.1:50;
t=spline([vertices(path(ii), 1), vertices(path(ii + 1), 1)], ...
[vertices(path(ii), 2), vertices(path(ii + 1), 2)], ...
h);
hold on
plot([vertices(path(ii), 1), vertices(path(ii + 1), 1)], ...
[vertices(path(ii), 2), vertices(path(ii + 1), 2)], ...
h,t);
end
end
However this gives me the following picture:
How can I make this interpolation go through the nodes with one line instead of creating two separate lines?
And is if it is possible, how can I make it work in the case I change the start and end locations?
Thank you

Vimal Rathod on 23 Jun 2021
Hi,
For splines to work effectively, you have to pass more than one point. Instead of the for loop which you were using, you could use the following code to plot data.
h = 0:0.1:50;
x = vertices(:,1);
y = vertices(:,2);
% xPath and yPath are variables taken from vertices of desired path
xPath = x(path(1:pathCount-1));
yPath = y(path(1:pathCount-1));
t = spline(xPath, yPath, h);
plot(xPath, yPath, h,y);
This way you can pass more than one point into spline function getting a smooth single curve. You could change start and end point and run spline again to replot the plot.

### Categories

Find more on Interpolation in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by