Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Please help me understand how to solve the problem "Code of Polar function"

  1. Sep 30, 2016 #1
    Hi everybody,
    I have a code for Polar function in Bilayer graphene. I compile and run it. Everything is ok but at x=2, the value of Polar function is so large. I don't know why this happens ? Please help me
    Code (C):
    //====================LIBRARY===================================================
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    //====================CONSTANTS(CGS)============================================
    #define  pi               3.141592654
    #define  kB               1.3806503*1e-16
    #define  hbar             1.0545716*1e-27
    #define    msao           0.033*9.10938188*1e-28
    #define    elec           4.80320427*1e-10
    #define    kappa          2.45
    //====================ROUTINE===================================================
    double PI(double x,double T1);
    //====================MAIN======================================================
    double x,T1;
    int main()
    {
      FILE *output;
      output=fopen ("Pi.dat","w+" );
    //   n=2*1e12;
      T1=0.01;
      for (x=0.; x<=4.; x+=0.1)
        {
                  printf("%f %f\n ",x,PI(x,T1));
                  fprintf(output,"%f %f\n",x,PI(x,T1));
        }
      fclose(output);
      printf("\nfin\n");
      getch();
    }
    double PI(double x,double T1)
    { double g0,f0,g1,f1,kq;
    //  x=2*k1*sin(phi/2);
      g0=sqrt(4+pow(x,4))/2-log((1+sqrt(1+pow(x,4)/4))/2);
      f0=((2+x*x)/2/x)*sqrt(x*x-4)+log((x-sqrt(x*x-4))/(x+sqrt(x*x-4)));
      g1=(1+(pow(x,4)/2)-sqrt(1+pow(x,4)/4))/sqrt(1+pow(x,4)/4);
      f1=((x*x-1)*(pow(x,4)-5*x*x+2))/(x*sqrt(pow(x*x-4,3)));
      if (x>2)
            kq=g0-f0+(pi*pi/6)*(T1*T1)*(g1-f1);
       else if (x<2)
           kq=g0+(pi*pi/6)*(T1*T1)*g1;
       else
              kq= sqrt(5)-log((1+sqrt(5))/2)-sqrt(pi/4)*(1-sqrt(2))*(-1.4603545)*sqrt(T1)-sqrt(pi)*(1-sqrt(2)/2)*2.612*pow(sqrt(T1),3);
    return kq;
      }
     
     
  2. jcsd
  3. Sep 30, 2016 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    Well, have you stepped through the code with x=2 to see what it does? Do you understand code debugging?
     
  4. Oct 2, 2016 #3

    pasmith

    User Avatar
    Homework Helper

    The denominator of
    Code (C):
    f1=((x*x-1)*(pow(x,4)-5*x*x+2))/(x*sqrt(pow(x*x-4,3)));
    appears to be [itex]x(x^2 - 4)^{3/2}[/itex]. This is zero when [itex]x = \pm2[/itex]. This is a problem.
     
  5. Oct 2, 2016 #4
    Thanks phinds, pasmith :)
    I tried to test with code:
    Code (C):
    //====================LIBRARY===================================================
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    //====================CONSTANTS(CGS)============================================
    #define  N                1000.
    #define  pi               3.141592654
    #define  kB               1.3806503*1e-16
    #define  hbar             1.0545716*1e-27
    #define    msao           0.033*9.10938188*1e-28
    #define    elec           4.80320427*1e-10
    #define    kappa          2.45
    //====================ROUTINE===================================================
    double PI(double x,double T1);
    double test(double T1);
    //====================MAIN======================================================
    double x,T1;
    int main()
    {
      FILE *output;
      output=fopen ("Pi.dat","w+" );
    //   n=2*1e12;
      T1=0.01;
      for (x=0.; x<=4.; x+=0.1)
        {
                  printf("%f %f %f\n ",x,PI(x,T1),test(T1));
                  fprintf(output,"%f %f\n",x,PI(x,T1));
        }
      fclose(output);
      printf("\nfin\n");
      getch();
    }
    double PI(double x,double T1)
    { double g0,f0,g1,f1,kq;
    //  x=2*k1*sin(phi/2);
      g0=sqrt(4+pow(x,4))/2-log((1+sqrt(1+pow(x,4)/4))/2);
      f0=((2+x*x)/2/x)*sqrt(x*x-4)+log((x-sqrt(x*x-4))/(x+sqrt(x*x-4)));
      g1=(1+(pow(x,4)/2)-sqrt(1+pow(x,4)/4))/sqrt(1+pow(x,4)/4);
      f1=((x*x-1)*(pow(x,4)-5*x*x+2))/(x*sqrt(pow(x*x-4,3)));
      if (x==2)
    //       kq= sqrt(5)-log((1+sqrt(5))/2)-sqrt(pi/4)*(1-sqrt(2))*(-1.4603545)*sqrt(T1)-sqrt(pi)*(1-sqrt(2)/2)*2.612*pow(sqrt(T1),3);
           kq= 1.75486-(0.536077*sqrt(T1))-(1.35599*sqrt(T1)*sqrt(T1)*sqrt(T1));
           else
      {
        if (x<2)
             kq=g0+(pi*pi/6)*(T1*T1)*g1;
       else
           kq=g0-f0+(pi*pi/6)*(T1*T1)*(g1-f1); }
    return kq;
      }
    double test(double T1)
    { double kq;
      kq= sqrt(5)-log((1+sqrt(5))/2)-sqrt(pi/4)*(1-sqrt(2))*(-1.4603545)*sqrt(T1)-sqrt(pi)*(1-sqrt(2)/2)*2.612*pow(sqrt(T1),3);
      return kq;
    }
    when x=2, I add this line:
    Code (C):
      if (x==2)
    //       kq= sqrt(5)-log((1+sqrt(5))/2)-sqrt(pi/4)*(1-sqrt(2))*(-1.4603545)*sqrt(T1)-sqrt(pi)*(1-sqrt(2)/2)*2.612*pow(sqrt(T1),3);
           kq= 1.75486-(0.536077*sqrt(T1))-(1.35599*sqrt(T1)*sqrt(T1)*sqrt(T1));
    My purpose is to eliminate divergence at x=2 in my code. But, this command line don't work :( . I don/t know why ? :(
     
  6. Oct 2, 2016 #5
    f1 is evaluated and used only in case of x!=2, so try wrapping it up in the same scope level of kq. For example
    PHP:
    if(x!=2)
    {
        if (x>2) //do something, related f,g
        else //do something else , related f,g
    }
    else //x=2
    {
    }
    Also your loop uses float data x as an iterative value and you make comparison with it in your PI calculation, which is not really good, so you may think about changing your loop method to use integral value as well as your floating point value comparison (we human know 1.9999999!=2.0 but computer might likely not).
    Remember to declare and define your objects only within their valid scopes e.g right before they are used, not long before they are used.
     
    Last edited: Oct 2, 2016
  7. Oct 2, 2016 #6
    Thanks you so much, Pepper Mint. Your ideas are so great. I was successful. My code worked.

    Code (C):
    //====================LIBRARY===================================================
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    //====================CONSTANTS(CGS)============================================
    #define  pi               3.141592654
    #define  kB               1.3806503*1e-16
    #define  hbar             1.0545716*1e-27
    #define    msao           0.033*9.10938188*1e-28
    #define    elec           4.80320427*1e-10
    #define    kappa          2.45
    //====================ROUTINE===================================================
    double PI(double x,double T1);
    //====================MAIN======================================================
    double x,T1;
    int main()
    {
      FILE *output;
      output=fopen ("Pi.dat","w+" );
    //   n=2*1e12;
      T1=0.001;
      for (x=0.; x<=4.; x+=0.1)
        {
                  printf("%f %f\n ",x,PI(x,T1));
                  fprintf(output,"%f %f\n",x,PI(x,T1));
        }
      fclose(output);
      printf("\nfin\n");
      getch();
    }
    double PI(double x,double T1)
    { double g0,f0,g1,f1,kq;
    //  x=2*k1*sin(phi/2);
      g0=sqrt(4+pow(x,4))/2-log((1+sqrt(1+pow(x,4)/4))/2);
      f0=((2+x*x)/2/x)*sqrt(x*x-4)+log((x-sqrt(x*x-4))/(x+sqrt(x*x-4)));
      g1=(1+(pow(x,4)/2)-sqrt(1+pow(x,4)/4))/sqrt(1+pow(x,4)/4);
      f1=((x*x-1)*(pow(x,4)-5*x*x+2))/(x*sqrt(pow(x*x-4,3)));
      if (x>2.0000001)
            kq=g0-f0+(pi*pi/6)*(T1*T1)*(g1-f1);
       else if (x<2)
           kq=g0+(pi*pi/6)*(T1*T1)*g1;
       else
              kq= sqrt(5)-log((1+sqrt(5))/2)-sqrt(pi/4)*(1-sqrt(2))*(-1.4603545)*sqrt(T1)-sqrt(pi)*(1-sqrt(2)/2)*2.612*pow(sqrt(T1),3);
    return kq;
      }
     
     
  8. Oct 4, 2016 #7

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    You are doing a lot of calculations of functions of x*x-4 which are very treacherous if the computer thinks that is negative at x=2.0. You should skip all those calculations if x=2.0. But don't test for exact equality. Use a test like (fabs( x-2 ) < 0.000001)
     
  9. Oct 4, 2016 #8
    Thanks FactChecker due to your ideas :). I think that computer don't see (x*x-4) as a negative number at x=2. Because if x<2, the command line is :
    Code (Text):
    else if (x<2)
           kq=g0+(pi*pi/6)*(T1*T1)*g1;
    So, f0 doesn't appear, ie sqrt(x*x-4) doesn't appear when we consider case x=2
     
  10. Oct 4, 2016 #9

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    You may be right. But unless you want to figure out how the round-off of x*x-4 works inside the processor, I think it is better to be safe. If you figure out that it is safe and execute that code, you should document it so that you and others will not have to figure it out again later. By the time you test x<2 it has already done a lot of powers and sqrts using x*x-4. Are all of those safe?
     
  11. Oct 4, 2016 #10
    I think this has already been alluded to, but f0 and f1 are only used in one if-block, so they should only be calculated in that if-block.
    Code (Text):
       ...
       g0=sqrt(4+pow(x,4))/2-log((1+sqrt(1+pow(x,4)/4))/2);
       g1=(1+(pow(x,4)/2)-sqrt(1+pow(x,4)/4))/sqrt(1+pow(x,4)/4);
       if (x > 2.0)
       {
          double f0=((2+x*x)/2/x)*sqrt(x*x-4)+log((x-sqrt(x*x-4))/(x+sqrt(x*x-4)));
          double f1=((x*x-1)*(pow(x,4)-5*x*x+2))/(x*sqrt(pow(x*x-4,3)));
          kq=g0-f0+(pi*pi/6)*(T1*T1)*(g1-f1);
       }
       else if (x < 2.0)
          kq=g0+(pi*pi/6)*(T1*T1)*g1;
       else
          kq=sqrt(5)-log((1+sqrt(5))/2)-sqrt(pi/4)*(1-...
     
  12. Oct 14, 2016 #11

    ChrisVer

    User Avatar
    Gold Member

    just a shorter way to merge an ifs....
    Code (C):

    if (abs(x-2)<0.000001){
       kq= sqrt(5)-log((1+sqrt(5))/2)-sqrt(pi/4)*(1-...;
    }
    else {
       double f0=... *(!(x<2.0));
       double f1=... *(!(x<2.0));
       kq= g0-f0+(pi*pi/6)*(T1*T1)*(g1-f1);
    }
     
    does that look ok? the if and else if were turned into boolean expressions (that return 0 and 1) since the kq expression looks the same except for the fact that you used the f0,f1. So if x<2.0, !(x<2.0) returns False (or 0) the f0=f1=0 and add nothing to kq (they don't exist) else it's True (or 1) f0=(f0's expression) and f1=(f1's expression) and are included in the kq calculation. They act like Theta's functions.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Please help me understand how to solve the problem "Code of Polar function"
  1. Please help me (Replies: 3)

Loading...