Calculating cpu usage when running matlab program.

Hi there,
Would this code be correct in giving me the cpu usage?
close all; clear;
tstart = tic;
initime = cputime;
prompt = ('Please enter time step');
result = input(prompt);
tstep = result;
%define known variables
g = 9.81; c = 12.5; m = 70;
%define analytic time vector
t = 0 : tstep : 10;
%get analytic velocity using equation from lectures
v_analytic = (g*m/c)*(1-exp(-c*t/m));
%set up plot and plot analytic velocity
figure(1); hold on; grid on; box on;
plot(t,v_analytic,'b*-')
title('parachutist');
xlabel('time (s)');
ylabel('velocity (m/s)');
%do numerical calculations here using a 'for' loop
v1 = 0;
N = length(t);
v(1)=0;
for i = 2 : N;
v(i) = v(i-1) + (g-c*v(i-1)/m)*(tstep);
end
%numerical results
plot(t,v,'ro-')
legend('analytical','numerical')
%do error calculations at 5s here
Eabs = abs(v_analytic(5) - v(5));
Erel = Eabs/v_analytic(5);
fintime = cputime;
telapsed = toc(tstart);
display(tstep, 'tsize');
display(Eabs,'Absolute error');
display(Erel,'Relative error');
display((fintime -initime)/telapsed,'CPU usage (%):');
I think this is correct. I want to establish the cpu usage under different input conditions (stepsizes) as if the stepsize is smaller then more calculations are made by the program.

댓글 수: 1

You cannot bold formatted code. If you want to bring attention to particular code lines, add a comment to them.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 25일

0 개 추천

It is recommended that you use timeit() . If you have an older MATLAB that does not have that built in, you can find it in the File Exchange.

댓글 수: 3

like this?
%set up plot and plot analytic velocity
figure(1); hold on; grid on; box on;
plot(t,v_analytic,'b*-')
title('parachutist');
xlabel('time (s)');
ylabel('velocity (m/s)');
%do numerical calculations here using a 'for' loop
v1 = 0;
N = length(t);
v(1)=0;
initime = cputime;
for i = 2 : N;
v(i) = v(i-1) + (g-c*v(i-1)/m)*(tstep);
end
fintime = cputime; telapsed = timeit(v(i));
%numerical results
plot(t,v,'ro-')
legend('analytical','numerical')
%do error calculations at 5s here
Eabs = abs(v_analytic(5) - v(5));
Erel = Eabs/v_analytic(5);
display(tstep, 'tsize');
display(Eabs,'Absolute error');
display(Erel,'Relative error');
display((fintime -initime)/telapsed,'CPU usage (%):');
jonathan douglas-smith
jonathan douglas-smith 2016년 1월 25일
편집: jonathan douglas-smith 2016년 1월 25일
This way I am using the cputime function to record cputime before and after the for loop and then using the timeit() function to record the real time used by the program to complete the for loop?
Then I calculate the cpu usage using: fintime -initime)/telapsed.
Does this seem right?
There is an example at http://www.mathworks.com/help/matlab/ref/timeit.html . You put the code to be timed in a function or function handle and timeit() the handle.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2016년 1월 25일

댓글:

2016년 1월 25일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by