Message Boards Message Boards

0
|
5752 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Fit a recursive function?

Posted 8 years ago

Hi there, I wanted to use Mathematica to fit some experimental data (Current I, voltage V) to a recursive function which looks like this:

enter image description here

The function is a two-diode equation describing a solar cell. The current also appears in the rhs of the equation turning it into a unpleasant recursive function. I haven't found anything obvious in Mathematica how to deal with this, but then again, I am still a superficial user. Fit parameters would be I01, I02, Rs, Rp. n1 and n2 can be assumed constants.

POSTED BY: s.rupp
5 Replies

Actually you need a constraint for every data point. I can illustrate the approach with data that can be fitted using Fit. First generate the data and fit it to a straight line.

In[3]:= data = Table[{x, 2*x + 3 + RandomReal[{-.25, .25}]}, {x, 5}]

Out[3]= {{1, 4.86828}, {2, 7.17623}, {3, 9.03998}, {4, 11.2155}, {5, 
  13.1595}}

In[4]:= Fit[data, {1, x}, x]

Out[4]= 2.90539 + 2.06216 x

Now do the fit by minimizing the sum of the squares of the error terms.

In[5]:= {xvals, yvals} = Transpose[data]

Out[5]= {{1, 2, 3, 4, 5}, {4.86828, 7.17623, 9.03998, 11.2155, 
  13.1595}}

In[8]:= errterms = yvals - m*xvals - b

Out[8]= {4.86828 - b - m, 7.17623 - b - 2 m, 9.03998 - b - 3 m, 
 11.2155 - b - 4 m, 13.1595 - b - 5 m}

In[9]:= FindMinimum[errterms.errterms, {{m, 2}, {b, 3}}]

Out[9]= {0.0410029, {m -> 2.06216, b -> 2.90539}}

Now do it by defining constraints from the fitting equation. It is not implicit but the method would work if it were.

In[10]:= errtermsc = Table[yfit[i] - yvals[[i]], {i, Length[data]}]

Out[10]= {-4.86828 + yfit[1], -7.17623 + yfit[2], -9.03998 + 
  yfit[3], -11.2155 + yfit[4], -13.1595 + yfit[5]}

In[11]:= cons = Table[yfit[i] == m*xvals[[i]] + b, {i, Length[data]}]

Out[11]= {yfit[1] == b + m, yfit[2] == b + 2 m, yfit[3] == b + 3 m, 
 yfit[4] == b + 4 m, yfit[5] == b + 5 m}

In[12]:= vars = 
 Join[{{m, 2}, {b, 3}}, 
  Table[{yfit[i], yvals[[i]]}, {i, Length[data]}]]

Out[12]= {{m, 2}, {b, 3}, {yfit[1], 4.86828}, {yfit[2], 
  7.17623}, {yfit[3], 9.03998}, {yfit[4], 11.2155}, {yfit[5], 13.1595}}

In[13]:= FindMinimum[{errtermsc.errtermsc, cons}, vars]

Out[13]= {0.0410029, {m -> 2.06216, b -> 2.90539, yfit[1] -> 4.96756, 
  yfit[2] -> 7.02972, yfit[3] -> 9.09189, yfit[4] -> 11.1541, 
  yfit[5] -> 13.2162}}
POSTED BY: Frank Kampas
Posted 8 years ago

Thank you Frank, sorry for the late reply, I have been away. I will digest this and try it out. Ta.

POSTED BY: s.rupp

I suggest using FindMinimum or NMinimize, minimizing the sum of the squares of the error terms and using your equation as a constraint.

POSTED BY: Frank Kampas
Posted 8 years ago

Thank you Prof. Binder, I will give that a shot.

POSTED BY: s.rupp

The equation does not have a closed-form solution, given the mixture of linear and exponential terms. It can be solved numerically using FindRoot[equation==0,{I,I_guess}] by putting in values for V and all the parameters. You might also try a ParametricPlot for I(V) for a specified set of parameters.

Another possibility. You could expand the exponentials to give a linear approximation to the equation.

POSTED BY: S M Blinder
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract