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
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
guo qing
guo qing 2024-6-17
Thank you very much. I think the key to the problem lies in setting the initial value in the integrator module, just like you mentioned. As long as it is set, the problem is solved. However, it seems that when using block modular modeling, it is not necessary to set the initial value in the integrator module. This seems to be a unique aspect of the MATLAB Function block?

请先登录,再进行评论。

采纳的回答

Sam Chak
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
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. 🤦‍♂️
guo qing
guo qing 2024-6-17
Thank you very much. From my understanding, modular modeling refers to constructing a system using modules such as gain, product, and multiply, among others. In the systems built with these modules, it seems that Simulink can automatically determine the dimensions, and you only need to assign an initial value of 0 to the integrator module. Of course, the S-function you mentioned is also a good suggestion.

请先登录,再进行评论。

更多回答(1 个)

Nipun
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
  1 个评论
guo qing
guo qing 2024-6-17
Thank you for your reply.
Index expression out of bounds. Attempted to access element 2. The valid range is 1-1. More information on function 'MATLAB Function' (#64.77.78), line 7, column 7: "2" Diagnostic report initiated.
Component: MATLAB Function | Category: Coder Error
Error parsing MATLAB function 'untitled/MATLAB Function'.
Component: MATLAB Function | Category: Coder Error
Due to errors in the module body or limitations of foundational analysis, Simulink cannot determine the output size and/or type of module 'untitled/MATLAB Function'. The error may not be accurate. Please fix the indicated errors or explicitly specify the size and/or type of all module outputs.
Component: MATLAB Function | Category: Coder Error
Due to errors in the module body or limitations of foundational analysis, Simulink cannot determine the output size and/or type of module 'untitled/MATLAB Function'. The error may not be accurate. Please fix the indicated errors or explicitly specify the size and/or type of all module outputs.
Component: Simulink | Category: Model Error
Error propagating data type 'double' through 'untitled/MATLAB Function'.
Component: Simulink | Category: Model Error
This is all the error reports I have.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Inputs 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by