# error using spline: The first input must contain unique values.

10 views (last 30 days)
Stefi on 9 Apr 2024
Commented: Bruno Luong on 10 Apr 2024
i am using spline function and its showing error message: The first input must contain unique values.
i am running matlab code for the initial geometry of the semicircular curved beam. When same code is run for quarter circular beam, spline is not showing any error.
How can I proceed ?
%x represents all the gauss quadrature points on the beam which are already calculated.
%initial geometry
th = linspace(pi/2, -pi/2, ng); %semicircle beam
x = R*cos(th) ;
y = R*sin(th)+1;
plot(x,y); axis equal;
theta = spline(x,dd(y),1); %calculating the end point slope of the beam .
% dd function is created for numerical differentiation
I can provide other parts of code if required.
##### 3 CommentsShow 1 older commentHide 1 older comment
Stefi on 10 Apr 2024
whats looks wrong with the usage ? I am just trying to calculate the slope using spline.
Bruno Luong on 10 Apr 2024
What is wrong? I keep saying that
• you cannot call spline on x since what ever the second argument i is not a function of x.
• The end points has x = 0 in your example (cos(p/2) and cos(-pi/2), why you call spline with 1 as query point? It is not the end point.
• If you already have dd(y), from your own word it's already a slope, then you already have a slope at all the x points, includng the end point. Whar is the purpose of calluing spline that only make INTERPOLATION of whatever you have provided as second input, meaning dd(y).

Bruno Luong on 9 Apr 2024
Edited: Bruno Luong on 9 Apr 2024
You did not post your spline command, my guess is you do spline(x,y)
Try to remove the last (or the first value of th, then do the rest
th = linspace(pi/2, -pi/2, ng); %semicircle beam
th(end) = [];
% ...
Or better do the before last two commands before callinfg splie(x,y)
This will fix the error but I doubt it will do what you want. Please see mu othe answer
tt = linspace(-pi,pi);
x = cos(tt);
y = sin(tt);
% spline(x,y); % error
[xu,~,J] = unique(x);
yu = accumarray(J(:), y(:), [], @mean);
s = spline(xu, yu);
Stefi on 9 Apr 2024
Edited: Stefi on 9 Apr 2024
theta = spline(x,dd(y),1);
this is my spline command . here dd is a function for differentiation.. basically it is used to calculate slope
Bruno Luong on 9 Apr 2024
Edited: Bruno Luong on 9 Apr 2024
At the glance your math/code looks strange to me.
My comment still applies: you cannot use spline on parametric data by ignoring the parameter (here th). The second argument dd(y) MUST implicitly be a function of the first argument (x).As x turns around each y has 2 branches values for each x. This is NOT a function.

Bruno Luong on 9 Apr 2024
Edited: Bruno Luong on 9 Apr 2024
Your spline command wouldn't do what you want, since for every x you have made corresponding two y values. It is NOT a function of x; so it cannot be represented/approximated by spline function/
You better call twice spline! x wrt tt and y wrt to tt.yi
ng = 7;
th = linspace(pi/2, -pi/2, ng); %semicircle beam
x = R*cos(th) ;
y = R*sin(th)+1;
plot(x,y,'o-.'); axis equal;
ti = linspace(min(th),max(th));
xi = spline(th,x,ti);
yi = spline(th,y,ti);
hold on
plot(xi,yi);
legend('data','spline interpolation')
Stefi on 10 Apr 2024
can you help me with the spline according to my code as I am really new to matlab coding. I have edited my ques and provided the spline code part. I am basically trying to find the slope at the end point of the beam using spline

### Categories

Find more on Spline Postprocessing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!