필터 지우기
필터 지우기

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