필터 지우기
필터 지우기

Making My Own Simpson's Rule Function

조회 수: 1 (최근 30일)
Jack Bush
Jack Bush 2018년 12월 11일
편집: Jack Bush 2018년 12월 14일
Can someone make me a simpsons rule function
  댓글 수: 2
Aoi Midori
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))
Jack Bush
Jack Bush 2018년 12월 12일
편집: Jack Bush 2018년 12월 12일
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.

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

답변 (1개)

John D'Errico
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.
  댓글 수: 1
Jack Bush
Jack Bush 2018년 12월 12일
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.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by