The foloowing code is the matlab code to minimize a function using the nelder mead method.(adsbygoogle = window.adsbygoogle || []).push({});

This is supposed to be the equivelent C code, however it doesnt produce the same results. Can anyone see what I may have done wrong?Code (Text):

Starting=[1 1 -1]; options=optimset('Display','iter'); Estimates=fminsearch(@myfit,Starting,options,t_d,Data) Calling: function sse=myfit(params,Input,Actual_Output) a=params(1); b=params(2); c=params(3);

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);

ThanksCode (Text):

static double function(int n, double x[])

{

double c;

double Fitted_Curve[5];

double Error_Vector[5];

int i;

double a, b, sum = 0;

double v1[5], v2[5], geom_inv[5], norm = 0, norm_2 = 0, v2sum = 0, x_coeff = 0;

// Actual_Output[5] = {1.2, 2.693, 4.325, 6.131, 8.125};

a = x[0];

b=x[1];

c=x[2];

for (i = 0; i <= 4; i++)

{

Fitted_Curve[i] = (1/(a*b))*(c-asinh(sinh(c)*exp(a*Input[i]*2*pi)));

Error_Vector[i] = Actual_Output[i]-Fitted_Curve[i];

}

for (i = 0; i <= 4; i++)

{

sum = sum + Error_Vector[i]*Error_Vector[i];

}

printf("sum = %f\n", sum);

a_global = a;

b_global = b;

// x_coeff_global = x_coeff;

return sum;

}

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Minimizing function in Matlab and C

Loading...

Similar Threads - Minimizing function Matlab | Date |
---|---|

Calc of variations, minimizing functionals question | Nov 20, 2015 |

Minimizing functions | Apr 7, 2015 |

Minimizing distance between two function | Apr 13, 2013 |

Minimizing a functional definite integral | Dec 19, 2012 |

Use of a derivative or a gradient to minimize a function | Aug 6, 2012 |

**Physics Forums - The Fusion of Science and Community**