Array indices must be positive integers
Show older comments
I'm trying to use the midpoint formula to compute the value of the integral, but it keeps showing the error message "Array indices must be positive integrs or logical values." I don't know how to solve it. Thanks!


Accepted Answer
More Answers (1)
assign the func to f inside the custom function as below , probably you mean this
f = @(x) x.^3.*exp(-x.^3);
a = 0; b = 10; n = 100;
mid_val = integral_mid(f,a,b,n)
function mid_val = integral_mid(f,a,b,n)
h = (b-a)/n;
%x = a:h:b;
% assign the func to f
func = f;
xmid = (a+h/2):h:b;
mid_val = h*sum(func(xmid));
end
Categories
Find more on Creating and Concatenating Matrices 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!