Question

I am trying to solve a system of equations for a symbolic vector. The equations contain parameters though. Running the attached code gives me to many and incorrect solutions. I guess the function aussumes the second input to be equations, too:

Warning: 4 equations in 3 variables. 

ans = 

    a_tilde2: [1x1 sym]
    a_tilde3: [1x1 sym]
          p1: [1x1 sym]

I can fix this problem by listing the entries of a_tilde_sym(iRange) individually, as in:

solve(bounday_conditions, a_tilde_sym(iRange(1)), a_tilde_sym(iRange(2)), etc.)

But since the length of this vector is dependet on a function input I can't do this dynamically. Any suggestions?

%function variation_polynomial ( sysord, reldeg )
sysord = 2;
reldeg = 1; % e.g.

% setting up symbolic variables
disp('calculating variation from desired trajectory');
fprintf('N =%3.0f, R =%3.0f\n', sysord, reldeg);

iRange = reldeg+1:2*reldeg+1;
jRange = 1:sysord-reldeg;
syms T_sym real % maneuver time
syms t_sym real % time
p_sym = sym('p', [sysord-reldeg, 1]); % parameters of polynom
assume(p_sym, 'real');
a_tilde_sym = sym('a_tilde', [iRange(end), 1]); % parameters of polynom
assume(a_tilde_sym, 'real');
y_tilde = sym('derivatives', [reldeg+1, 1]);

% setting up system of equations
i_polynomial_sum = 0;
for iIdx = iRange
    i_polynomial_sum = i_polynomial_sum + a_tilde_sym(iIdx)*(t_sym/T_sym)^iIdx;
end

j_polynomial_sum = 0;
for jIdx = jRange
    j_polynomial_sum = j_polynomial_sum + p_sym(jIdx)*(t_sym/T_sym)^(jIdx + 2*reldeg + 1);
end

y_tilde(1) = i_polynomial_sum + j_polynomial_sum;
for rIdx = 1:reldeg
    y_tilde(rIdx+1) = diff(y_tilde(1), t_sym, rIdx);
end

% solving
bounday_conditions = subs(y_tilde, t_sym, T_sym);
for runIdx = 1:reldeg+1
    bounday_conditions(runIdx) = bounday_conditions(runIdx) == 0;
end
bounday_conditions = simplify(bounday_conditions);

solve(bounday_conditions, a_tilde_sym(iRange))
Was it helpful?

Solution

Yes, solve takes your second input, a_tilde_sym(iRange) to be a set of equations rather than the variable(s) to be solved for. There's a good reason for this. Your first two equations (bounday_conditions (sic)) are in terms of the scalar variables a_tilde2 and a_tilde3, not the vector [a_tilde2;a_tilde3]. You can solve this by explicitly indicating your variables:

s = solve(bounday_conditions, a_tilde_sym(2), a_tilde_sym(3))

or

[a_tilde2,a_tilde3] = solve(bounday_conditions, a_tilde_sym(2), a_tilde_sym(3))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top