Quadratic Equation using Simulink
48 views (last 30 days)
Show older comments
Fergal Ahern
on 3 May 2019
Answered: Sulaymon Eshkabilov
on 5 May 2019
Hi, Im new to Simulink, and I was wondering how I would model the quadratic equation
ax^2+bx+c=0
0 Comments
Accepted Answer
Sulaymon Eshkabilov
on 4 May 2019
% 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 Comment
Walter Roberson
on 4 May 2019
Edited: Walter Roberson
on 4 May 2019
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.
More Answers (3)
Walter Roberson
on 3 May 2019
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 Comments
Sulaymon Eshkabilov
on 4 May 2019
In this case, the solver has to be a Fixed Step and no problems will happen.
0 Comments
Sulaymon Eshkabilov
on 5 May 2019
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 Comments
See Also
Categories
Find more on General Applications in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!