MATLAB Answers

Undefined function 'prob2struct' for input arguments of type 'sym'

7 views (last 30 days)
Raghav Khandelwal
Raghav Khandelwal on 30 Dec 2019
Answered: Alan Weiss on 2 Jan 2020
I am trying to create an MPC controller. The controller predicts 3 state vectors (x,y and theta) with two control inputs (v and omega) for 3 horizons. I have used symbolic variables to calculate the final state matrix in form of the control variables. My objective function consists of 3X3 matrix in terms of v and omega. I am trying to solve the optimization problem by converting the matrix into scalar form so that i can use 'optimalproblem' function. But I am not able to convert the matrix to scalar form and getting this error:
""Undefined function 'prob2struct' for input arguments of type 'sym'
Error in MPCFF (line 65)
nlp = prob2struct(obj);
""
Following is my code:
"""
= 0.2;
n = 3;
dia = 0.3;
vmax = 0.6;
vmin = -0.6;
omegamax = pi/4;
omegamin = -pi/4;
syms x;
syms y;
syms theta;
states = [x;y;theta];
n_states = length(states);
syms v;
syms omega;
controls = [v; omega];
n_cont = length(controls);
rhs = [v*cos(theta); v*sin(theta); omega];
fun = f(states,controls);
syms u0;syms v0;syms omega0;
syms u1;syms v1;syms omega1;
syms u2;syms v2;syms omega2;
syms u3;syms v3;syms omega3;
syms u4;syms v4;syms omega4;
syms u5;syms v5;syms omega5;
u0 = v0;
u1 = omega0;
u2 = v1;
u3 = omega1;
u4 = v2;
u5 = omega2;
U = [u0, u2, u4; u1, u3, u5];
syms p0;
syms p1;
syms p2;
syms p3;
syms p4;
syms p5;
P = [0,0,0,4,4,0];
syms x0;syms x6;
syms x1;syms x7;
syms x2;syms x8;
syms x3;syms x9;
syms x4;syms x10;
syms x5;syms x11;
X = [x0,x3,x6,x9;x1,x4,x7,x10;x2,x5,x8,x11];
X(:,1) = P(1:3);
for k = 1:n
sts = X(:,k); con = U(:,k);
st_nxt = sts + (t*f(sts,con));
X(:,k+1) = st_nxt;
end
v0 = optimvar('v0');
v1 = optimvar('v1');
v2 = optimvar('v2');
omega0 = optimvar('omega0');
omega1 = optimvar('omega1');
omega2 = optimvar('omega2');
hg = [v0,v1,v2;omega0,omega1,omega2];
obj = Calculate_objective(X,U);
nlp = prob2struct(obj);
prob = optimproblem('ObjectiveSense','min');
prob.Objective = nlp;
prob.Constraints.cons1 = g;
showproblem(prob);
function y = Calculate_objective(sta, cont)
y=0;
n=3;
P = [0,0,0,5,5,0];
Q = zeros(3,3); R = zeros(2,2);
Q(1,1) = 1; Q(2,2) = 5; Q(3,3) = 0.1;
R(1,1) = 0.5; R(2,2) = 0.05;
for k = 1:n
y = y+ (sta(:,k)-P(4:6)).'*Q*(sta(:,k)-P(4:6)) + cont(:,k).'*R*cont(:,k);
end
end
function y = f(s,c)
y = [c(1,1)*cos(s(3,1)); c(1,1)*sin(s(3,1)); c(2,1)];
end
"""

  0 Comments

Sign in to comment.

Answers (1)

Alan Weiss
Alan Weiss on 2 Jan 2020
You cannot use symbolic variables directly with Optimization Toolbox™ functions. Either convert your symbolic expressions using matlabFunction as in this example, or, what will probably work better for you, don't use any symbolic variables at all, and instead use optimvar variables. See Problem-Based Optimization Setup.
Alan Weiss
MATLAB mathematical toolbox documentation

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by