How to fit two equations to two data set simultaneously and with multiple fitting parameters?
Afficher commentaires plus anciens
Hi everyone,
I want to fit a set of two specific equations (f1 and f2, in the following) with respect to a set of multiple parameters (g1, g2, t1 and t2, in the following). The dataset is given by vectors x, G1 and G2.
Actually, my code works when I consider only g1 and g2 in the formulation for f1 and f2:
x = [100 63.1 39.8 25.1 15.8 10 6.31 3.98 2.51 1.58 1 0.631 0.398 0.251 0.158 0.1];
G1 = [18 15.397 15.014 11.07 7.6413 4.8958 3.1734 2.1804 1.5285 1.0407 0.73853 0.5509 0.39496 0.24756 0.17789 0.11671];
G2 = [914.23 599.24 388.97 251.14 161.92 103.29 66.23 42.402 27.141 17.48 11.303 7.3379 4.8356 3.1736 2.1078 1.4052];
syms g1 t1 g2 t2
f1=((g1.*(t1.*x).^2)./(1+(t1.*x).^2))+0*((g2.*(t2.*x).^2)./(1+(t2.*x).^2));
f2=((g1.*(t1.*x).^1)./(1+(t1.*x).^2))+0*((g2.*(t2.*x).^1)./(1+(t2.*x).^2));
residue1 = sum((f1 - G1).^2);
residue2 = sum((f2 - G2).^2);
residue = residue1 + residue2;
g1_partial = solve(diff(residue,g1),g1)
eqn = subs(residue,g1,g1_partial);
t1_partial = vpasolve(diff(eqn,t1),t1)
t1_partial(abs(imag(t1_partial)) > 1e-30) = [];
t1_partial(((t1_partial)) < 0) = [];
t1_partial = real(t1_partial);
a_full = double(subs(g1_partial, t1_partial))
b_full = double(t1_partial)
G1_pred=subs(f1,g1,a_full);
G1_pred=double(subs(G1_pred,t1,b_full));
G2_pred=subs(f2,g1,a_full);
G2_pred=double(subs(G2_pred,t1,b_full));
figure
hold on
plot(x,G1,'bo-');
plot(x,G1_pred,'ro-');
legend('G1 exp','G1 fit');
hold off
figure
hold on
plot(x,G2,'bo-');
plot(x,G2_pred,'ro-');
legend('G2 exp','G2 fit');
hold off
As You can see, in f1 and f2 I have not taken into account g2 and t2, and the correspondence between fit and expected data is quite low especially on G1.
It is expected that by considering also g2 and t2 the fit would be better.
How can I generalize my code to consider also these two additional parameters?
Thanks in advance,
Alessio
3 commentaires
Mathieu NOE
le 25 Jan 2024
sorry to ask you this way, but is there any mistake in the f1 and f2 formulas ?
Star Strider
le 25 Jan 2024
In my version of the code, they both appear to fit their respective data well, using the common parameters.
Alex Sha
le 25 Jan 2024
@Alessio Pricci, there seems to be something wrong with the fitting functions:
f1=((g1.*(t1.*x).^2)./(1+(t1.*x).^2))+0*((g2.*(t2.*x).^2)./(1+(t2.*x).^2));
f2=((g1.*(t1.*x).^1)./(1+(t1.*x).^2))+0*((g2.*(t2.*x).^1)./(1+(t2.*x).^2));
Note the part "0*((g2.*(t2.*x)...", since this part will always be zero due to multiply by 0, so the parameters of "g2" and "t2" will have no effect and can be any values.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

