필터 지우기
필터 지우기

Quadratic Equation using Simulink

조회 수: 33 (최근 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 CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by