# converting function to include vectors

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-09-2011
a.mlw.walker
converting function to include vectors
I have the following function that is being called from an algorithm.

Code:

```double rosen(double x[])  {         return (100*(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+(1.0-x[0])*(1.0-x[0]));  }```
however I have tried to adjust the function so that it is mine:

Code:

```double rosen(double x[])  {         double c[5];     double Fitted_Curve[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};   for (i = 1; i < 5; i++)   {   c[i] = -1/atanh((exp(x[0]*2*pi)-cosh(x[0]*x[1]*Actual_Output[i]))/sinh(x[0]*x[1]*Actual_Output[i]));   Fitted_Curve[i] = (1/(x[0]*x[1]))*(c[i]-asinh(sinh(c[i])*exp(x[0]*Input[i]*2*pi)));     }      }```
I am trying to minimize the parameters x[0], x[1] and x[2]. i need to tell it the vectors Actual_Output and Input.

the original code had no vectors except the ones it was solving for, so i am not sure how to return my function.
• 08-09-2011
CommonTater
Well, for sure you're not going to return an array... C can't do that.

What exactly are you trying to return?
• 08-09-2011
AndrewHunter
Quote:

Originally Posted by a.mlw.walker
Code:

```double rosen(double x[])  {       double c[5];       double Fitted_Curve[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};       for (i = 1; i < 5; i++){           c[i] = -1/atanh((exp(x[0]*2*pi)-cosh(x[0]*x[1]*Actual_Output[i]))/sinh(x[0]*x[1]*Actual_Output[i]));           Fitted_Curve[i] = (1/(x[0]*x[1]))*(c[i]-asinh(sinh(c[i])*exp(x[0]*Input[i]*2*pi)));       }      }```

Everything in red is local to your function and ceases to exist once your function exits. Perhaps you want to pass your solution array to your function as an argument and then adjust it in your function?

Quote:

Originally Posted by a.mlw.walker
I am trying to minimize the parameters x[0], x[1] and x[2]. i need to tell it the vectors Actual_Output and Input.

What are you saying here? What do you mean by "minimize parameters..."?
• 08-09-2011
a.mlw.walker
I am trying to return the error so since posting that i have
Code:

```for (i = 0; i <= 4; i++)   {   c[i] = -1/atanh((exp(x[0]*2*pi)-cosh(x[0]*x[1]*Actual_Output[i]))/sinh(x[0]*x[1]*Actual_Output[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];     }     for (i = 0; i <= 4; i++)     {         sum = sum + Error_Vector[i]*Error_Vector[i];     }     return sum;  }```
i think this code is calculating a c value, then using that to calculate a Fitted_Curve Value, then and Error Value, it calculates all of them and then calculate the sum of the errors. it then returns the sum.

the code runs however but doesnt manage to produce real values, but #nans...
• 08-09-2011
AndrewHunter
Ok, so what is the question? The only thing I don't see between your posts is you initializing sum to a value before using it in your final for loop, e.g. double sum=0; at the top of your function.
• 08-09-2011
AndrewHunter
Quote:

Originally Posted by a.mlw.walker
the code runs however but doesnt manage to produce real values, but #nans...

Define 'produce real values'. The only thing this function returns is sum. Did you initialize sum like I pointed out? What value of sum are you getting? Note: You cannot access any of your arrays you defined in your function outside of your function.
• 08-09-2011
tabstop
If you're getting #nan, it might help to make sure that none of your x's are zero, since you are dividing by them.
• 08-09-2011
a.mlw.walker
the problem is the function i have shown you is sent by main to another function to do a downhill simplex method on.
The function i have been showing you is in full
Code:

```double rosen(double x[])  {         double c[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++)   {   c[i] = -1/atanh((exp(x[0]*2*pi)-cosh(x[0]*x[1]*Actual_Output[i]))/sinh(x[0]*x[1]*Actual_Output[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];     }     for (i = 0; i <= 4; i++)     {         sum = sum + Error_Vector[i]*Error_Vector[i];     }     return sum;  }```
it is sent to the function simplex in main such that:
Code:

```int main()  {         double start[] = {1,1.0,1};         double min;         int i;         min=simplex(rosen,start,3,1.0e-4,1,my_constraints);         //min=simplex(rosen,start,2,1.0e-4,1,NULL);         for (i=0;i<3;i++) {                 printf("%f\n",start[i]);         }         return 0;  }```
my_constraints is just another function sent to simplex
and when i say nan, i mean 1.#QNAN0
• 08-09-2011
AndrewHunter
Quote:

Originally Posted by a.mlw.walker
and when i say nan, i mean 1.#QNAN0

Ok, well this is telling you that your math is resulting in an error. It could be anything from:

1. the values in your x[] array being 0 and then dividing by them
2. any of your math that uses exponents resulting in undefined exponent operations.
3. Anything where you are using your trig functions that would result in a value out of bounds.

You are going to need to go through and debug your loops to find out where your math operations are failing. I would recommend using a debugger so you could step through your loop and see the variables of your arrays. Additionally, you could just add printf statements to watch your output as the loops cycle through.
• 08-09-2011
a.mlw.walker
OK thanks, I'll do that, however could you please confirm whether the functions i pasted above are doing what i think they are doing. My job is programming in Matlab however for this task i am required to write it in C. Therefore my understanding of programming with matrices does cut the mustard in C. Ideally I would like to pass the function rosen the vectors Actual_Output and Input from main, but I cant get the pass to work as it goes through simplex. Could you help me with that? I think I just include double[] everytime I pass an array to a new function, but how do you send it from a function to a function through a function??

Cheers Andrew

Edit: After doing what you said the variable c is not being calculated at all, it is coming up as #QNAN0, therefore nothing else can be calculated.
Edit2: So i have narrowed it down by seperating the formulation of the variable c into two parts. Trying it in matlab the values given by x_coeff (below) cannot me arc-tanh-ed. Therefore there is something wrong with my equation for x_coeff:
Code:

```  double rosen(double x[])  {         double c, x_coeff;     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; x_coeff = (exp(x[0]*2*pi)-cosh(x[0]*x[1]*Actual_Output[0]))/sinh(x[0]*x[1]*Actual_Output[0]); c = ((-1)/atanh(x_coeff)); printf("variable c = %f variable x_coeff = %f\n",c,x_coeff); printf("variable x[0] = %f  x[1] = %f  AO[0] = %f\n",x[0],x[1],Actual_Output[0]);   for (i = 0; i <= 4; i++)   { //stick c back in here if doesnt work, and remember to change c's below to arrays plus double above.   Fitted_Curve[i] = (1/(x[0]*x[1]))*(c-asinh(sinh(c)*exp(x[0]*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("variable Fitted_Curve = %f\n",Fitted_Curve[0]);     return sum;  }```
• 08-09-2011
quzah
Quote:

Originally Posted by a.mlw.walker
Could you help me with that? I think I just include double[] everytime I pass an array to a new function, but how do you send it from a function to a function through a function??

The same way you got it there in the first place:
Code:

```void foo( type array[] ) {   ... do stuff on array } void bar( type array[] ) {     ... do stuff on array     foo( array ); /* pass it to foo */ } ... int main( void ) {     type array[ SOMESIZE ];     bar( array );     return 0; }```
Where type is whatever type you want your array to hold.

Quzah.
• 08-09-2011
AndrewHunter
You can pass your array through multiple functions, since arrays just decay to pointers when passed if that is what you asking. Something like:
Code:

```#include <stdio.h> void foo(int [], int); void foo1(int [], int); void foo2(int [], int); int main (void) {           int myArray[3] = {0,1,2};                foo(myArray, 3);         printf("\nIn main()");         for(int i=0; i < 3; i++)                 printf("%d ", myArray[i]);         getchar();         return 0; } void foo(int Array[], int size){         for(int i=0; i < size; i++)                 Array[i]+=1;         foo1(Array, 3); } void foo1(int Array[], int size){         for(int i=0; i < size; i++)                 Array[i]+=1;         foo2(Array, 3); } void foo2(int Array[], int size){         printf("\nIn foo2()");         for(int i=0; i < size; i++)                 printf("%d ", Array[i]); }```
EDIT: Too slow
• 08-10-2011
CommonTater
Quote:

Originally Posted by AndrewHunter
You can pass your array through multiple functions, since arrays just decay to pointers when passed if that is what you asking. Something like:
Code:

```void foo(int [], int); void foo1(int [], int); void foo2(int [], int);```
EDIT: Too slow

And a rather obvious missed opportunity...

Code:

```void foot (int [], int); void fool (int [], int); void food (int [], int);```
But then, I probably shouldn't give up my day job either...
• 08-10-2011
AndrewHunter
haha....nice :biggrin: I am definitely going to keep that in mind next time. :tongue:
• 08-10-2011
wygant78
The only thing I don't see between your posts is you initializing sum to a value before using it in your final for loop, e.g. double sum=0; at the top of your function.

http://www.ywseo.net/seo4.jpghttp://www.ywseo.net/seo3.jpg
http://www.ywseo.net/signature.jpghttp://www.ywseo.net/uk.jpg
http://www.ywseo.net/guilai2.jpg
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last