Azzera filtri
Azzera filtri

"Array indices must be positive integers or logical values". Please help

1 visualizzazione (ultimi 30 giorni)
%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?

Risposta accettata

Steven Lord
Steven Lord il 2 Ott 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.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by