Clear Filters
Clear Filters

How to get X values given an Y from a Spline

7 views (last 30 days)
João
João on 12 Feb 2018
Edited: John D'Errico on 12 Feb 2018
Hey guys, I obtained a bunch of splines (given one at a time from a for cycle). From this curves I would like to know the values of x at random y points, example for ymin:ymax:10 and store each one of these points from each spline for later use.
My code is this (the for cycles leading up to the spline functionare there to change the data from errors and small deviations):
z1 = zeros(1,numWls);
z = zeros(1,numWls);
ymin = newSec.FOB;
ymax = newSec.DWL;
xx=zeros(1,numWls+2);
yy=zeros(1,numWls);
y = linspace(ymin , ymax , numWls);
for j=1:numWls
z(j) = z1(j);
z1(j) = newSec.FSectBWL(y(j));
end;
xx=(y);
yy=z1;
for j=2:numWls-1
if yy(j)<= yy(j-1)
yy(j)=(yy(j+1)+yy(j-1))/2.0;
end
end
for j=1:numWls
if xx(j)>newSec.FOB
if yy(j)<0
yy(j)=0.001;
end
else
yy(j)=(1-newSec.KeelRise);
end
end
cs=spline(xx,[tan(deg2rad(newSec.KeelSlope)) yy tan(deg2rad(newSec.DwlSlope))]);
intx=linspace(newSec.FOB,newSec.DWL,numWls);
for j=1:numWls
if intx(j)<newSec.FOB
y = 0;
elseif intx(j)>=newSec.FOB
y = ppval(cs,intx);
end
end
plot(intx,y,intx,0.9);
grid on;

Answers (1)

John D'Errico
John D'Errico on 12 Feb 2018
Edited: John D'Errico on 12 Feb 2018
I provide code for this in my SLM toolbox , which provides additional tools for computations with splines that also work on pp form functions returned from spline or pchip. All that you need is the slmsolve utility, which generates all the roots (in case there are multiple solutions.)
For example...
x = 0:.25:5;
y = sin(x);
pp = spline(x,y);
xsol = slmsolve(pp,.5)
xsol =
0.5236 2.618
The values returned are those which produce the indicated function value, so effectively inverting the spline.
format long g
ppval(pp,xsol)
ans =
0.5 0.5

Community Treasure Hunt

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

Start Hunting!