本帖最后由 dabinlee 于 2018-1-8 22:41 编辑
本人用lsqcurvefit对阻抗模型进行数据拟合,拟合公式Z=a(1)+(2000.*pi.*xdata).*a(2).*1i+(((((((a(5).^(-1))+(((2000.*pi.*xdata).*a(6).*1i).^(-1))).^(-1))+(2000.*pi.*xdata).*a(4).*1i).^(-1))+(a(3).^(-1))).^(-1)),其中xdata为自变量,Z为因变量。a(1)~a(6)应均为正实数,但拟合出来的结果a(1)~a(6)都为复数。恳请问一下如何加限制条件?或者事先定义a(1)~a(6)为正实数?十分感谢!
代码如下
%输入选定频率值xdata/kHz
xdata=[0.02 1 2 3 4 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100];
wL=[0.021296228
0.825987526
1.635136116
2.428765239
3.201157196
3.947096943
7.27215855
10.04304322
12.51484828
14.85659141
17.14744073
19.41818386
21.67950221
23.93705066
26.18831591
28.43047055
30.66445705
32.89310284
35.10666899
37.31269531
39.50866854
41.69490283
43.87611057
46.0450661
48.20459685
]';
R=[0.09208
0.14407
0.2094
0.31065
0.44347
0.60245
1.596
2.585
3.398
4.045
4.574
5.024
5.424
5.792
6.139
6.47
6.791
7.102
7.406
7.703
7.995
8.28
8.559
8.832
9.1
]';
Z=wL.*1i+R;
fun=@(a,xdata)a(1)+(2000.*pi.*xdata).*a(2).*1i+(((((((a(5).^(-1))+(((2000.*pi.*xdata).*a(6).*1i).^(-1))).^(-1))+(2000.*pi.*xdata).*a(4).*1i).^(-1))+(a(3).^(-1))).^(-1));
a0=[2.82,0.000009,25,0.00002,3.57,0.0000115];
options = optimset('MaxFunEvals',15000,'MaxIter',3000);
a=lsqcurvefit(fun,a0,xdata,Z,[],[],options);
%绘图观察
times = linspace(xdata(1),xdata(end));
subplot(2,1,1);
plot(xdata,R,'ko',times,real(fun(a,times)),'b-')
legend('R Data','R Fitted exponential')
title('Resistance Data and Fitted Curve')
subplot(2,1,2);
plot(xdata,wL,'ko',times,imag(fun(a,times)),'b-')
legend('L Data','L Fitted exponential')
title('Inductance Data and Fitted Curve')