Need Help Fast!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 2
Reputation: jonbuckets is an unknown quantity at this point 
Solved Threads: 0
jonbuckets jonbuckets is offline Offline
Newbie Poster

Need Help Fast!!!

 
0
  #1
Oct 25th, 2007
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

  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,855
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Need Help Fast!!!

 
0
  #2
Oct 25th, 2007
Looks like it is a precision problem.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need Help Fast!!!

 
0
  #3
Oct 25th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Need Help Fast!!!

 
0
  #4
Oct 25th, 2007
Originally Posted by Duoas View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: jonbuckets is an unknown quantity at this point 
Solved Threads: 0
jonbuckets jonbuckets is offline Offline
Newbie Poster

Thanks for the help!

 
0
  #5
Oct 25th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC