Quick Problem - Force MATLAB to substitute assigned variable values
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Dinesh
on 18 Sep 2023
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)
% equation = (a * b * b). (a * b) gets replaced with value 5.
subs(a*b^2,a*b,5)
For more details regarding the subs function please refer the following MATLAB documentation
Hope this helps.
Best regards
Dinesh Reddy Gatla.
0 Comments
See Also
Categories
Find more on Symbolic Math Toolbox 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!