How to fit two equations to two data set simultaneously and with multiple fitting parameters?
이전 댓글 표시
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
Mathieu NOE
2024년 1월 25일
sorry to ask you this way, but is there any mistake in the f1 and f2 formulas ?
Star Strider
2024년 1월 25일
In my version of the code, they both appear to fit their respective data well, using the common parameters.
Alex Sha
2024년 1월 25일
@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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

