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

조회 수: 10 (최근 30일)
Stefi
Stefi 2024년 4월 9일
댓글: Bruno Luong 2024년 4월 10일
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
R = 1; % radius
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
Stefi
Stefi 2024년 4월 10일
whats looks wrong with the usage ? I am just trying to calculate the slope using spline.
Bruno Luong
Bruno Luong 2024년 4월 10일
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).

댓글을 달려면 로그인하십시오.

답변 (2개)

Bruno Luong
Bruno Luong 2024년 4월 9일
편집: Bruno Luong 2024년 4월 9일
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);
  댓글 수: 2
Stefi
Stefi 2024년 4월 9일
편집: Stefi 2024년 4월 9일
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
Bruno Luong 2024년 4월 9일
편집: Bruno Luong 2024년 4월 9일
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
Bruno Luong 2024년 4월 9일
편집: Bruno Luong 2024년 4월 9일
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
R = 1; % radius
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')

카테고리

Help CenterFile Exchange에서 Splines에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by