Custom fitting using equation from differential function
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sabella Huang
le 1 Juin 2022
Réponse apportée : Sabella Huang
le 3 Juin 2022
Hello guys,
I have some data that need to be fit by the equation from differential equation solver. I want to obtain parameter from syms A and B as an answer. Here is my code that I try to solve.
syms t y(t) A B
eqn_ass = diff(y,t) == A-B*y;
cond_ass = y(0) == 0;
R_ass = dsolve(eqn_ass,cond_ass); % I want to use equation from here as my fitting equation to obtain parameter A and B
0 commentaires
Réponse acceptée
Bjorn Gustavsson
le 1 Juin 2022
Modifié(e) : Bjorn Gustavsson
le 1 Juin 2022
Use matlabFunction to make a function out of the symbolic solution, then fit that one numerically to your data:
R_a_fcn = matlabFunction(R_ass);
err_fcn = @(par,t,y,fcn) sum((y - fcn(par(1),par(2),t)).^2); % error-function
res_fcn = @(par,t,y,fcn) (y - fcn(par(1),par(2),t));
% mock up test-example
t = 0:31;
y = 12*(1-exp(-t/11));
% Fit with fminsearch:
PAR = fminsearch(@(par) err_fcn(par,t,y,R_a_fcn),[1 1]);
% Fit with lsqnonlin:
PAR2 = lsqnonlin(@(par) res_fcn(par,t,y,R_a_fcn),[1 1]);
% Fit with lsqcurvefit:
PAR3 = lsqcurvefit(@(par,t) R_a_fcn(par(1),par(2),t),[1 1],t,y);
% Compare:
plot(t,y)
hold on
plot(t,R_a_fcn(PAR(1),PAR(2),t),'--')
plot(t,R_a_fcn(PAR2(1),PAR2(2),t),'-.')
plot(t,R_a_fcn(PAR3(1),PAR3(2),t),':')
HTH
0 commentaires
Plus de réponses (2)
Alan Stevens
le 1 Juin 2022
You equation has the analytical solution y = (A/B)*(1-exp(-B*t)). Use fminsearch to fit the parameters, or the curve fitting toolbox if you have it.
0 commentaires
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!