Matlab Functiion Block in Simulink.

3 vues (au cours des 30 derniers jours)
Hammad
Hammad le 2 Mai 2018
Commenté : Hammad le 3 Mai 2018
I have a MATLAB function in my Simulink simulation. This Matlab-Function runs in its entirety at every sampling interval. There is a for-loop in this function, which runs from 1:27 at every sampling interval.
I have to log values of (g1 g2 g gopt eopt at line 42, 43, 44, 47, 48) at every iteration but I can only get these vales at the end of the loop when MATLAB-function outputs a value.
For example if simulation is run for 1 sec and sampling time is 0.1 sec then for-loop would run 10 times and total number of iterations will be 270 (27x10), so I need to log vales of above parameters at 270 instances.
I have tried using timeseries() but MATLAB-Function in simulink does not support Code Generation so timeseries() does not work.
Please give ideas on how to approach this problem
I have tried to eliminate MATLAB_Function altogether, and I have converted most of the code to simulink blocks but I am not able to understand/use IF block (to replace if statement at line 46 to 49) in Simulink.
My Code: Code Link
function [Sopt, gee] =MC_control(I_ref,Io,Ve,Is,Vs)
global A Ts R L Aq11 Aq12 Aq21 Aq22 Bq11 Bq12 Bq21 Bq22 S counter
Irefalpha = 2*(I_ref(1) - 0.5*I_ref(2) - 0.5*I_ref(3))/3;
Irefbeta = 2*(sqrt(3)*I_ref(2)*0.5 - sqrt(3)*I_ref(3)*0.5)/3;
gopt = 10000000000;
for k= 1:27;
Ie = S(:,:,k)*Io;
Is_p_3f = Aq21*Ve + Aq22*Is + Bq21*Vs + Bq22*Ie;
Is_p_re = 2*(Is_p_3f(1) - 0.5*Is_p_3f(2)- 0.5*Is_p_3f(3))/3;
Is_p_im = 2*(sqrt(3)*Is_p_3f(2)*0.5- sqrt(3)*Is_p_3f(3)*0.5)/3;
VxN = S(:,:,k)*Ve;
Vo=[0;0;0];
Vo(1) = VxN(1) -(VxN(1) + VxN(2) + VxN(3))/3;
Vo(2) = VxN(2) -(VxN(1) + VxN(2) + VxN(3))/3;
Vo(3) = VxN(3) -(VxN(1) + VxN(2) + VxN(3))/3;
Io_p_3f = (1 - R*Ts/L)*Io + (Ts/L)*Vo;
Io_p_re = 2*(Io_p_3f(1) - 0.5*Io_p_3f(2)- 0.5*Io_p_3f(3))/3;
Io_p_im = 2*(sqrt(3)*Io_p_3f(2)*0.5- sqrt(3)*Io_p_3f(3)*0.5)/3;
Vs_p_3f = Vs;
Vs_p_re = 2*(Vs_p_3f(1) - 0.5*Vs_p_3f(2)- 0.5*Vs_p_3f(3))/3;
Vs_p_im = 2*(sqrt(3)*Vs_p_3f(2)*0.5- sqrt(3)*Vs_p_3f(3)*0.5)/3;
g1 = (abs(Irefalpha - Io_p_re) + abs(Irefbeta-Io_p_im));
g2= abs(Vs_p_re*Is_p_im - Vs_p_im*Is_p_re);
g = g1 + A*g2;
if (g<gopt)
gopt = g;
eopt = k;
end
end
%=============================
SAa = S(1,1,eopt);
SBa = S(1,2,eopt) ;
SCa = S(1,3,eopt);
SAb = S(2,1,eopt);
SBb = S(2,2,eopt);
SCb = S(2,3,eopt);
SAc = S(3,1,eopt);
SBc = S(3,2,eopt);
SCc = S(3,3,eopt);
Sopt = [SAa SBa SCa SAb SBb SCb SAc SBc SCc];

Réponse acceptée

TAB
TAB le 3 Mai 2018
Simulink logs the latest value at the end of sample time. To log the each value in loop iteration, you can create array signal (not scalar) and save output value from each iteration in array elements.
For example:
g1 = zeros(1,27);
g2 = zeros(1,27);
g = zeros(1,27);
for k= 1:27
g1(k) = (abs(Irefalpha - Io_p_re) + abs(Irefbeta-Io_p_im));
g2(k) = abs(Vs_p_re*Is_p_im - Vs_p_im*Is_p_re);
g(k) = g1 + A*g2;
end
  1 commentaire
Hammad
Hammad le 3 Mai 2018
This Worked, Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by