How to get cost function value in MATLAB

Hello,

I am using acados in Matlab. I wonder how to get the objective function value from ocp in Matlab. I tried to directly call “ocp.get_cost” to get the cost value. But I compared the value with the cost I computed by myself and they are totally different. Is the function “ocp.get_cost” exactly output the cost value or is there something I missed?

Thanks a lot in advance!

Yes, it should evaluate and output the cost function at the current iterate.
Note that the stage cost terms are weighted with the time step that is covored by the subsequent shooting interval.

1 Like

Ah, maybe I compute the cost wrongly. I will check this. Thanks very much.

Could you give more details about this? I tried to compare the cost provided by acados with the one computed as (25) and (26) here, starting from the output trajectories, but I get different results.

More in detail, in minimal_example_ocp.m (Matlab interface), I compared:

cost = ocp.get_cost();
cost =

     2.242828140598096e+03

and

cost_ = 0;
ytraj = [xtraj(:,1:end-1); utraj];
for i = 1:N
   cost_ = cost_ +  ytraj(:,i)'*model.W*ytraj(:,i);
end
cost_ = cost_ +  xtraj(:,N+1)'*model.W_e*xtraj(:,N+1);
cost_ =

     4.485490903707016e+04

Thanks.

Hi,

as stated above: How to get cost function value in MATLAB - #2 by FreyJo

Note that the stage cost terms are weighted with the time step that is covored by the subsequent shooting interval.

Cheers