MATLAB Answers

How can I interpolate a graph with 3 nodes?

조회 수: 1(최근 30일)
Filipe Silva
Filipe Silva 2021년 6월 20일
답변: Vimal Rathod 2021년 6월 23일
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개)

Community Treasure Hunt

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

Start Hunting!

Translated by