Hi tabstop: yeah I know not very good at posting am I. As confidence grows though... I have managed to get further with the function. I didnt before understand what the '/' was actually doing in matlab, I later found out t is taking the norm of the vector, so my function code has now been updated to calculate this. the good new is I am getting real numbers, the bad news is they dont match to my matlab:
Code:
static double function(int n, double x[])
{
double c;
double Fitted_Curve[5];
double Error_Vector[5];
int i;
double Input[5]={1, 2, 3, 4, 5};
double a, b, sum = 0;
double Actual_Output[5]={1.2, 2.693, 4.325, 6.131, 8.125}, v1[5], v2[5], geom_inv[5], norm, norm_2, v2sum, x_coeff = 0;
a = x[0];
b=x[1];
for (i = 0; i<=4; i++)
{
v1[i] = (exp(a*2*pi)-cosh(a*b*Actual_Output[i]));
v2[i] = (sinh(a*b*Actual_Output[i]));
//printf("%f\n", v1[i]);
}
for (i = 0; i<=4; i++)
{
v2sum=v2sum+(v2[i]*v2[i]);
}
norm = sqrt(v2sum);
//printf("%f", norm);
norm_2 = norm*norm;
for (i = 0; i<=4; i++)
{
geom_inv[i] = v2[i]/norm_2;
}
for (i = 0; i<=4; i++)
{
x_coeff = x_coeff + v1[i]*geom_inv[i];
}
printf("a = %f, b = %f, x = %f\n",a,b, x_coeff);
c = (-1)/(atanh(x_coeff));
//printf(" c(%d)= %f\n", i, c);
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];
}
return sum;
}
I worked this out by writing a program in C to replicate the matlab calculation of x:
Code:
void main()
{
double Actual_Output[5]={1.2, 2.693, 4.325, 6.131, 8.125}, v1[5], v2[5], geom_inv[5], norm, norm_2, v2sum, a, b, x_coeff;
int i;
a = -1;
b=1;
for (i = 0; i<=4; i++)
{
v1[i] = (exp(a*2*pi)-cosh(a*b*Actual_Output[i]));
v2[i] = (sinh(a*b*Actual_Output[i]));
//printf("%f\n", v1[i]);
}
for (i = 0; i<=4; i++)
{
v2sum=v2sum+(v2[i]*v2[i]);
}
norm = sqrt(v2sum);
//printf("%f", norm);
norm_2 = norm*norm;
for (i = 0; i<=4; i++)
{
geom_inv[i] = v2[i]/norm_2;
}
for (i = 0; i<=4; i++)
{
x_coeff = x_coeff + v1[i]*geom_inv[i];
}
printf("%f", x_coeff);
}
I then just added that back in to calculate x_coeff.
When the program is run by itself it calculates the same value as the matlab code for x_coeff, however run in the loop and it changes the value of x_coeff where matlab keeps it constant.
The equivelent matlab is:
Code:
Data=[1.2 2.693 4.325 6.131 8.125]
a=1;b=1;
v1=(exp(a*2*pi)-cosh(a*b*Data));
v2=sinh(a*b*Data);
x = (exp(a*2*pi)-cosh(a*b*Data))/sinh(a*b*Data)
x1=v1*(v2/norm(v2)^2)' %Inverse Vector using Geometric Multiplication
i.e in the above x and x1 are the same, therefore i used the basis of x1 to calculate it in C.
I started x[0] on -1, and x[1] on 1. pi is defined as 3.1415 at the moment.