How can I integrate something times a function file?

2 次查看(过去 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 CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by