How can I interpolate a graph with 3 nodes?

5 次查看(过去 30 天)
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
Vimal Rathod 2021-6-23
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.
Refer to the following link to know more about splines

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Splines 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by