The provided Matlab code encounters the error 'Failure in initial objective function evaluation.' This usually occurs due to issues within the objective function used in the optimization process. Let's break down the errors and provide a corrected version of the code.

Error Analysis:

  1. Undefined Function: The function 'c7fopt2' is not defined before being used in 'fminunc'. It needs to be defined separately.
  2. Typo: The variable 'y0' is likely a typo and should be 'yO'.
  3. Inconsistent Input Arguments: The order of input arguments in 'c7fopt2' doesn't match its usage in 'fminunc'. The correct order is 'x', 's', 'G', 't'.
  4. Variable Redefinition: The variable 't' is redefined inside the function, conflicting with the input argument 't'. It's best to remove this redefinition.

Corrected Code:

function y = c7fopt2(x, s, G, t)
    Gc = x(1) + x(2)/s + x(3)*s;
    E = 1/(1 + G*Gc);
    yO = step(E, t);
    y = t * abs(yO) * (t(2) - t(1));
end

s = tf('s');
G1 = 3/(s+1);
G1.ioDelay = 3;
G = (1 + G1)/(s + 1);
t = 0:0.02:30;
x = fminunc(@(x) c7fopt2(x, s, G, t), [1, 1, 1], optimset);

Explanation:

  • The 'c7fopt2' function is defined correctly, taking 'x', 's', 'G', and 't' as input arguments.
  • The typo in 'y0' is corrected to 'yO'.
  • The variable 't' is not redefined inside the function, avoiding conflicts.
  • 'fminunc' now uses an anonymous function '@(x) c7fopt2(x, s, G, t)' to pass the correct input arguments to 'c7fopt2'.

Important Note: Make sure to define the 'c7fopt2' function according to your specific optimization problem. The code provided here is a general solution and may need adjustments based on your intended application. This correction should resolve the 'Failure in initial objective function evaluation' error and allow your optimization process to run smoothly.

Matlab Optimization Error: 'Failure in initial objective function evaluation' -  Troubleshooting Guide

原文地址: https://www.cveoy.top/t/topic/pepw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录