I wrote this function, which should calculates the values of the linear interpolation
Show older comments
I wrote this function, which should calculates the values of the linear interpolation function x values given by Xl = [xmin,
xmax]:
function [ X, Y ] = linterp( a, b, Xl, n ) % Xl = [xmin, xmax]
x = linspace( Xl(1), Xl(2), n + 1 ) ;
result = a*x + b ;
X = result(1) ;
Y = result(2) ;
end
I compared the results with polyval
Y2 = polyval( [ a b ], X )
and in 2 out of 3 cases the function works...
What is going wrong?
for linterp( 1, 0, [0 1], 2 ) - the answer should be 0, but it 0.5
for other examples answers are great.
3 Comments
For a=1, b=0, Xl = [0 1] and n = 2, your function "linterp" works as follows:
x = linspace(0,1,2+1) gives x = [0 0.5 1]
result = a*x + b then gives result = 1*[0 0.5 1] + 0 = [0 0.5 1]
X = result(1) gives X = 0
Y = result(2) gives Y = 0.5.
So X = 0, Y = 0.5 is returned from "linterp".
If you want your function to return something different, you will have to explain what.
Ihor
on 10 Dec 2022
Y = polyval( [ 1 0 ], [0 1] )
This call to "polyval" evaluates the linear function p(x) = 1*x + 0 = x at x=0 and x=1.
What does this has to do with interpolation ?
Accepted Answer
More Answers (0)
Categories
Find more on Interpolation 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!