Hi, I actually do not understand what I have done wrong, but the code just gives out an error all the time.
function s=integrate(f,a,b,n)
x=0:(n-1);
h=(b-a)/n;
x=a+h*x;
s=0;
for i=1:n
s=s+f(x(i))*h;
end
end
For integrate(1,2,3,4) i get the error
Index exceeds the number of array elements (1).
Error in integrate (line 7)
s=s+f(x(i))*h;'
and for f=sin(x)
'Undefined function or variable 'x'.

 채택된 답변

Walter Roberson
Walter Roberson 2019년 3월 3일

1 개 추천

You probably need to pass @sin or @(x) sin(x) and you are probably instead passing sin(x) without any @

댓글 수: 1

tim123
tim123 2019년 3월 3일
thanks, i have not known anything about @ yet

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Special Functions에 대해 자세히 알아보기

질문:

2019년 3월 3일

댓글:

2019년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by