where am I wrong in this SIMPLE code in an embedded matlab function?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I've written a simple matlab function which calculate the roots of a polynomial function. It takes in input the coefficients of the polynomials, and gives in output the four values of the roots of x (x_1, x_2, x_3, x_4). The function works well in matlab, but when I put it in an embedded matlab function, it doesn't work, and I don't know the reasons.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [x_1,x_2,x_3,x_4] = fcn(b,c,d,e)
%#codegen
A1=b;
A2=c;
A3=d;
A4=e;
xr_0=-2.3*A4/A3;
toll=10^(-15);
nmax=1000;
xr= (3*xr_0^4+2*A1*xr_0^3+A2*xr_0^2-A4)/(4*xr_0^3+3*A1*xr_0^2+2*A2*xr_0+A3);
kk=0;
kz=xr-xr_0;
kz1=sqrt(kz);
while ((kz1>toll) && kk<nmax)
kk=kk+1;
xr_0=xr;
xr= (3*xr_0^4+2*A1*xr_0^3+A2*xr_0^2-A4)/(4*xr_0^3+3*A1*xr_0^2+2*xr_0*A2+A3);
end
x_1=xr;
k=xr;
B1=k+b;
B2=B1*k+c;
B3=B2*k+d;
xr_0= -2.3*B3/B2;
xr= (2*xr_0^3+B1*xr_0^2-B3)/(3*xr_0^2+2*B1*xr_0+B2);
kk=0;
while (abs(xr-xr_0))>toll && kk<nmax
kk=kk+1;
xr_0=xr;
xr= (2*xr_0^3+B1*xr_0^2-B3)/(3*xr_0^2+2*B1*xr_0+B2);
end
x_2=xr;
k=xr;
C1=k+B1;
C2=C1*k+B2;
x_3=(-C1+sqrt(C1^2-4*C2))/2;
x_4=(-C1-sqrt(C1^2-4*C2))/2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
thanks in advance
回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!