Why isn't my parametric spline in Matlab going through all points?

In summary, the conversation was about using splines in Matlab to write a name and a problem with getting a parametric spline to work. The issue was solved by normalizing the curve and adjusting the linspace incrementation. This resulted in a correct curve that goes through all of the given points.
  • #1
arian487
5
0
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?
 
Physics news on Phys.org
  • #2
arian487 said:
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.
 

FAQ: Why isn't my parametric spline in Matlab going through all points?

What is a parametric spline in Matlab?

A parametric spline in Matlab is a mathematical curve that is defined by a set of control points and a set of parameters. It is commonly used in computer graphics and numerical analysis to create smooth and continuous curves that can be easily manipulated and controlled.

How do I create a parametric spline in Matlab?

To create a parametric spline in Matlab, you can use the "spline" function. This function takes in a set of control points and a parameter vector as inputs and returns a piecewise polynomial representation of the curve. You can then use this representation to plot the parametric spline.

What is the difference between a parametric spline and a non-parametric spline?

A parametric spline is defined by a set of control points and a set of parameters, while a non-parametric spline is defined solely by a set of control points. This means that a parametric spline can be easily adjusted and manipulated by changing the parameters, while a non-parametric spline is more rigid.

How can I manipulate a parametric spline in Matlab?

In Matlab, you can manipulate a parametric spline by changing the control points or the parameter vector. You can also use the "fplot" function to plot the spline over a desired range and see how it changes with different parameters. Additionally, there are various functions and tools in Matlab that can be used to modify and manipulate parametric splines.

Can I convert a parametric spline to a non-parametric spline in Matlab?

Yes, you can convert a parametric spline to a non-parametric spline in Matlab by using the "fnplt" function. This function takes in the piecewise polynomial representation of the parametric spline and returns a set of control points that define the equivalent non-parametric spline.

Similar threads

Replies
2
Views
3K
Replies
7
Views
3K
Replies
10
Views
2K
Replies
1
Views
2K
Replies
10
Views
17K
Back
Top