Array indices must be positive integers
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Dan
am 5 Mai 2023
Beantwortet: Steven Lord
am 5 Mai 2023
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!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1375889/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1375894/image.png)
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 5 Mai 2023
The variable func is a vector, the value of the function handle f evaluated at the points in x. On the line where you compute mid_val you're indexing into that vector with values that likely aren't integer values. Were you trying to evalate the function handle f at those points? If so use f instead of func on that line.
0 Kommentare
Weitere Antworten (1)
VBBV
am 5 Mai 2023
Bearbeitet: VBBV
am 5 Mai 2023
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!