[R] How to get correct integration in C for step function?
Lynette
fuyaonv at hotmail.com
Mon Jan 22 20:03:20 CET 2007
Dear all, especially to Thomas,
I have figured out the problem. For the step function, something wrong with
my C codes. I should use the expression ((x>=0.25)&&(x<=0.75)) ? 2:1 instead
of ((x>=1/4)&&(x<=3/4)) ? 2:1 ). Have no idea why 0.25 makes difference from
1/4 in C. But now I can go ahead with the correct integration in C. Thank
you all. And hope this helps to others.
Best wishes,
Lynette
----- Original Message -----
From: "Lynette" <fuyaonv at hotmail.com>
To: <r-help at stat.math.ethz.ch>
Cc: "AJ Rossini" <blindglobe at gmail.com>; <ess-help at stat.math.ethz.ch>
Sent: Sunday, January 21, 2007 6:24 PM
Subject: [R] How to get correct integration in C for step function?
> Dear all,
>
> I am using Rdqags in C to realize the integration. It seems for the
> continous C function I can get correct results. However, for step
> functions,
> the results are not correct. For example, the following one, when
> integrated
> from 0 to 1 gives 1 instead of the correct 1.5
>
> void func( double *x, int n, void *ex )
> {
> int i;
>
> for(i=0;i<n;i++) { x[i]=( ((x>=1/4)&&(x<=3/4)) ? 2:1 ) ; }
> return;
> }
>
> while the following one when integrated from 0 to 1 gives the correct
> 0.7853983
>
> void func( double *x, int n, void *ex )
> {
> int i;
>
> for(i=0;i<n;i++) { x[i]= pow(1-x[i]*x[i],.5); }
> return;
> }
>
> Please advise the problems. Thanks a lot.
>
> Best,
> Lynette
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list