function [error, x_rep] = error_param_func(u,nn,p,C,CP,iopen)
global Constraints 

Invariant_u = precal_generic(u); 
Matrix = zeros(nn);
b1 = zeros(nn,1);
Nshape = Invariant_u.Nshape;
spv = Invariant_u.spv;
for i=1:length(u)
    k=1:(p+1);
    kk=(spv(i)+k-1);
    Matrix(kk,kk)= Matrix(kk,kk) + Nshape(i,:)'*Nshape(i,:);
    b1(kk) = b1(kk) + C(i)*Nshape(i,:)';
end



if iopen ==3 || iopen==6 || iopen ==7
    L1 = zeros(size(Matrix,1),2);
    L1((length(L1)),1) = 1;
    L1((length(L1)-1),2) = 1;
    L1((length(L1)),2) = -1;
    b1 = [b1 ; CP(end); 0];
    Matrix_=[Matrix L1; L1' zeros(2)];
    x_rep=Matrix_\b1;
    x_rep(end-1:end)=[];
else
    x_rep=Matrix\b1;
end




[C_,C1, C2_] = interpolate_func_2(Invariant_u,x_rep);

error = sum((C(:)-C_(:)).^2);
