Thread: simpson's 1/3rd rule

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    simpson's 1/3rd rule

    hello everyone, I am trying to write a program in simpson's 1/3rd rule.The program is not complete but am stuck with some problem.The problem is that the values I printout are somewhat absurd.The code is
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
    	double l1,l2,h,*x,*y;
    	int l,i;
    	printf("*********************PROGRAM TO IMPLEMENT SIMPSON'S RULE************************");
    	printf("\nGIVE THE RANGE OF THE VALUE OF INTEGRATION FOR THE INTEGRAND \n\n\t\t\ty=(log (sin x))^2 \n\nlower limit = ");
    	scanf("%lf",&l1);
    	printf("upper limit = ");
    	scanf("%lf",&l2);
    	printf("\nENTER IN HOW MANY PARTS THE INTERVAL SHOULD BE DIVIDED(ATLEAST GREATER THAN 4)????");
    	printf("\nno. of intervals = ");
    	scanf("%d",&l);
    	while(l<4)
    	{
    		printf("\nERROR!!! : no. of intervals should be atleast greater than 4");
    		printf("\nno. of intervals = ");
    		scanf("%d",&l);
    	}
    	h=(l2-l1)/l;
    	x=(double *)malloc((l+1) * sizeof(double));
    	y=(double *)malloc((l+1) * sizeof(double));
    	for(i=0;i<(l+1);i++)
    	{
    		*(x+i)=l1+(i*h);
    		*(y+i)=pow(log10 (sin ((180.0/3.141593) * (*(x+i)))),2.0);
    		printf("\n%lg",*(y+i));
    	}
    	printf("\nTHE CORRESPONDING TABLE FOR THE DATAS ARE:\n");
    	printf("x:              ");
    	for(i=0;i<(l+1);i++)
    	{
    		printf(" %-7lf  ",*(x+i));
    	}
    	printf("\ny=(log(sin x))^2");
    	for(i=0;i<(l+1);i++)
    	{
    		printf(" %-7lf  ",*(y+i));
    	}
    	printf("\n\nTHE LENGTH OF EACH INTERVAL, h = %lg",h);
    	return 0;
    }
    When i give the input as 4 and 5.2 for l1 & l2 respectively and no. of parts i.e.,l as 6 the outputs for y are absurd.
    Can you find the fault in my code......THANKS

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    printf("\n%lg",*(y+i));
    What's 'lg'?

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    hi claudiu, this line

    printf("\n%lg",*(y+i));

    need not be written.I wrote it to verify the value.
    lg is the conversion character for data output like we use %lf for long double.
    by using %g we can undisplay the trailing zeros after decimal point

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by claudiu
    printf("\n%lg",*(y+i));
    What's 'lg'?
    A format specifier for double.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by rakeshkool27 View Post
    hi claudiu, this line

    printf("\n%lg",*(y+i));

    need not be written.I wrote it to verify the value.
    lg is the conversion character for data output like we use %lf for long double.
    by using %g we can undisplay the trailing zeros after decimal point

    Ah, I see. Can't say I've ever used it myself.

    So can you tell us what your output is for a specific input?

  6. #6
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    yeah,as i told when i give inputs like

    l1=4 l2=5.2 l=6

    then values of *y are absurd type like -1.#IND00

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Why do you use *(x + i) all the time? It's a lot more readable to use x[i]...

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    f(x) = sin x(part of the original function),
    if you are expecting results, with x in degrees, then this line may be the offending line

    Code:
    *(y+i)=pow(log10 (sin ((180.0/3.141593) * (*(x+i)))),2.0);
    1 radian = 180 / pi degrees .
    So you are converting the values to degrees, but the sin function expects the values in radians.
    So , it should have been either x[i] or x[i] * pi / 180.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by EVOEx View Post
    Why do you use *(x + i) all the time? It's a lot more readable to use x[i]...
    yeah dereferencing using * all the time is confusing.......quite true!!!!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You do realize that log is only defined for positive inputs? I'm guessing your function is supposed to be log((sin x)^2).

  11. #11
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by zalezog View Post
    f(x) = sin x(part of the original function),
    if you are expecting results, with x in degrees, then this line may be the offending line

    Code:
    *(y+i)=pow(log10 (sin ((180.0/3.141593) * (*(x+i)))),2.0);
    1 radian = 180 / pi degrees .
    So you are converting the values to degrees, but the sin function expects the values in radians.
    So , it should have been either x[i] or x[i] * pi / 180.
    yeah, this is the part i am confused about.look you can see that the number i am providing is a pure integer which is not in radian or degrees.Now the formula

    x = pi/180 * x

    is to be used for converting degrees to radian not for integer numbers......So how to resolve this......it seems the question might be wrong

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rakeshkool27 View Post
    yeah, this is the part i am confused about.look you can see that the number i am providing is a pure integer which is not in radian or degrees.
    Integer is wrong, since your values are/can be floating point. Radians are "unitless", so you're already there without any converting.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by tabstop View Post
    Integer is wrong, since your values are/can be floating point. Radians are "unitless", so you're already there without any converting.
    without converting the output is wrong

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rakeshkool27 View Post
    without converting the output is wrong
    Define "wrong". (After all, have you already fixed the log(negative) issue, as well as being sure that you're supposed to use log10 instead of log?)

  15. #15
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by tabstop View Post
    Define "wrong". (After all, have you already fixed the log(negative) issue, as well as being sure that you're supposed to use log10 instead of log?)
    isn't log10 means logarithm to base 10?
    without converting the value of sin shall come negative and negative values aren't expected in log

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Misra Rule 11.2
    By hammer1234 in forum C Programming
    Replies: 1
    Last Post: 04-06-2006, 07:28 AM
  2. SImpson's Rule
    By Brent in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 09:34 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Who should rule the world?
    By CoderBob in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 07:01 AM