kennydota2
引用回帖:
独孤神宇 at 2020-06-03 17:50:12
有范围的设置范围,没有就用-inf +inf 代替
...
劳烦您看下代码,我不知道在哪设置。代码中x(4),x(5),x(10)的范围都是(0,1)
clear all; % Removes all variables, globals, functions and MEX links;
clear; % Clear variables and functions from memory;
clc; % Clear command window;
% MParam =[ f u' u" ]
clc,clear,close all
MParam = [0.5 14.227 0.128
0.5875 14.33 0.129
0.675 14.315 0.129
0.7625 14.338 0.131
0.85 14.345 0.119
0.9375 14.318 0.143
1.025 14.331 0.137
1.1125 14.322 0.148
1.2 14.324 0.153
1.2875 14.343 0.157
1.375 14.324 0.17
1.4625 14.346 0.185
1.55 14.336 0.178
1.6375 14.343 0.206
1.725 14.369 0.21
1.8125 14.355 0.23
1.9 14.378 0.242
1.9875 14.378 0.253
2.075 14.385 0.278
2.1625 14.403 0.293
2.25 14.405 0.3
2.3375 14.41 0.324
2.425 14.436 0.337
2.5125 14.425 0.352
2.6 14.442 0.367
2.6875 14.448 0.379
2.775 14.452 0.395
2.8625 14.47 0.413
2.95 14.461 0.423
3.0375 14.467 0.448
3.125 14.485 0.461
3.2125 14.477 0.477
3.3 14.5 0.493
3.3875 14.485 0.5
3.475 14.493 0.529
3.5625 14.506 0.536
3.65 14.514 0.558
3.7375 14.526 0.558
3.825 14.517 0.592
3.9125 14.53 0.592
4 14.52 0.627
4.0875 14.537 0.635
4.175 14.549 0.674
4.2625 14.579 0.682
4.35 14.559 0.718
4.4375 14.611 0.755
4.525 14.617 0.788
4.6125 14.656 0.852
4.7 14.641 0.886
4.7875 14.648 0.947
4.875 14.648 0.977
4.9625 14.664 1.075
5.05 14.647 1.1
5.1375 14.634 1.188
5.225 14.656 1.223
5.3125 14.612 1.312
5.4 14.634 1.36
5.4875 14.569 1.438
5.575 14.595 1.52
5.6625 14.52 1.584
5.75 14.496 1.666
5.8375 14.422 1.734
5.925 14.405 1.839
6.0125 14.312 1.894
6.1 14.243 1.977
6.1875 14.132 2.015
6.275 14.04 2.103
6.3625 13.91 2.104
6.45 13.805 2.192
6.5375 13.68 2.159
6.625 13.533 2.216
6.7125 13.442 2.16
6.8 13.258 2.16
6.8875 13.224 2.11
6.975 13.031 2.037
7.0625 12.983 1.997
7.15 12.807 1.882
7.2375 12.783 1.845
7.325 12.677 1.723
7.4125 12.643 1.692
7.5 12.583 1.548
7.5875 12.541 1.52
7.675 12.55 1.385
7.7625 12.493 1.352
7.85 12.488 1.214
7.9375 12.456 1.193
8.025 12.476 1.066
8.1125 12.434 1.047
8.2 12.492 0.952
8.2875 12.403 0.89
8.375 12.491 0.859
8.4625 12.404 0.773
8.55 12.522 0.801
8.6375 12.449 0.655
8.725 12.519 0.724
8.8125 12.506 0.574
8.9 12.492 0.649
8.9875 12.539 0.502
9.075 12.486 0.572
9.1625 12.597 0.477
9.25 12.457 0.475
9.3375 12.605 0.445
9.425 12.479 0.4
9.5125 12.574 0.439
9.6 12.502 0.352
9.6875 12.575 0.423
9.775 12.516 0.296
9.8625 12.506 0.383
9.95 12.525 0.266
10.0375 12.504 0.339
] ; % f, u', u"; here please input the measured data
u1 = MParam(:,2); %u1为矩阵的第二列
u2 = MParam(:,3); %u1为矩阵的第3列
f = MParam(:,1); % the frequency value corresponding to each of the u value;
U = [u1
u2];
x0=[12.504,14.227,6.625,0.27,0.83,1.6,2,2.1,2,0.5];
options = optimset('Algorithm','Trust-Region-Reflective','Tolx',1e-8,'TolFun',1e-8,'MaxIter',10000);
% options=optimset(‘optionName’,’optionValue’),这里的optionName就可以换成RBFFunction or Stats等。
% 'Algorithm','Levenberg-Marquardt' 算法设置为L-M算法,默认算法为'Trust-Region-Reflective';
% 'Tolx',1e-8 估计参数x的终止容忍限度:默认为1e-6;
% 'TolFun',1e-2 函数值的终止容忍限度:默认为1e-6;
% 'MaxIter',10000 最大迭代次数:默认为400;
format long
[x,resnorm,residual] = lsqcurvefit(@Lorentz12,x0,f,U,[],[],options)
% x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
%x0为初始解向量;xdata,ydata为满足关系ydata=F(x, xdata)的数据;
%lb、ub为解向量的下界和上界lb≤x≤ub,若没有指定界,则lb=[ ],ub=[ ];
%options为指定的优化参数;
%fun为待拟合函数,计算x处拟合函数值,其定义为 function F = myfun(x,xdata)
u01=x(1);
u02=x(2);
fr1=x(3);
u03=x(4);
u04=x(5);
u05=x(6);
u06=x(7);
u07=x(8);
u08=x(9);
u09=x(10);
u = u01+(u02-u01)./((1+i*(f./fr1).^(1-u03)).^u04)-i*((u05+i*u06)+2*pi*f*(u07+i*u08))./(8.851e-12*(2*pi*f).^u09);
uu1 = real(u);
uu2 = -imag(u);
subplot(1,2,1);
semilogx(f,u1,'o',f,uu1,'r');
xlabel('f (GHz)','FontWeight','Bold', 'FontSize', 18');
ylabel('\epsilon^r^e^a^l','FontWeight','Bold', 'FontSize', 18');
legend('experiment','fitting');
hold on
subplot(1,2,2);
semilogx(f,u2,'o',f,uu2,'r');
xlabel('f (GHz)','FontWeight','Bold', 'FontSize', 18');
ylabel('\epsilon^i^m^a^g','FontWeight','Bold', 'FontSize', 18');
legend('experiment','fitting');
主代码调用函数
function zero1 = Lorentz12(x,f)
% 根据输入x重建复数输入
u01=x(1);
u02=x(2);
fr1=x(3);
u03=x(4);
u04=x(5);
u05=x(6);
u06=x(7);
u07=x(8);
u08=x(9);
u09=x(10);
zerocomp =u01+(u02-u01)./((1+i*(f./fr1).^(1-u03)).^u04)-i*((u05+i*u06)+2*pi*f*(u07+i*u08))./(8.851e-12*(2*pi*f).^u09);
% 将结果转换成一个列向量
% 其中第一部分是实部,第二部分是虚部
numx = length(f); % 实部长度
zero1=real(zerocomp); %实部
zero1(numx+1:2*numx)=-imag(zerocomp); % 虚部将矩阵zero1(a:b)中的第a到第b的元素赋给-imag()
end
,