How do I compute double integrals in Simulink?
7 views (last 30 days)
Show older comments
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/971830/image.png)
function Fx = ExFx_fun(x,y)
coder.extrinsic("integral2")
Fx_fun=@(x,y) x^2+y^2;
Fx=integral2(Fx_fun,0,x,0,y);
end
.....................................
function Fx = Fx_fcn(x,y)
Fx=ExFx_fun(x,y);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/971835/image.png)
0 Comments
Answers (1)
Amish
on 5 Feb 2025 at 10:28
In Simulink, you can compute double integrals using the integral2 function, but there are some limitations. The integral2 function is not supported for code generation, which means you cannot directly use it within a MATLAB Function block that generates code. However, you can still use it by marking it as an extrinsic function, but you need to handle function handles differently since they cannot be passed directly to extrinsic functions in Simulink.
The workaround would be to declare integral2 as an extrinsic function to signal that it should be executed in the MATLAB environment rather than being compiled for code generation. Instead of passing a function handle directly to an extrinsic function, define the function handle within the extrinsic context. Create a separate MATLAB function file that performs the integration and call it from the MATLAB Function block.
Note that, using extrinsic functions can slow down the simulation because the computation is offloaded to the MATLAB workspace and hence this might not be suitable for real-time based applications.
Hope this helps!
1 Comment
Paul
on 5 Feb 2025 at 13:05
Don't know if integral2 was supported for code generation in R2022a when the question was asked, but it is in R2024b (Extended Capabilities) when this answer was posted (and has been since at least R2024a, if not earlier).
The OP's MatlabFunction works fine in 2024a w/o making integral2 extrinsic.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!