How can i minimize computaion time ?

2 views (last 30 days)
Hesham
Hesham on 17 May 2014
Answered: Sylvan Ransom on 8 Jun 2017
%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]
  1 Comment
dpb
dpb on 17 May 2014
Format your code to be legible. Add two blanks in front of first line and reintroduce line breaks where they belong. Keep at it until looks right in preview window. Don't put extra blank lines in; if do need one, reinsert the leading double-blanks to get back to code formatting (yes, I know, it's a pita; but TMW in their infinite wisdom uses a textbox w/ default word-wrap 'on' for a code forum... :( )

Sign in to comment.

Accepted Answer

Jan
Jan on 17 May 2014
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.
  1 Comment
Hesham
Hesham on 17 May 2014
Thank you very much , that was really helpful .

Sign in to comment.

More Answers (1)

Sylvan Ransom
Sylvan Ransom on 8 Jun 2017
One thing you can do to speed up your code would be to consolidate the constants you use in your for loop to decrease the number of computations Matlab needs to perform. When you write 2*pi/beta, Matlab will multiply 2 times pi and divide by beta for every iteration of the 4 loop, meaning it's doing the exact same calculation 600 times and getting the same answer, which eats up unnecessary computation time. To prevent this extra computing, try defining a variable that stands for the product of these constants. For example, before your for loop, define something like abbrev1 = 2*pi/beta;, and then in your for loop, replace 2*pi/beta with abbrev1. Matlab will already know the value of this and won't need to compute it over and over again. Do this for all of your expressions made up of pure constants and matlab will execute your code much more quickly.

Categories

Find more on Price and Analyze Financial Instruments 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!