Optimisation problem in matlab
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello guys,
So I have this scatte plot in the form of a measured impedance according to frequency Z(f), and I have an RL ladder circuit seen in the following figure
.
I need some help in writing an optimisation problem that will allow me to get the values of the parameters R_i L_i that will eventually give an equivalent impedance of the RL ladder similar to the one measured- Z(f) -.
Thanks in advance.
Wallflower
댓글 수: 2
Rik
2021년 6월 21일
This looks like a homework assignment. You can find guidelines for posting homework on this forum here. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks). If your main issue is with understanding the underlying concept, you may consider re-reading the material you teacher provided and ask them for further clarification.
답변 (1개)
Sergey Kasyanov
2021년 6월 21일
편집: Torsten
2022년 7월 12일
Hello!
Try that:
% tune only next 2 lines
L = 5;% steps in ladder
Z_max = 1e2;% max Z for one R or Xl
% goal Z(w)
% w0 - array with measured angular frequences
% Z0 - array with measured impedances
% R0 - Rs_1.txt
% L0 - 2*Self_Energy_1.txt
w0 = (2.*pi.*[[1e-3,50,500],1e3:2e3:500e3])';
% Start inserted to make code work
R0 = rand(size(w0));
L0 = rand(size(w0));
% End inserted to make code work
Z0 = R0(:,1) + 1i*w0.*L0(:,1);
w0 = reshape(w0, 1, []);
Z0 = reshape(Z0, 1, []);
%% calculate z(w) for ladder
R = sym('R', [1,L+1]);
L = sym('L', [1,length(R)]);
w = sym('w');
par = @(a, b) 1/(1/a + 1/b);
h1 = @(a, i) R(i) + par(a, 1i*w*L(i));
Z = R(end);
for i = (length(R)-1):-1:1
Z = h1(Z, i);
end
% get lambda function for optimization
fun1 = matlabFunction(Z);
% do not look at that code
s = 'fun2 = @(vals, w) fun1(';
for i = 1:(length(R)*2 - 1)
s = [s, sprintf('vals(%i), ', i)];
end
s = [s, 'w);'];
eval(s);
% create optimization function
fun3 = @(vals) sum(abs(fun2(vals, w0) - Z0).^2);
% number of variables
N = length(symvar(Z)) - 1;
%the better way is to use more powerfull algorithms such as genetic algoritms which can stands
%against a huge amount of local min. There are a lot of algorithms that
%realized in matlab toolboxes. Try!
options = optimoptions('ga', 'Display', 'iter', 'PopulationSize', N*200, 'MaxGenerations', N*200);
res = ga(fun3, N, [], [], [], [], zeros(1, N), Z_max*[ones(1,floor(N/2))/2/pi, ones(1, ceil(N/2))], [], options);
% first floor(N/2) of res - L(1), L(2), ...
% last ceil(N/2) of res - R(1), R(2), ...
%plot results
figure;plot(w0, abs(Z0), w0, abs(fun2(res,w0)));
figure;plot(w0, angle(Z0), w0, angle(fun2(res,w0)));
댓글 수: 14
Anes MESSADI
2022년 7월 12일
there are no errors, it works, but the fitting is not good, I used only the Rs_1.txt and Self_Energy_1.txt just to test the code.
Thank you for your help
Sergey Kasyanov
2022년 7월 13일
Try to use that code with your w0 and Z0. Also you need function ladder_z in the file.
% tune only next 2 lines
L = 10;% steps in ladder
Z_max = 1e2;% max Z for one R or Xl
% w0 - array with measured angular frequences
% Z0 - array with measured impedances
% create data for testing
w0 = 2 * pi * logspace(-3, 2, 10);
z0 = [rand(1, L);rand(1, L)];
Z0 = [];
for i = 1:length(w0)
Z0(i) = ladder_z(z0(1, :) + z0(2, :) * 1i * w0(i));
end
% main code
h1 = @(vals, w) ladder_z(vals(1:fix(length(vals) / 2)) + 1i * w * vals(fix(length(vals) / 2) + 1:end));
h2 = @(vals) arrayfun(@(iw) h1(vals, iw), w0);
f = @(vals) sum(abs(h2(vals) - Z0).^2);
options = optimoptions('ga', 'Display', 'iter', 'PopulationSize', L*200, 'MaxGenerations', L*200, 'UseParallel', true);
res = ga(f, L * 2, [], [], [], [], zeros(1, 2 * L), Z_max*[ones(1, L), ones(1, L) / 2 / pi], [], options);
% check
figure;plot(w0, abs(Z0), w0, abs(h2(res)));
참고 항목
카테고리
Help Center 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!