# Defining complex input types to make a complex mex function

2 views (last 30 days)
Mohammad Shojaei Arani on 4 Jul 2022
Hello friends,
Recently I became familliar with mex function. I am quite new to this and have lots of hopes to speed up my slow codes. However, I ran into a problem and really need your help (this is not easy for me to fix). I have a function
Cost_Spline3(par,J,Coeff,N,X0,X,dt,EZ,H,H_coeff,mesh,mesh_fine,dmesh_fine,M,SplineType)
but I have difficulty to 'define input types' for 4 inputs: H,EZ,Coeff,H_coeff. H is a matlab function which accepts the vector x (1*N) and as output gives a vector (actually a matrix)
H =
function_handle with value:
@(x)[-x.*(x.^2-3.0);x.^2.*(x.^2-6.0)+3.0]
but, note that depending on J the number of outputs of H are (J-2). EZ is a rather complex function handle. For instance,
EZ = @(Dm0,Dm1,Dm2,Dm3,Ds0,Ds1,Ds2,Ds3)[(Ds0.*(Ds0.*(Ds0.*(10.*5.^(1./2).*Dm2-5.*5.^(1./2).*Ds0.*Ds3)-5.^(1./2).*(4.*Ds1+20.*Dm0.*Ds2+20.*Dm1.*Ds1))+5.^(1./2).*(8.*Dm0+20.*Dm0.*Ds1.^2+20.*Dm0.*Dm1))-20.*5.^(1./2).*Dm0.^2.*Ds1)./(8.*Ds0.^2);(Ds0.*(Ds0.*(20.*Dm1+5.*Ds1.^2-10.*Ds0.*Ds2+4)-40.*Dm0.*Ds1)+20.*Dm0.^2)./(4.*Ds0.^2);(6.*5.^(1./2).*Dm0-3.*5.^(1./2).*Ds0.*Ds1)./(2.*Ds0);0.*Ds0+3]
so, the number of inputs of EZ are dynamic. Based on J and K, EZ can have up to 4K-1 inputs and exactly J outputs. Coeff is a cell array of matlab functions. For instance,
Coeff =
1×2 cell array
{@(w)[0.*w+1,-3.*w,3.*w.^2,-w.^3]} {@(w)[0.*w+1,-4.*w,6.*w.^2,-4.*w.^3,w.^4]}
where w would be column vectors later with different lengths. H_coeff is a J*1 cell array with variable array lengths.
Babak