How should I call animation from classdef?

11 views (last 30 days)
ryunosuke tazawa
ryunosuke tazawa on 27 Oct 2021
Edited: Walter Roberson on 4 Nov 2024 at 1:13
I want to know the way to call the animation from classdef.
I defined the animatedline in classdef, and I called the animation to other code from the classdef.
But it did not showed the animation.
close all;
Length1 = 1.0;
Length2 = 1.0;
Mass1 = 1.0;
Mass2 = 1.0;
Ts = 0.01;
Theta_Initial1 = 0;
Theta_Initial2 = 0;
AngularVelocity_Initial1 = 0;
AngularVelocity_Initial2 = 0;
Pendulum2 = classDoublePendulum(Length1,Length2,Mass1,Mass2,Theta_Initial1,Theta_Initial2,AngularVelocity_Initial1,AngularVelocity_Initial2,Ts);
Trq = [10; 10];
for i=1:100
Pendulum2.Dstep(Trq); % call classdef but animation did not work.
end
Please someone help me , teach me the way.
classdef classDoublePendulum < handle
properties
Length1 = 1;
Length2 = 1;
Mass1 = 1;
Mass2 = 1;
AngularVelocity1 = 0;
AngularAcceleration1 = 0;
AngularVelocity2 = 0;
AngularAcceleration2 = 0;
Theta1 = 0;
Theta2 = 0;
Ts = 0.01;
X_Position1;
Y_Position1;
X_Position2;
Y_Position2;
Ball_Arrival = 0;
num1_1;
num1_2;
num1_3;
num1_4;
den1;
num2_1;
num2_2;
num2_3;
num2_4;
den2;
end
properties(Access = private)
Gravity = 9.8;
Pendulum_Double;
Curve;
Range;
end
methods
function obj = classDoublePendulum(Length1,Length2, Mass1,Mass2, Theta1, Theta2, AngularVelocity1, AngularVelocity2, Ts)
obj.Length1 = Length1;
obj.Length2 = Length2;
obj.Mass1 = Mass1;
obj.Mass2 = Mass2;
obj.Theta1 = Theta1;
obj.Theta2 = Theta2;
obj.AngularVelocity1 = AngularVelocity1;
obj.AngularVelocity2 = AngularVelocity2;
obj.Ts = Ts;
obj.X_Position1 = obj.Length1 * sin(obj.Theta1);
obj.Y_Position1 = -obj.Length1 * cos(obj.Theta1);
obj.X_Position2 = obj.X_Position1 + obj.Length2 * sin(obj.Theta1 + obj.Theta2);
obj.Y_Position2 = obj.Y_Position1 - obj.Length2 * cos(obj.Theta1 + obj.Theta2);
obj.Ball_Arrival = obj.X_Position2 + +abs(obj.AngularVelocity2)*(sqrt(2*abs(obj.Y_Position2)/obj.Gravity)); % ボールの距離
% animationcall
obj.Pendulum_Double = animatedline('Color','b','Marker','o','MarkerSize',10,'MarkerFaceColor','b','LineWidth',3);
obj.Curve = animatedline('Color','w','LineStyle','-','Marker','none','Linewidth',2);
obj.Range = [(obj.Length1 + obj.Length2)*(-2), (obj.Length1 + obj.Length2) * 2];
axis equal; axis off;
set(gca, 'Xlim', obj.Range, 'Ylim',obj.Range);
set(gcf, 'Color', 'w');
end
function Dstep(obj,tau)
obj.num1_1 = -obj.Gravity * (2 * obj.Mass1 + obj.Mass2) * sin(obj.Theta1);
obj.num1_2 = -obj.Mass2 * obj.Gravity * sin(obj.Theta1 - 2 * obj.Theta2);
obj.num1_3 = -2 * sin(obj.Theta1 - obj.Theta2) * obj.Mass2;
obj.num1_4 = obj.AngularVelocity2^2 * obj.Length2 + obj.AngularVelocity1^2 * obj.Length1 * cos(obj.Theta1 - obj.Theta2);
obj.den1 = obj.Length1 * (2 * obj.Mass1 + obj.Mass2 - obj.Mass2 * cos(2 * obj.Theta1 - 2 * obj.Theta2));
obj.AngularAcceleration1 = (obj.num1_1 + obj.num1_2 + obj.num1_3 * obj.num1_4)/obj.den1;
obj.num2_1 = 2 * sin(obj.Theta1 - obj.Theta2);
obj.num2_2 = obj.AngularVelocity1^2 * obj.Length1 * (obj.Mass1 + obj.Mass2);
obj.num2_3 = obj.Gravity * (obj.Mass1 + obj.Mass2 ) * cos(obj.Theta1);
obj.num2_4 = obj.AngularVelocity2^2 * obj.Length2 * obj.Mass2 * cos(obj.Theta1 - obj.Theta2);
obj.den2 = obj.Length2 * (2 * obj.Mass1 + obj.Mass2 - obj.Mass2 * cos(2 * obj.Theta1 - 2*obj.Theta2));
obj.AngularAcceleration2 = (obj.num2_1 * (obj.num2_2 + obj.num2_3 + obj.num2_4))/obj.den2;
obj.AngularVelocity1 = obj.AngularVelocity1 + obj.AngularAcceleration1 * obj.Ts;
obj.AngularVelocity2 = obj.AngularVelocity2 + obj.AngularAcceleration2 * obj.Ts;
obj.Theta1 = obj.Theta1 + obj.AngularVelocity1 * obj.Ts;
obj.Theta2 = obj.Theta2 + obj.AngularVelocity2 * obj.Ts;
obj.X_Position1 = obj.Length1 * sin(obj.Theta1);
obj.Y_Position1 = -obj.Length1 * cos(obj.Theta1);
obj.X_Position2 = obj.X_Position1 + obj.Length2 * sin(obj.Theta1 + obj.Theta2);
obj.Y_Position2 = obj.Y_Position1 - obj.Length2 * cos(obj.Theta1 + obj.Theta2);
obj.Ball_Arrival = obj.X_Position2 + +abs(obj.AngularVelocity2)*(sqrt(2*abs(obj.Y_Position2)/obj.Gravity));
% animation call and upadate
obj.Curve.addpoints(obj.X_Position2, obj.Y_Position2);
drawnow limitrate
obj.Pendulum_Double.addpoints([0 obj.X_Position1 obj.X_Position2], [0 obj.Y_Position1 obj.Y_Position2]);
clearpoints(obj.Pendulum_Double);
end
end
end

Answers (1)

Pavl M.
Pavl M. on 14 Oct 2024
Edited: Walter Roberson on 4 Nov 2024 at 1:13
This answer was flagged by Steven Lord
classdef classDoublePendulum %< handle
properties
Length1 = 1;
Length2 = 1;
Mass1 = 1;
Mass2 = 1;
AngularVelocity1 = 0;
AngularAcceleration1 = 0;
AngularVelocity2 = 0;
AngularAcceleration2 = 0;
Theta1 = 0;
Theta2 = 0;
Ts = 0.01;
X_Position1;
Y_Position1;
X_Position2;
Y_Position2;
Ball_Arrival = 0;
num1_1;
num1_2;
num1_3;
num1_4;
den1;
num2_1;
num2_2;
num2_3;
num2_4;
den2;
end
properties(Access = private)
Gravity = 9.8;
Pendulum_Double =animatedline('Color','g','Marker','+','MarkerSize',8,'MarkerFaceColor','b','LineWidth',2);
Curve = animatedline('Color','g','Marker','+','MarkerSize',8,'MarkerFaceColor','b','LineWidth',2);
Range =0;
end
methods
function obj = classDoublePendulum(Length1,Length2, Mass1,Mass2, Theta1, Theta2, AngularVelocity1, AngularVelocity2, Ts)
obj.Length1 = Length1;
obj.Length2 = Length2;
obj.Mass1 = Mass1;
obj.Mass2 = Mass2;
obj.Theta1 = Theta1;
obj.Theta2 = Theta2;
obj.AngularVelocity1 = AngularVelocity1;
obj.AngularVelocity2 = AngularVelocity2;
obj.Ts = Ts;
obj.X_Position1 = obj.Length1 * sin(obj.Theta1);
obj.Y_Position1 = -obj.Length1 * cos(obj.Theta1);
obj.X_Position2 = obj.X_Position1 + obj.Length2 * sin(obj.Theta1 + obj.Theta2);
obj.Y_Position2 = obj.Y_Position1 - obj.Length2 * cos(obj.Theta1 + obj.Theta2);
obj.Ball_Arrival = obj.X_Position2 + +abs(obj.AngularVelocity2)*(sqrt(2*abs(obj.Y_Position2)/obj.Gravity)); % ボールの離
% animation call
obj.Pendulum_Double = animatedline('Color','b','Marker','o','MarkerSize',10,'MarkerFaceColor','b','LineWidth',3);
obj.Curve = animatedline('Color','w','LineStyle','-','Marker','none','Linewidth',2);
drawnow limitrate
obj.Range = [(obj.Length1 + obj.Length2)*(-2), (obj.Length1 + obj.Length2) * 2];
axis equal; axis off;
set(gca, 'Xlim', obj.Range, 'Ylim',obj.Range);
set(gcf, 'Color', 'w');
end
function Dstep(obj,tau)
obj.num1_1 = -obj.Gravity * (2 * obj.Mass1 + obj.Mass2) * sin(obj.Theta1);
obj.num1_2 = -obj.Mass2 * obj.Gravity * sin(obj.Theta1 - 2 * obj.Theta2);
obj.num1_3 = -2 * sin(obj.Theta1 - obj.Theta2) * obj.Mass2;
obj.num1_4 = obj.AngularVelocity2^2 * obj.Length2 + obj.AngularVelocity1^2 * obj.Length1 * cos(obj.Theta1 - obj.Theta2);
obj.den1 = obj.Length1 * (2 * obj.Mass1 + obj.Mass2 - obj.Mass2 * cos(2 * obj.Theta1 - 2 * obj.Theta2));
obj.AngularAcceleration1 = (obj.num1_1 + obj.num1_2 + obj.num1_3 * obj.num1_4)/obj.den1;
obj.num2_1 = 2 * sin(obj.Theta1 - obj.Theta2);
obj.num2_2 = obj.AngularVelocity1^2 * obj.Length1 * (obj.Mass1 + obj.Mass2);
obj.num2_3 = obj.Gravity * (obj.Mass1 + obj.Mass2 ) * cos(obj.Theta1);
obj.num2_4 = obj.AngularVelocity2^2 * obj.Length2 * obj.Mass2 * cos(obj.Theta1 - obj.Theta2);
obj.den2 = obj.Length2 * (2 * obj.Mass1 + obj.Mass2 - obj.Mass2 * cos(2 * obj.Theta1 - 2*obj.Theta2));
obj.AngularAcceleration2 = (obj.num2_1 * (obj.num2_2 + obj.num2_3 + obj.num2_4))/obj.den2;
obj.AngularVelocity1 = obj.AngularVelocity1 + obj.AngularAcceleration1 * obj.Ts;
obj.AngularVelocity2 = obj.AngularVelocity2 + obj.AngularAcceleration2 * obj.Ts;
obj.Theta1 = obj.Theta1 + obj.AngularVelocity1 * obj.Ts;
obj.Theta2 = obj.Theta2 + obj.AngularVelocity2 * obj.Ts;
obj.X_Position1 = obj.Length1 * sin(obj.Theta1);
obj.Y_Position1 = -obj.Length1 * cos(obj.Theta1);
obj.X_Position2 = obj.X_Position1 + obj.Length2 * sin(obj.Theta1 + obj.Theta2);
obj.Y_Position2 = obj.Y_Position1 - obj.Length2 * cos(obj.Theta1 + obj.Theta2);
obj.Ball_Arrival = obj.X_Position2 + +abs(obj.AngularVelocity2)*(sqrt(2*abs(obj.Y_Position2)/obj.Gravity));
% animation call & update
obj.Curve.addpoints(obj.X_Position2, obj.Y_Position2);
drawnow limitrate
obj.Pendulum_Double.addpoints([0 obj.X_Position1 obj.X_Position2], [0 obj.Y_Position1 obj.Y_Position2]);
clearpoints(obj.Pendulum_Double);
end
end
end
Length1 = 1.0;
Length2 = 1.0;
Mass1 = 1.0;
Mass2 = 1.0;
Ts = 0.01;
Theta_Initial1 = 0;
Theta_Initial2 = 0;
AngularVelocity_Initial1 = 0;
AngularVelocity_Initial2 = 0;
Pendulum2 = classDoublePendulum(Length1,Length2,Mass1,Mass2,Theta_Initial1,Theta_Initial2,AngularVelocity_Initial1,AngularVelocity_Initial2,Ts);
Trq = [10; 10];
for i=1:100
Pendulum2.Dstep(Trq); % classdef has been called but animation has not been working.
end

Categories

Find more on Animation 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!