How can I integrate something times a function file?

조회 수: 5 (최근 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 CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by