"Array indices must be positive integers or logical values". Please help
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kevin Zhou
le 2 Oct 2020
Réponse apportée : Steven Lord
le 2 Oct 2020
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/370123/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/370126/image.png)
%For this problem write a script file called NC.m that implements
%the Newton-Cotes method of integration for an arbitrary function f(x). It
%should take as inputs the function and the limits of integration [a: b] and
%output the value of the definite integral. Specifically, you should use the
%Trapezoid rule as presented in Equation (11.73)
function [f]= NC(a,b,fun) %newton-cotes
%a and b are limits of intergration
%setting it up
f(a)= fun(a); %y value for lower limit
f(b)= fun(b); %y value for upper limit
%the actual function
f= (b-a)*(f(a)+f(b))/2;
end
What am i doing wrong? Whe I type, [f]= NC(-3,0,fun) and set fun= @(x)normpdf(x) . it keeps on returning "Array indices must be positive integers or logical values". Can someone shine some light on this?
0 commentaires
Réponse acceptée
Steven Lord
le 2 Oct 2020
By the way you called your function:
[f]= NC(-3,0,fun)
you're trying to assign to the -3rd element of f with the code:
f(a)= fun(a); %y value for lower limit
and to the 0th element of f with:
f(b)= fun(b); %y value for upper limit
Neither of those are valid in MATLAB. The first element of an array in MATLAB is element 1.
Try just assigning to fa and fb instead and using those variables later in your code.
0 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!