# Quick Problem - Force MATLAB to substitute assigned variable values

5 次查看（过去 30 天）
Jude Diab 2022-4-3

I have a code simulating a robotic arm but need to address some issues within it (complex numbers...sigh). To do so I must seperate out sections of my code. but MATLAB is very reluctant to substitute in assigned variable values for me.
However, when I assign a value to the variable t for a long/complicated equation, MATLAB won't substitute in this assigned value for me. For instance, when I run:
problem = acos(((z0.*cos((2642680826589289.*t)/36028797018963968) + (36028797018963968.*v1(3).*sin((2642680826589289.*t)/36028797018963968))/2642680826589289).^2 + (y0 - (72057594037927936.*v1(1))/2642680826589289 - t.*(3.*v1(2) + (7928042479767867.*x0)/18014398509481984) + (72057594037927936.*v1(1).*cos((2642680826589289.*t)/36028797018963968))/2642680826589289 + sin((2642680826589289.*t)/36028797018963968).*((144115188075855872.*v1(2))/2642680826589289 + 6.*x0)).^2 + ((72057594037927936.*v1(2))/2642680826589289 + 4.*x0 + (36028797018963968.*v1(1).*sin((2642680826589289.*t)/36028797018963968))/2642680826589289 - cos((2642680826589289.*t)/36028797018963968).*((72057594037927936.*v1(2))/2642680826589289 + 3.*x0)).^2 + L1.^2 - L2.^2)./((y0 - (72057594037927936.*v1(1))/2642680826589289 - t.*(3.*v1(2) + (7928042479767867.*x3)/18014398509481984) + (72057594037927936.*v1(1).*cos((2642680826589289.*t)/36028797018963968))/2642680826589289 + sin((2642680826589289.*t)/36028797018963968).*((144115188075855872.*v1(2))/2642680826589289 + 6.*x0)).^2 + 2.*((72057594037927936.*v1(2))/2642680826589289 + 4.*x0 + (36028797018963968.*v1(1).*sin((2642680826589289.*t)/36028797018963968))/2642680826589289 - cos((2642680826589289.*t)/36028797018963968).*((72057594037927936.*v1(2))/2642680826589289 + 3.*x0)).^2 + L1.*(z0.*cos((2642680826589289.*t)/36028797018963968) + (36028797018963968.*v1(3).*sin((2642680826589289.*t)/36028797018963968))/2642680826589289).^2));
where all variables are assigned values (if you wish to try x0,y0,z0=0, and all other values are arbritrary). However, it returns:
acos(((1565815393769912744046456672400*cos((2642680826589289*t)/36028797018963968)^2)/10622301927809647241275780768317441 + ((10057422861105304*sin((2642680826589289*t)/36028797018963968))/2576613805924556775 - (176530527569188108*cos((2642680826589289*t)/36028797018963968))/2576613805924556775 + 36311847799948152/858871268641518925)^2 + ((1262719709398513096*sin((2642680826589289*t)/36028797018963968))/2576613805924556775 - (2706998657796766952*cos((2642680826589289*t)/36028797018963968))/2576613805924556775 + 26706868099968188/34354850745660757)^2)/(((4164656525256473544014663708285387085997464737221633*sin((2642680826589289*t)/36028797018963968))/104519873204371423276067813771760764939987111116800 - (4160974066403753513123278688624796953781064124009473*cos((2642680826589289*t)/36028797018963968))/52259936602185711638033906885880382469993555558400 - (198201061994196675*cos((2642680826589289*t)/36028797018963968)*((32776373919501099224090370885186929*sin((2642680826589289*t)/36028797018963968))/92832295809916075125361180095283200 - (32776373919501099224090370885186929*cos((2642680826589289*t)/36028797018963968))/46416147904958037562680590047641600 + 11358149378044935377074331655830651/15472049301652679187560196682547200))/1125899906842624 + (9360*cos((2642680826589289*t)/36028797018963968)*((32776373919501099224090370885186929*sin((2642680826589289*t)/36028797018963968))/632811179633932117227154240123699200 - (32776373919501099224090370885186929*cos((2642680826589289*t)/36028797018963968))/316405589816966058613577120061849600 + 11358149378044935377074331655830651/105468529938988686204525706687283200))/11 + (198201061994196675*sin((2642680826589289*t)/36028797018963968)*((1405123083739594752*sin((2642680826589289*t)/36028797018963968)*((32776373919501099224090370885186929*sin((2642680826589289*t)/36028797018963968))/1265622359267864234454308480247398400 - (32776373919501099224090370885186929*cos((2642680826589289*t)/36028797018963968))/632811179633932117227154240123699200 + 11358149378044935377074331655830651/210937059877977372409051413374566400))/145347445462410895 + (1405123083739594752*cos((2642680826589289*t)/36028797018963968)*(cos((2642680826589289*t)/36028797018963968)/975 + (2*sin((2642680826589289*t)/36028797018963968))/975))/145347445462410895))/4503599627370496 + (198201061994196675*sin((2642680826589289*t)/36028797018963968)*((36028797018963968*cos((2642680826589289*t)/36028797018963968))/2576613805924556775 + (72057594037927936*sin((2642680826589289*t)/36028797018963968))/2576613805924556775))/1125899906842624 - (198201061994196675*cos((2642680826589289*t)/36028797018963968)*((1032559034367721397915848332348241*sin((2642680826589289*t)/36028797018963968))/2417507703383231123056280731648000 - (1032559034367721397915848332348241*cos((2642680826589289*t)/36028797018963968))/1208753851691615561528140365824000 - 3*cos((2642680826589289*t)/36028797018963968)*((32776373919501099224090370885186929*sin((2642680826589289*t)/36028797018963968))/92832295809916075125361180095283200 - (32776373919501099224090370885186929*cos((2642680826589289*t)/36028797018963968))/46416147904958037562680590047641600 + 11358149378044935377074331655830651/15472049301652679187560196682547200) + (2810246167479189504*cos((2642680826589289*t)/36028797018963968)*((32776373919501099224090370885186929*sin((2642680826589289*t)/36028797018963968))/632811179633932117227154240123699200 - (32776373919501099224090370885186929*cos((2642680826589289*t)/36028797018963968))/316405589816966058613577120061849600 + 11358149378044935377074331655830651/105468529938988686204525706687283200))/145347445462410895 + 3*sin((2642680826589289*t)/36028797018963968)*((36028797018963968*cos((2642680826589289*t)/36028797018963968))/2576613805924556775 + (72057594037927936*sin((2642680826589289*t)/36028797018963968))/2576613805924556775) - (2810246167479189504*sin((2642680826589289*t)/36028797018963968)*((2*cos((2642680826589289*t)/36028797018963968))/975 + (4*sin((2642680826589289*t)/36028797018963968))/975))/145347445462410895 + 11682667931703362103857487676406907/13296292368607771176809544024064000))/4503599627370496 - (9360*sin((2642680826589289*t)/36028797018963968)*((2*cos((2642680826589289*t)/36028797018963968))/975 + (4*sin((2642680826589289*t)/36028797018963968))/975))/11 + 242321889399991089177295329391562660298931712932633/2947996423713040143683963978177867729076559544320)^2 + (10984194987295937899485893556886*cos((2642680826589289*t)/36028797018963968)^2)/10622301927809647241275780768317441 + 2*((10057422861105304*sin((2642680826589289*t)/36028797018963968))/2576613805924556775 - (176530527569188108*cos((2642680826589289*t)/36028797018963968))/2576613805924556775 + 36311847799948152/858871268641518925)^2));
It has substituted in for all variables but t.
Furthermore, if I manually replace all t with a value, it will swap these values of t with the variable t, leaving me in the same place. I can work around this, but it is very slow and frustrating. Any idea how I can force it to substitute t?
##### 2 个评论显示 1更早的评论隐藏 1更早的评论
Jude Diab 2022-4-3
In the original code, yes, but in this segment of code used for debugging, no, I assigned t a value and removed syms.
The full code is a 400 line mess and I don't have a lot of time to tidy it haha. Here is the debugging code I am using:
t=100;
prob = acos(((z0.*cos((2642680826589289.*t)/36028797018963968) + (36028797018963968.*v1(3).*sin((2642680826589289.*t)/36028797018963968))/2642680826589289).^2 + (y0 - (72057594037927936.*v1(1))/2642680826589289 - t.*(3.*v1(2) + (7928042479767867.*x0)/18014398509481984) + (72057594037927936.*v1(1).*cos((2642680826589289.*t)/36028797018963968))/2642680826589289 + sin((2642680826589289.*t)/36028797018963968).*((144115188075855872.*v1(2))/2642680826589289 + 6.*x0)).^2 + ((72057594037927936.*v1(2))/2642680826589289 + 4.*x0 + (36028797018963968.*v1(1).*sin((2642680826589289.*t)/36028797018963968))/2642680826589289 - cos((2642680826589289.*t)/36028797018963968).*((72057594037927936.*v1(2))/2642680826589289 + 3.*x0)).^2 + L1.^2 - L2.^2)./((y0 - (72057594037927936.*v1(1))/2642680826589289 - t.*(3.*v1(2) + (7928042479767867.*x3)/18014398509481984) + (72057594037927936.*v1(1).*cos((2642680826589289.*t)/36028797018963968))/2642680826589289 + sin((2642680826589289.*t)/36028797018963968).*((144115188075855872.*v1(2))/2642680826589289 + 6.*x0)).^2 + 2.*((72057594037927936.*v1(2))/2642680826589289 + 4.*x0 + (36028797018963968.*v1(1).*sin((2642680826589289.*t)/36028797018963968))/2642680826589289 - cos((2642680826589289.*t)/36028797018963968).*((72057594037927936.*v1(2))/2642680826589289 + 3.*x0)).^2 + L1.*(z0.*cos((2642680826589289.*t)/36028797018963968) + (36028797018963968.*v1(3).*sin((2642680826589289.*t)/36028797018963968))/2642680826589289).^2));
[num, den]= numden(prob);
snum = simplify(num);
snolution = simplify(den/num)
As you can see I tried to use the simplify function in an attempt to remove t but it doesn't do this.
and here is the code you can run to obtain the variables that aren't t (I know it contains syms but I understand that should be overruled by assigning it a value the start of the debugging code)
%%
x0 = 0; % [m] , Part0 is onboard so no initial displacement
y0 = 0; % [m] , Part0 is onboard
z0 = 0; % [m] , Part0 is onboard
xdot0 = 0.01; % [m/s]
ydot0 = 0.01; % [m/s]
zdot0 = 0.01; % [m/s]
%% Constants
GravPara = 3.986004418*10^14 ; % [m^3/s^2] , Standard gravitational parameter (Earth)
OrbHeight = 420*10^3 ; % [m] , Height of orbit
w = sqrt(GravPara/(OrbHeight)^3); % [rad/s] , Orbital velocity
L = 14.07; %[m]
%% Assembly details
m1 = 400 ; %[kg] , mass of part1 r
m2 = 3500 ; %[kg] , mass of part2
m3 = 1600 ; %[kg] , mass of part3
m4 = 1000 ;
total_mass = m1+m2+m3+m4;
%% Time
t_prt0 = 0 ; %[s], time elapsed when part1 is added
t_prt1 = (2*L/sqrt(xdot0^2 + ydot0^2 + zdot0^2)); %[s], time elapsed when part2 is added
t_prt2 = (2*2*L/sqrt(xdot0^2 + ydot0^2 + zdot0^2)); %[s], time elapsed when part3 is added
t_prt3 = (3*2*L/sqrt(xdot0^2 + ydot0^2 + zdot0^2)); %[s], time elapsed when part4 is added
t0 = [0:t_prt0]'; %[s], defining time for 1st stage of calc
%t0 = [linspace(0:t_prt0)]';
v0 = (m1/m1).*[xdot0,ydot0,zdot0]; %Part1 dropped into space
x1 = (v0(1)/w)*sin(w.*t0) - (3*x0 + 2*v0(2)/w)*cos(w.*t0)+(4*x0 + 2*v0(2)/w);
y1 = (6*x0 + 4*v0(2)/w)*sin(w*t0)+(2*v0(1)/w)*cos(w*t0)-(6*w*x0 + 3*v0(2))*t0+(y0-2*v0(1)/w);
z1 = z0*cos(w*t0) + (v0(3)/w)*sin(w*t0);
xdot1 = v0(1)*cos(w*t0) + (3*w*x0 + 2*v0(2))*sin(w*t0);
ydot1 = (6*w*x0 + 4*v0(2))*cos(w*t0) - (2*v0(1))*sin(w*t0) - (6*w*x0 + 3*v0(2));
zdot1 = (-w*z0)*sin(w*t0) + v0(3)*cos(w*t0);
t1 = [(t_prt0+1):t_prt1]'; %[s], defining time for 2nd stage of calc
%t1 = [linspace(t_prt0+1,t_prt1)]';
v1 = (m1/(m1+m2)).*[xdot1,ydot1,zdot1]; %Momentum transfer - Part2 added to Part1
x2 = ((v1(1))/w).*sin(w.*t1) - (3.*x1 + 2.*(v1(2))/w).*cos(w.*t1)+(4.*x1 + 2.*(v1(2))/w);
y2 = (6*x1 + 4*v1(2)/w).*sin(w*t1)+(2*v1(1)/w).*cos(w*t1)-(6*w*x1 + 3*v1(2)).*t1+(y1-2*v1(1)/w);
z2 = z1.*cos(w.*t1) + (v1(3)/w).*sin(w.*t1);
xdot2 = v1(1).*cos(w.*t1) + (3*w*x1 + 2*v1(2)).*sin(w.*t1);
ydot2 = (6*w*x1 + 4*v1(2)).*cos(w.*t1) - (2*v1(1)).*sin(w.*t1) - (6*w*x1 + 3*v1(2));
zdot2 = (-w*z1).*sin(w.*t1) + v1(3).*cos(w.*t1);
t2 = [(t_prt1+1):t_prt2]'; %[s], defining time for 3rd stage of calc
%t2 = [linspace(t_prt1+1,t_prt2)]';
v2 = ((m1+m2)/(m1+m2+m3)).*[xdot2,ydot2,zdot2]; %Momentum transfer - Part3 added to Part1&Part2
x3 = (v2(1)/w).*sin(w.*t2) - (3*x2 + 2*v2(2)/w).*cos(w.*t2)+(4*x2 + 2*v2(2)/w);
y3 = (6*x2 + 4*v2(2)/w).*sin(w.*t2)+(2*v2(1)/w).*cos(w.*t2)-(6*w*x2 + 3*v2(2)).*t2+(y2-2*v2(1)/w);
z3 = z2.*cos(w.*t2) + (v2(3)/w).*sin(w.*t2);
xdot3 = v2(1).*cos(w.*t2) + (3*w*x2 + 2*v2(2)).*sin(w.*t2);
ydot3 = (6*w*x2 + 4*v2(2)).*cos(w.*t2) - (2*v2(1)).*sin(w.*t2) - (6*w*x2 + 3*v2(2));
zdot3 = (-w*z2).*sin(w.*t2) + v2(3).*cos(w.*t2);
t3 = [(t_prt2+1):t_prt3]'; %[s], defining time for 3rd stage of calc
%t3 = [linspace(t_prt2+1,t_prt3)]';
v3 = ((m1+m2+m3)/(m1+m2+m3+m4)).*[xdot3,ydot3,zdot3]; %Momentum transfer - Part3 added to Part1&Part2
x4 = (v3(1)/w).*sin(w.*t3) - (3*x3 + 2*v3(2)/w).*cos(w.*t3)+(4*x3 + 2*v3(2)/w);
y4 = (6*x3 + 4*v3(2)/w).*sin(w.*t3)+(2*v3(1)/w).*cos(w.*t3)-(6*w*x3 + 3*v3(2)).*t3+(y3-2*v3(1)/w);
z4 = z3.*cos(w.*t3) + (v3(3)/w).*sin(w.*t3);
xdot4 = v3(1).*cos(w.*t3) + (3*w*x3 + 2*v3(2)).*sin(w.*t3);
ydot4 = (6*w*x3 + 4*v3(2)).*cos(w.*t3) - (2*v3(1)).*sin(w.*t3) - (6*w*x3 + 3*v3(2));
zdot4 = (-w*z3).*sin(w.*t3) + v3(3).*cos(w.*t3);

### 回答（1 个）

Dinesh 2023-9-18
Hi Jude
I understand that you are trying to substitute the value of 't' in the long complicated equation that you have. You can achieve this by using the 'subs' function, which is a symbolic substitution function in MATLAB.
The 'subs' function allows you to substitute the value of a variable in an equation and returns the result. Here is an example to illustrate its usage:
syms a b
% replace a with 4
subs(a + b,a,4)
ans =
% equation = (a * b * b). (a * b) gets replaced with value 5.
subs(a*b^2,a*b,5)
ans =
For more details regarding the subs function please refer the following MATLAB documentation
1. https://in.mathworks.com/help/symbolic/subs.html
Hope this helps.
Best regards
Dinesh Reddy Gatla.

### 类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by