How to Solve nonlinear equations in simulink
4 次查看(过去 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 中查找有关 Inputs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!