From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Help to implement algorithm

Solved!
Go to solution

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

0 Kudos
Message 1 of 9
(2,702 Views)
Solution
Accepted by topic author EdmondDantes

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.

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 9
(2,690 Views)

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;

0 Kudos
Message 3 of 9
(2,610 Views)

Hi Edmond,

 

can you please convert the VI to LV2017 (edit->save for previous)?

 

some errors that I cannot find

Which errors do you encountered?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 9
(2,593 Views)

Here  there is 2017 version. Thanks

0 Kudos
Message 5 of 9
(2,591 Views)

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:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 9
(2,586 Views)

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!

0 Kudos
Message 7 of 9
(2,572 Views)

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…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 9
(2,562 Views)

Hi Edmond,

 

as I see the same inefficient or "hard to read" code in your new thread I will provide some more snippets:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 9
(2,525 Views)