MATLAB: Error using lsqcurvefit (line 262) Function value and YDATA sizes are not equal.

lsqcurvefit

I am using the following code:
%Import data as time and voltage:
load VoltageData.mat;
load('OCP_Cathode.mat'); %Load the OCP data for the cathode
load('OCP_aNODE.mat'); %Load OCP data for the anode;
SOC_a=OCP_Anode(:,1);
SOC_c=OCP_Cathode(:,1);
OCV_a=flip(OCP_Anode(:,2));
OCV_c=OCP_Cathode(:,2);
time=VoltageData(:,1);
voltage=VoltageData(:,2);
%Import the applied current data
%Set it to the correct value:
I_app=0.12*ones(1,10);
Time=time/time(end);
%Get the data to be used:
t_data=linspace(0,Time(end),10);
L=length(Time);
V_data=interp1(Time,voltage,t_data);
%Choose initial conditions for the parameters:
X_0=zeros(10,1);
X(1)=0.9;
X(2)=0.2;
X(3)=1;
X(4)=1.5;
X(5:end)=10^-6;
X_0=X;
lb=zeros(1,10);
ub=ones(1,10);
%Set up the function for lsqcurvefit:
fun = @(X,t)terminal_voltage(V_data,I_app,mu_n,t_data,X,SOC_a,SOC_c,OCV_c,OCV_a);
x = lsqcurvefit(fun,X_0',t_data,V_data,lb,ub);
The function terminal_voltage is given by:
function V=terminal_voltage(V_exp,I_app,mu_n,t,X,SOC_a,SOC_c,OCV_c,OCV_a)
gamma_plus=X(5);
gamma_minus=X(6);
nu_plus=X(9);
nu_minus=X(10);
SOC_cathode=cathode_c(I_app,mu_n,1,t,X);
SOC_anode=anode_c(I_app,mu_n,1,t,X);
O_plus=interp1(1-SOC_c,OCV_c,SOC_cathode);
O_minus=interp1(SOC_a,OCV_a,SOC_anode);
V=eta_plus(SOC_cathode,I_app,X)-eta_minus(SOC_anode,I_app,X)+O_plus-O_minus-V_exp';
I get the error:
Error using lsqcurvefit (line 262)
Function value and YDATA sizes are not equal.
Error in inverse_SPM (line 49)
x = lsqcurvefit(fun,X_0',t_data,V_data,lb,ub);
I'm not too sure what's going on. How do I check the size of the function value? How do I correct it, so lsqcurvefit works?

Best Answer

Evaluate fun(X_0') adn make sure that its output has the same size as V_data.
Related Question