How to fix error "Array indices must be positive integers or logical values."
2 ビュー (過去 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
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
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
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Particle & Nuclear Physics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!