
How to Solve nonlinear equations in simulink
19 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Sam Chak
2024 年 6 月 17 日
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
採用された回答
Sam Chak
2024 年 6 月 17 日
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 件のコメント
Sam Chak
2024 年 6 月 17 日
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. 🤦♂️
その他の回答 (1 件)
Nipun
2024 年 6 月 17 日
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
参考
カテゴリ
Help Center および File Exchange で Discontinuities についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
