Array indices must be positive integers or logical values.
Show older comments
clc;
x = -6; %input value for following expression
if x < -5
f(x) = 0;
elseif -5 <= x && x < 5;
f(x) = 2 * x^1/2 + exp(x/4);
elseif 5 <= x && x <= 20;
f(x) = log((x^2));
else
f(x) = 0;
end
f(x)
how can i get my code to work for values -infinity to 0 for this expression? i keep getting this error code
Array indices must be positive integers or logical values.
Error in Homework_two (line 11)
f(x) = 0;
Accepted Answer
More Answers (1)
Steven Lord
on 5 Nov 2022
x = -6; %input value for following expression
if x < -5
f(x) = 0;
There's no such thing as element -6 of an array in MATLAB. If you want f to be one element long and you want its value to depend on x, assign to f not f(x).
Categories
Find more on Logical 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!