emergency9177
New member
- Joined
- Dec 16, 2021
- Messages
- 19
Plase can someone help me with this computational problem:
Write a program that will numerically integrate a given function at a given interval using the trapezoidal rule method. Try to write the algorithm in such a way that Integral achieves the desired accuracy or that you evaluate the accuracy of the integration (eg. perform two calculations with different steps). Solve the problem in C programming language. Submit the text file with the program containing the solution.
f(x) = exp(-x2/10)•sin(3x) / 5x
Calculate integral of f(x) in interval between 0 and +∞.
I'v try to solve the problem:
1 #include<conio.h>
2 #include<stdio.h>
3 void main()
4 {
5 float a,b,h,x,y,s=0;
6 float x0,y0,xn,yn,r;
7 int i,n;
8 float f(float);
9 printf("\nEnter lower limit value:: ");
10 scanf("%f",&a);
11 printf("\nEnter upper limit value:: ");
12 scanf("%f",&b);
13 printf("\nEnter the number of intervals:: ");
14 scanf("%d",&n);
15 h=(b-a)/n;
16 x0=a;
17 y0=f(a);
18 yn=f(b);
19 x=x0+h;
20 for(i=1;i<=n-1;i++)
21 {
22 y=f(x);
23 s=s+y;
24 x=x+h;
25 }
26 r=(h/2)*(y0+yn+2*s);
27 printf("\nResult is:: %f",r);
28 getch();
29 }
30 float f(float x)
31 {
32 return exp(-x*x/10.0)*sin(3.0*x) / (5.0*x);
33 }
After running the program I receive this:
main.c:32:9: warning: implicit declaration of function ‘exp’ [-Wimplicit-function-declaration]
32 | return exp(-x*x/10.0)*sin(3.0*x) / (5.0*x);
| ^~~
main.c:32:9: warning: incompatible implicit declaration of built-in function ‘exp’
main.c:3:1: note: include ‘<math.h>’ or provide a declaration of ‘exp’
2 | #include<stdio.h>
+++ |+#include <math.h>
3 | void main()
main.c:32:24: warning: implicit declaration of function ‘sin’ [-Wimplicit-function-declaration]
32 | return exp(-x*x/10.0)*sin(3.0*x) / (5.0*x);
| ^~~
main.c:32:24: warning: incompatible implicit declaration of built-in function ‘sin’
main.c:32:24: note: include ‘<math.h>’ or provide a declaration of ‘sin’
Enter lower limit value:: 0
Enter upper limit value:: +∞
Enter the number of intervals::
Result is:: -nan
...Program finished with exit code 0
Press ENTER to exit console.
_____________________________________________________________________________________________________________________________________________________________________
I've already asked my prof. if the expression f(x) = exp(-x2/10)•sin(3x) / 5x is correct, he told me yes, and I also aksed him if I have to use the Gauss-Laguerre quadrature since I have to Calculate integral of f(x) in interval between 0 and +∞.
I'm stuck, please some help, any hint?
Thank you very much.
Write a program that will numerically integrate a given function at a given interval using the trapezoidal rule method. Try to write the algorithm in such a way that Integral achieves the desired accuracy or that you evaluate the accuracy of the integration (eg. perform two calculations with different steps). Solve the problem in C programming language. Submit the text file with the program containing the solution.
f(x) = exp(-x2/10)•sin(3x) / 5x
Calculate integral of f(x) in interval between 0 and +∞.
I'v try to solve the problem:
1 #include<conio.h>
2 #include<stdio.h>
3 void main()
4 {
5 float a,b,h,x,y,s=0;
6 float x0,y0,xn,yn,r;
7 int i,n;
8 float f(float);
9 printf("\nEnter lower limit value:: ");
10 scanf("%f",&a);
11 printf("\nEnter upper limit value:: ");
12 scanf("%f",&b);
13 printf("\nEnter the number of intervals:: ");
14 scanf("%d",&n);
15 h=(b-a)/n;
16 x0=a;
17 y0=f(a);
18 yn=f(b);
19 x=x0+h;
20 for(i=1;i<=n-1;i++)
21 {
22 y=f(x);
23 s=s+y;
24 x=x+h;
25 }
26 r=(h/2)*(y0+yn+2*s);
27 printf("\nResult is:: %f",r);
28 getch();
29 }
30 float f(float x)
31 {
32 return exp(-x*x/10.0)*sin(3.0*x) / (5.0*x);
33 }
After running the program I receive this:
main.c:32:9: warning: implicit declaration of function ‘exp’ [-Wimplicit-function-declaration]
32 | return exp(-x*x/10.0)*sin(3.0*x) / (5.0*x);
| ^~~
main.c:32:9: warning: incompatible implicit declaration of built-in function ‘exp’
main.c:3:1: note: include ‘<math.h>’ or provide a declaration of ‘exp’
2 | #include<stdio.h>
+++ |+#include <math.h>
3 | void main()
main.c:32:24: warning: implicit declaration of function ‘sin’ [-Wimplicit-function-declaration]
32 | return exp(-x*x/10.0)*sin(3.0*x) / (5.0*x);
| ^~~
main.c:32:24: warning: incompatible implicit declaration of built-in function ‘sin’
main.c:32:24: note: include ‘<math.h>’ or provide a declaration of ‘sin’
Enter lower limit value:: 0
Enter upper limit value:: +∞
Enter the number of intervals::
Result is:: -nan
...Program finished with exit code 0
Press ENTER to exit console.
_____________________________________________________________________________________________________________________________________________________________________
I've already asked my prof. if the expression f(x) = exp(-x2/10)•sin(3x) / 5x is correct, he told me yes, and I also aksed him if I have to use the Gauss-Laguerre quadrature since I have to Calculate integral of f(x) in interval between 0 and +∞.
He answered me, that we defined the value of a function in lectures when x = 0. We looked at the function limit when x goes to 0. And also that we have to program this as follows: when calculating the function we check if x is equal to 0. In this case we return the value of the limit, otherwise we calculate the value.
I'm stuck, please some help, any hint?
Thank you very much.