• Remix
  • Share
  • New Entry

on 29 Nov 2023
  • 24
  • 131
  • 0
  • 2
  • 1964
drawframe(1);
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
Write your drawframe function below
function drawframe(f)
persistent L1 L2 L3 L4 L5 L6 L7 L8 t2 t3 t4 t5 t6 ...
Bar2 Bar3 Bar4 Bar5 Bar6 f1 f2
if f==1
L1=5;
L2=2;
L3=6;
L4=4;
L5=6;
L6=4;
L7=5;
L8=1;
f1=@(t2,t)(L2*cos(t2)-(L1+L4*cos(t)))^2+(L2*sin(t2)-L4*sin(t))^2-L3^2;
f2=@(x5,y5,T)[x5+L5*cos(T(1))-(L1+L7+0.9*L6*cos(T(2)))
y5+L5*sin(T(1))-(L8+0.9*L6*sin(T(2)))];
t=linspace(0,2*pi,7);
brown=[206,179,140]/255;
s=sin(t);
c=cos(t);
R=1.15;
wclr=0.7*[1,1,1];
hold on;
fill(R*c,R*s,wclr);
fill(L1+R*c,R*s,wclr);
fill((L1+L7)+R*c,L8+R*s,wclr);
xline(0);
yline(0);
axis equal
t4=acos(((L2+L3)^2-L1^2-L4^2)/(2*L1*L4));
c4=cos(t4);
s4=sin(t4);
t2=asin(sin(t4*L4/(L2+L3)));
c2=cos(t2);
s2=sin(t2);
x3=L2*c2;
y3=L2*s2;
t3=atan2(L4*s4-L2*s2,L1+L4*c4-L2*c2);
x5=L2*c2+(2/3)*((L1+L4*c4)-L2*c2);
y5=L2*s2+(2/3)*(L4*s4-L2*s2);
t56=fsolve(@(T)f2(x5,y5,T),[1.5*pi;1.5*pi]);
t5=t56(1);
t6=t56(2);
Bar2=BarMaker(L2,'g',0);
Bar2.Rotate(t2);
Bar3=BarMaker(L3,'b',[0,0.45]);
Bar3.Rotate(t3);
Bar3.Trans([x3,y3]);
Bar4=BarMaker(L4,'g',0);
Bar4.Rotate(t4);
Bar4.Trans([L1,0]);
Bar5=BarMaker(L5,brown,0.3);
Bar5.Rotate(t5);
Bar5.Trans([x5,y5]);
Bar6=BarMaker(L6,'r',0);
Bar6.Rotate(t6);
Bar6.Trans([L1+L7,L8]);
axis([-2.6,11.4,-5,5])
else
t2=t2+2*pi/48;
c2=cos(t2);
s2=sin(t2);
x3=L2*c2;
y3=L2*s2;
t4=fzero(@(t)f1(t2,t),t4);
c4=cos(t4);
s4=sin(t4);
t3=atan2(L4*s4-L2*s2,L1+L4*c4-L2*c2);
x5=L2*c2+(2/3)*((L1+L4*c4)-L2*c2);
y5=L2*s2+(2/3)*(L4*s4-L2*s2);
t56=fsolve(@(T)f2(x5,y5,T),[t5;t6]);
t5=t56(1);
t6=t56(2);
Bar2.Rotate(t2);
Bar3.Rotate(t3);
Bar3.Trans([x3,y3]);
Bar4.Rotate(t4);
Bar5.Rotate(t5);
Bar5.Trans([x5,y5]);
Bar6.Rotate(t6);
end
end
function B=BarMaker(L,clr,F)
t1=pi/2+linspace(0,pi);
t2=t1+pi;
s1=sin(t1);
s2=sin(t2);
c1=cos(t1);
c2=cos(t2);
Xl=[0.5*c1,L+0.5*c2];
Yl=[0.5*s1,0.5*s2];
X=Xl;
Y=Yl;
Xl=[Xl,Xl(1)];
Yl=[Yl,Yl(1)];
for n=1:numel(F)
f=F(n);
X=[X,f*L,f*L+0.3*[c1,c2],f*L];
Y=[Y,0.5,0.3*[s1,s2],0.5];
Xl=[Xl,nan,f*L+0.3*[c1,c2]];
Yl=[Yl,nan,0.3*[s1,s2]];
end
ax=gca;
H=hgtransform('Parent',ax);
K=hgtransform('Parent',H);
B.line=plot(Xl,Yl,'k','Parent',K);
B.fill=fill(X,Y,clr,'EdgeColor','none','Parent',K);
B.Rotate=@(t)set(K,'Matrix',makehgtform('zrotate',t));
B.Trans=@(p)set(H,'Matrix',makehgtform('translate',[p,0]));
end
Animation
Remix Tree