Setting cost function weights from c++ code


I am solving a nonlinear MPC (#states = 21; #controls = 7) problem using acados. I code generate c code from MATLAB for a particular set of cost weights and ultimately use these c generated files to solve problem written in a C++ script. I observe cost value in C++ script and I tried updating cost weights from the C++ script itself. I use the following command:
ocp_nlp_cost_model_set(nlp_config, nlp_dims, nlp_in, i, "W", W);
where W is a vector of size 28 for 0<=i =< (N-1) and 21 for i = N; N is the horizon length.
I observe whenever I try to update weights from within the C++ script the cost value explodes and it seems something wrong happens with the solver. If I export solver again with the same set of weights from MATLAB and use it in C++ the solver does not become unstable. Since it is more convenient to tune weights from within the C++ script, I would like to find out what wrong happens if I update weights from C++.

Am I missing something important while updating weights from C++ script?

Thanks in advance,


I the weight matrix W is of dimension ny*ny.
It is not limited to be diagonal.
I guess you have ny = 28, ny_e = 21 and that is the issue.