Using S_function in Matlab/Simulink


My goals are:
1.Run a Simulink model using the S-function
2.Online Change reference y_ref and y_ref_e;
It’s compiled. But,some questions:
1.the Input of the S-function,I cannot match the inputs in minimal_example_ocp.m.
mabe this:
ocp.set(‘init_x’, x_traj_init);
ocp.set(‘init_u’, u_traj_init);
ocp.set(‘init_pi’, zeros(nx, N))

 % change values for specific shooting node using:
 %   ocp.set('field', value, optional: stage_index)
  ocp.set('constr_lbx', x0, 0)

 this  can update x0.
 but y_ref and y_ref_e?, I cannot find how to set.
  1. N = 20,nx = 4 ,nu = 1;but y_ref = zeros(100,1) y_ref_e = zeros(4,1),I cannot understand. I think y_ref = zeros(5,1)?

Thanks a lot,


Online Change reference y_ref and y_ref_e;

Can be done like here

Please try phrasing your question more clearly.


Thank you for your reply.
The second.

N = 20,nx = 4 ,nu = 2;y_ref = zeros(120,1) ,y_ref_e = zeros(4,1).
But,y_ref = zeros(120,1)? I don’t know how to explain it.

Thanks a lot,

The Simulink block input for y_ref contains the y_ref values for all stages (but the terminal one) concatenated, see:

Probably you have ny = 6, N = 20 in the problem you made the screenshot from.

Does this make sense?


@FreyJo. That’s right. N*ny, that’s how I understand it.
But why set this form?
I think we are not concerned about all stages.
Maybe,I just want to set the first stage.


This gives you the option to change y_ref at all stages, if you don’t want to change them for some stages, just set them to your default y_ref value…

thanks for the positive feedback!
I got it.
in addition,I had get the answers from the old topic.

All my problems have been solved.
Thanks for the amazing work you done!
:wink: :wink: :wink:

Best regards,