How to Solve nonlinear equations in simulink
Show older comments
Hello everyone, I want to define and solve a system of nonlinear equations in Simulink using the MATLAB Function block, but I encountered an error. I would like to ask, why is this happening?
Here is the code inside the MATLAB Function block:
function dx = fcn(x)
dx1=x(1)+1
dx2=2*x(2)
dx=[dx1;dx2]
end

8 Comments
Sam Chak
on 17 Jun 2024
Your code for the differential equations
dx1 = x(1) + 1
dx2 = 2*x(2)
can interpreted in State-space form as

where
.
More importantly, you should have noticed that state
doesn't affect state
, both state equations will produce unstable reqponses for
and
.
By the way, both state equations are linear.
If the above interpretation is incorrect, please show exact mathematical model in image form.
guo qing
on 17 Jun 2024
guo qing
on 17 Jun 2024
Aquatris
on 17 Jun 2024
What error do you actually get? Since you have divisions by state, is it possible for one of your states to become 0 at one point?
guo qing
on 17 Jun 2024
Aquatris
on 17 Jun 2024
Then the error seems trivial. The integrator block has a 'Initial condition' property, which is by default set to 0, a scalar value. However your matlab function requires 2 inputs and has 2 outputs. So change the 'Initial condition' to be 'zeros(2,1)' and your code should run fine. For your actual problem, it seems like you have 5 states, so Initial condition:'' should be 'zeros(5,1)' instead.
You can also define initial condition to be different than 0 depending on the problem you wanna solve.
Hi @guo qing, Preliminary test shows that the dynamic equations look okay. Just make sure that the initial value for 5th state is non-zero, else you'll get a division-by-zero, leading to Inf or NaN reading.
The next step is to carry out the test in Simulink.
function dx=fcn(t, x)
ks = 29114;
kt = 233350;
cs = 925.8;
mw = 56.9;
u_asterisk = 100;
dx = [x(2);
cs/x(5)*(x(4)-x(2))+ks/x(5)*(x(3)-x(1));
x(4);
cs/mw*(x(2)-x(4))+ks/mw*(x(1)-x(3))+kt/mw*u_asterisk-kt/mw*x(3);
0];
end
[t, x] = ode45(@fcn, [0 0.2], [1; 0; 0; 0; 1]); % x1(0) & x5(0) = 1, the rest are 0.
plot(t, x), grid on
guo qing
on 17 Jun 2024
Accepted Answer
More Answers (1)
Nipun
on 17 Jun 2024
0 votes
Hi Quo,
I understand that you want to solve a set of equations in Simulink and are encountering an error.
Without the knowledge of the exact error message, it will be difficult to the exact cause of the error encountered.
I recommend changing the "initial value" of the integrator function so that it gives exact result on the initial condition and does not cause any error. Here is the documentation to do so: https://www.mathworks.com/help/simulink/slref/integrator.html
By default, the initial value is set to 0 which may not be true depending upon the function being fed.
Refer to the following MathWorks documentation for more information on equation solving in Simulink: https://www.mathworks.com/help/symbolic/equation-solving.html
Hope this helps.
Regards,
Nipun
Categories
Find more on Discontinuities 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!


