-3

I doing integration of a complex function using gsl library in C programming language. In this code I had to declare two variables using pointer that I have done successfully. But I am facing a problem when I pass these variables in main function.

Please help me out.

My code is being written here:

struct har{

double t;
double k;
double x;
};

#include"str.h"
struct har H1( void  * params, float q , int p )

    {

    struct har  *p_params = (void *)params;


    float  mu=2;
    double x =p_params ->x ;
    double t = p_params -> t;
    double  k = p_params -> k;
    //printf("%d",k);
    struct har  H1 =  {x*cos(t*x)/(pow((mu*t*k),2)+pow(x,2))};
    return H1;
    }


 double H (double x,void * params )
{
double e;
double t = *(double *) params;
//printf("%f\n",t);
//struct har t1,z;
//double t = z.params ->t1;
double H  = (pow(e,-x)/x) ;//I(x,&t)*(sin(x*t));  
return H;
}


#include<stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include"H.h"
#include"H1.h"

int
main (void)
{
  gsl_integration_workspace * w
    = gsl_integration_workspace_alloc (10000);

  struct har t,k;

  double x, qtr, qbartr, qdottr, qddotr,q7r, qatr, qbtr, qt1r, error;
  double expected = -4.0;
  double a1 = 1e-14;
  double a= 150;//150;
  double pi = 3.141;
  double b = 1.05263;
  double mu = (2*pi)/b;
  double q0=0;
  double p0=6.5;
  double om=7.07;
  double tau=0.141;
  double gamma = 0.180;

for(int m=2; m<4; m++)
{


 float  t = 4.14937*(m-1);


 gsl_function qt;
 qt.function = &H;
 qt.params =&t;
 gsl_integration_qags (&qt, a1, a, 0,1e-7, 10000, w, &qtr, &error);
// printf ("qtresult = % .18f\n\n", qtr);

 for(int k=1; k<=2; k++)
    {
      {
struct har item = {x,t,k};
struct har *p_params = &item;


       double  gama=gamma/((mu*k*tau)+1);
//      printf("gama = % .18f\n", gama);

       gsl_function qt1;

       qt1.function = &H1;
       qt1.params = &p_params;
    // qt1.params = &k;
       gsl_integration_qags (&qt1, a1, a, 0,1e-7, 10000, w, &qt1r, &error);
      printf("qt1result= % .18f\n", qt1r);


     }

}


  gsl_integration_workspace_free (w);

  return 0;
}

In above code I had four file by namely code.c, str.h, H1.h and H.h. Where H1 and H contains two user defined functions and str is a file for structure and code.c is my main to compile.

Thanks for your time and observation.

Regards

Ramesh

1 Answers1

1

The field qt1.function has type double (*)(double, void *). The function H1 which you attempt to assign to this field has type struct har (*)( void *, float, int).

These types are incompatible as the number of arguments, the types of the arguments, and the return type are all different. As it is right now, you have two parameters called p and q that are never being used. Also, the struct har you construct as a return value doesn't even set all the fields, just the first one, so why are you even returning a struct?

In order for H1 to work with gsl_function, is must accept a double and a void * and return a double. The x value corresponds to the double that is passed in, so you don't need that in your struct. Since you're also only building a single value to return, that's what you return.

struct har{
    double t;
    double k;
};

double H1( double x, void  *params)
{
    struct har *p_params = params;   // no need to cast to/from void *

    float mu=2;
    double t = p_params->t;
    double k = p_params->k;

    return  x*cos(t*x)/(pow((mu*t*k),2)+pow(x,2));
}

Also, you're not setting the values of qt1 properly:

qt1.params = &p_params;

Since p_params is a struct har *, its address is a struct har **. Your function however is expecting a struct har * to be passed in. So change this to the address of the struct, not the address of a pointer pointing to it:

qt1.params = &item;

You also have a mismatch when you set qt.params. You're giving it a float *, but your function H is expecting a double *. Just change the type of t to match:

double t = 4.14937*(m-1);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thanks for your kind response sir, In my case I need to declare a structure in my code because gsl params pointer (in gsl_integration.h file) can access only one member either "t" or "k" at a time. That's why I am using struct har type In H1 else its showing an another error ( returning type ‘double’ but ‘struct har’ was expected). So plz help me out how should I declare these data type. Thanks for your valuable time and observation. – Ramesh Kumar Jun 22 '17 at 07:31
  • @RameshKumar See my edit. You need to change `H1` to match the function pointer. – dbush Jun 22 '17 at 17:05
  • Thanks a lot Sir, Now my code is running very well. – Ramesh Kumar Jun 23 '17 at 10:39
  • @RameshKumar Glad I could help. Feel free to [accept this answer](https://stackoverflow.com/help/accepted-answer) if you found it useful. – dbush Jun 23 '17 at 11:07