How to Solve nonlinear equations in simulink
14 views (last 30 days)
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
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
Accepted Answer
Sam Chak
on 17 Jun 2024
Code for the MATLAB Function block:
function dx = fcn(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
3 Comments
Sam Chak
on 17 Jun 2024
You are welcome, @guo qing. If you find the approach or solution helpful, please consider clicking 'Accept' ✔ on the Answer. Additionally, you can show your appreciation by voting 👍 for other helpful answers.
By the way, what do you mean by "block modular modeling"? If you use the S-Function block, then the Integrator block is unnessary. However, you will still need to supply the initial values in the code of the S-Function block. However, not many examples are provided for using the S-Function block to simulate the dynamic systems. Clicking the links directs users to other more links. 🤦♂️
More Answers (1)
Nipun
on 17 Jun 2024
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
See Also
Categories
Find more on Inputs 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!