How can I integrate something times a function file?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
tk27182
am 30 Sep. 2014
Kommentiert: Star Strider
am 30 Sep. 2014
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?
0 Kommentare
Akzeptierte Antwort
Star Strider
am 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 Kommentare
Star Strider
am 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.
Weitere Antworten (1)
Iain
am 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);
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!