MATLAB: How to solve this nonlinear system of 4 equations using fsolve

sum

I am trying to solve this nonlinear system of 4 equations with 4 unknowns using fsolve but I am getting error message. This is the matlab codes I have wriiten in my matlab script (myfun2.m);
function T = myfun2(x,y,n)
format long
y=[6;12;18;24;30];
n=length(y);
tol = 10.^-4; %tol is a convergence tolerance
%initial guess or values
x0 = [0.002;0.0002;0.0002;0.00002];
T = [n-sum(x(1)*y.^x(2)*exp(x(3)*y))-2*sum(x(1)*x(4)*y.^x(2)*exp(-x(1)*y.^x(2)*exp(x(3)*y)+x(3)*y)*(1-x(4)*exp(-x(1)*y.^x(2)*exp(x(3)*y)))^(-1));
(log(y)+sum(1/(x(2)+x(3)*y))-sum(x(1)*y.^x(2)*exp(x(3)*y)*log(y))-2*sum(x(2)*x(4)*y.^x(2)*exp(-x(1)*y.^x(2)*exp(x(3)*y)+x(3)*y)*log(y)*(1-x(4)*exp(-x(1)*y.^x(2)*exp(x(3)*y)))^(-1)));
sum(y./(x(2)+x(3)*y))-sum(x(1)*y.^(x(2)+1)*exp(x(3)*y))-sum(y)-2*sum(x(1)*x(4)*y.^(x(2)+1)*exp(-x(1)*y.^x(2)*exp(x(3)*y)+x(3)*y)*(1-x(4)*exp(-x(1)*y.^x(2)*exp(x(3)*y)))^(-1));
n/(1-x(4))-2*sum(exp(x(1)*y.^x(2)*exp(x(3)*y))*(1-x(4)*exp(-x(1)*y.^x(2)*exp(x(3)*y)))^(-1))];
options = optimset('Display','iter')
[x,fval] = fsolve(@(x) myfun2(x,y,n),x0,options)
end
x is the parameter vector to be estimated for, y is the data vector and n is the number of data. Please, I need your assistance to make this script run.
Thank you.

Best Answer

Your error is that inside myfun, you call fsolve with myfun as an argument. So fsolve will then call myfun, which will call fsolve, etc. This rather incestuously recursive behavior will cause fsolve to fail in frustration. It won't return anything useful either as you have written the code.
Learn to use fsolve. Do so by reading the tutorials for fsolve. READ THE HELP. There are examples of use in there.