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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!