# How to include variable offsets in polynomial equations

2 views (last 30 days)
Connor LeClaire on 4 Nov 2021
I am attempting to display a series of equations, of which some need offsets to the variable. The equations are (at maximum) cubic polynomials, however some require an offset while others do not.
No offset: a*t^3 + d
Offset: a*(t-5)^3 + b(t-5)^2 + d
The offset will be the constant in each polynomial.
Is there an easy way to apply this? Code is given below, the offset of each polynomial is equal to t_0 of the function. So for segment 2, the offset is t_1 should be shown as (t-2)^n in the polynomial:
clear all;
clc;
syms t real;
% Position in [deg deg m]
q_0 = [0 0 0]';
q_1 = [-8 45 0.2]';
q_2 = [-90 90 0.4]';
% Speeds in [deg/s deg/s m/s]
q_dot_0 = [0 0 0]';
q_dot_1 = [10 40 0.2]';
q_dot_2 = [0 0 0]';
% Times in s
t_0 = 0;
t_1 = 2;
t_2 = 4;
coefficients = zeros(2,4,3);
% Segment 1: 0 < t < 2
coefficients(1,:,1) = cpsCoefficients(q_0(1), q_1(1), q_dot_0(1), q_dot_1(1), t_0, t_1);
coefficients(1,:,2) = cpsCoefficients(q_0(2), q_1(2), q_dot_0(2), q_dot_1(2), t_0, t_1);
coefficients(1,:,3) = cpsCoefficients(q_0(3), q_1(3), q_dot_0(3), q_dot_1(3), t_0, t_1);
% Segment 2: 2 < t < 4
coefficients(2,:,1) = cpsCoefficients(q_1(1), q_2(1), q_dot_1(1), q_dot_2(1), t_1, t_2);
coefficients(2,:,2) = cpsCoefficients(q_1(2), q_2(2), q_dot_1(2), q_dot_2(2), t_1, t_2);
coefficients(2,:,3) = cpsCoefficients(q_1(3), q_2(3), q_dot_1(3), q_dot_2(3), t_1, t_2);
eqn(1,1) = vpa(poly2sym(fliplr(coefficients(1,:,1)),t));
eqn(2,1) = vpa(poly2sym(fliplr(coefficients(2,:,1)),t));
eqn(3,1) = vpa(poly2sym(fliplr(coefficients(1,:,2)),t));
eqn(4,1) = vpa(poly2sym(fliplr(coefficients(2,:,2)),t));
eqn(5,1) = vpa(poly2sym(fliplr(coefficients(1,:,3)),t));
eqn(6,1) = vpa(poly2sym(fliplr(coefficients(2,:,3)),t));
eqn = string(eqn); %For table display purposes
table_segment_1 = table(eqn(1:2), eqn(3:4), eqn(5:6),'RowNames',{'Segment 1 (0 < t < 2)','Segment 2 (2 < t < 4)'},'VariableNames',{'Joint 1','Joint 2','Joint 3'});
disp(table_segment_1);
function [coeffs] = cpsCoefficients(theta_0, theta_f, theta_dot_0, theta_dot_f, t_0, t_f)
%Calculates and returns the CPS coefficients
a0 = theta_0;
a1 = theta_dot_0;
a2 = (3*(theta_f-theta_0)-(2*theta_dot_0+theta_dot_f)*t_f)/t_f;
a3 = (2*(theta_0-theta_f)+(theta_dot_0+theta_dot_f)*(t_f-t_0))/((t_f-t_0)^3);
coeffs = [a0 a1 a2 a3];
end

Bjorn Gustavsson on 4 Nov 2021
Have a look at the help and documentation of taylor. That should help you a good part of the way - especially if you use the 'expansionpoint' variable you will get rather close to what you want:
>> syms X
>> A = sym('A',[1,4]);
>> f = A(1) + A(2)*X + A(3)*X^2 + A(4)*X^3;
>> asd = taylor(f,X,'expansionpoint',5);
% Returns
%
% asd =
% A1 + 5*A2 + 25*A3 + 125*A4 + (A3 + 15*A4)*(X - 5)^2 + A4*(X - 5)^3 + (X - 5)*(A2 + 10*A3 + 75*A4)
a0 = 12;
a1 = 7;
a2 = 5;
a3 = -3;
zxc = subs(asd,A,[a0 a1 a2 a3]);
% Returns:
% zxc =
%
% 637 - 40*(X - 5)^2 - 3*(X - 5)^3 - 168*X
HTH
Bjorn Gustavsson on 4 Nov 2021
My pleasure, glad it helped.

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!