- You are updating 'unit_delay' before using it in the calculation of' new_Vgrid_hat_dot_final'. This means you are using the current value instead of the previous one. To fix this, assign 'new_Vgrid_hat_dot_final' first using the old 'unit_delay', then update 'unit_delay' afterward.
- The term involving cos(377 * TIMEBASE1_SAMPLING_TIME) seems to be a constant, but if you want to model the actual grid voltage dynamics (cos(wt)), you need to include the time index or sample count properly. Otherwise, the observer won’t correctly track the time-varying grid voltage.
- Make sure the persistent variables are properly initialized and that your MATLAB Function block input/output ports correspond exactly to your function signature.
ask MATLAB function block
3 views (last 30 days)
Show older comments

In here d1(k) and d1(k-1) is just the previous sample value. Actually, d1(k) is -Esin(wt) value.
Input term is Igrid(k) and u(k) that is Vconverter.
please someone check this code what is problem.
Code is below.
function [y1, y2, y3, y4, y5] = fcn(igrid_ds, Vcon_ds)
persistent icon_hat_final Vcap_hat_final igrid_hat_final Vgrid_hat_final Vgrid_hat_dot_final
persistent unit_delay
if isempty(Vgrid_hat_final)
icon_hat_final = 0;
Vcap_hat_final = 0;
igrid_hat_final = 0;
Vgrid_hat_final = 0;
Vgrid_hat_dot_final = 0;
unit_delay = 0; % unit delay: 이전 샘플의 Vgrid_hat_final_2를 저장
end
R1 = 0.1;
L1 = 1.4e-3;
R2 = 0.14;
L2 = 0.7e-3;
C = 5e-6;
l11 = 1.2970;
l12 = -15.9383;
l13 = 1.5569;
l14 = -31.8002;
l15 = 0.0016;
TIMEBASE1_SAMPLING_TIME = 1/10000; % 100 μs
A11 = -R1 / L1;
A12 = -1 / L1;
A21 = 1 / C;
A23 = -1 / C;
A32 = 1 / L2;
A33 = -R2 / L2; % 세미콜론 추가
A34 = 1 / L2;
A44 = 2 * cos(377 * TIMEBASE1_SAMPLING_TIME);
A45 = 1;
A54 = -1;
B11 = 1 / L1;
igrid_tild = igrid_ds - igrid_hat_final;
icon_hat1 = A11 * icon_hat_final;
icon_hat2 = A21 * Vcap_hat_final;
icon_hat3 = B11 * Vcon_ds;
icon_hat4 = l11 * igrid_tild;
icon_hat5 = icon_hat1 + icon_hat2 + icon_hat3 + icon_hat4;
Vcap_hat1 = A21 * icon_hat_final;
Vcap_hat2 = A23 * igrid_hat_final;
Vcap_hat3 = l12 * igrid_tild;
Vcap_hat4 = Vcap_hat1 + Vcap_hat2 + Vcap_hat3;
igrid_hat1 = A32 * Vcap_hat_final;
igrid_hat2 = A33 * igrid_hat_final;
igrid_hat3 = A34 * Vgrid_hat_final;
igrid_hat4 = l13 * igrid_tild;
igrid_hat5 = igrid_hat1 + igrid_hat2 + igrid_hat3 + igrid_hat4;
Vgrid_hat1 = A44 * Vgrid_hat_final;
Vgrid_hat2 = Vgrid_hat_dot_final;
Vgrid_hat3 = l14 *cos(377*TIMEBASE1_SAMPLING_TIME)* igrid_tild; % 단순화: cos() 항은 관측기로 이미 반영됨
Vgrid_hat4 = Vgrid_hat1 + Vgrid_hat2 + Vgrid_hat3;
Vgrid_hat_dot1 = A54 * Vgrid_hat_final;
Vgrid_hat_dot2 = -l15 * igrid_tild;
Vgrid_hat_dot3 = Vgrid_hat_dot1 + Vgrid_hat_dot2;
new_icon_hat_final = icon_hat_final + icon_hat5 * TIMEBASE1_SAMPLING_TIME;
new_Vcap_hat_final = Vcap_hat_final + Vcap_hat4 * TIMEBASE1_SAMPLING_TIME;
new_igrid_hat_final = igrid_hat_final + igrid_hat5 * TIMEBASE1_SAMPLING_TIME;
new_Vgrid_hat_final = Vgrid_hat_final + Vgrid_hat4;
unit_delay = new_Vgrid_hat_final;
new_Vgrid_hat_dot_final = unit_delay + Vgrid_hat_dot2*TIMEBASE1_SAMPLING_TIME;
y1 = new_icon_hat_final;
y2 = new_Vcap_hat_final;
y3 = new_igrid_hat_final;
y4 = new_Vgrid_hat_final;
y5 = new_Vgrid_hat_dot_final;
icon_hat_final = new_icon_hat_final;
Vcap_hat_final = new_Vcap_hat_final;
igrid_hat_final = new_igrid_hat_final;
Vgrid_hat_final = new_Vgrid_hat_final;
Vgrid_hat_dot_final = new_Vgrid_hat_dot_final;
end
0 Comments
Answers (1)
Shishir Reddy
on 24 Jul 2025
The implementation of the Luenberger observer in the MATLAB Function block looks mostly good, but there are a couple of important points that could cause problems:
I hope fixing these should help your observer behave correctly.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!