# Parametric spline in Matlab

• MATLAB
arian487
Basically, I'm supposed to write my name using splines in Matlab. Now, I'm having some trouble getting a parametric spline to work and I can't for the life of me figure out the problem. I'm making use of the spline toolbox and I have written a script as so:

Code:
%segment for the S
x1 = [0 2 1 2 3];
y1 = [1 0 2.5 1 3];

[x_t y_t t] = ParametricSpline(x1, y1);

xref = ppval (x_t, t);
yref = ppval(y_t, t);

plot(x1, y1, 'o', xref, yref);

The parametric spline does the following:

Code:
function [ x_t, y_t, tt ] = ParametricSpline(x , y)
%ParametricSpline Summary of this function goes here
%   Detailed explanation goes here
arc_length = 0;
n = length(x);
t = zeros(n, 1);

for i=2:n
arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2);
t(i) = t(i-1) + arc_length;
end

x_t = spline(t, x);
y_t = spline(t, y);

tt = linspace(1,n,1000);
end

When I run this, I get a parametric curve but it doesn't go through all of the points. For some reason it leaves a few out. However, if I don't run a linspace on tt it DOES go through all the points but it isn't a curve, just straight lines connecting the points. Any ideas on what I'm doing wrong?

maaud147
Basically, I'm supposed to write my name using splines in Matlab. Now, I'm having some trouble getting a parametric spline to work and I can't for the life of me figure out the problem. I'm making use of the spline toolbox and I have written a script as so:

Code:
%segment for the S
x1 = [0 2 1 2 3];
y1 = [1 0 2.5 1 3];

[x_t y_t t] = ParametricSpline(x1, y1);

xref = ppval (x_t, t);
yref = ppval(y_t, t);

plot(x1, y1, 'o', xref, yref);

The parametric spline does the following:

Code:
function [ x_t, y_t, tt ] = ParametricSpline(x , y)
%ParametricSpline Summary of this function goes here
%   Detailed explanation goes here
arc_length = 0;
n = length(x);
t = zeros(n, 1);

for i=2:n
arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2);
t(i) = t(i-1) + arc_length;
end

x_t = spline(t, x);
y_t = spline(t, y);

tt = linspace(1,n,1000);
end

When I run this, I get a parametric curve but it doesn't go through all of the points. For some reason it leaves a few out. However, if I don't run a linspace on tt it DOES go through all the points but it isn't a curve, just straight lines connecting the points. Any ideas on what I'm doing wrong?

Hi, you just have to normalize the curve you are studying. linspace (0,n,1000) considerer that the length of the path is n, what is wrong. The incrementation t must be from 0 to 1 (total length). I give you the rigth code now:

function [ xt, yt, tt ] = ParametricSpline(x,y)
%ParametricSpline Summary of this function goes here
% Detailed explanation goes here
arc_length = 0;
n = length(x);
t = zeros(n, 1);

for i=2:n
arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2);
t(i) = t(i-1) + arc_length;
end
t=t./t(length(t));
xt = spline(t, x);
yt = spline(t, y);

tt = linspace(0,1,1000);
end

The other code is correct.