Quadratic Equation using Simulink

43 ビュー (過去 30 日間)
Fergal Ahern
Fergal Ahern 2019 年 5 月 3 日
回答済み: Sulaymon Eshkabilov 2019 年 5 月 5 日
Hi, Im new to Simulink, and I was wondering how I would model the quadratic equation
ax^2+bx+c=0

採用された回答

Sulaymon Eshkabilov
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
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
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

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 5 月 4 日
In this case, the solver has to be a Fixed Step and no problems will happen.

Sulaymon Eshkabilov
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.

カテゴリ

Help Center および File ExchangeGeneral Applications についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by