Thread: converting function to include vectors

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    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.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, for sure you're not going to return an array... C can't do that.

    What exactly are you trying to return?

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by a.mlw.walker View Post
    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 View Post
    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..."?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Reply:

    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...
    Last edited by a.mlw.walker; 08-09-2011 at 01:24 PM.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by a.mlw.walker View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Yeah i had inititalised it.
    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
    Last edited by a.mlw.walker; 08-09-2011 at 03:16 PM.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by a.mlw.walker View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    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;
    
     }
    Last edited by a.mlw.walker; 08-09-2011 at 07:44 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by a.mlw.walker View Post
    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.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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
    Last edited by AndrewHunter; 08-09-2011 at 07:30 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    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...

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    haha....nice I am definitely going to keep that in mind next time.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    1
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with stl vectors function push_back
    By _lazycat_ in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2009, 02:53 PM
  2. Include some function form library
    By thepolo in forum C Programming
    Replies: 4
    Last Post: 05-24-2009, 05:11 PM
  3. Vectors of pointers and function templates
    By 7words in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2004, 11:39 AM
  4. Converting byte arrays to vectors
    By kasun in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2004, 10:31 AM
  5. ternary operator(?), vectors, and recursive function
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2003, 03:19 PM

Tags for this Thread