I need some help to finish my simulink model of an ESP sistem. I need to find the real roots of a 6th order polynomial in Simulink and use it for the simulation. I Made a MATLAB function block and wrote this code:
function x = fcn(A)
%#codegen
K=0.25;
P = [K -(K+1) K+1 -(K+1) K+1 -(K+1) 1-A];
r = roots(P);
real = r(imag(r)==0);
x = real(real<1.1);
A is an variable and K is an constant. x has to be just one number. After Runing the code in i get this errors in simulink:
Data 'x' is inferred as a variable size matrix, while its specified type is something else.
Error in port widths or dimensions. Invalid dimension has been specified for input 'A'.
To be honest im not sure if this will work in any way but this is my try. Ive checked the code in MATALB and it gives me what i want. Im not rly sure what these errors means. How can i make it work to get the roots of an polynomial in Simulink?

 採用された回答

Walter Roberson
Walter Roberson 2016 年 5 月 2 日

0 投票

xt = real(real<1.1);
x = xt(1);
However: your equation has no solutions below A = -1065489/4000000

3 件のコメント

Zijah Jelkic
Zijah Jelkic 2016 年 5 月 3 日
It works for A>1 but it fails if 0<A<1. In the simulation A changes from 0 to 7.5. I get this error messages after starting the simulation:
Attempted to access 1 element of data A whose runtime size is 0.
MATLAB function eML_blk_kernel in MATLAB Function MATLAB Function: x = xt(1);
^^^^^
This error will stop the simulation.
An error occurred while running the simulation and the simulation was terminated
Simulation stopped because of a runtime error: out of bounds.
Walter Roberson
Walter Roberson 2016 年 5 月 4 日
Generalized to the entire range of unique solutions
function x = fcn(A)
%#codegen
%no solutions below below value
assert(A>-52.76780387858024)
K=0.25;
P = [K -(K+1) K+1 -(K+1) K+1 -(K+1) 1-A];
r = roots(P);
%second root is above this value
xt = r(imag(r)==0 & real(r)<3.358319463627252);
x = xt(1);
Below A = -52.76780387858024 there are no solutions because 52.76780387858024 is the global maximum for [K -(K+1) K+1 -(K+1) K+1 -(K+1) 1]
In cases where a second root exists, the second root will be above 3.358319463627252, which is the real root of the derivative of the function.
Zijah Jelkic
Zijah Jelkic 2016 年 5 月 24 日
Thank you very much. I made a big mistake. Ive first used the "MATLAB Function block" and got all the errors whit it. Then i finnaly used the "Interpreted MATLAB Function" blok and it works perfect now with:
x=xt(1)
And the it never runs below -2 so i dont need to filter the solutions. You are the man!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFPGA, ASIC, and SoC Development についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by