Making My Own Simpson's Rule Function
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Can someone make me a simpsons rule function
2 Kommentare
Aoi Midori
am 12 Dez. 2018
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))
Antworten (1)
John D'Errico
am 12 Dez. 2018
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.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!