MATLAB: How can i minimize computaion time

computation timeminimize

%Plots the displacement , velocity , acceleration and jerk of a Cam follower with cycloidal motion
L=10;
beta=120;
for theta=0:0.2:120
y=L*((theta/beta)-(sin(2*pi*theta/beta)/(2*pi)));
y1=(L/beta)*(1-cos(2*pi*theta/beta));
y2=(2*pi*L/beta^2)*sin(2*pi*theta/beta);
y3=((4*pi^2*L)/(beta^3))*cos(2*pi*theta/beta);
figure(1)
subplot(2,1,1)
plot(theta,y)
hold on
subplot(2,1,2)
plot(theta,y1)
hold on
figure(2)
subplot(2,1,1)
plot(theta,y2)
hold on
subplot(2,1,2)
plot(theta,y3)
hold on
end
for theta=120:0.2:190
y=L*sin(pi/2);
y1=(L/beta)*(1-cos(2*pi));
y2=(2*pi*L/beta^2)*sin(2*pi);
y3=((4*pi^2*L)/(beta^3))*cos(pi/2);
figure (1)
subplot(2,1,1)
plot(theta,y)
subplot(2,1,2)
plot(theta,y1)
figure(2)
subplot(2,1,1)
plot(theta,y2)
subplot(2,1,2)
plot(theta,y3)
end
beta=90;
for theta=190:0.2:280
omega=theta-190;
y=L*(1-(omega/beta)+(sin(2*pi*omega/beta)/(2*pi)));
y1=(-L/beta)*(1-cos(2*pi*omega/beta));
y2=(-2*pi*L/beta^2)*sin(2*pi*omega/beta);
y3=((-4*pi^2*L)/(beta^3))*cos(2*pi*omega/beta);
figure(1)
subplot(2,1,1)
plot(theta,y)
subplot(2,1,2)
plot(theta,y1)
figure(2)
subplot(2,1,1)
plot(theta,y2)
subplot(2,1,2)
plot(theta,y3)
end
for theta=280:0.2:360
y=L*sin(pi);
y1=(L/beta)*(1-cos(2*pi));
y2=(2*pi*L/beta^2)*sin(2*pi);
y3=((4*pi^2*L)/(beta^3))*cos(pi/2);
figure(1)
subplot(2,1,1)
plot(theta,y)
subplot(2,1,2)
plot(theta,y1)
figure(2)
subplot(2,1,1)
plot(theta,y2)
subplot(2,1,2)
plot(theta,y3)
end
figure(1)
subplot(2,1,1)
xlabel('\theta');
ylabel('Y');
title('Displacement');
subplot(2,1,2)
xlabel('\theta');
ylabel('Y1');
title('1st Derivative(Velocity)');
figure(2)
subplot(2,1,1)
xlabel('\theta');
ylabel('Y2');
title('2nd derivative(Acceleration)');
subplot(2,1,2)
xlabel('\theta');
ylabel('Y3');
title('3rd Derivative(Jerk)');
[EDITED, Please format your code, thanks]

Best Answer

The actual computations are very fast in your case. The problem ist the iterative creation of the diagrams, which consume more than 99% of the time.
You can omit the FOR loop and define theta as vector instead:
theta = 0:0.2:120;
Remove the end also. The rest of the code does not need changes and the code runs in less than a second.