How to include variable offsets in polynomial equations
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!