Effacer les filtres
Effacer les filtres

How to fix error "Array indices must be positive integers or logical values."

1 vue (au cours des 30 derniers jours)
I am fairly new to MATLAB, and I am trying to write a code in MATLAB for the Bisect method, like so:
% Bisect Method
% Computes approximate solution of f(x) = 0
% Input: function handle f: a, b, s.t. f(a) * f(b) < 0
% and tolerance handle tol
% Output: Approximate solution xc
function xc = mybisect(f, a, b, tol)
format longg;
a_vals = zeros(a, length(tol));
b_vals = zeros(b, length(tol));
c_vals = zeros(1, length(tol));
if sign(f(a)) * sign(f(b)) >= 0
error ('f(a)f(b) < 0 NOT SATISFIED!')
end
fa = f(a);
fb = f(b);
while (b-a) / 2 > tol
c = (a+b) / 2;
fc = f(c);
c_vals(1i) = c;
if (fc == 0) % c is a solution
break
end
if sign(fc) * sign(fa) < 0 % a and c make new interval
b = c;
fb = fc;
b_vals(2i) = b;
else % b and c make new interval
a = c;
fa = fc;
a_vals(2i) = a;
end
xc = (a + b) / 2;
iter_nr = size(c_vals, 2);
end
fprintf('\n');
disp("A-VALUES:")
perLine = 5;
fmt = [repmat('%8.10f ', 1, perLine), '\n'];
fprintf(fmt, a_vals);
if mod( length(a_vals), perLine) ~= 0
fprintf('\n');
end
fprintf('\n');
disp("B-VALUES:")
perLine = 5;
fmt = [repmat('%8.10f ', 1, perLine), '\n'];
fprintf(fmt, b_vals);
if mod( length(b_vals), perLine) ~= 0
fprintf('\n');
end
fprintf('\n');
disp("C-VALUES:")
perLine = 5;
fmt = [repmat('%8.10f ', 1, perLine), '\n'];
fprintf(fmt, c_vals);
if mod( length(c_vals), perLine) ~= 0
fprintf('\n');
end
fprintf('\n');
disp("Number of iterations : " + iter_nr);
end
But I get this error-message:
Array indices must be positive integers or logical values.
Error in mybisect (line 24)
c_vals(1i) = c;
I don't understand why. How do I fix this?
  2 commentaires
Torsten
Torsten le 5 Oct 2022
a_vals = zeros(a, length(tol));
b_vals = zeros(b, length(tol));
c_vals = zeros(1, length(tol));
a and b are real numbers - it's not possible to give an array the size of a real number.
tol is a scalar - thus length(tol) = 1. Also this setting in the dimensioning part does not make sense.
c_vals(1i) = c;
b_vals(2i) = b;
a_vals(2i) = a;
1i and 2i are not defined before. Further, variable names are not allowed to start with a number.
But worse: MATLAB interprets i in the expressions as the complex unit.
Thus complete confusion.
Rik
Rik le 6 Oct 2022
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.

Connectez-vous pour commenter.

Réponse acceptée

Chunru
Chunru le 5 Oct 2022
function xc = mybisect(f, a, b, tol)
format longg;
a_vals = zeros(a, length(tol));
b_vals = zeros(b, length(tol));
c_vals = zeros(1, length(tol));
if sign(f(a)) * sign(f(b)) >= 0
error ('f(a)f(b) < 0 NOT SATISFIED!')
end
fa = f(a);
fb = f(b);
i = 1; % initialize i <===============================
while (b-a) / 2 > tol
c = (a+b) / 2;
fc = f(c);
%c_vals(1i) = c; % 1i is the complex number
c_vals(i) = c; % <===========================
if (fc == 0) % c is a solution
break
end
if sign(fc) * sign(fa) < 0 % a and c make new interval
b = c;
fb = fc;
b_vals(2i) = b;
else % b and c make new interval
a = c;
fa = fc;
a_vals(2i) = a;
end
xc = (a + b) / 2;
iter_nr = size(c_vals, 2);
i = i+1; % <==========================================
end

Plus de réponses (0)

Catégories

En savoir plus sur Particle & Nuclear Physics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by