944,058 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1036
  • C++ RSS
Oct 25th, 2007
0

Need Help Fast!!!

Expand Post »
Need to figure out what is going wrong with my source code. When I imput the values of 72, 69, and 2.8 respectively I should output pdf=0.85801, however I am not getting that value. I don't know why. I am doing the adaptive simpsons method to find the probability density of data under a normal curve. I believe that I might have the "approx" in the wrong spot, or I'm not calling my functions correctly. This like will take you to a PDF of what I am trying to do, I'm totally lost at this point.
http://ezekiel.vancouver.wsu.edu/~cs...ts/pdf/pdf.pdf

C++ Syntax (Toggle Plain Text)
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. double INV_SQRT_2PI= 0.39894228; /*1/sqrt(2*pi)*/
  5. double a, b, u, U, O, c, left, right, d, approx, x;
  6. double epsilon= 0.0000001; /*epsilon defined as 0.0000001*/
  7. double f(double x); /*prototype functions*/
  8. double S(double a, double b);
  9. double asimpson(double a, double b, double approx, double epsilon);
  10.  
  11.  
  12. int main(void)
  13. {
  14. printf("Enter x Value: ");
  15. scanf("%lf", &x);
  16. printf("Enter Mean: ");
  17. scanf("%lf", &U);
  18. printf("Enter Standard Deviation: ");
  19. scanf("%lf", &O);
  20. double pdf, u;
  21. u=((x-U)/O);
  22. pdf=((1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),epsilon));
  23.  
  24. printf("pdf= %f",pdf);
  25.  
  26. return 0;
  27. }
  28. double f(double x)
  29. {
  30. return (exp(double(x*x*(-1))/2));
  31. }
  32. double S(double a,double b)
  33. {
  34. return ((b-a)/6)*(f(a)+(4*f((a+b)/2))+f(b));
  35. }
  36. double asimpson(double a,double b,double approx, double epsilon)
  37. {
  38. approx= S(a,b);
  39. c=((a+b)/2);
  40. left=S(a,c);
  41. right=S(c,b);
  42. d=((left+right-approx)/15);
  43. if(abs(d)<=epsilon)
  44. return (left+right+d);
  45. return (asimpson(a,c,left,epsilon/2)+asimpson(c,b,right,epsilon/2));
  46. }

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonbuckets is offline Offline
2 posts
since Oct 2007
Oct 25th, 2007
0

Re: Need Help Fast!!!

Looks like it is a precision problem.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 25th, 2007
0

Re: Need Help Fast!!!

This is actually one of the most insidious problems people learning C/C++ get.

The problem is one of type.

1/2 is integer
1/2.0 is floating point

So, for example, when you go to calculate the PDF you say
pdf=((1/2)+INV_SQRT_2PI * ...

That one-half there is an integer expression, which gets promoted to double after it is evaluated. Hence, 1 idiv 2 is 0, promoted to double is 0.0, + INV_SQRT_2PI etc..

All the places where you have something like /2 check to make sure your numerator is a double before division occurs. If it isn't, change your denominator to a double (say, /2.0).

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 25th, 2007
0

Re: Need Help Fast!!!

Click to Expand / Collapse  Quote originally posted by Duoas ...
All the places where you have something like /2 check to make sure your numerator is a double before division occurs. If it isn't, change your denominator to a double (say, /2.0).
Or, more generally, make sure at least one of the values being divided is a floating point value.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 25th, 2007
0

Thanks for the help!

Thanks for the help, that was exactly what was wrong. That was a pain in the ass... I wont forget the way that works anytime soon. Thanks alot everyone! This thread can be removed/deleted/whatever now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonbuckets is offline Offline
2 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I'm back...More problems, different program
Next Thread in C++ Forum Timeline: Please Help Dynamic Memory Allocation.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC