Clear Filters
Clear Filters

roots in Simulink

11 views (last 30 days)
Cosmin Darab
Cosmin Darab on 9 Jan 2012
hy, I'm having some differences between matlab and simulink when using function roots():
"p=[1 xc b z]; xc= 0.005117782794100; b= 4.267869584093415e-008; z= -1.778279410038923e-019; roots(p);"
in matlab the result is: -0.005109429866868 -0.000008352931399 0.000000000004167
and in simulink(i put the code in a embeded matlab function): -0.005109429866875 -0.000008352927225 -0.000000000000000 I have to use the positive root but simulink does not have one. Am I doing something wrong? Does Simulink use diferent compiler? Thank you

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 9 Jan 2012
Hi Cosmin,
I took a look at the implementation of roots for the Embedded MATLAB Function block (<matlabroot>\toolbox\eml\lib\matlab\polyfun\roots.m). It's stated there:
% Limitations:
% Output is always variable size.
% Output is always complex.
% Roots may not be in the same order as MATLAB.
% Roots of poorly conditioned polynomials may not match MATLAB.
The last sentence is what makes you the headache (and yes, your polynomial is badly conditioned!). If you take a look at the plot you will see, that the curve hardly touches the x-axis.
I have a suggestion though: the value -z/b is a (very) good approximation of the root you are looking for ...?
Titus
  1 Comment
Cosmin Darab
Cosmin Darab on 10 Jan 2012
Thank you Titus
I can use the aprox -z/b, that was a lifesavier :)

Sign in to comment.

More Answers (2)

Titus Edelhofer
Titus Edelhofer on 9 Jan 2012
Hi Cosmin,
up to roundoff error both solutions are fine. If you take the second solution and compute the polynomial within MATLAB:
r2=[-0.005109429866875 -0.000008352927225 -0.000000000000000];
p2=conv(conv([1 -r2(1)], [1, -r2(2)]), [1 -r2(3)])
% what's the difference to p?
p-p2
ans =
1.0e-017 *
0 0 -0.168584041049737 0.017782794100389
So: both solutions are equally "good"...
Titus
  2 Comments
Cosmin Darab
Cosmin Darab on 9 Jan 2012
I don't get it should I use conv function in simulink?
because it's not working
Cosmin Darab
Cosmin Darab on 9 Jan 2012
i get what you are saying but I need a positive root because I use it afterthat in a logarithmic expresion

Sign in to comment.


Muhammet Öztürk
Muhammet Öztürk on 4 May 2017
I have got a similar question; I have a code polynomial as
Ka=1.8e-5; Kw=1e-14; sigma=0.0120; ksi=0.0482; %sigma=0.0608; %ksi=0.0406;
Poli=[1 Ka+ksi Ka*(ksi-sigma)-Kw -Kw*Ka]; Hcal = roots(Poli)
Matlab gives the results as correct Hcal =
-0.0482
-0.0000
0.0000
But in simulink function block I take the answer Hcal = -0.0482 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i
İn simulink I can not take the correct answer. How can I solve this problem.

Community Treasure Hunt

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

Start Hunting!