problem with coding for linear interpolation
6 views (last 30 days)
Show older comments
Hello all,
I am new to matlab and I am running this code but it is not running at all and the loop won't end. Can you please help me why this happens?
blow = -2;
bhigh = 4;
bnum = 10;
lb = log(blow-blow+1.0)/log(10);
ub = log(bhigh-blow+1.0)/log(10);
bgrid = logspace(lb,ub,bnum);
bgrid = bgrid+ones(size(bgrid))*(blow-1.0);
for i=1:1:bnum
bfval = bgrid(i);
ilow = 1; ihigh = 10;
distance=2;
value = bfval;
while (distance>1)
if (value>-0.6254)
ilow=5;
else
ihigh = 5;
end
distance = ihigh- ilow;
end
iloc = ilow;
if (value > bgrid(iloc))&&(value<bgrid(iloc+1))
weight = (bgrid(iloc+1)-value)/(bgrid(iloc+1)-bgrid(iloc));
elseif (value<=grid(1))
weight = 1; iloc = 1;
else
weight = 0; iloc = 9;
end
end
Thank you so much for your help.
1 Comment
KSSV
on 23 Nov 2021
The condition
distance>1
is always met as the value of distance = 4 is fixed always and not changing.
Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!