MATLAB: This functions doesn’t work on matlab coder

matlab coder

function [xsol1,xsol2]= NewRaph2(F1,F2,F11,F22,F12,F21,x1i,x2i,iter,Err)
%F1 et F2 représentent 2 équations de départ
%F11 et F22 sont des dérivées premières
%F12, F21 sont des dérivées croisées
%x1i et x2i représentent 2 valeurs initiales correspondant x1 et x2
%iter est nombre d'itérations
%Err est l'erreur relative
%xsol1 et xsol2 correspondent aux valeurs de x1 et x2
F1= @(x1,x2) 4*x1^2-x2^3+28;
F2= @(x1,x2) 3*x1^3+4*x2^2-145;
F11= @(x1,x2) 8*x1;
F22= @(x1,x2) 8*x2;
F12= @(x1,x2) -3*x2^2;
F21= @(x1,x2) 9*x1^2;
for j=1:iter
A=F11(x1i,x2i)*F22(x1i,x2i)-F21(x1i,x2i)*F12(x1i,x2i);
deltax1=(-F1(x1i,x2i)*F22(x1i,x2i)+F2(x1i,x2i)*F12(x1i,x2i))/A;
deltax2=(-F2(x1i,x2i)*F11(x1i,x2i)+F1(x1i,x2i)*F21(x1i,x2i))/A;
x1=x1i+deltax1;
x2=x2i+deltax2;
Errx1=abs((x1-x1i)/x1i);
Errx2=abs((x2-x2i)/x2i);
if Errx1 < Err && Errx2 , Err;
xsol1=x1;
xsol2=x2;
break
else
x1i=x1;
x2i=x2;
end
end
It is saying that Output argument 'xsol1' is not assigned on some execution paths.
How can I resolve this problem? Thank you

Best Answer

First of all, you have
if Errx1 < Err && Errx2 , Err;
which is the same as
if (Errx1 < Err) && (Errx2 ~= 0)
Err;
You probably wanted
if Errx1 < Err && Errx2 < Err
Secondly, you have the structure
for j=1:iter
...
if Errx1 < Err && Errx2 < Err
xsol1 = x1;
break;
end
end
Consider what happens if Errx1 < Err && Errx2 < Err does not become true before the end of the "for j" loop: xsol1 will not get assigned to in that situation.
When you use MATLAB Coder, or MATLAB Function Block (in Simulink) you should get into the habit of always assigning some value to your output variables as one of the first things you do. That will have three benefits: (1) It will tell the code generation what size and datatype the variable is expected to be; (2) If you assign the result of a function call to the variable, it will tell the code generation that it needs to copy back from the MxArray format into the direct C/C++ variables used for code generation; and (3) If you accidentally have any paths in which you did not assign a value to the variable, it gives the variable a well defined value to return.