Array indices must be positive integers or logical values.
1 view (last 30 days)
Show older comments
Hey, I m trying to do a bissecion method code.
But when trying to give a var fa the value of f(a) it gives me a :'Array indices must be positive integers or logical values.'
Could anyone explain me why please ?
clear;
tol = 10^(-6);
x1= -2:0.01:2;
fx = x1.^2-1-sin(x1);
a=input('Intervalo inferior: ');
b=input('Intervalo superior: ');
fa=fx(a);
Array indices must be positive integers or logical values.
fb=fx(b);
if sign(fa)==sign(fb)
disp('no tiene solucion')
return
end
while (b-a)/2 > tol
c = (a+b)/2;
fc= fx(c);
if sign(a)==sign(c)
a=c;
fa=fc;
else
b=c;
fb=fc;
end
end
c
2 Comments
DGM
on 15 Feb 2024
a and b are not constrained to being positive nonzero integers, so you can't use them as array indices.
Accepted Answer
More Answers (1)
VBBV
on 15 Feb 2024
Matlab arrays are indexed using whole integers, when you give a fractional or decimal values as input it throws such errors
clear;
tol = 10^(-6);
x1= -2:0.01:2;
fx = x1.^2-1-sin(x1)
a=2.7 %input('Intervalo inferior: ');
b=3.2 %input('Intervalo superior: ');
fa=fx(round(a)) % round the input values
clear;
tol = 10^(-6);
x1= -2:0.01:2;
fx = x1.^2-1-sin(x1)
a=2.5; %input('Intervalo inferior: ');
b=3.2; %input('Intervalo superior: ');
fa=fx(a)
See Also
Categories
Find more on Matrix Indexing 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!