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

조회 수: 2 (최근 30일)
%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?

채택된 답변

Steven Lord
Steven Lord 2020년 10월 2일
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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by