where am I wrong in this SIMPLE code in an embedded matlab function?

조회 수: 1 (최근 30일)
max
max 2013년 3월 18일
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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by