Represent Simulink Integrator block as Matlab Function

60 views (last 30 days)
I need to implement the following behavior :
The Integrator and my_Integrator blocks have to be equivalent I/O.
How should I write the Matlab Function ?
Thanks for any reply.

Accepted Answer

Ryan G
Ryan G on 4 Dec 2012
As this looks like a homework problem, I can't answer directly. However I will point you in the direction of persistent variables.

More Answers (4)

Azzi Abdelmalek
Azzi Abdelmalek on 7 Dec 2012
Edited: Azzi Abdelmalek on 8 Dec 2012
I don't know why do you need this, maybe if you explain exactly what you need, there is better way
River Rock
River Rock on 8 Dec 2012
Changing the sample time of the Step block to 0.01 removed the previous offset. Thanks

Sign in to comment.

Guy Rouleau
Guy Rouleau on 5 Dec 2012
This is not a good idea. The MATLAB function is not designed for this purpose.
  1 Comment
Edward Rodriguez
Edward Rodriguez on 10 Aug 2020
Excuse me, so, What would be a good idea to implement numerical integration methods in blocks in Simulink?

Sign in to comment.

River Rock
River Rock on 5 Dec 2012
Edited: River Rock on 6 Dec 2012
My main goal is to implement the differential equations of a physical system using a single Matlab Function. As the sums and gains were easy to represent, I couldn't find any alternative for the integration.
River Rock
River Rock on 7 Dec 2012
Any idea on how to get rid of this offset ?
Code looks like:
function y = fcn(u)
persistent yOld;
persistent uOld;
if (isempty(yOld))
yOld = 0;
if (isempty(uOld))
uOld = 0;
y = yOld + (T/2)* (u + uOld);
%y=yOld + u*T;
yOld = y;
uOld = u;

Sign in to comment.

River Rock
River Rock on 5 Dec 2012
Can anybody suggest a better way of implementing the numerical integration ? The code has to be written inside the Matlab Function Block though.
  1 Comment
Kiran Mahmood
Kiran Mahmood on 21 Oct 2018
I'm working on a similar problem. Did u find the solution ? Need help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!