My lsqcurve fit is not working due YDATA and function value sizes are not being equal. (but they are?)
    1 view (last 30 days)
  
       Show older comments
    
    Victor Carneiro da Cunha Martorelli
 on 21 Dec 2023
  
    
    
    
    
    Commented: Alex Sha
      
 on 7 Jan 2024
            %I double checked the sizes and they are all set to 1,116. So I do not understand the problem. 
This is my code:
Zreal = [101.8	104.7	105.7	106.8	106.7	108.5	107.7	108.9	108.1	108.6	109	108.8	109.1	107.8	109.5	109	109.9	109.9	108.4	110.7	110	112.4	110.7	112.1	112.9	114.1	115	116.2	118.8	121.3	125.8	127.3	130.8	122.8	138.3	143	146.9	151.9	158.3	162.5	169.1	177.9	189.7	205.3	228.2	259.1	304.5	366.1	453.5	568.1	730.6	933.1	1140	1368	1604	1746	1943	2126
];
Zim = [25.5800000000000	21.4300000000000	16.5700000000000	13.7500000000000	11.1300000000000	8.84500000000000	5.54700000000000	4.27600000000000	5.55100000000000	3.92100000000000	2.55200000000000	2.68300000000000	3.26400000000000	3.53800000000000	3.27400000000000	4.54300000000000	4.71500000000000	3.58100000000000	3.97600000000000	5.26300000000000	7.75600000000000	8.47000000000000	7.79900000000000	9.77800000000000	11.7600000000000	13.8800000000000	15.4500000000000	18.3700000000000	22.8200000000000	26.4400000000000	29.6200000000000	35.5500000000000	40.2700000000000	45.5900000000000	54.4900000000000	62.5100000000000	73.5100000000000	86.7100000000000	102.300000000000	125.200000000000	150.300000000000	181.400000000000	220.900000000000	270.200000000000	332	401.300000000000	486.300000000000	581.500000000000	689.500000000000	805.500000000000	915.900000000000	1008	1017	1010	999.300000000000	850.900000000000	818.700000000000	738.700000000000];
freq = [100100	79450	63140	50200	39890	31640	25170	20020	15890	12610	10080	8016	6328	5016	3984	3171	2528	1976	1578	1266	998.300000000000	796.900000000000	627.800000000000	505.500000000000	398	315.500000000000	252.400000000000	198.600000000000	158.400000000000	125.600000000000	100.400000000000	79	63.3400000000000	49.8700000000000	39.7200000000000	31.6700000000000	24.9300000000000	19.8600000000000	15.8400000000000	12.4000000000000	9.93100000000000	7.94500000000000	6.31700000000000	5.00800000000000	3.94600000000000	3.15900000000000	2.50400000000000	1.99800000000000	1.58500000000000	1.26700000000000	0.999000000000000	0.792300000000000	0.633400000000000	0.504000000000000	0.400600000000000	0.316700000000000	0.252000000000000	0.200300000000000];
parms = [2339	6.44000000000000e-05	68.0400000000000	0.00292000000000000	3.01500000000000	100.800000000000	0.000998600000000000	4.81200000000000	90.8500000000000	108.700000000000	0.000137600000000000	0.924800000000000];
concatenated_Z = [Zreal, Zim]';
size1 = size(my_fit_functioncpe(freq,parms))
size2 = size(concatenated_Z)
x = lsqcurvefit(@my_fit_functioncpe,[100 0],[freq,parms],concatenated_Z);
function  concatz = my_fit_functioncpe(freq,parameters)
    Rsolc = parameters(1);
    Cm = parameters(2);
    Rq = parameters(3);
    Rn = parameters(4);
    Ln = parameters(5);
    RKch = parameters(6);
    Rm = parameters(7);
    Lm = parameters(8);
    Rl = parameters(9);
    Rctc = parameters(10);
    T = parameters(11);
    qc = parameters(12);
    w = 2 * pi * freq;
    Z = Rctc + 1./((1./Rsolc+T*(1j*w).^qc)) + 1./((1j*w*Cm)+ 1/Rl + 1/Rq + 1/RKch  + 1./(Rn + (1j*w*Ln)) + 1./(Rm + (1j*w*Lm)));%4c
    reZ = real(Z);
    imZ = imag(Z);
    concatz = cat(2,reZ,imZ)';
end
1 Comment
  Torsten
      
      
 on 21 Dec 2023
				
      Edited: Torsten
      
      
 on 21 Dec 2023
  
			Why do you still work with all these parameters to be fitted ?
It's like trying to fit a function of the form 
y = (a+b)*x + c + d^2
You cannot get senseful results for a, b, c and d.
Say you get a = 2, b = 3, c = 4 and d = -1.
Is this better than
a = 3, b = 2, c = 1 and d = -2 ?
You see the problem ?
You can only fit two parameters, namely ab = a + b and cd = c + d^2:
y = ab*x + cd
Overfitting like above makes the Jacobian of the problem become singular.
Accepted Answer
  Star Strider
      
      
 on 21 Dec 2023
        There were several problems.  First, the objective function arguments were reversed, and the data and calculations returned by it were definitely not the same sizes.  This required a bit of tweaking, however it now works.  
Try this — 
Zreal = [101.8	104.7	105.7	106.8	106.7	108.5	107.7	108.9	108.1	108.6	109	108.8	109.1	107.8	109.5	109	109.9	109.9	108.4	110.7	110	112.4	110.7	112.1	112.9	114.1	115	116.2	118.8	121.3	125.8	127.3	130.8	122.8	138.3	143	146.9	151.9	158.3	162.5	169.1	177.9	189.7	205.3	228.2	259.1	304.5	366.1	453.5	568.1	730.6	933.1	1140	1368	1604	1746	1943	2126];
Zim = [25.5800000000000	21.4300000000000	16.5700000000000	13.7500000000000	11.1300000000000	8.84500000000000	5.54700000000000	4.27600000000000	5.55100000000000	3.92100000000000	2.55200000000000	2.68300000000000	3.26400000000000	3.53800000000000	3.27400000000000	4.54300000000000	4.71500000000000	3.58100000000000	3.97600000000000	5.26300000000000	7.75600000000000	8.47000000000000	7.79900000000000	9.77800000000000	11.7600000000000	13.8800000000000	15.4500000000000	18.3700000000000	22.8200000000000	26.4400000000000	29.6200000000000	35.5500000000000	40.2700000000000	45.5900000000000	54.4900000000000	62.5100000000000	73.5100000000000	86.7100000000000	102.300000000000	125.200000000000	150.300000000000	181.400000000000	220.900000000000	270.200000000000	332	401.300000000000	486.300000000000	581.500000000000	689.500000000000	805.500000000000	915.900000000000	1008	1017	1010	999.300000000000	850.900000000000	818.700000000000	738.700000000000];
freq = [100100	79450	63140	50200	39890	31640	25170	20020	15890	12610	10080	8016	6328	5016	3984	3171	2528	1976	1578	1266	998.300000000000	796.900000000000	627.800000000000	505.500000000000	398	315.500000000000	252.400000000000	198.600000000000	158.400000000000	125.600000000000	100.400000000000	79	63.3400000000000	49.8700000000000	39.7200000000000	31.6700000000000	24.9300000000000	19.8600000000000	15.8400000000000	12.4000000000000	9.93100000000000	7.94500000000000	6.31700000000000	5.00800000000000	3.94600000000000	3.15900000000000	2.50400000000000	1.99800000000000	1.58500000000000	1.26700000000000	0.999000000000000	0.792300000000000	0.633400000000000	0.504000000000000	0.400600000000000	0.316700000000000	0.252000000000000	0.200300000000000];
parms = [2339	6.44000000000000e-05	68.0400000000000	0.00292000000000000	3.01500000000000	100.800000000000	0.000998600000000000	4.81200000000000	90.8500000000000	108.700000000000	0.000137600000000000	0.924800000000000];
concatenated_Z = [Zreal; Zim]';
size1 = size(my_fit_functioncpe(parms,freq))
size2 = size(concatenated_Z)
x = lsqcurvefit(@my_fit_functioncpe,parms,freq,concatenated_Z)
fittedZ = my_fit_functioncpe(x,freq);
figure
semilogx(freq, concatenated_Z, 'DisplayName','Z Data')
hold on
plot(freq, fittedZ, 'DisplayName','Fitted Z')
hold off
grid
xlabel('Frequency')
ylabel('Z')
legend('Location','best')
function  concatz = my_fit_functioncpe(parameters,freq)
    Rsolc = parameters(1);
    Cm = parameters(2);
    Rq = parameters(3);
    Rn = parameters(4);
    Ln = parameters(5);
    RKch = parameters(6);
    Rm = parameters(7);
    Lm = parameters(8);
    Rl = parameters(9);
    Rctc = parameters(10);
    T = parameters(11);
    qc = parameters(12);
    w = 2 * pi * freq;
    Z = Rctc + 1./((1./Rsolc+T*(1j*w).^qc)) + 1./((1j*w*Cm)+ 1/Rl + 1/Rq + 1/RKch  + 1./(Rn + (1j*w*Ln)) + 1./(Rm + (1j*w*Lm)));%4c
    reZ = real(Z);
    imZ = imag(Z);
    concatz = [reZ; imZ].';
    % concatz = cat(2,reZ,imZ)';
end
.
7 Comments
  Alex Sha
      
 on 7 Jan 2024
				The result obtained below seems to be not bad:
Sum Squared Error (SSE): 7003.74699273406
Root of Mean Square Error (RMSE): 7.77027214759608
Correlation Coef. (R): 0.999816870896314
R-Square: 0.999633775328896
Parameter	Best Estimate       
---------	-------------       
rsolc    	30.774676380514     
cm       	-4.99365889506396E-5
rq       	-569988.599981876   
rn       	-20379.2697022919   
ln      	8935.93817911551    
rkch     	-10793.0313410194   
rm       	-185.85114389438    
lm       	2.27934259827143    
rl       	167.792508228924    
rctc     	110.815314265582    
t        	1.99088288553436E-13
qc       	37.7983339540601 
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





