Trapezoidal Integration in CEquation for trapezoidal wave equationgsl openmp failed integrationimplementation of trapezoidal numerical integration in CHow to get block cyclic distribution?How to implement Trapezoidal Integration with Infinite Limits in C?Cleaning up an argv programPrint a text-trapezoid in CIs it possible to pass arguments from argv to a function called with dlsym?make: No targets provided near line 28 (CMake)double integral with trapezoid rule in language C
Creepy dinosaur pc game identification
 
 What happens if you are holding an Iron Flask with a demon inside and walk into an Antimagic Field?
 
 Does malloc reserve more space while allocating memory?
 
 Does Doodling or Improvising on the Piano Have Any Benefits?
 
 How could a planet have erratic days?
 
 Biological Blimps: Propulsion
 
 What should you do when eye contact makes your subordinate uncomfortable?
 
 Hero deduces identity of a killer
 
 Plot of a tornado-shaped surface
 
 Calculating total slots
 
 What exact color does ozone gas have?
 
 Is there a RAID 0 Equivalent for RAM?
 
 How can "mimic phobia" be cured or prevented?
 
 Mimic lecturing on blackboard, facing audience
 
 Why is so much work done on numerical verification of the Riemann Hypothesis?
 
 It grows, but water kills it
 
 Strong empirical falsification of quantum mechanics based on vacuum energy density
 
 Is aluminum electrical wire used on aircraft?
 
 Open a doc from terminal, but not by its name
 
 Is there an injective, monotonically increasing, strictly concave function from the reals, to the reals?
 
 Extract more than nine arguments that occur periodically in a sentence to use in macros in order to typset
 
 Yosemite Fire Rings - What to Expect?
 
 Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
 
 On a tidally locked planet, would time be quantized?
Trapezoidal Integration in C
Equation for trapezoidal wave equationgsl openmp failed integrationimplementation of trapezoidal numerical integration in CHow to get block cyclic distribution?How to implement Trapezoidal Integration with Infinite Limits in C?Cleaning up an argv programPrint a text-trapezoid in CIs it possible to pass arguments from argv to a function called with dlsym?make: No targets provided near line 28 (CMake)double integral with trapezoid rule in language C
I am trying to compute the integral of the function f(x)=(1-x^2)^(1/2) from x=0 to x=1. The answer should be approximately pi/4. I am currently getting 2.
My current implementation of the trapezoidal rule is the following:
double
def_integral(double *f, double *x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( x[i+1] - x[i] ) * ( f[i] + f[i+1] );
 
 return F;
I'm creating N divisions to approximate the area under the curve between x_1=0 and x_N=1 by looping through i to N with x_i = i / N.
int
main(int argc, char **argv)
 int N = 1000;
 double f_x[N];
 double x[N];
 for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
 
 double F_x = def_integral(f_x, x, N);
 printf("The integral is %gn", F_x);
The result of 2 that I am currently getting should be dependent on the number of N division, however, no matter if I make N=10000 or N=100, I still get 2.
Any suggestions?
c
|
show 1 more comment
I am trying to compute the integral of the function f(x)=(1-x^2)^(1/2) from x=0 to x=1. The answer should be approximately pi/4. I am currently getting 2.
My current implementation of the trapezoidal rule is the following:
double
def_integral(double *f, double *x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( x[i+1] - x[i] ) * ( f[i] + f[i+1] );
 
 return F;
I'm creating N divisions to approximate the area under the curve between x_1=0 and x_N=1 by looping through i to N with x_i = i / N.
int
main(int argc, char **argv)
 int N = 1000;
 double f_x[N];
 double x[N];
 for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
 
 double F_x = def_integral(f_x, x, N);
 printf("The integral is %gn", F_x);
The result of 2 that I am currently getting should be dependent on the number of N division, however, no matter if I make N=10000 or N=100, I still get 2.
Any suggestions?
c
 
 
 1
 
 
 
 
 
 That loop in- main()goes out of bounds of- f_x... And you never initialize your- xarray before trying to use it. (Plus you have an array and scalar variable both named- xwhich is confusing). Also out of bounds array accesses in- def_integral()...
 
 – Shawn
 Mar 8 at 2:36
 
 
 
 
 
 
 2
 
 
 
 
 
 You should initialize- double Fto zero.
 
 – user58697
 Mar 8 at 2:37
 
 
 
 
 
 
 
 
 
 
 Compiling with a healthy set of warnings (- -Wall -Wextrafor gcc and clang) will help with some of your problems; also compiling with- -fsanitize=addressor running through valgrind might help with others.
 
 – Shawn
 Mar 8 at 2:41
 
 
 
 
 
 
 
 
 
 
 Yes @user58697! Your answer in combination with @LocTran 's answer solved the issue, I am now getting the correct answer. Thank you so much.
 
 – Chris Wong
 Mar 8 at 3:11
 
 
 
 
 
 2
 
 
 
 
 
 Side note:- (1. - pow(x, 2.)is not as numerically stable nor precise as- (1.0 - x)*(1.0 + x).
 
 – chux
 Mar 8 at 3:14
 
 
 
|
show 1 more comment
I am trying to compute the integral of the function f(x)=(1-x^2)^(1/2) from x=0 to x=1. The answer should be approximately pi/4. I am currently getting 2.
My current implementation of the trapezoidal rule is the following:
double
def_integral(double *f, double *x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( x[i+1] - x[i] ) * ( f[i] + f[i+1] );
 
 return F;
I'm creating N divisions to approximate the area under the curve between x_1=0 and x_N=1 by looping through i to N with x_i = i / N.
int
main(int argc, char **argv)
 int N = 1000;
 double f_x[N];
 double x[N];
 for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
 
 double F_x = def_integral(f_x, x, N);
 printf("The integral is %gn", F_x);
The result of 2 that I am currently getting should be dependent on the number of N division, however, no matter if I make N=10000 or N=100, I still get 2.
Any suggestions?
c
I am trying to compute the integral of the function f(x)=(1-x^2)^(1/2) from x=0 to x=1. The answer should be approximately pi/4. I am currently getting 2.
My current implementation of the trapezoidal rule is the following:
double
def_integral(double *f, double *x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( x[i+1] - x[i] ) * ( f[i] + f[i+1] );
 
 return F;
I'm creating N divisions to approximate the area under the curve between x_1=0 and x_N=1 by looping through i to N with x_i = i / N.
int
main(int argc, char **argv)
 int N = 1000;
 double f_x[N];
 double x[N];
 for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
 
 double F_x = def_integral(f_x, x, N);
 printf("The integral is %gn", F_x);
The result of 2 that I am currently getting should be dependent on the number of N division, however, no matter if I make N=10000 or N=100, I still get 2.
Any suggestions?
c
c
edited Mar 8 at 2:34
Chris Wong
asked Mar 8 at 2:28
Chris WongChris Wong
334
334
 
 
 1
 
 
 
 
 
 That loop in- main()goes out of bounds of- f_x... And you never initialize your- xarray before trying to use it. (Plus you have an array and scalar variable both named- xwhich is confusing). Also out of bounds array accesses in- def_integral()...
 
 – Shawn
 Mar 8 at 2:36
 
 
 
 
 
 
 2
 
 
 
 
 
 You should initialize- double Fto zero.
 
 – user58697
 Mar 8 at 2:37
 
 
 
 
 
 
 
 
 
 
 Compiling with a healthy set of warnings (- -Wall -Wextrafor gcc and clang) will help with some of your problems; also compiling with- -fsanitize=addressor running through valgrind might help with others.
 
 – Shawn
 Mar 8 at 2:41
 
 
 
 
 
 
 
 
 
 
 Yes @user58697! Your answer in combination with @LocTran 's answer solved the issue, I am now getting the correct answer. Thank you so much.
 
 – Chris Wong
 Mar 8 at 3:11
 
 
 
 
 
 2
 
 
 
 
 
 Side note:- (1. - pow(x, 2.)is not as numerically stable nor precise as- (1.0 - x)*(1.0 + x).
 
 – chux
 Mar 8 at 3:14
 
 
 
|
show 1 more comment
 
 
 1
 
 
 
 
 
 That loop in- main()goes out of bounds of- f_x... And you never initialize your- xarray before trying to use it. (Plus you have an array and scalar variable both named- xwhich is confusing). Also out of bounds array accesses in- def_integral()...
 
 – Shawn
 Mar 8 at 2:36
 
 
 
 
 
 
 2
 
 
 
 
 
 You should initialize- double Fto zero.
 
 – user58697
 Mar 8 at 2:37
 
 
 
 
 
 
 
 
 
 
 Compiling with a healthy set of warnings (- -Wall -Wextrafor gcc and clang) will help with some of your problems; also compiling with- -fsanitize=addressor running through valgrind might help with others.
 
 – Shawn
 Mar 8 at 2:41
 
 
 
 
 
 
 
 
 
 
 Yes @user58697! Your answer in combination with @LocTran 's answer solved the issue, I am now getting the correct answer. Thank you so much.
 
 – Chris Wong
 Mar 8 at 3:11
 
 
 
 
 
 2
 
 
 
 
 
 Side note:- (1. - pow(x, 2.)is not as numerically stable nor precise as- (1.0 - x)*(1.0 + x).
 
 – chux
 Mar 8 at 3:14
 
 
 
1
1
That loop in
main() goes out of bounds of f_x... And you never initialize your x array before trying to use it. (Plus you have an array and scalar variable both named x which is confusing). Also out of bounds array accesses in def_integral()...– Shawn
Mar 8 at 2:36
That loop in
main() goes out of bounds of f_x... And you never initialize your x array before trying to use it. (Plus you have an array and scalar variable both named x which is confusing). Also out of bounds array accesses in def_integral()...– Shawn
Mar 8 at 2:36
2
2
You should initialize
double F to zero.– user58697
Mar 8 at 2:37
You should initialize
double F to zero.– user58697
Mar 8 at 2:37
Compiling with a healthy set of warnings (
-Wall -Wextra for gcc and clang) will help with some of your problems; also compiling with -fsanitize=address or running through valgrind might help with others.– Shawn
Mar 8 at 2:41
Compiling with a healthy set of warnings (
-Wall -Wextra for gcc and clang) will help with some of your problems; also compiling with -fsanitize=address or running through valgrind might help with others.– Shawn
Mar 8 at 2:41
Yes @user58697! Your answer in combination with @LocTran 's answer solved the issue, I am now getting the correct answer. Thank you so much.
– Chris Wong
Mar 8 at 3:11
Yes @user58697! Your answer in combination with @LocTran 's answer solved the issue, I am now getting the correct answer. Thank you so much.
– Chris Wong
Mar 8 at 3:11
2
2
Side note:
(1. - pow(x, 2.) is not as numerically stable nor precise as (1.0 - x)*(1.0 + x).– chux
Mar 8 at 3:14
Side note:
(1. - pow(x, 2.) is not as numerically stable nor precise as (1.0 - x)*(1.0 + x).– chux
Mar 8 at 3:14
|
show 1 more comment
 2 Answers
 2
 
active
oldest
votes
In this for loop, you forgot updatin array x as well. 
for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
So, for loop should be replaced by
for (int i = 0 ; i <= N ; i++) 
 double xi = i * 1. / N;
 x[i] = xi;
 f_x[i] = sqrt(1. - pow(xi , 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
 
 
 
 
 
 
 
 Okay this definitely caused some things to change. In this state, I get- The integral is 2.7851which is still not quite right. However, if I simply uncomment the- printffunction which prints the functions values, I get the correct answer of 0.785. What is happening here?
 
 – Chris Wong
 Mar 8 at 2:52
 
 
 
 
 
 
 
 
 
 
 This code prints the good result on my computer whithout uncommenting printf function. But, I think I am a lucky man. Look at the comments of Shawn in the question. If you declare an array :- arr[N], then the indices go from 0 to N-1 and in the for loop you go from 0 to N. If you want N intervals, the array size must be N+1 because the function must be calculated N+1 times. (For example N=4 : F(0)<->F(1)<->F(2)<->F(3) )
 
 – Stef1611
 Mar 8 at 8:34
 
 
 
add a comment |
In your main code, you call def_integral with a double (x) and in the function an array of x (double * x) is expected. Perhaps (it is what I suppose), the problem comes from the fact you formula needs x(i+1)-x(i) but you use a constant step. Indeed, x(i+1)-x(i)=step_x is constant so you do not need each x(i) but only value : 1./N
Other remark, with a constant step, your formula could be simplified to : 
F_x=step_x* ( 0.5*f_x(x0)+ f_x(x1)+...+f_x(xn-1)+ 0.5*f_x(xn) ) . It helps to simplify the code and to write a better efficient one.
Everything is commented in the code above. I hope it could help you. 
Best regards.
#include <stdio.h>
#include <math.h>
double
def_integral(double *f, double step_x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( step_x ) * ( f[i] + f[i+1] );
 
 return F;
int main()
 int N = 1001; // 1001 abscissas means 1000 intervalls (see comment on array size and indices)
 double f_x[N]; // not needed for the simplified algorithm
 double step_x = 1. / N; // x(i+1)-x(i) is constant
 for (int i = 0 ; i < N ; i++) // Note : i<N and not i<=N
 double xi = i * step_x; // abscissa calculation
 f_x[i] = sqrt((1. - xi )*(1. + xi )); // cf chux comment
 
 double F_x = def_integral(f_x, step_x, N);
 printf("The integral is %.10gn", F_x);
// simplified algorithm
// F_x=step_x*( 0.5*f_x(x0)+f_x(x1)+...+f_x(xn-1)+0.5f_x(xn) )
 double xi;
 xi=0; // x(0)
 F_x=0.5*sqrt((1. - xi )*(1. + xi ));
 for (int i=1 ; i<=N-1 ; i++) 
 xi=step_x*i;
 F_x+=sqrt((1. - xi )*(1. + xi ));
 
 xi=step_x*N;
 F_x+=0.5*sqrt((1. - xi )*(1. + xi ));
 F_x=step_x*F_x;
 printf("The integral is %.10gn", F_x);
 
 
 1
 
 
 
 
 
 May I suggest a more organized (IMHO) approach ?
 
 – Bob__
 Mar 8 at 15:32
 
 
 
 
 
 
 
 
 
 
 
 @Bob. I agree it is much more organized.
 
 – Stef1611
 Mar 8 at 16:17
 
 
 
 
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55055857%2ftrapezoidal-integration-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
 2 Answers
 2
 
active
oldest
votes
 2 Answers
 2
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
In this for loop, you forgot updatin array x as well. 
for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
So, for loop should be replaced by
for (int i = 0 ; i <= N ; i++) 
 double xi = i * 1. / N;
 x[i] = xi;
 f_x[i] = sqrt(1. - pow(xi , 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
 
 
 
 
 
 
 
 Okay this definitely caused some things to change. In this state, I get- The integral is 2.7851which is still not quite right. However, if I simply uncomment the- printffunction which prints the functions values, I get the correct answer of 0.785. What is happening here?
 
 – Chris Wong
 Mar 8 at 2:52
 
 
 
 
 
 
 
 
 
 
 This code prints the good result on my computer whithout uncommenting printf function. But, I think I am a lucky man. Look at the comments of Shawn in the question. If you declare an array :- arr[N], then the indices go from 0 to N-1 and in the for loop you go from 0 to N. If you want N intervals, the array size must be N+1 because the function must be calculated N+1 times. (For example N=4 : F(0)<->F(1)<->F(2)<->F(3) )
 
 – Stef1611
 Mar 8 at 8:34
 
 
 
add a comment |
In this for loop, you forgot updatin array x as well. 
for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
So, for loop should be replaced by
for (int i = 0 ; i <= N ; i++) 
 double xi = i * 1. / N;
 x[i] = xi;
 f_x[i] = sqrt(1. - pow(xi , 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
 
 
 
 
 
 
 
 Okay this definitely caused some things to change. In this state, I get- The integral is 2.7851which is still not quite right. However, if I simply uncomment the- printffunction which prints the functions values, I get the correct answer of 0.785. What is happening here?
 
 – Chris Wong
 Mar 8 at 2:52
 
 
 
 
 
 
 
 
 
 
 This code prints the good result on my computer whithout uncommenting printf function. But, I think I am a lucky man. Look at the comments of Shawn in the question. If you declare an array :- arr[N], then the indices go from 0 to N-1 and in the for loop you go from 0 to N. If you want N intervals, the array size must be N+1 because the function must be calculated N+1 times. (For example N=4 : F(0)<->F(1)<->F(2)<->F(3) )
 
 – Stef1611
 Mar 8 at 8:34
 
 
 
add a comment |
In this for loop, you forgot updatin array x as well. 
for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
So, for loop should be replaced by
for (int i = 0 ; i <= N ; i++) 
 double xi = i * 1. / N;
 x[i] = xi;
 f_x[i] = sqrt(1. - pow(xi , 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
In this for loop, you forgot updatin array x as well. 
for (int i = 0 ; i <= N ; i++) 
 double x = i * 1. / N;
 f_x[i] = sqrt(1. - pow(x, 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
So, for loop should be replaced by
for (int i = 0 ; i <= N ; i++) 
 double xi = i * 1. / N;
 x[i] = xi;
 f_x[i] = sqrt(1. - pow(xi , 2.));
 //printf("%.2f %.5fn", x, f_x[i]); //uncomment if you wanna see function values
answered Mar 8 at 2:40
Loc TranLoc Tran
1,177714
1,177714
 
 
 
 
 
 
 
 Okay this definitely caused some things to change. In this state, I get- The integral is 2.7851which is still not quite right. However, if I simply uncomment the- printffunction which prints the functions values, I get the correct answer of 0.785. What is happening here?
 
 – Chris Wong
 Mar 8 at 2:52
 
 
 
 
 
 
 
 
 
 
 This code prints the good result on my computer whithout uncommenting printf function. But, I think I am a lucky man. Look at the comments of Shawn in the question. If you declare an array :- arr[N], then the indices go from 0 to N-1 and in the for loop you go from 0 to N. If you want N intervals, the array size must be N+1 because the function must be calculated N+1 times. (For example N=4 : F(0)<->F(1)<->F(2)<->F(3) )
 
 – Stef1611
 Mar 8 at 8:34
 
 
 
add a comment |
 
 
 
 
 
 
 
 Okay this definitely caused some things to change. In this state, I get- The integral is 2.7851which is still not quite right. However, if I simply uncomment the- printffunction which prints the functions values, I get the correct answer of 0.785. What is happening here?
 
 – Chris Wong
 Mar 8 at 2:52
 
 
 
 
 
 
 
 
 
 
 This code prints the good result on my computer whithout uncommenting printf function. But, I think I am a lucky man. Look at the comments of Shawn in the question. If you declare an array :- arr[N], then the indices go from 0 to N-1 and in the for loop you go from 0 to N. If you want N intervals, the array size must be N+1 because the function must be calculated N+1 times. (For example N=4 : F(0)<->F(1)<->F(2)<->F(3) )
 
 – Stef1611
 Mar 8 at 8:34
 
 
 
Okay this definitely caused some things to change. In this state, I get
The integral is 2.7851 which is still not quite right. However, if I simply uncomment the printf function which prints the functions values, I get the correct answer of 0.785. What is happening here?– Chris Wong
Mar 8 at 2:52
Okay this definitely caused some things to change. In this state, I get
The integral is 2.7851 which is still not quite right. However, if I simply uncomment the printf function which prints the functions values, I get the correct answer of 0.785. What is happening here?– Chris Wong
Mar 8 at 2:52
This code prints the good result on my computer whithout uncommenting printf function. But, I think I am a lucky man. Look at the comments of Shawn in the question. If you declare an array :
arr[N], then the indices go from 0 to N-1 and in the for loop you go from 0 to N. If you want N intervals, the array size must be N+1 because the function must be calculated N+1 times. (For example N=4 : F(0)<->F(1)<->F(2)<->F(3) )– Stef1611
Mar 8 at 8:34
This code prints the good result on my computer whithout uncommenting printf function. But, I think I am a lucky man. Look at the comments of Shawn in the question. If you declare an array :
arr[N], then the indices go from 0 to N-1 and in the for loop you go from 0 to N. If you want N intervals, the array size must be N+1 because the function must be calculated N+1 times. (For example N=4 : F(0)<->F(1)<->F(2)<->F(3) )– Stef1611
Mar 8 at 8:34
add a comment |
In your main code, you call def_integral with a double (x) and in the function an array of x (double * x) is expected. Perhaps (it is what I suppose), the problem comes from the fact you formula needs x(i+1)-x(i) but you use a constant step. Indeed, x(i+1)-x(i)=step_x is constant so you do not need each x(i) but only value : 1./N
Other remark, with a constant step, your formula could be simplified to : 
F_x=step_x* ( 0.5*f_x(x0)+ f_x(x1)+...+f_x(xn-1)+ 0.5*f_x(xn) ) . It helps to simplify the code and to write a better efficient one.
Everything is commented in the code above. I hope it could help you. 
Best regards.
#include <stdio.h>
#include <math.h>
double
def_integral(double *f, double step_x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( step_x ) * ( f[i] + f[i+1] );
 
 return F;
int main()
 int N = 1001; // 1001 abscissas means 1000 intervalls (see comment on array size and indices)
 double f_x[N]; // not needed for the simplified algorithm
 double step_x = 1. / N; // x(i+1)-x(i) is constant
 for (int i = 0 ; i < N ; i++) // Note : i<N and not i<=N
 double xi = i * step_x; // abscissa calculation
 f_x[i] = sqrt((1. - xi )*(1. + xi )); // cf chux comment
 
 double F_x = def_integral(f_x, step_x, N);
 printf("The integral is %.10gn", F_x);
// simplified algorithm
// F_x=step_x*( 0.5*f_x(x0)+f_x(x1)+...+f_x(xn-1)+0.5f_x(xn) )
 double xi;
 xi=0; // x(0)
 F_x=0.5*sqrt((1. - xi )*(1. + xi ));
 for (int i=1 ; i<=N-1 ; i++) 
 xi=step_x*i;
 F_x+=sqrt((1. - xi )*(1. + xi ));
 
 xi=step_x*N;
 F_x+=0.5*sqrt((1. - xi )*(1. + xi ));
 F_x=step_x*F_x;
 printf("The integral is %.10gn", F_x);
 
 
 1
 
 
 
 
 
 May I suggest a more organized (IMHO) approach ?
 
 – Bob__
 Mar 8 at 15:32
 
 
 
 
 
 
 
 
 
 
 
 @Bob. I agree it is much more organized.
 
 – Stef1611
 Mar 8 at 16:17
 
 
 
 
add a comment |
In your main code, you call def_integral with a double (x) and in the function an array of x (double * x) is expected. Perhaps (it is what I suppose), the problem comes from the fact you formula needs x(i+1)-x(i) but you use a constant step. Indeed, x(i+1)-x(i)=step_x is constant so you do not need each x(i) but only value : 1./N
Other remark, with a constant step, your formula could be simplified to : 
F_x=step_x* ( 0.5*f_x(x0)+ f_x(x1)+...+f_x(xn-1)+ 0.5*f_x(xn) ) . It helps to simplify the code and to write a better efficient one.
Everything is commented in the code above. I hope it could help you. 
Best regards.
#include <stdio.h>
#include <math.h>
double
def_integral(double *f, double step_x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( step_x ) * ( f[i] + f[i+1] );
 
 return F;
int main()
 int N = 1001; // 1001 abscissas means 1000 intervalls (see comment on array size and indices)
 double f_x[N]; // not needed for the simplified algorithm
 double step_x = 1. / N; // x(i+1)-x(i) is constant
 for (int i = 0 ; i < N ; i++) // Note : i<N and not i<=N
 double xi = i * step_x; // abscissa calculation
 f_x[i] = sqrt((1. - xi )*(1. + xi )); // cf chux comment
 
 double F_x = def_integral(f_x, step_x, N);
 printf("The integral is %.10gn", F_x);
// simplified algorithm
// F_x=step_x*( 0.5*f_x(x0)+f_x(x1)+...+f_x(xn-1)+0.5f_x(xn) )
 double xi;
 xi=0; // x(0)
 F_x=0.5*sqrt((1. - xi )*(1. + xi ));
 for (int i=1 ; i<=N-1 ; i++) 
 xi=step_x*i;
 F_x+=sqrt((1. - xi )*(1. + xi ));
 
 xi=step_x*N;
 F_x+=0.5*sqrt((1. - xi )*(1. + xi ));
 F_x=step_x*F_x;
 printf("The integral is %.10gn", F_x);
 
 
 1
 
 
 
 
 
 May I suggest a more organized (IMHO) approach ?
 
 – Bob__
 Mar 8 at 15:32
 
 
 
 
 
 
 
 
 
 
 
 @Bob. I agree it is much more organized.
 
 – Stef1611
 Mar 8 at 16:17
 
 
 
 
add a comment |
In your main code, you call def_integral with a double (x) and in the function an array of x (double * x) is expected. Perhaps (it is what I suppose), the problem comes from the fact you formula needs x(i+1)-x(i) but you use a constant step. Indeed, x(i+1)-x(i)=step_x is constant so you do not need each x(i) but only value : 1./N
Other remark, with a constant step, your formula could be simplified to : 
F_x=step_x* ( 0.5*f_x(x0)+ f_x(x1)+...+f_x(xn-1)+ 0.5*f_x(xn) ) . It helps to simplify the code and to write a better efficient one.
Everything is commented in the code above. I hope it could help you. 
Best regards.
#include <stdio.h>
#include <math.h>
double
def_integral(double *f, double step_x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( step_x ) * ( f[i] + f[i+1] );
 
 return F;
int main()
 int N = 1001; // 1001 abscissas means 1000 intervalls (see comment on array size and indices)
 double f_x[N]; // not needed for the simplified algorithm
 double step_x = 1. / N; // x(i+1)-x(i) is constant
 for (int i = 0 ; i < N ; i++) // Note : i<N and not i<=N
 double xi = i * step_x; // abscissa calculation
 f_x[i] = sqrt((1. - xi )*(1. + xi )); // cf chux comment
 
 double F_x = def_integral(f_x, step_x, N);
 printf("The integral is %.10gn", F_x);
// simplified algorithm
// F_x=step_x*( 0.5*f_x(x0)+f_x(x1)+...+f_x(xn-1)+0.5f_x(xn) )
 double xi;
 xi=0; // x(0)
 F_x=0.5*sqrt((1. - xi )*(1. + xi ));
 for (int i=1 ; i<=N-1 ; i++) 
 xi=step_x*i;
 F_x+=sqrt((1. - xi )*(1. + xi ));
 
 xi=step_x*N;
 F_x+=0.5*sqrt((1. - xi )*(1. + xi ));
 F_x=step_x*F_x;
 printf("The integral is %.10gn", F_x);
In your main code, you call def_integral with a double (x) and in the function an array of x (double * x) is expected. Perhaps (it is what I suppose), the problem comes from the fact you formula needs x(i+1)-x(i) but you use a constant step. Indeed, x(i+1)-x(i)=step_x is constant so you do not need each x(i) but only value : 1./N
Other remark, with a constant step, your formula could be simplified to : 
F_x=step_x* ( 0.5*f_x(x0)+ f_x(x1)+...+f_x(xn-1)+ 0.5*f_x(xn) ) . It helps to simplify the code and to write a better efficient one.
Everything is commented in the code above. I hope it could help you. 
Best regards.
#include <stdio.h>
#include <math.h>
double
def_integral(double *f, double step_x, int n)
 double F;
 for (int i = 0 ; i < n ; i++) 
 F += 0.5 * ( step_x ) * ( f[i] + f[i+1] );
 
 return F;
int main()
 int N = 1001; // 1001 abscissas means 1000 intervalls (see comment on array size and indices)
 double f_x[N]; // not needed for the simplified algorithm
 double step_x = 1. / N; // x(i+1)-x(i) is constant
 for (int i = 0 ; i < N ; i++) // Note : i<N and not i<=N
 double xi = i * step_x; // abscissa calculation
 f_x[i] = sqrt((1. - xi )*(1. + xi )); // cf chux comment
 
 double F_x = def_integral(f_x, step_x, N);
 printf("The integral is %.10gn", F_x);
// simplified algorithm
// F_x=step_x*( 0.5*f_x(x0)+f_x(x1)+...+f_x(xn-1)+0.5f_x(xn) )
 double xi;
 xi=0; // x(0)
 F_x=0.5*sqrt((1. - xi )*(1. + xi ));
 for (int i=1 ; i<=N-1 ; i++) 
 xi=step_x*i;
 F_x+=sqrt((1. - xi )*(1. + xi ));
 
 xi=step_x*N;
 F_x+=0.5*sqrt((1. - xi )*(1. + xi ));
 F_x=step_x*F_x;
 printf("The integral is %.10gn", F_x);
answered Mar 8 at 10:01


Stef1611Stef1611
300212
300212
 
 
 1
 
 
 
 
 
 May I suggest a more organized (IMHO) approach ?
 
 – Bob__
 Mar 8 at 15:32
 
 
 
 
 
 
 
 
 
 
 
 @Bob. I agree it is much more organized.
 
 – Stef1611
 Mar 8 at 16:17
 
 
 
 
add a comment |
 
 
 1
 
 
 
 
 
 May I suggest a more organized (IMHO) approach ?
 
 – Bob__
 Mar 8 at 15:32
 
 
 
 
 
 
 
 
 
 
 
 @Bob. I agree it is much more organized.
 
 – Stef1611
 Mar 8 at 16:17
 
 
 
 
1
1
May I suggest a more organized (IMHO) approach ?
– Bob__
Mar 8 at 15:32
May I suggest a more organized (IMHO) approach ?
– Bob__
Mar 8 at 15:32
@Bob. I agree it is much more organized.
– Stef1611
Mar 8 at 16:17
@Bob. I agree it is much more organized.
– Stef1611
Mar 8 at 16:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55055857%2ftrapezoidal-integration-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
 
1
That loop in
main()goes out of bounds off_x... And you never initialize yourxarray before trying to use it. (Plus you have an array and scalar variable both namedxwhich is confusing). Also out of bounds array accesses indef_integral()...– Shawn
Mar 8 at 2:36
2
You should initialize
double Fto zero.– user58697
Mar 8 at 2:37
Compiling with a healthy set of warnings (
-Wall -Wextrafor gcc and clang) will help with some of your problems; also compiling with-fsanitize=addressor running through valgrind might help with others.– Shawn
Mar 8 at 2:41
Yes @user58697! Your answer in combination with @LocTran 's answer solved the issue, I am now getting the correct answer. Thank you so much.
– Chris Wong
Mar 8 at 3:11
2
Side note:
(1. - pow(x, 2.)is not as numerically stable nor precise as(1.0 - x)*(1.0 + x).– chux
Mar 8 at 3:14