View difference between Paste ID: adDfzz96 and 87cuwG6S
SHOW: | | - or go back to the newest paste.
1
function quadruplependulumsim
2-
close all
2+
    t = 0:0.001:60;
3-
t=0:0.001:60;
3+
    g = 9.81;
4-
g=9.81;
4+
    m1 = 1;
5-
m1=1;
5+
    m2 = 1;
6-
m2=1;
6+
    m3 = 1;
7-
m3=1;
7+
    m4 = 1;
8-
m4=1;
8+
    l1 = 1.6;
9-
l1=1.6;
9+
    l2 = 1.4;
10-
l2=1.4;
10+
    l3 = 1.2;
11-
l3=1.2;
11+
    l4 = 1;
12-
l4=1;
12+
13-
theta1_initial = 0.2;
13+
    % initial solution: [theta1, theta1dot, theta2, theta2dot, theta3, theta3dot, theta4, theta4dot]
14-
theta1dot_initial = 0;
14+
    x0 = [0.2, 0, 0.4, 0, 0.6, 0, 0.8, 0];
15-
theta2_initial = 0.4;
15+
    [t, x] = ode45(@rhs, t, x0);
16-
theta2dot_initial = 0;
16+
17-
theta3_initial = 0.6;
17+
    figure(1)
18-
theta3dot_initial = 0;
18+
    plot(t, x(:,[1 3 5 7]))
19-
theta4_initial = 0.8;
19+
    xlabel('t (sec)')
20-
theta4dot_initial = 0;
20+
    ylabel('Angle (rad)')
21-
[t,x]=ode45(@rhs,t,[theta1_initial theta1dot_initial theta2_initial theta2dot_initial theta3_initial theta3dot_initial theta4_initial theta4dot_initial]);
21+
22-
plot(t,x(:,1),t,x(:,3),t,x(:,5),t,x(:,7));
22+
    l = [l1, l2, l3, l4];
23-
xlabel('t (s)'),ylabel('Angle (rad)');
23+
    X = cumsum(bsxfun(@times, sin(x(:,[1 3 5 7])), l), 2);
24-
%pause
24+
    Y = cumsum(bsxfun(@times, cos(x(:,[1 3 5 7])), l), 2);
25-
    function f = rhs(t,x)
25+
26
    isHG2 = ~verLessThan('matlab', '8.4');
27
28
    figure(2)
29
    clr = 'mcgb';
30
    opts = {'LineStyle','none', 'Marker','.', 'MarkerSize',1}; % 6
31
    for i=4:-1:1
32
        if isHG2
33
            hLines(i) = animatedline('Color',clr(i), opts{:});
34-
        f = [f_1;f_2;f_3;f_4;f_5;f_6;f_7;f_8];
34+
        else
35
            hLines(i) = line(NaN, NaN, 'Color',clr(i), opts{:});
36
        end
37-
X1 = l1*sin(x(:,1));
37+
38-
Y1 = l1*cos(x(:,1));
38+
    hLine = line(NaN, NaN, ...
39-
X2 = X1 + l2*sin(x(:,3));
39+
        'LineWidth',1, 'Marker','o', 'Color','r', 'MarkerFaceColor','r');
40-
Y2 = Y1 + l2*cos(x(:,3));
40+
    axis equal
41-
X3 = X2 + l3*sin(x(:,5));
41+
    axis([-1 1 -1 1] * sum(l))
42-
Y3 = Y2 + l3*cos(x(:,5));
42+
43-
X4 = X3 + l4*sin(x(:,7));
43+
    for n=1:10:size(X,1)
44-
Y4 = Y3 + l4*cos(x(:,7));
44+
        for i=1:4
45-
X0 = 0*X1;
45+
            if isHG2
46-
Y0 = 0*Y1;
46+
                addpoints(hLines(i), X(n,i), Y(n,i));
47-
X = [X0 X1 X2 X3 X4];
47+
            else
48-
Y = [Y0 Y1 Y2 Y3 Y4];
48+
                set(hLines(i), 'XData',X(1:10:n,i), 'YData',Y(1:10:n,i));
49
            end
50-
for n=1:10:50000
50+
        end
51
        set(hLine, 'XData',[0 X(n,:)], 'YData',[0 Y(n,:)]);
52-
      P = plot(X(n,:),Y(n,:),'ro-');
52+
        drawnow
53-
      set(P,'LineWidth',1);
53+
54-
      set(P,'MarkerFaceColor','r');
54+
55-
      plot(X4(n),Y4(n),'b.');
55+
    function f = rhs(t, x)
56-
      plot(X3(n),Y3(n),'g.');
56+
57-
      plot(X2(n),Y2(n),'c.');
57+
58-
      plot(X1(n),Y1(n),'m.');
58+
59-
      hold on
59+
60-
      pbaspect([1 1 1])
60+
61-
      xlim([-(l1+l2+l3+l4) (l1+l2+l3+l4)])
61+
62-
      ylim([-(l1+l2+l3+l4) (l1+l2+l3+l4)]) 
62+
63-
      pause(0.001)
63+
64-
      delete(P)
64+
        f = [f_1; f_2; f_3; f_4; f_5; f_6; f_7; f_8];
65
    end
66-
end
66+
67
end