Alex Gordon
Alex Gordon

Reputation: 60691

c# math formula output inconsistency

here is my formula:

y = b*exp(-((x-c)^2)/2(d^2)) if x < c, else y = b*exp(-(x-c)^2/2g^2);

this is how i coded it:

        for (double x = 0; x <= 7D; x += .01D)
        {
            b = 6.0410638; c = 2.1344769; d = 0.59183047; g = 0.77384504;
            if (x < c)
                y = b * Math.Exp(-Math.Pow((x - c), 2)) / (2 * Math.Pow(d, 2D));
            else
                y = b * Math.Exp(-Math.Pow((x - c), 2)) / (2 * Math.Pow(g, 2D));
            qResults.Rows.Add(x, y);
        }

the output:

    0.0529826172
0.05528786
0.05768187
0.0601675063
0.0627477
0.06542546
0.06820385
0.07108601
0.074075155
0.0771745443
0.08038754
0.08371756
0.08716809
0.0907426849
0.0944449753
0.0982786641
0.102247514
0.106355369
0.110606134
0.115003787
0.119552381
0.124256022
0.1291189
0.13414526
0.139339417
0.144705743
0.150248691
0.155972764
0.16188252
0.167982608
0.1742777
0.180772528
0.187471911
0.1943807
0.201503783
0.2088461
0.2164127
0.224208578
0.232238829
0.2405086
0.24902302
0.257787317
0.2668067

is there a problem with my syntax?

the output should be:

0.009332048
0.009915393
0.010532198
0.011184179
0.011873133
0.012600931
0.013369526
0.014180954
0.015037339
0.015940891
0.016893914
0.017898806
0.01895806
0.020074273
0.021250142
0.022488471
0.023792173
0.025164271
0.026607905
0.028126332
0.029722928
0.031401194
0.033164757
0.035017372
0.036962928
0.039005447
0.041149089
0.043398156
0.045757092
0.048230485
0.050823073
0.053539744
0.05638554
0.059365657
0.062485449
0.065750429
0.069166271
0.072738815
0.076474061
0.08037818
0.084457508
0.088718551
0.093167983
0.097812651
0.102659571
0.107715931
0.112989091
0.118486582
0.124216105
0.130185532
0.136402905
0.142876432
0.149614489
0.156625615
0.163918513
0.171502045
0.17938523
0.187577239
0.196087395
0.204925165
0.21410016
0.223622125
0.233500937
0.2437466
0.254369235
0.265379079
0.276786473
0.288601856
0.30083576
0.313498797
0.326601652
0.340155077
0.354169874
0.368656891
0.383627009
0.39909113
0.415060167
0.431545027
0.448556606
0.466105768
0.484203337
0.502860078
0.522086688
0.541893773
0.562291841
0.583291279
0.604902341
0.627135126
0.649999569
0.673505413
0.697662199
0.722479244
0.747965622
0.774130146
0.800981347
0.828527458
0.856776387
0.885735705
0.915412619
0.945813957
0.976946142
1.008815173
1.041426608
1.074785537
1.108896564
1.143763787
1.179390775
1.21578055

Upvotes: 0

Views: 221

Answers (1)

Mike Dinescu
Mike Dinescu

Reputation: 55720

Based on your formula, the code should be:

    for (double x = 0; x <= 7D; x += .01D)
    {
        b = 6.0410638; c = 2.1344769; d = 0.59183047; g = 0.77384504;
        if (x < c)
            y = b * Math.Exp(-Math.Pow(x - c, 2) / (2 * Math.Pow(d, 2D)));
        else
            y = b * Math.Exp(-Math.Pow(x - c, 2) / (2 * Math.Pow(g, 2D)));
        qResults.Rows.Add(x, y);
    }

Basically, the Math.Exp function should be applied to the result of the division, not just to the result of -Math.Pow(x - c, 2)

Also, it's not very clear based on the formula and your code example whether you need to divide by 2 in the exponent computation, then multiply the result by d^2, or actually divide by 2 * d^2 as you are doing in the implementation.

Upvotes: 6

Related Questions