# simpson's 1/3rd rule

This is a discussion on simpson's 1/3rd rule within the C Programming forums, part of the General Programming Boards category; hello everyone, I am trying to write a program in simpson's 1/3rd rule.The program is not complete but am stuck ...

1. ## 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. printf("\n%lg",*(y+i));
What's 'lg'?

3. 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. Originally Posted by claudiu
printf("\n%lg",*(y+i));
What's 'lg'?
A format specifier for double.

5. Originally Posted by rakeshkool27
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. 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. Why do you use *(x + i) all the time? It's a lot more readable to use x[i]...

8. 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. Originally Posted by EVOEx
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. 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. Originally Posted by zalezog
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. Originally Posted by rakeshkool27
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. Originally Posted by tabstop
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. Originally Posted by rakeshkool27
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. Originally Posted by tabstop
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

Page 1 of 2 12 Last
Popular pages Recent additions