Thread: Issue with math

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    42

    Issue with math

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    int xPosCalc(int sigmaMatter, int muMatter, int sigmaAntimatter, int muAntimatter, int xRand)
    {
        int seed = time(NULL);
        srand(seed);
        xRand = rand() % 36;
        int xPosMatter = 1/(sigmaMatter*sqrt(2*3.14159))*exp(-((xRand-muMatter)*(xRand-muMatter))/(2*(sigmaMatter*sigmaMatter)));
        int xPosAntimatter = 1/(sigmaAntimatter*sqrt(2*3.14159))*exp(-pow((xRand-muAntimatter), 2)/(2*(pow(sigmaAntimatter, 2))));
        int xProbability = xPosMatter * xPosAntimatter;
        //printf("\nMatter: %d\nAntimatter: %d", xPosMatter, xPosAntimatter);
        //printf("\nProbability is: %d", xProbability);
    }
    
    int main(void)
    {
        int sigmaMatter = 4.2;
        int muMatter = 27.0;
        int sigmaAntimatter = 6.0;
        int muAntimatter = 17.0;
        int xRand = 0.0;
        xPosCalc(sigmaMatter, muMatter, sigmaAntimatter, muAntimatter, xRand);
        int i, p;
        int count = 0; 
        int pTotal = 0;
        for(i =0; i <= 4999; i=i+1)
        {
            p = 1/(4.2*sqrt(2*3.14159))*exp(-((27-xRand)*(27-xRand))/((2*4.2)*(2*4.2)))*1/(6*sqrt(2*3.14159))*exp(-((17-xRand)*(17-xRand))/((2*6)*(2*6)));
            count++;
            pTotal = pTotal + p;
        }
        int pFinal = pTotal/count;
        printf("\npTotal: %d", pTotal);
        printf("\nTotal iterations: %d", count);
        printf("\nFinal probability: %d\n", pFinal);
    }
    Alright, p in the main routine is supposed to be the first attachment,
    Issue with math-p-gif
    and xPosmatter/xPosantimatter are supposed to be the second attachment.
    Issue with math-xposmatter-xposantimatter-gif
    I'm getting the wrong output, so I just want to make sure I translated the math right before I go any further.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Gah! Put some white space around your operators in those long equations, even stretch them onto multiple lines, it will make it much easier to read your equations. Also, using temporary variables for intermediate values may make it easier to keep things straight.

    You are misinterpreting a couple of the denominators of the first equation:
    2*42 is 2*4*4, not (2*4)*(2*4)
    same with 2*62
    Last edited by anduril462; 05-22-2013 at 10:56 AM. Reason: minor additions

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    42
    I'll separate out the steps with some temporary variables and see if I can get anywhere.

    Also, if you look at the attachments I posted, the square is on the outside of the parentheses. (2*4)^2 does equal (2*4)*(2*4).

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Paul Omans View Post
    Also, if you look at the attachments I posted, the square is on the outside of the parentheses. (2*4)^2 does equal (2*4)*(2*4).
    Well, I don't see (2*4)^2 in your attachments. I only see 2 * 4.2^2 which is 2 * 4.2 * 4.2.

    Bye, Andreas

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int sigmaMatter = 4.2;
    ...
    int count = 0;
    int pTotal = 0;
    ...
    int pFinal = pTotal/count;
    printf("\nFinal probability: %d\n", pFinal);
    Are you sure you should be using int types here? Things like probabilities are usually represented as floating point values. If that is the case then you are also doing integer division which probably isn't what you want.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Since the summation does not contain the summation variable i in (using Maple syntax)
    Code:
    P := 1/5000 + sum( (1/(4.2*sqrt(2*Pi))) * exp(-(27-x)^2/(2*4.2^2)) / (6*sqrt(2*Pi)) * exp(-(17-x)^2/(2*6^2)), i=0..4999 );
    the sum is equivalent to
    Code:
    P = 0.0000000000001211344564478819687921177341468858685481 * exp(x*(2.002834467120181405895691609977324263038 - 0.04223356009070294784580498866213151927438 * x);
    I suspect both x in the summation should have been xi? In that case, P can be written in pseudocode as
    Code:
    P = 0.0
    for i = [0, 4999]:
        P = P + exp( -4.013888888888888888888888888888888888889
                   + x[i] * ( 2.002834467120181405895691609977324263038
                     - x[i] * ( 0.04223356009070294784580498866213151927438 )))
    end for
    P = P * 0.00000000000000134124305433139340656790345171524598923


    The second one is a simple Gaussian probability density function. Using Maple syntax,
    Code:
    p := 1/(sigma * sqrt(2*Pi)) * exp( - (x - mu)^2 / (2*sigma^2) );
    it boils down to
    Code:
    p = 0.3989422804014326779399460599343818684758 / sigma * exp(-0.5*((x-mu)/sigma)*((x-mu)/sigma));


    I'm a very firm believer in first simplifying the formulas with a computer algebra system (I like Maple, but Maxima is free and open source), then writing them in pseudocode (often awk for quick tests!), and only then implementing them in C or other low-level language. As long as you check your results on each step, you always have a reliable basis you can rely on.

    The OP is in a difficult position: it is very hard to say where the problem lies. Could be the math (notation error in original equations), could be the formula, could be the C implementation.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    42
    Quote Originally Posted by AndiPersti View Post
    Well, I don't see (2*4)^2 in your attachments. I only see 2 * 4.2^2 which is 2 * 4.2 * 4.2.

    Bye, Andreas
    Oh shoot, you're right. Now I feel pretty stupid. I was looking at the wrong section.

    Thanks for all the helpful replies people!! I'm still working on the code, I'll post my progress later tonight (it's difficult to sit indoors and code when it's 85 F outside and breezy!)

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Paul Omans View Post
    Thanks for all the helpful replies people!! I'm still working on the code, I'll post my progress later tonight (it's difficult to sit indoors and code when it's 85 F outside and breezy!)
    So code outside.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    42
    Quote Originally Posted by anduril462 View Post
    So code outside.
    I've heard of people coding while longboarding, but personally I would find that a little dangerous.

    Here's what I got:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    int xPosCalc(float sigmaMatter, float muMatter, float sigmaAntimatter, float muAntimatter, float xRand)
    {
        float seed = time(NULL);
        srand(seed);
        xRand = rand() % 36;
        float xPosMatter = 1/(sigmaMatter*2.506627216)*exp(-((xRand-muMatter)*(xRand-muMatter))/(2*(sigmaMatter*sigmaMatter)));
        float xPosAntimatter = 1/(sigmaAntimatter*2.506627216)*exp(-pow((xRand-muAntimatter), 2)/(2*(pow(sigmaAntimatter, 2))));
        float xProbability = xPosMatter * xPosAntimatter;
        //printf("\nMatter: %d\nAntimatter: %d", xPosMatter, xPosAntimatter);
        //printf("\nProbability is: %d", xProbability);
    }
    
    int main(void)
    {
        float sigmaMatter = 4.2;
        float muMatter = 27.0;
        float sigmaAntimatter = 6.0;
        float muAntimatter = 17.0;
        float xRand = 0.0;
        xPosCalc(sigmaMatter, muMatter, sigmaAntimatter, muAntimatter, xRand);
        int i;
        int count = 0; 
        float p = 0;
        float pTotal = 0;
        for(i =0; i <= 4999; i=i+1) //in step 2, addition between should be changed to multiplication
        {
            p = 0.09498629735*exp(-((27-xRand)*(27-xRand))/25.28)*0.06649040814*exp(-((17-xRand)*(17-xRand))/72);
            count++;
            pTotal = pTotal + p;
        }
        float pFinal = pTotal/count;
        printf("\npTotal: %d", pTotal);
        printf("\nTotal iterations: %d", count);
        printf("\nFinal probability: %d\n", pFinal);
    }
    I condensed the math, and I now get numbers for pTotal and pFinal instead of just zeros. I'll continue working on it tomorrow, I still need to split the functions into separate .c/.h files and create a makefile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  2. Issue with pointer math
    By Kudose in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 04:25 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math...
    By TeQno in forum C Programming
    Replies: 4
    Last Post: 07-24-2003, 07:21 AM
  5. how much math?
    By Yaj in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 06-06-2002, 09:42 AM