MATLAB: Optimization: fmincon, error

optimization

Can anyone please help me out why i am getting this error. Thank You.

Best Answer

Your cost function only assigns to y within the for loop. Your for loop executes from 1 to H. If H is empty or is less than one then your for loop will not execute at all, leaving y undefined.
Your H is declared global. You do not show any code that assigns values to any of the global variables. The default value for global variables is empty.
Note that in order to assign a value to a global variable you need to declare it global and then assign to it. In trial_u you assign some values to variables that appear to be local variables (or possibly in the base workspace). Unless there is a global you have not shown us, those are not the same variables as the global variables. When you use global, it does not mean that every reference to a variable of that name everywhere is suddenly shared: only the places that declare it global share it.
Every iteration of your for loop overwrites ALL of y.
You are assigning a function handle to y. The return from a cost function needs to be a numeric scalar.
Your calculation is inefficient. You calculate a number of expressions that do not change between calls. It would be more efficient to precalculate them and pass their values into the function. You should read about Paramaterizing Functions, which is also a good way to avoid using global variables. As you have already noticed, global variables can be difficult to debug.