How can I integrate something times a function file?

4 views (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 30 Sep 2014
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 Comments
tk27182
tk27182 on 30 Sep 2014
Edited: tk27182 on 30 Sep 2014
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 on 30 Sep 2014
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.

Sign in to comment.

More Answers (1)

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

Community Treasure Hunt

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

Start Hunting!