How can I integrate something times a function file?

2 ビュー (過去 30 日間)
tk27182
tk27182 2014 年 9 月 30 日
コメント済み: Star Strider 2014 年 9 月 30 日
I'm trying to integrate a function that is a derivative squared and multiplied by a constant. The derivative I have saved as a separate function file. I've tried to use a.*(@funct).^2 as the integrand but I'd get errors. I also tried to call that integrand a separate function and call that function, tried fhandle=@funct, but none worked. Can I not use @funct that way and if not, how should I use it?

採用された回答

Star Strider
Star Strider 2014 年 9 月 30 日
If I understand your Question correctly, you will have to create an anonymous function as the integrand, that combines the constant and your function.
I use an anonymous function as ‘func’ here, but the construction is the same as for your function file, because it creates a function handle for the integrand in the process:
func = @(x) 2.*x;
a = 10;
Ifunc = integral(@(x) func(x).*a, 0, 5);
That should work for your function as well.
You could create a completely separate function as well if you want to:
funcxa = @(x) func(x).*a;
Ifunc = integral(funcxa, 0, 5);
IT does the same thing but in a separate line.
  2 件のコメント
tk27182
tk27182 2014 年 9 月 30 日
編集済み: tk27182 2014 年 9 月 30 日
I think this is it, the only part I'm still having issues with is that I want to use a function that I've created (deriv.m) because this is the derivative I want to integrate and I'm not sure where that fits in your solution. deriv.m requires two inputs.
Star Strider
Star Strider 2014 年 9 月 30 日
If deriv.m is a function of two variables (I assume that it what you mean by two inputs), you are likely doing a double integration and so, unless you have an array-valued function, will likely benefit from using integral2. (If your function is array-valued, use integral twice.)
If one input is a variable and the other a parameter that deriv.m gets from the workspace, it will get it by default. All you need to do is to define it in the workspace.
If you are doing something else, please elaborate on it.

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

その他の回答 (1 件)

Iain
Iain 2014 年 9 月 30 日
This example uses "sin", instead of whatever function you're using to get your integrand.
to_be_integrated = @(x)(sin(x).^2*5+2);

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by