parameter estimation using MATLAB
Show older comments
Having problems. I have test data and i want to estimate the parameters which will give me similar curve. I dont know what to do. Please help. Attached is my code. % ---- Main Code ------ % % First guess for parameters p0 = [10000, 1e-12, 50000, 20000];
% Load data
data = load('dev5_a.dat', 'ascii');
% First column is frequency in Hz, convert to rad/sec
freq = 2*pi*data(:, 1);
% Second column is magnitude impedance in Ohms
data(:,2) = (data(:,2));
% Third column is impedance angle in deg, convert to rads
data(:, 3) = data(:, 3);
z = [data(:,2) data(:,3)];
p0 = fminsearch(@(p0) costfun2(p0,z,freq),...
p0) ;
new = origmodel(p0,freq) ;
% plot original
close all
figure
%scatter(data(:,1),data(:,2))
plot(data(:,1),log(data(:,2)),'b')
% Plot estimate
hold on
%scatter(data(:,1),new(:,1))
plot(data(:,1),log(new(:,1)),'r')
% -- Calling function ----
function [out] = costfun2(p,z, f)
R1 = (p(1)); %*1e3;
C1 = (p(2)); %*1e-12;
R2 = (p(3)); %*1e3;
C2 = (p(4)); %*1e-12;
z1 = R1 - i./(f*C1);
z2_inv = (1/R2+1./z1);
z2 = (z2_inv).^-1;
zt = z2 - i./(f*C2);
znew(:, 1) = (abs(zt));
znew(:, 2) = angle(zt);
er = z(:) - znew(:);
out = er'*er;
txt = sprintf('The error is %f',out);
disp(txt)
% --- orig model ----
function [z] = origmodel(p, f)
R1 = (p(1)); %*1e3;
C1 = (p(2)); %*1e-12;
R2 = (p(3)); %*1e3;
C2 = (p(4)); %*1e-12;
z1 = R1 - i./(f*C1);
z2_inv = (1/R2+1./z1);
z2 = (z2_inv).^-1;
zt = z2 - i./(f*C2);
z(:, 1) = (abs(zt));
z(:, 2) = angle(zt);
return;
%%%%%------- Test DATA -----------
% Freq. [Hz] |Z| [Ohm] Angle [deg]
2.00000E+1 2.17111E+4 -4.97566E+1
2.40000E+1 1.95677E+4 -5.09048E+1
2.88000E+1 1.75852E+4 -5.18520E+1
3.45600E+1 1.57614E+4 -5.25666E+1
4.14720E+1 1.41032E+4 -5.31513E+1
4.97664E+1 1.25956E+4 -5.35962E+1
5.97197E+1 1.12928E+4 -5.40219E+1
7.16636E+1 1.00903E+4 -5.41847E+1
8.59963E+1 9.04188E+3 -5.43254E+1
1.03196E+2 8.09931E+3 -5.43871E+1
1.23835E+2 7.25300E+3 -5.43497E+1
1.48602E+2 6.49768E+3 -5.41649E+1
1.78322E+2 5.83313E+3 -5.38856E+1
2.13986E+2 5.23557E+3 -5.35058E+1
2.56784E+2 4.70178E+3 -5.30118E+1
3.08140E+2 4.24002E+3 -5.24886E+1
3.69769E+2 3.82403E+3 -5.19025E+1
4.43722E+2 3.45443E+3 -5.12147E+1
5.32467E+2 3.12396E+3 -5.04663E+1
6.38960E+2 2.82972E+3 -4.96907E+1
7.66752E+2 2.56319E+3 -4.88257E+1
9.20102E+2 2.32655E+3 -4.78864E+1
1.10294E+3 2.11354E+3 -4.68502E+1
1.32979E+3 1.92099E+3 -4.57067E+1
1.59574E+3 1.75442E+3 -4.44931E+1
1.89394E+3 1.61488E+3 -4.32674E+1
2.27273E+3 1.48251E+3 -4.18808E+1
2.72727E+3 1.36475E+3 -4.03953E+1
3.28947E+3 1.25807E+3 -3.87924E+1
3.94474E+3 1.16634E+3 -3.71557E+1
4.68750E+3 1.08959E+3 -3.55618E+1
5.68182E+3 1.01377E+3 -3.37151E+1
6.81818E+3 9.51390E+2 -3.19480E+1
8.33333E+3 8.91081E+2 -2.99645E+1
1.00000E+4 8.43392E+2 -2.81755E+1
1.19084E+4 8.03597E+2 -2.65023E+1
1.41176E+4 7.69179E+2 -2.48993E+1
1.71429E+4 7.34613E+2 -2.31238E+1
2.06897E+4 7.05560E+2 -2.14962E+1
2.40000E+4 6.85431E+2 -2.02984E+1
2.94118E+4 6.60834E+2 -1.87544E+1
3.52941E+4 6.41529E+2 -1.74947E+1
4.28571E+4 6.23334E+2 -1.63007E+1
5.00000E+4 6.10356E+2 -1.54652E+1
6.00000E+4 5.96319E+2 -1.46004E+1
7.24286E+4 5.84014E+2 -1.38975E+1
8.57143E+4 5.72033E+2 -1.32940E+1
1.00000E+5 5.62451E+2 -1.28862E+1
1.25000E+5 5.49142E+2 -1.24387E+1
1.50000E+5 5.38642E+2 -1.22093E+1
1.66667E+5 5.32645E+2 -1.21344E+1
2.00000E+5 5.22345E+2 -1.20683E+1
2.50000E+5 5.09576E+2 -1.21084E+1
3.20000E+5 4.95257E+2 -1.22910E+1
4.00000E+5 4.81978E+2 -1.25578E+1
4.80000E+5 4.70751E+2 -1.28357E+1
5.00000E+5 4.68159E+2 -1.29117E+1
6.40000E+5 4.52411E+2 -1.33648E+1
8.00000E+5 4.37780E+2 -1.38281E+1
9.60000E+5 4.25443E+2 -1.42423E+1
1.00000E+6 4.22626E+2 -1.43434E+1
Answers (0)
Categories
Find more on Multivariate Models in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!