Trying to Create a Riemann Sum function from scratch, keep getting function error.

So i have to create a riemannsum calculator function, its been a little bit since ive used MatLab, but this seems right i just dont understand why i keep getting this error.
function [ Result ] = RiemannSum(Fun,a,b) %This function will calculate the riemann sum of a function defined from an %interval from a to b. This will be done using 10 subintervals.
dx=(b-a)/10;
XS=(a+(dx/2):dx:b-(dx/2));
Result=sum((feval(Fun,XS)).*dx);
The error i get is : RiemannSum(2*x,1,10) Undefined function or variable 'x'.
Even if i set x=XS it doesnt fix the error, im really stuck here.
Or when i tinker around and first run x=[] ill get this error instead.
Error using feval Argument must contain a string or function_handle.
Error in RiemannSum (line 6) Result=sum((feval(Fun,XS))*dx); end

 採用された回答

Rick Rosson
Rick Rosson 2014 年 9 月 1 日
Please type the following at the MATLAB Command Prompt:
F = @(x) 2*x;
R = RiemannSum(F,1,10);

7 件のコメント

Amar
Amar 2014 年 9 月 1 日
編集済み: Amar 2014 年 9 月 1 日
That's fine and dandy but for this assignment I need the function to take the function I want evaluated as an input. Or is there no way to do this without defining it beforehand. What i need is for me to type RiemannSum(x^2,1,10) and it'll calculate the riemann sum for x^2 from 1 to 10. I know what it needs to do mathematically but MatLab isn't cooperating with my brain haha.
Rick Rosson
Rick Rosson 2014 年 9 月 1 日
編集済み: Rick Rosson 2014 年 9 月 1 日
That is exactly what this solution does. It takes any function as an input argument.
Please try:
G = @(x) 3*x^2;
R = RiemannSum(G,1,10);
H = @(x) cos(x);
R = RiemannSum(H,1,10);
That is the whole point of using an anonymous function.
Amar
Amar 2014 年 9 月 1 日
Okay, now is there any way instead of storing the function in H or G or declaring it beforehand, it THE FUNCTION ITSELF would be the input argument?
Rick Rosson
Rick Rosson 2014 年 9 月 1 日
編集済み: Rick Rosson 2014 年 9 月 1 日
I don't know if this will work, but you could try:
R = RiemannSum(@(x) 2*x, 1, 10)
I am not sure why it matters. What difference does it make?
Amar
Amar 2014 年 9 月 1 日
Blame my professor, he's a picky man.
Rick Rosson
Rick Rosson 2014 年 9 月 1 日
Did it work?
Amar
Amar 2014 年 9 月 1 日
Yes it did finally, thank you very very much. I know it must have been a bit annoying.

サインインしてコメントする。

その他の回答 (1 件)

Amar
Amar 2014 年 9 月 1 日

0 投票

I was told by an individual to just put my function in '' and that did not work, i now get this error: Invalid function name '2*x'.
Error in RiemannSum (line 6) Result=sum((feval(Fun,XS)).*dx);

1 件のコメント

Rick Rosson
Rick Rosson 2014 年 9 月 1 日
Please do not post answers to your own question. Instead, post a comment to your question or to an answer, or edit your question.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2014 年 8 月 31 日

コメント済み:

2014 年 9 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by