Making My Own Simpson's Rule Function
조회 수: 1 (최근 30일)
이전 댓글 표시
Can someone make me a simpsons rule function
댓글 수: 2
Aoi Midori
2018년 12월 12일
The first thing that popped up in my mind is that you are trying to access to x(0) when the variable i takes its initial value 1, which is unavailable.
I still have something unclear. What would you like to input as 'f'?
The thing is, if 'f' is just a variable, you better multiple it using '*', as well, like:
f*(x(2*i-1))
답변 (1개)
John D'Errico
2018년 12월 12일
Your code will certainly fail here:
for i = 1:n/2
q1 = q1 + f(x((2*i-2)));
end
What happens when i==1? We can see that 2*1-2 = 0. Sinve this is a vector index, you will attempt to access the element x(0). Yet we know that MATLAB is not a language that allows a zero index to an array. That would fail immediately.
Is that your current problem? No, because the error message would have been very different.
An error of not enough input arguments suggests that perhaps you have not defined the function f properly. But we have not been told where or how f is defined at all.
I might also comment that it seems unnecesary to have a,b and n as explicit arguments to your function, when you immediately overwrite them using input. (Input is my first choice of the one function I would so happily delete from the MATLAB language, because it is so heavily abused by new users.) But this is not a programming problem with your code, just a poor programming style.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!