How can i fit the data to the custom equation without using Curve fitting Toolbox ?

Asked by KUNAL PATIL

KUNAL PATIL (view profile)

on 6 Sep 2019
Latest activity Edited by Matt J

Matt J (view profile)

on 6 Sep 2019
Accepted Answer by Matt J

Matt J (view profile)

I have a data for voltage(V) with respect to time(t). I want to fit the curve with equation without using the curve fiting tool box,
V(t)=A+B*(1-exp(-t/C))+D*(1-exp(-t/E)),
where the parameters to be estimated are A, B, C, D, E
please go through the attached document for V-t data...

Rik

Rik (view profile)

on 6 Sep 2019
Your question is not urgent. That being said, you can define a cost function and use fminsearch to find the values of the parameters. Note that fminsearch is sensitive to initial estimate errors.

R2018b

Answer by Matt J

on 6 Sep 2019
Edited by Matt J

Matt J (view profile)

on 6 Sep 2019

FMINSPLEAS will work well for this problem. It uses fminsearch, but in a savvy way that reduces the problem to only 2 unknowns. It can be downloaded from here,
For you, its application would look like,
flist={1,@(x,t) 1-exp(-t/x(1)) , @(x,t) 1-exp(-t/sum(x)) };
[x,y]=fminspleas(flist,[C0,C0+E0],tdata,Vdata,[0,0]); %C0 and E0 are initial guesses of C and E
A=y(1);
B=y(2);
C=x(1);
D=y(3);
E=x(2)-C;

Answer by Walter Roberson

Walter Roberson (view profile)

on 6 Sep 2019

You can fit any equation by calculating
SSE = @(Current_Parameters) sum( (Predicted_Value(Input_Independent_Variable, Current_Parameters) - Actual_Values).^2 );
and minimizing SSE .
Here, Predicted_Value would be your model function that takes your input independent variable and the current guesses at model parameters, and predicts the outputs for those parameters at the values of the independent variable. The current parameters will be input as a vector, so like
@(t,ABCDE) ABCDE(1) + ABCDE(2) .* (1-exp(-t./ABCDE(3))) + ABCDE(4) .* (1-exp(-t./ABCDE(5)))

Rik (view profile)

on 6 Sep 2019

Sligthly exanpding on what Walter suggests:
fun=@(ABCDE,t) ABCDE(1) + ABCDE(2) .* (1-exp(-t./ABCDE(3))) + ABCDE(4) .* (1-exp(-t./ABCDE(5)));
initial_guess=[1 1 1 1 1];%enter appropriate initial estimate
ABCDE=fit_fminsearch(f,x,y,initial_guess)
I attached my fit_fminsearch function. I don't feel it is quite ready for the FEX, but it will probably end up there is due time. This function doesn't require any toolbox and should work on all releases of both Matlab and GNU Octave. It uses fminsearch, so it is still sensitive to initial estimates being close to a local minimum or being too far off.