Hi I have written a function in matlab that I now want to convert to C. However Matlab has matrices functions where C does not. I have converted the function and I now think that the two should be doing the same thing however the C version is calculating #QNAN0 in other words the value is tending to infinity due to a /0.

I was wondering if there is someone out there who has used both matlab and C and can tell if i have made a mistake - I cant see it

So I have solved a problem in matlab and it works brilliantly. However my boss requires the method in C. I am doing a least squares minimization using fminsearch. I am trying to replicate the function being solved for in C (I already have the neldermead function in C)

My matlab function to be solved is:

And this is what I am trying to replicate in C:Code:function sse=myfit(params,Input,Actual_Output) global c a=params(1); b=params(2); x = (exp(a*2*pi)-cosh(a*b*Actual_Output))/sinh(a*b*Actual_Output); c = -1/atanh(x); Fitted_Curve = (1/(a*b))*(c-asinh(sinh(c)*exp(a*Input*2*pi))); Error_Vector=Actual_Output-Fitted_Curve ; sse=sum(Error_Vector.^2);

I know I am not passing the function the vectors in C but declaring them everytime, but I dont think this will effect the result as the vectors are constant?Code:static double function(int n, double x[]) { double c[5], x_coeff[5]; double Fitted_Curve[5]; double Error_Vector[5]; int i; double Actual_Output[5]={1.2, 2.693, 4.325, 6.131, 8.125}; double Input[5]={1, 2, 3, 4, 5}; double sum = 0; for (i = 0; i <= 4; i++) { x_coeff[i] = (exp(x[0]*2*pi)-cosh(x[0]*x[1]*Actual_Output[0]))/(sinh(x[0]*x[1]*Actual_Output[0])); c[i] = (-1)/(atanh(x_coeff[i])); Fitted_Curve[i] = (1/(x[0]*x[1]))*(c[i]-asinh(sinh(c[i])*exp(x[0]*Input[i]*2*pi))); Error_Vector[i] = Actual_Output[i]-Fitted_Curve[i]; printf(" x_coeff(%d) = %f %f\n", i, x_coeff[i], c[i]); } for (i = 0; i <= 4; i++) { sum = sum + Error_Vector[i]*Error_Vector[i]; } return sum; }

It doesnt give the same answer (atanh(x>1) = inf) which obviously suggests they are not doing the same thing. Can anyone see where I have gone wrong?

Thanks