How to include variable offsets in polynomial equations

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
Bjorn Gustavsson 2021년 11월 4일
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

댓글 수: 4

It appears that MATLAB automatically expands and simplifies the first order term which changes the 0th order term, is there a way to keep the offset instead of automatically simplifying?
So it seems. I don't know and lack interest to find out...
right, ok thanks!
My pleasure, glad it helped.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by