How to find maximum value of all signals in Simulink?

3 visualizzazioni (ultimi 30 giorni)
SM
SM il 14 Apr 2025
Risposto: Anudeep Kumar il 24 Apr 2025
I am trying to find out the optimized values of a controller, like say, PID controller for a closed loop control system in Simulink using an optimization algorithm. The objective function J to be minimized is , where e(k) is the error at kth instant, u(k) is the controller output at kth instant, is the maximum value of error, is the maximum value of error, and are the weighting factors, .
I am unable to create the block diagram of this objective function in Simulink which can run correctly. So, this is a weighted and normalized objective function. Using function block, if I create this objective function, it is unable to take the maximum of all the values of error e(k) or control signal u(k). A sample objective function using function block is attached. T is the sampling time time, and N=Tf/T, where Tf is the total simulation time. So, this function is like integration in continuous time.

Risposte (1)

Anudeep Kumar
Anudeep Kumar il 24 Apr 2025
Hey SM,
I reviewed your model, and if I understand your requirements correctly, it seems the issue may be due to incorrect logic in the for loop used to calculate ‘t2’ or the maximum of the input array in both MATLAB function blocks.
Assuming ‘e’ and ‘u’ are double datatype arrays of size MxN, the functions ‘y’ and ‘y1’ can be defined as follows:
function y = fcn(e)
t2=max(e);
um=((e).^2)/(t2^2);
y = um;
function y1 = fcn(u)
t2=max(u);
un=(((u).^2))/(t2^2);
y1 = un;
Here, you can avoid using a for loop because the ‘max’ function automatically finds the maximum value in the array.
Make sure to use ‘.^’ for element-wise exponentiation.
I believe this approach should resolve your issue with calculating the maximum value of all entries in the error ‘e(k)’ or control signal ‘u(k)’ arrays.
Here is the documentation of max function in case you want to explore how to calculate max in different cases:
https:/www.mathworks.com/help/releases/R2024b/matlab/ref/double.max.html
Here is a link to the documentation on calculating element wise power in case you would like to try different cases:
Hope this helps!

Prodotti


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by