Quadratic Equation using Simulink
25 次查看(过去 30 天)
显示 更早的评论
Hi, Im new to Simulink, and I was wondering how I would model the quadratic equation
ax^2+bx+c=0
0 个评论
采纳的回答
Sulaymon Eshkabilov
2019-5-4
% Step 1. Drag and drop three [Constant] blocks into your Model window for a, b, c whose values you can define directly in each block, or via workspace (MATLAB command window), or via Model properties:==> File ->Model Properties -> Callbacks ->IntitFcn: a = ???; b= ???; c =???;
% Step 2. Drag and drop [Display] block into your Model window.
% Step 3. Drag [MATLAB Function] block and double click on it. Write the following two lines of code in the opened M-file editor and save it:
function y = fcn(a, b, c)
y=roots([a, b, c]);
% Step 4. When you save your fcn() file - M-file editor saves your Simulink model. Now, your [MATLAB Function] block will have three input ports for [a], [b],[c], respectively.
% Step 5. Connect three [Constant] block with the [MATLAB Function] block and output port of it with [Display] block. Now the model is complete, BUT it has a problem with the output data size.
% Step 6. Click on Model Explorer (Ports and Data Manager) -> [MATLAB Function] -> y Output -> Enter: 2 in size and put a tick mark on Variable Size option -> hit [Apply].
% Step 7. Hit [Run] and have fun ... :) . See the screenshot of the complete model and its results shown here.
1 个评论
Walter Roberson
2019-5-4
编辑:Walter Roberson
2019-5-4
You will probably need to use
y = zeros(2, 1);
before the roots() call, as otherwise Simulink might complain about being unable to access a mwarray
Note that if you use Math blocks in the way I suggested, then you can use Continuous time, but that when you use a MATLAB Function block it might restrict you to discrete time.
更多回答(3 个)
Walter Roberson
2019-5-3
I would suggest sending the three parameters, a, b, c, to a series of Math Function Blocks, with a signal fork with one fork using + on the sqrt() and the other fork using - on the sqrt(). Be sure to mark the square root as expected to return complex unless you can be certain that b^2 >= 4*a*c
0 个评论
Sulaymon Eshkabilov
2019-5-4
In this case, the solver has to be a Fixed Step and no problems will happen.
0 个评论
Sulaymon Eshkabilov
2019-5-5
zeros(2,1) will not make any effect on the solver selection and thus, the solver has to be a fixed step. Otherwise, the model does work.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!