# forum.alglib.net

ALGLIB forum
 It is currently Tue Nov 21, 2017 11:58 am

 All times are UTC

### Forum rules

1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: spline1dintegrate functionPosted: Wed Jan 25, 2017 2:22 am

Joined: Tue Jan 17, 2017 11:16 pm
Posts: 10
Dear Support,

I have an array of X nodes = [0, 1/12, 3/12, 1/2, 1, 2, 5, 10,15, 20] and a spline built successfully.

I try to use the spline1dintegrate(spline,X) function to integrate the function. I have produced the results for the integration. However, I am uncertain that if the spline1dintegrate(spline,X) function integrates from the starting node in the array to next node or from each pair of the consecutive nodes?
e.g. [0, 1/12], [0, 3/12], [0,1/2]..... or [0,1/12], [1/12,3/12] or [3/12, 1/2]

Please help ! Many thanks.
Van
[#include <iostream>

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <interpolation.h>
#include "interpolation.cpp"
#include "ap.cpp"
#include "alglibinternal.cpp"
#include "alglibmisc.cpp"
#include "linalg.cpp"
#include "solvers.cpp"
#include "optimization.cpp"
#include "specialfunctions.cpp"
#include "integration.cpp"

#include <vector>

using namespace std;

double B(double a,double T);
double B(double a,double T)
{
return (1/a*(1-exp(-a*T)));
}
double A(double a, double b, double var, double T);
double A(double a, double b, double var, double T)
{
return -(var/(4*a))*pow(B(a,T),2)+(b-(var/(2*pow(a,2))))*(B(a,T)-T);
}
double p(double T, double a, double b, double var, double r0);
double p(double T, double a, double b, double var, double r0)
{
return exp(A(a,b,var,T)-r0*B(a,T));
}

double D1G(int n, double a, double b, double c, double T);
double D1G(int n, double a, double b, double c, double T)
{

if(n == 0){
return (1/a)*((1/a*(1-exp(-a*T)))-T);
}
else {
return (1/a)*(pow(T,n)*B(a,T)+n*D1G(n-1,a,b,c,T)-pow(T,n+1)/(n+1));
}
}

double D2G(int n, double a, double b, double c, double T);
double D2G(int n, double a, double b, double c, double T)
{
if(n == 0){
return -(1/(2*a))*pow(B(a,T),2)- (1/pow(a,2))*(B(a,T)-T);

}
else {
//-(1/(2*a))*(T^n*B(T,a,b,sigma2)^2-n*D2G(n-1,a,b,sigma2))-(1/a^2)*(T^n*B(T,a,b,sigma2)+n*D1G(n-1,a,b,sigma2)-T^(n+1)/(n+1));
return -(1/(2*a))*(pow(T,n)*pow(B(a,T),2)-n*D2G(n-1, a, b, c, T))- (1/pow(a,2))*(pow(T,n)*B(a,T)+n*D1G(n-1, a, b, c, T)-pow(T,n+1)/(n+1));
}
}

int main()
{
double a= 3.5, b = 0.03, var = 0.3, r0 = 0.03, q2;
//int i;
//float X[9], Y[9];
std::vector<double> X(9), Y(9);
X[0]=0;
X[1]=1.0/12;
X[2]=0.25;
X[3]=0.5;
X[4]=1;
X[5]=2;
X[6]=5;
X[7]=10;
X[8]=15;
X[9]=20;

/*for (i=0;i<10;i++){
cout << "x:" << X[i]<< ", f(x):";
Y[i] = X[i]*log(p(X[i],a,b,var,r0));
cout << Y[i] << endl;
}*/
cout << "\n"<< endl;
for (int n = 0; n<3; n++){
Y[0] = pow(X[0],n)*log(p(X[0],a,b,var,r0));
Y[1] = pow(X[1],n)*log(p(X[1],a,b,var,r0));
Y[2] = pow(X[2],n)*log(p(X[2],a,b,var,r0));
Y[3] = pow(X[3],n)*log(p(X[3],a,b,var,r0));
Y[4] = pow(X[4],n)*log(p(X[4],a,b,var,r0));
Y[5] = pow(X[5],n)*log(p(X[5],a,b,var,r0));
Y[6] = pow(X[6],n)*log(p(X[6],a,b,var,r0));
Y[7] = pow(X[7],n)*log(p(X[7],a,b,var,r0));
Y[8] = pow(X[8],n)*log(p(X[8],a,b,var,r0));
Y[9] = pow(X[9],n)*log(p(X[9],a,b,var,r0));
}

alglib::real_1d_array AX, AY; // alglib building vector X and Y
AX.setcontent(X.size(), &(X[0]));
AY.setcontent(Y.size(), &(Y[0]));

alglib::spline1dinterpolant spline; //alglib defining a spline
alglib::spline1dbuildlinear(AX, AY,spline); //alglib building the linear spline
for (int i = 0; i <10;i++){
q2 = spline1dintegrate(spline,X[i]);
cout <<q2 << endl;
}
//cout <<q2 << endl; //alglib integrating at value X = 20
//int k =2;
//double T = 20.0;
//(T^(n+1)/(n+1))*A(T,a,b,sigma2)-(sigma2/(2*(n+1)))*D2G(n+1,a,b,sigma2) ...
// -(a*b/(n+1))*D1G(n+1,a,b,sigma2)+r0*D1G(n,a,b,sigma2)-q(n);

//cout <<(pow(T,k+1)/(k+1))*A(a,b,var,T)-(var/(2*(k+1)))*D2G(k+1,a,b, var,T)-(a*b/(k+1))*D1G(k+1,a,b, var,T)+r0*D1G(k,a,b, var,T)<< endl;
return EXIT_SUCCESS;
}]

Top

 Post subject: Re: spline1dintegrate functionPosted: Fri Jan 27, 2017 3:21 pm
 Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 788
Hi! It calculates integral with fixed lower bound equal to min(x[]). In your case it calculates integral(S(x)dx,0,t) for t=1/12, 3/12 and so on.

Top

 Post subject: Re: spline1dintegrate functionPosted: Mon Jan 30, 2017 3:29 am

Joined: Tue Jan 17, 2017 11:16 pm
Posts: 10
Dear Sergey,

Thank you so much.

Cheers,
Van

Top

 Post subject: Re: spline1dintegrate functionPosted: Mon Jan 30, 2017 3:37 am

Joined: Tue Jan 17, 2017 11:16 pm
Posts: 10
Dear Sergey,

I also have another query in regards to the spline1dbuildlinear(AX, AY,spline) function.

As I understand that the function currently only accepts array of physical values, if I have X nodes = [0, 1/12, 3/12, 1/2, 1, 2, 5, 10,15, 20] and Y[i] = X[i]*log(p(X[i],a,b,var,r0)), how do I store the values of Y[i] as array to apply to the spline1dbuildlinear(AX, AY,spline) function ?

Your help is much appreciated. Many thanks.

Cheers,
Van

Top

 Post subject: Re: spline1dintegrate functionPosted: Mon Jan 30, 2017 10:11 am
 Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 788
See separate topic on this question :)

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: Bing [Bot] and 4 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ALGLIB forum    ALGLIB-discuss
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group