Optimisation problem in matlab
古いコメントを表示
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.
wallflower
2021 年 6 月 21 日
回答 (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 件のコメント
wallflower
2021 年 6 月 21 日
Sergey Kasyanov
2021 年 6 月 21 日
sorry, use fmincon instead:
res = fmincon(fun,rand(1,N),[],[],[],[],zeros(1,N),9e9*ones(1,N),[],options);
wallflower
2021 年 6 月 21 日
Sergey Kasyanov
2021 年 6 月 22 日
What is about ga()?
wallflower
2021 年 6 月 22 日
Sergey Kasyanov
2021 年 6 月 22 日
Can you load Z(f) here as .mat file?
wallflower
2021 年 6 月 22 日
Sergey Kasyanov
2021 年 6 月 24 日
Are Z(f) pure resistance? That means that it does not depend on frequency, so problem is not correct.
I corrected code in answer.
wallflower
2021 年 6 月 24 日
Sergey Kasyanov
2021 年 6 月 24 日
I corrected code. There were some misprints.
Now it is working, but It is very sensitive to limits of Z_max and amount of ladder steps. Try, please. Does it working on your machine?
Anes MESSADI
2022 年 7 月 12 日
I tried to use the code you proposed but it doesn't work, can you please share the corrected one.
Thank you,
Sergey Kasyanov
2022 年 7 月 12 日
I can't test and correct code at the moment. What error is there?
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)));
カテゴリ
ヘルプ センター および File Exchange で Ladder Diagram Integration についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


