09-25-2019 04:50 AM
Hi everybody, I would ask you some about how to implement on a Labview VI an algorithm developed on MATLAB.
This algorithm is about a optimization problem: I want to find the optimum value of F (given and known the arrays of values c and b) which reduces (c - F*b).^2. It is a sort of least square method problem.
Also F_ref is set and known. Trapz is the numerical trapezoidal integration function.
% My optimization problem
output = @(F)(c - F*b).^2;
% Solve the optimization problem
for i = (1:1:N)
% Get old output
if (i > 1)
output_old = output_new(i-1);
else
output_old = trapz(output(0));
end;
% Get new output
output_new(i) = trapz(output(i*F_ref));
% Find minimum
if (output_new(i) > output_old)
% Select optimal F value
F_opt = (i-1)*F_ref
% Break the loop
break;
end;
end;
Thank you all in advance
Solved! Go to Solution.
09-25-2019 04:55 AM
Hi Edmond,
for i = (1:1:N) % Break the loop break; end;
This is a simple FOR loop in LabVIEW, with conditional stop enabled.
if (i > 1) output_old = output_new(i-1); else output_old = trapz(output(0)); end;
IF-THEN-ELSE is a CASE structure in LabVIEW.
When you need values from previous iterations you should use a shift register.
if (output_new(i) > output_old) % Select optimal F value F_opt = (i-1)*F_ref
Again a CASE structure with some simple math.
09-26-2019 03:25 AM
I tried to develop the VI and I ran it. Here I attach my VI.
It works (the results are not exactly the ones expected), but, as I am quite new on LabVIEW, I think it can be debugged and improved because of some errors that I cannot find. Unfortunately I cannot provide the data/signals I used to run the VI.
I attach again the algorithm developed in MATLAB.
% My optimization problem
output = @(F)(c - F*b).^2;
% Solve the optimization problem
for i = (1:1:N)
% Get old output
if (i > 1)
output_old = output_new(i-1);
else
output_old = trapz(output(0));
end;
% Get new output
output_new(i) = trapz(output(i*F_ref));
% Find minimum
if (output_new(i) > output_old)
% Select optimal F value
F_opt = (i-1)*F_ref
% Break the loop
break;
end;
end;
09-26-2019 04:32 AM
09-26-2019 04:37 AM
Here there is 2017 version. Thanks
09-26-2019 04:54 AM
Hi Edmond,
there's no shift register to access values from previous iteration.
There also is no stop condition in the loop…
Why do you operate on waveforms? Why don't you read the sample array (aka "Y") from the waveforms before the loop?
Why do you multiply the complete waveform by "i * 1e-7"?
I'm sure it can be simplified much more:
09-26-2019 06:49 AM
Thank you very much for the explanation and your help. Now the vi has more sense.
But I don't understand how you solved the first "case structure", this one:
% Solve the optimization problem
for i = (1:1:N)
% Get old output
if (i > 1)
output_old = output_new(i-1);
else
output_old = trapz(output(0));
end;
And why did you put the final number indicator as a result of the false case?
Thanks in advance!
09-26-2019 07:42 AM
Hi Edmond,
But I don't understand how you solved the first "case structure", this one:
I just simplified the code you created by using the MinMax function…
why did you put the final number indicator as a result of the false case?
Because you did it in your VI: I didn't change the placing of that terminal…
09-30-2019 12:47 PM