Azzera filtri
Azzera filtri

Making My Own Simpson's Rule Function

1 visualizzazione (ultimi 30 giorni)
Jack Bush
Jack Bush il 11 Dic 2018
Modificato: Jack Bush il 14 Dic 2018
Can someone make me a simpsons rule function
  2 Commenti
Aoi Midori
Aoi Midori il 12 Dic 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))
Jack Bush
Jack Bush il 12 Dic 2018
Modificato: Jack Bush il 12 Dic 2018
Ahh I see, what would be your suggestion? To increase the initial value for that piece of code to 2?
The input of f in this scenario is:
where p=5
But I wasn't sure where I needed to specify what the function was, would this need to be written while I'm writing the function or afterwards?
The feedback is much appreciated.

Accedi per commentare.

Risposte (1)

John D'Errico
John D'Errico il 12 Dic 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.
  1 Commento
Jack Bush
Jack Bush il 12 Dic 2018
I understand, I should've checked the maths aswell. I haven't come across that problem yet but as you suggested I would have got that problem soon after.
f is defined like this:
Simpsons Rule Function.JPG
However I'm unsure of how to include this in my code (in this scenario p=5 so it is essentially x^(7)*sin(x).
So are you saying I should take out the beginning part where I have written all my inputs and attempt this part differently?
Thank you for getting back to me.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by