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.