I am trying to program the pieceewise quadratic lagrange, but I have to deal with these intervals at which the point I am trying to evaluate belongs to. So, essentially you need to have an if statement before we apply the formula, here is a picture of it.

I am not sure how to write the if statement, if anyone can help me I would greatly appreciate it, here is the code I have thus far:

/*Your experiments should include the following functions on [−1, 1]:
* f(x) = |ax|
* f(x) = |ax| + x/2 - x^2
* f(x) = 1/(1+ ax^2)
*/

#include <iostream>
#include <math.h>

using namespace std;

// Piecewise linear Lagrange --- Lagrange Form
float LagrangeQuad(float x[], float x_eval, float f[], int n);

int main() {
int n = 100;

// 100 x points
float x[n+1];
// Step size for interval [-1,1]
float h = (1.0 - (-1.0))/100.0;
for(int i = 0; i <= n; i++){
x[i] = -1 + i*h;
}

//Function values single
float alpha = 1.0;
float f_0[n+1], f_1[n+1], f_2[n+1];
for(int i = 0; i <= n; i++){
f_0[i] = fabs(alpha*x[i]);
f_1[i] = fabs(alpha*x[i]) + x[i]/2 - pow(x[i],2);
f_2[i] = 1/(1 + alpha*pow(x[i],2));
}

// Create new set of x values that we will use to compare the interpolation
float x_test[n+1];
float h_test = (1.0 - (-1.0))/1000.0;
for(int i = 0; i <= n; i++){
x_test[i] = -1 + i*h_test;
}

// Compute the Lagrange polynomial single
float f0_eval[n+1];
float f1_eval[n+1];
float f2_eval[n+1];
for(int i = 0; i <= n; i++){
cout << f0_eval[i] << endl;
}

return 0;
}

float LagrangeQuad(float x[], float x_eval, float f[], int n){
float fun;
float fun1;
float fun2;
float xdiff;
float xdiff1;
float xdiff2;
float xdiff3;
float xdiff4;
float xdiff5;
float result;

for(int i = 0; i < n; i++){
if(x_eval >= x[i] && x_eval <= x[i+1] ){
fun = f[i];
fun1 = f[i+1];
fun2 = f[i+2];
xdiff = (x_eval - x[i+1])*(x_eval - x[i+2]);
xdiff1 = (x[i] - x[i+1])*(x[i] - x[i+2]);
xdiff2 = (x_eval - x[i])*(x_eval - x[i+2]);
xdiff3 = (x[i+1] - x[i])*(x[i+1] - x[i+2]);
xdiff4 = (x[i+2] - x[i])*(x[i+2] - x[i+1]);
}
}
result = fun*(xdiff/xdiff1) + fun1*(xdiff2/xdiff3) + fun2*(xdiff4/xdiff5);

return result;
}


Ok in your Langrange function, should it be:

for(int i = 0; i < (n-1)/2; i++){
if(x_eval >= x[2*i] && x_eval <= x[2*i+2] ){
fun = f[2*i];
fun1 = f[2*i+1];
fun2 = f[2*i+2];
xdiff = (x_eval - x[2*i+1])*(x_eval - x[2*i+2]);
xdiff1 = (x[2*i] - x[2*i+1])*(x[2*i] - x[2*i+2]);
xdiff2 = (x_eval - x[2*i])*(x_eval - x[2*i+2]);
xdiff3 = (x[2*i+1] - x[2*i])*(x[2*i+1] - x[2*i+2]);
xdiff4 = (x_eval - x[2*i])*(x_eval - x[2*i+1]);
xdiff5 = (x[2*i+2] - x[2*i])*(x[2*i+2] - x[2*i+1]);
}
}


Also I think you want an odd number of points, like [x0, x1, x2, x3, x4] that way there is an even number of intervals (in this case 2). In the question it says $n$ must be even but I think this might be referring to the number of intervals and not the number of points? I could be wrong though.

Edit I think I see now what you did. You make n=100 (even) but have the arrays be length n+1 (odd) which is fine. Just make sure you call your langrange function like:

LagrangeQuad(x,x_test[i],f_0,n+1);   //make sure its n+1, not n
//your arrays are of length n+1 remember!

• I have already created the points at which I am going to evaluate the interpolation but in terms of specifying where they are in the interval $x_i$ is the problem I will attach the code to the problem then maybe you can see what is going on better – Wolfy Oct 9 '15 at 18:20
• No problem. I am happy it works for you! – James Oct 9 '15 at 19:00