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













0















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?










share|improve this question



















  • 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







  • 2





    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











  • 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















0















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?










share|improve this question



















  • 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







  • 2





    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











  • 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













0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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





    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











  • 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





    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





    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











  • 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












2 Answers
2






active

oldest

votes


















1














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






share|improve this answer























  • 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


















0














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);







share|improve this answer


















  • 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











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
);



);













draft saved

draft discarded


















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









1














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






share|improve this answer























  • 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















1














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






share|improve this answer























  • 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













1












1








1







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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.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

















  • 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
















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













0














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);







share|improve this answer


















  • 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
















0














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);







share|improve this answer


















  • 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














0












0








0







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);







share|improve this answer













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);








share|improve this answer












share|improve this answer



share|improve this answer










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













  • 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


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme