Represent Simulink Integrator block as Matlab Function

Hi.
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.

 Akzeptierte Antwort

Ryan G
Ryan G am 4 Dez. 2012

1 Stimme

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

Weitere Antworten (4)

Azzi Abdelmalek
Azzi Abdelmalek am 7 Dez. 2012
Bearbeitet: Azzi Abdelmalek am 8 Dez. 2012

2 Stimmen

I don't know why do you need this, maybe if you explain exactly what you need, there is better way

9 Kommentare

The model diagram should be as light as possible, as shown in the first attached picture: one input and one output, no additional simulink blocks.
Azzi Abdelmalek
Azzi Abdelmalek am 8 Dez. 2012
Bearbeitet: Azzi Abdelmalek am 8 Dez. 2012
You can't do it
Sure we can. Ryan's idea works.
Would it be easier to use a S-function instead of a MATLAB Function ?
What about time t?
Can you post the solution?
Azzi Abdelmalek
Azzi Abdelmalek am 8 Dez. 2012
Bearbeitet: Azzi Abdelmalek am 8 Dez. 2012
It works with Ryan's hint, but you still need a clock as an input.
River Rock
River Rock am 8 Dez. 2012
Bearbeitet: River Rock am 8 Dez. 2012
The solution it's already posted above. But since you and Guy disapprove this, I am waiting for the better solution.
And even if I did, I guess it could have been fetched using the get_param command (never tried this though)
Ok, I see, If T is constant, you must then set, in model configuration parameters your fixed step time to T, and also your step block sample time to T. In this case you don't need a clock.
function y = fcn(u)
persistent uold yold
T=0.01;
if isempty(uold)
uold=0;yold=0;
end
y = u*T+yold-(u-uold)*T/2
yold=y;uold=u;
Changing the sample time of the Step block to 0.01 removed the previous offset. Thanks

Melden Sie sich an, um zu kommentieren.

Guy Rouleau
Guy Rouleau am 5 Dez. 2012

0 Stimmen

This is not a good idea. The MATLAB function is not designed for this purpose.

1 Kommentar

Excuse me, so, What would be a good idea to implement numerical integration methods in blocks in Simulink?

Melden Sie sich an, um zu kommentieren.

River Rock
River Rock am 5 Dez. 2012
Bearbeitet: River Rock am 6 Dez. 2012

0 Stimmen

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.

4 Kommentare

Why do you need to use MATLAB over SL blocks? Look into the persistent variables, it will get the job done, although as Guy mentioned, it's not the best way to do it.
Your idea would be to declare a persistent variable for the numeric integration ?
Y(s) = U(s) / s => y(z) = yOld + u(z)
so you declare persistent x = y to stand for yOld ?
Can I avoid this low-level arithmetic and call a predefined method instead (ode45 for example) ?
What you have written is close it would be more like:
y(z) = yOld+u(z)/SampleTime
You cannot use the ODE solver in the MATLAB function block.
Any idea on how to get rid of this offset ?
Code looks like:
function y = fcn(u)
%#codegen
T=0.01;
persistent yOld;
persistent uOld;
if (isempty(yOld))
yOld = 0;
end
if (isempty(uOld))
uOld = 0;
end
y = yOld + (T/2)* (u + uOld);
%y=yOld + u*T;
yOld = y;
uOld = u;

Melden Sie sich an, um zu kommentieren.

River Rock
River Rock am 5 Dez. 2012

0 Stimmen

Can anybody suggest a better way of implementing the numerical integration ? The code has to be written inside the Matlab Function Block though.

1 Kommentar

I'm working on a similar problem. Did u find the solution ? Need help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by