How to find maximum value of all signals in Simulink?

조회 수: 6 (최근 30일)
SM
SM 2025년 4월 14일
답변: Anudeep Kumar 2025년 4월 24일
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.

답변 (1개)

Anudeep Kumar
Anudeep Kumar 2025년 4월 24일
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!

카테고리

Help CenterFile Exchange에서 Control Design in Simulink에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by