How to plot a function containing an integral?

6 ビュー (過去 30 日間)
Pawel Rochowski
Pawel Rochowski 2016 年 3 月 31 日
回答済み: Star Strider 2016 年 3 月 31 日
Hello all! I've got very simple code:
function y=sc()
a=2;
x=linspace(0,100);
fun1 =quadgk(@(m) m.*a .*x, 0, 2);
y = plot(x,fun1);
end
In the specific case above the value of the integral =2, and also a=2, so one could expect the linear plot as a result o function sc. Instead there is an error: error using .*, matrix dimensions must agree. The fun1 gives exact values, when user provides a and x values. Is there a simple way to create a plot(x,fun1) without making any loops?
  2 件のコメント
Star Strider
Star Strider 2016 年 3 月 31 日
What is ‘fun2’?
Pawel Rochowski
Pawel Rochowski 2016 年 3 月 31 日
編集済み: Pawel Rochowski 2016 年 3 月 31 日
Thats my mistake, should be fun1. I mind a code like this: function y=fun1(a,x) y =quadgk(@(m) m.*a .*x, 0, 2); end. Then in Matlab's Command window one may put values for a and x and get the value of fun1 .

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

採用された回答

Star Strider
Star Strider 2016 年 3 月 31 日
I had to delve back in the documentation, but I found the quadv function that will do what you want without a loop:
fun1 = quadv(@(m) m.*a .*x, 0, 2);
That works.
If you have R2012a or later, use the integral function with the 'ArrayValued' option:
a=2;
x=linspace(0,100);
fun1 = integral(@(m) m.*a .*x, 0, 2, 'ArrayValued',1);
y = plot(x,fun1);
It produces the same results as quadv for this problem. Otherwise, I doubt there is any way to do it with any of the numerical integration routines without using a loop.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by