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

1 次查看(过去 30 天)
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 个评论
Torsten
Torsten 2022-10-5
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 2022-10-6
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.

请先登录,再进行评论。

采纳的回答

Chunru
Chunru 2022-10-5
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by