# 1*0 empty double row vector

5 visualizzazioni (ultimi 30 giorni)
Chirag il 17 Mar 2023
Commentato: Chirag il 17 Mar 2023
X = [-14 -12 -10. -6. -2 2. 6. 8 10 12 14 16 18];
Y = [-0.8 -1.1 -.88 -.44 0 .44 .88 1.1 1.32 1.54 1.76 1.98 1.7];
y1 = intrp(-20,X,Y)
out = -0.8000
y1 = -0.8000
y2 = intrp(-11,X,Y)
out = -0.9900
y2 = -0.9900
y3 = intrp(9,X,Y)
out = 1.2100
y3 = 1.2100
y4 = intrp(19,X,Y)
out = 1×0 empty double row vector y4 = 1×0 empty double row vector
function out = intrp(in,X,Y)
% interpolate a 2-d function
% in: input value of x
% X: input vector
% Y: output vector
% out: output value of Y=f(X) for X = in
X = [-14 -12 -10. -6. -2 2. 6. 8 10 12 14 16 18];
Y = [-0.8 -1.1 -.88 -.44 0 .44 .88 1.1 1.32 1.54 1.76 1.98 1.7];
if (in < -14)
loc = find(in < X,1);
out = Y(loc)
elseif (in > 18)
loc = find(in < X,1);
out = Y(loc-1)
else
loc = find(in < X,1);
out = Y(loc-1)+(Y(loc)-Y(loc-1))/(X(loc)-X(loc-1))*(in-X(loc-1))
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (1)

Walter Roberson il 17 Mar 2023
19 < X is never true, so find() is going to return empty.
What result were you hoping for in the case where the input value is greater than all of the X values?
Question: why are you passing X into your function but then ignoring the input X and re-assigning values to X inside the function?
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Walter Roberson il 17 Mar 2023
I suspect you want an algorithm closer to:
in value less than or equal to X(1) should return Y(1)
in value greater than or equal to X(end) should return Y(end)
Chirag il 17 Mar 2023
Yes. Something like that.

Accedi per commentare.

### Categorie

Scopri di più su Elementary Math in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by