Finding polynamial roots using Newton-Raphson takes more time than in-built 'roots' function
1 view (last 30 days)
Show older comments
Dear users,
I would like to find roots of a cubic equation in Simulink[ver: 8.0]
INPUTs to embedded Matlab function block: * initial guess (x0) and * coefficient vector.[a, b, c, d]
Matlab in-built 'roots' does the job, though, accuracy is an issue.
I went on finding roots manually using Newton-Raphson's method. Though, it really slows down the simulation. Here is the code I use:
-------------------------------------
function x1 = fcn(x0,a,b,c,d)
%#codegen
x1=x0;
flag=0;
while(flag==0)
fx=(a*(x1^3))+(b*(x1^2))+(c*x1)+d;
fdx=(3*a*(x1^2))+(2*b*x1)+c;
x1=x1-(fx/fdx);
diff=abs(x1-x0);
x0=x1;
if diff<=0.00000000001
flag=1;
end
end
------------------------
Do you have any suggestions to make it faster OR any better(accurate+fast) method to get roots in Simulink?
Looking forward to hearing.
Thank you.
2 Comments
Javier
on 25 Sep 2012
Accurate desired goes again faster desired. I made a tic toc on your code and was ok (time elapsed: 1.9829e-05). A more accurate result can be achieved via lower diff or other search method.
Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!