How to make this crank slide animation do 3 revolutions?
    5 views (last 30 days)
  
       Show older comments
    
I need to be able to use a while loop and some if  statements to make this animation of this crank slider do 3 revolutions while still keeping the same amount of data displayed in the command window but I do not know exactly what to do for this while loop and what I have tried has not been working. Any help would be awesome.
p01fig = figure;
r = 2;
d = 5;
t = mylinspace(0,2*pi,45);
[x,y] = mycircle(r,t);
Ax = 0;
Ay = 0;
Bx = 0;
By = r*sin(t) + d * cos((r*cos(t))/d);
Cx = r*cos(t);
Cy = r*sin(t);
[A,B,C] = myangle(Ax, Ay, Bx, By, Cx, Cy);
hold on;
plot(x,y,'-g');
box on;
axis('square',[-6 6 -4 9]);
i = 1;
n = length(t);
while i <= n
    p1 = plot(x(i), y(i), 'Marker','o', 'MarkerFaceColor', 'g', 'MarkerSize',7);
    p2 = plot(Ax,Ay, 'Marker','o', 'MarkerFaceColor', 'b', 'MarkerSize',7);
    p3 = plot(Bx, By(i),'Marker','o', 'MarkerFaceColor', 'r', 'MarkerSize',7);
    p4 = plot([Cx(i) Ax], [y(i) By(i)], '-b');
    p5 = plot([Ax Cx(i)], [Ay Cy(i)], '-b');
    [A,B,C] = myangle(Ax, Ay, Bx, By(i), Cx(i), Cy(i));
    p01(i,1) = A;
    p01(i,2) = B;
    p01(i,3) = C;
    p01(i,4) = A + B + C;
    s1 = 'Slider Height  ';
    s2 = num2str(By(i), '%0.2f');
    sa = [s1 s2];
    if A < 10
        s3 = '  A (in Deg)   ';
    elseif A < 100
        s3 = '  A (in Deg)  ';
    else
        s3 = '  A (in Deg) ';
    end
    s4 = num2str(A, '%0.2f');
    sb = [s3 s4];
    if B < 10
        s5 = '  B (in Deg)   ';
    elseif A < 100
        s5 = '  B (in Deg)  ';
    else
        s5 = '  B (in Deg)  ';
    end
    s6 = num2str(B, '%0.2f');
    sc = [s5, s6];
    if C < 10
        s7 = '  C (in Deg)   ';
    elseif C < 100
        s7 = '  C (in Deg)  ';
    else
        s7 = '  C (in Deg) ';
    end
    s8 = num2str(C, '%0.2f');
    sd = [s7, s8];
    s9 = 'sum([A,B,C]) ';
    s10 = num2str(A+B+C, '%0.2f');
    se = [s9, s10];
    tx1 = text(1,5.5,sa,'Fontname', 'FixedWidth', 'FontSize',10);
    tx2 = text(1,5,sb,'Fontname', 'FixedWidth', 'FontSize',10);
    tx3 = text(1,4.5,sc,'Fontname', 'FixedWidth', 'FontSize',10);
    tx4 = text(1,4,sd,'Fontname', 'FixedWidth', 'FontSize',10);
    tx5 = text(1,3.5,se,'Fontname', 'FixedWidth', 'FontSize',10);
    drawnow();
    pause(.05);
    i = i + 1;
    if i <= n
        delete([p1 p2 p3 p4 p5 tx1 tx2 tx3 tx4 tx5]);
    end
end
display(p01);
function [y] = mylinspace(lo,hi,n)
if n <= 0
    y = [];
else
    y(1) = lo;
    y(n) = hi;
    i = 2;
    v = (hi-lo)/(n-1);
    while i < n
        y(i) = y(i-1) + v;
        i = i + 1;
    end
end
end
function [x,y] = mycircle(radius, theta)
n = length(theta);
if n < 1
    x = [];
    y = [];
else
    x = zeros(1,n);
    y = zeros(1,n);
    i = 1;
    while i<=n
        x(i) = radius * cos(theta(i));
        y(i) = radius * sin(theta(i));
        i = i + 1;
    end   
end
end
function [A,B,C] = myangle(Ax, Ay, Bx, By, Cx, Cy)
a = sqrt(((Bx - Ax).^2)+((By - Ay).^2)); % Distance Formula
c = sqrt(((Cx - Ax).^2)+((Cy - Ay).^2)); % Length of c is Always d = 5
b = sqrt(((Bx - Cx).^2)+((By - Cy).^2)); % Same with b
cv = ((b.^2)+(c.^2)-(a.^2))./(2.*b.*c);  % Law of Cosines
bv = ((a.^2)+(c.^2)-(b.^2))./(2.*a.*c);
av = ((a.^2)+(b.^2)-(c.^2))./(2.*b.*a);
C = acosd(cv);
A = acosd(bv);
B = acosd(av);
end
0 Comments
Accepted Answer
  Maik
      
 on 4 Nov 2022
        
      Edited: Maik
      
 on 4 Nov 2022
  
      It can be done using two "for" statements.
p01fig = figure;
r = 2;
d = 5;
t = mylinspace(0,2*pi,45);
[x,y] = mycircle(r,t);
Ax = 0;
Ay = 0;
Bx = 0;
By = r*sin(t) + d * cos((r*cos(t))/d);
Cx = r*cos(t);
Cy = r*sin(t);
[A,B,C] = myangle(Ax, Ay, Bx, By, Cx, Cy);
hold on;
plot(x,y,'-g');
box on;
axis('square',[-6 6 -4 9]);
i = 1;
n = length(t);
% while i <= n
numOfRev = 3; 
for j = 1:numOfRev
    for i = 1:n
        p1 = plot(x(i), y(i), 'Marker','o', 'MarkerFaceColor', 'g', 'MarkerSize',7);
        p2 = plot(Ax,Ay, 'Marker','o', 'MarkerFaceColor', 'b', 'MarkerSize',7);
        p3 = plot(Bx, By(i),'Marker','o', 'MarkerFaceColor', 'r', 'MarkerSize',7);
        p4 = plot([Cx(i) Ax], [y(i) By(i)], '-b');
        p5 = plot([Ax Cx(i)], [Ay Cy(i)], '-b');
        [A,B,C] = myangle(Ax, Ay, Bx, By(i), Cx(i), Cy(i));
        p01(i,1) = A;
        p01(i,2) = B;
        p01(i,3) = C;
        p01(i,4) = A + B + C;
        s1 = 'Slider Height  ';
        s2 = num2str(By(i), '%0.2f');
        sa = [s1 s2];
        if A < 10
            s3 = '  A (in Deg)   ';
        elseif A < 100
            s3 = '  A (in Deg)  ';
        else
            s3 = '  A (in Deg) ';
        end
        s4 = num2str(A, '%0.2f');
        sb = [s3 s4];
        if B < 10
            s5 = '  B (in Deg)   ';
        elseif A < 100
            s5 = '  B (in Deg)  ';
        else
            s5 = '  B (in Deg)  ';
        end
        s6 = num2str(B, '%0.2f');
        sc = [s5, s6];
        if C < 10
            s7 = '  C (in Deg)   ';
        elseif C < 100
            s7 = '  C (in Deg)  ';
        else
            s7 = '  C (in Deg) ';
        end
        s8 = num2str(C, '%0.2f');
        sd = [s7, s8];
        s9 = 'sum([A,B,C]) ';
        s10 = num2str(A+B+C, '%0.2f');
        se = [s9, s10];
        tx1 = text(1,5.5,sa,'Fontname', 'FixedWidth', 'FontSize',10);
        tx2 = text(1,5,sb,'Fontname', 'FixedWidth', 'FontSize',10);
        tx3 = text(1,4.5,sc,'Fontname', 'FixedWidth', 'FontSize',10);
        tx4 = text(1,4,sd,'Fontname', 'FixedWidth', 'FontSize',10);
        tx5 = text(1,3.5,se,'Fontname', 'FixedWidth', 'FontSize',10);
        drawnow();
        pause(.05);
        %     i = i + 1;
        if i <= n
            delete([p1 p2 p3 p4 p5 tx1 tx2 tx3 tx4 tx5]);
        else
            break;
        end
    end
end
display(p01);
function [y] = mylinspace(lo,hi,n)
if n <= 0
    y = [];
else
    y(1) = lo;
    y(n) = hi;
    i = 2;
    v = (hi-lo)/(n-1);
    while i < n
        y(i) = y(i-1) + v;
        i = i + 1;
    end
end
end
function [x,y] = mycircle(radius, theta)
n = length(theta);
if n < 1
    x = [];
    y = [];
else
    x = zeros(1,n);
    y = zeros(1,n);
    i = 1;
    while i<=n
        x(i) = radius * cos(theta(i));
        y(i) = radius * sin(theta(i));
        i = i + 1;
    end
end
end
function [A,B,C] = myangle(Ax, Ay, Bx, By, Cx, Cy)
a = sqrt(((Bx - Ax).^2)+((By - Ay).^2)); % Distance Formula
c = sqrt(((Cx - Ax).^2)+((Cy - Ay).^2)); % Length of c is Always d = 5
b = sqrt(((Bx - Cx).^2)+((By - Cy).^2)); % Same with b
cv = ((b.^2)+(c.^2)-(a.^2))./(2.*b.*c);  % Law of Cosines
bv = ((a.^2)+(c.^2)-(b.^2))./(2.*a.*c);
av = ((a.^2)+(b.^2)-(c.^2))./(2.*b.*a);
C = acosd(cv);
A = acosd(bv);
B = acosd(av);
end
More Answers (0)
See Also
Categories
				Find more on 2-D and 3-D Plots 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!