For loop with Maxima

Multi tool use


For loop with Maxima
for i:1 thru 3 step 1 do;
posix:arithsum(li*cos(ri(t))),1,i-1)+(li*cos(ri(t))/2);
posiy:arithsum(li*sin(ri(t))),1,i-1)+(li*sin(ri(t))/2);
What I wanna do is to get 6 position functions(3 x and 3 y). It should give me values like following:
pos1x:l1*cos(r1(t))/2;
pos2x:l1*cos(r1(t))+l2*cos(r2(t))/2;
pos3x:l1*cos(r1(t))+l2*cos(r2(t))+l3*cos(r3(t))/2;
So, why my code is not working?
1 Answer
1
Couple of things here. (1) for
loop takes just one expression as its loop body; typically multiple expressions are combined into one as (e1, e2, e3)
or block(e1, e2, e3)
. Note that for ... do;
isn't correct syntax, since it doesn't have a loop body -- the semicolon terminates the for
expression. Note also that expressions in the body are separated by commas, not semicolons. (2) You can use subscript notation to index items; Maxima won't automatically construct symbol names such as pos1x
. Instead, use subscript notation: posx[1]
, posy[i]
, etc.
for
(e1, e2, e3)
block(e1, e2, e3)
for ... do;
for
pos1x
posx[1]
posy[i]
Given that, here's a solution.
(%i1) load (functs);
(%o1) /Applications/Maxima.app/Contents/Resources/opt/share/maxima/5.41.0/shar
e/simplification/functs.mac
(%i2) for i:1 thru 3 step 1 do
(posx[i]:arithsum(l[i]*cos(r[i](t)),1,i-1)+(l[i]*cos(r[i](t))/2),
posy[i]:arithsum(l[i]*sin(r[i](t)),1,i-1)+(l[i]*sin(r[i](t))/2));
(%o2) done
(%i3) [posx[1], posx[2], posx[3]];
l cos(r (t)) 3 l cos(r (t)) l cos(r (t))
1 1 2 2 1 3 3
(%o3) [-------------, ---------------, 2 (l cos(r (t)) + -) + -------------]
2 2 3 3 2 2
(%i4) [posy[1], posy[2], posy[3]];
l sin(r (t)) 3 l sin(r (t)) l sin(r (t))
1 1 2 2 1 3 3
(%o4) [-------------, ---------------, 2 (l sin(r (t)) + -) + -------------]
2 2 3 3 2 2
I am guessing that l[i]
and r[i]
should be subscripted too. I changed the parentheses in order to fix a syntax problem; if you intended something else, of course you can go ahead and change it again.
l[i]
r[i]
Note that in this formulation posx
and posy
are so-called undeclared arrays. Undeclared arrays are suitable for representing subscripted symbolic variables. You can get the list of elements via listarray
.
posx
posy
listarray
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.