how to linear interpolate using a for loop

3 ビュー (過去 30 日間)
PetronasAMG
PetronasAMG 2021 年 10 月 6 日
回答済み: Star Strider 2021 年 10 月 6 日
say I have a y = x^2 -2;
I want to find out which point of x it crosses y = 0 by linear interpolating. But by showing when the result flipes its signs. (meaning once we narrow out interpolation towards y=0, bottom side of y should be - and top portion of y should be +).Then narrow down until i get y = 0. How would I be able to use linear internpolation to find the exact data when y = 0?
I am getting some suggestion using a for loop. could you please show me an sample code how to linear internpolate such case?

採用された回答

Star Strider
Star Strider 2021 年 10 月 6 日
x = linspace(-50, 50, 250);
y = x.^2 -2;
zx = find(diff(sign(y)))
zx = 1×2
121 129
for k = 1:numel(zx)
idxrng = max(1,zx(k))-2 : min(zx(k)+2, numel(x));
xi(k) = interp1(y(idxrng), x(idxrng), 0);
end
fprintf('x(y=0) = %10.6f\n', xi)
x(y=0) = -1.413163 x(y=0) = 1.413163
figure
plot(x, y)
hold on
plot(xi, zeros(size(xi)), 'xr', 'MarkerSize',15)
hold off
grid
xlim([-10,10])
This is a representative approach to this sort of problem.
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by