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

  • Context: MATLAB 
  • Thread starter Thread starter arian487
  • Start date Start date
  • Tags Tags
    Matlab Parametric
Click For Summary
SUMMARY

The discussion centers on creating a parametric spline in Matlab using the spline toolbox to plot a curve that passes through specified points. The original implementation failed to include all points when using the linspace function. The solution provided involves normalizing the parameterization by adjusting the linspace range from 0 to 1, ensuring the spline accurately represents the intended path. The corrected code effectively resolves the issue, allowing the spline to pass through all specified points.

PREREQUISITES
  • Understanding of Matlab programming and syntax
  • Familiarity with spline interpolation techniques
  • Knowledge of parametric equations and their applications
  • Basic grasp of arc length calculation in 2D space
NEXT STEPS
  • Explore Matlab's spline toolbox documentation for advanced features
  • Learn about curve fitting techniques in Matlab
  • Investigate normalization methods for parametric curves
  • Study the effects of different parameterizations on spline behavior
USEFUL FOR

Matlab users, data visualization specialists, and engineers working on graphical representations of data through splines.

arian487
Messages
5
Reaction score
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
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 10 ·
Replies
10
Views
18K