- Index
- » Users
- » francesco.dallorto
- » Profile
Posts
Posts
Page Start Prev 1 Next End
Hello everyone,
I wrote this function in order to interpolate the value between 2 points of an n-points vector and extrapolate (linear extrapolation) the value over the last point of the vector (and previous the first).
Code:
function InterpolateVector
input Real x "Independent variable";
input Real vectorPoints[:, 2] "points of the function";
output Real y "Dependent variable";
protected
Integer i;
Integer n = size(vectorPoints, 1) "Number of interpolation points";
Real p;
Real y1;
Real y2;
Real x1;
Real x2;
Real q;
algorithm
if x < vectorPoints[1, 1] then
p := (x - vectorPoints[1, 1]) / (vectorPoints[2, 1] - vectorPoints[1, 1]);
y := p * vectorPoints[2, 2] + (1 - p) * vectorPoints[1, 2];
elseif x > vectorPoints[n, 1] then
p := (x - vectorPoints[n, 1]) / (vectorPoints[n-1, 1] - vectorPoints[n, 1]);
y := p * vectorPoints[n-1, 2] + (1 - p) * vectorPoints[n, 2];
else
i := 1;
while x >= vectorPoints[i + 1, 1] loop
i := i + 1;
end while;
p := (x - vectorPoints[i, 1]) / (vectorPoints[i + 1, 1] - vectorPoints[i, 1]);
y := p * vectorPoints[i + 1, 2] + (1 - p) * vectorPoints[i, 2];
end if;
end InterpolateVector;
The extrapolated value (y) became a NaN when the input value (x) coincides with the last value of the array.
I can't figure out of this problem, could you please help me?
Thanks,
Best Regards
Page Start Prev 1 Next End
- Index
- » Users
- » francesco.dallorto
- » Profile