matlab Solving matrix differential equations using MATLAB programming, converting it to Simulink simulation, and encountering errors when running the Simulink program.

조회 수: 15 (최근 30일)
Hello everybody.
I am new to simulink analysis. please help me to solve this question.please help me to solve this question.
When I converted the Matlab program into a Simulink program for simulation analysis.
Simulink program error occurred
w=0.1;
dt=0.01;
T=0:dt:3600;
N=length(T);
X3=zeros(20,1);
Fu3=zeros(2,N);
Q3=zeros(20,length(T));
w_0=2*pi/10;
for i=2:N%
Q3(:,i)=Q_1*(waveforce(:,i)+Fu3(:,i-1));
X3(:, i)=Kutta(X3(:,i-1),dt,P_control,Q3(:,i));
v3_ref(:,i)=H_PEAK*waveforce(:,i);
v3_real(:,i)=[X3(2,i);X3(12,i)];
v3_error(:,i)=v3_ref(:,i)-v3_real(:,i);
n=min([round(pi/w_0/dt),i]);
Fu3(:,i)=real(Z_0)*v3_error(:,i)*2*0.01+100*(sum(v3_error(:, i-n+1:i), 2)/n)+80000*(v3_error(:,i)-v3_error(:,i-1))/dt+Fu3(:,i-1);
end
while the Simulink program error occurred

답변 (1개)

Umar
Umar 2025년 10월 6일
이동: Sam Chak 2025년 10월 29일

Hi @陈勤立,

Based on your Simulink diagram and MATLAB code, the error is most likely an algebraic loop. Here's what's happening and how to fix it:

The Problem Your MATLAB code has this structure:

Fu3(:,i) = ... + Fu3(:,i-1)

In Simulink, you need to add a Unit Delay block in feedback paths to prevent the system from trying to calculate a variable using itself in the same time step. Looking at your diagram, the feedback path at the bottom (the "Q" summing junction) appears to create a direct loop without proper delays.

The Solution

Add Unit Delay blocks in your feedback paths:

1.The Unit Delay block holds and delays its input by one sample period - this breaks the algebraic loop

2.Set the Sample Time parameter to match your dt (0.01 in your code)

3.Set the Initial Condition to match your MATLAB initialization (e.g., zeros(2,1) for Fu3)

Specific Changes Needed

In your SIL:

Add a Unit Delay block on the feedback line going back to the lower summing junction

*Set Initial Condition: [0; 0]

*Set Sample time: 0.01

For the moving average calculation sum(v3_error(:, i-n+1:i), 2)/n, you'll need either:

*A Moving Average block (from DSP System Toolbox),

OR

*A MATLAB Function block with buffer logic to store previous n samples

Key Point

Both Unit Delay and Memory blocks provide an initial output and permit downstream calculations to initialize before feeding back. For fixed-step solvers (which you should use with dt=0.01), Unit Delay is the correct choice.

Try adding the Unit Delay block first - this should resolve your algebraic loop error.

Hope this helps.

  댓글 수: 1
陈勤立
陈勤立 2025년 10월 7일
이동: Sam Chak 2025년 10월 29일
Thank you for the answer, @Umar. Following your suggestion, I added a Unit Delay block and used a Moving Average block to improve the system, which effectively solved the problem. Your guidance was spot-on and pinpointed the root cause. Thanks again for your help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fuzzy Logic in Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by