Nonlinear Regression with nlinfit

1 view (last 30 days)
mcrane
mcrane on 9 Jan 2012
I'm trying to perform a linear regression on data, but keep getting the error
"MODELFUN should return a vector of fitted values the same length as Y."
Any help would be greatly appreciated.
Thanks.
function JVLinReg(filename)
Data = importdata(filename);
J_Data = Data(:, 1);
V_Data = Data(:, 2);
length(J_Data)
length(V_Data)
params = [1, 1, 1, 1];
nlinfit(J_Data, V_Data, @myJVFun, params);
end
function output = myJVFun(params, J_Value)
q = 1.60217646*10^-19;
k = 1.3806503*10^-23;
T = 300;
A = 1;
Jph = params(1);
Jo = params(2);
Rs = params(3);
Rsh = params(4);
syms V;
f = Jph - Jo*(exp(q*(V + J_Value*A*Rs)/(k*T)) - 1) - (V + J_Value*A*Rs)/Rsh - J_Value;
output = solve(f, 0);
end

Answers (1)

Walter Roberson
Walter Roberson on 9 Jan 2012
solve(f,0) is asking to solve for f=0 and for 0=0 . That is going to be a problem.
The solution for f involves the LambertW function in form such that, depending on the values of the parameter matrix ("params" in your code, "b" in the documentation), might encounter one of the branch cuts. Possibly, though, you only care about the principle branch; if so then that is probably what solve() would find.
I would not assume that the vector of outputs from solve() is going to be the same row vs column as J_Value would be. You would be safer with a trailing line
output = reshape(output, size(J_Value));

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!