Calculate Elapsed Time of calculation

In summary, the modified version of the program now includes a timer to calculate the elapsed time for finding the root of the given equation using the Bisection Method.
  • #1
shayaan_musta
209
2
Hi experts!
Here is my program.



%Bisection Method
clc
close all
clear all
f=input('Enter any Equation: ','s');
f=inline(f);
n=1;
a=input('Enter 1st guess: ');
b=input('Enter 2nd guess: ');
tol=input('Enter tolerance: ');
m=(a+b)/2;
while(abs(f(m))>tol)
n=n+1
if(f(m)*f(a)<0)
b=m
else
a=m
end
m=(a+b)/2;
end
fprintf('The root of equation is: %f\nNumber of iterations is: %d',m,n)
%END



I want to know how much time this program will take to calculate root of the given equation.
I tried it to do, i.e. given below,(The program below is with changes made for elapsed time)
%Bisection Method
clc
close all
clear all
f=input('Enter any Equation: ','s');
f=inline(f);
n=1;
tic
a=input('Enter 1st guess: ');
b=input('Enter 2nd guess: ');
tol=input('Enter tolerance: ');
m=(a+b)/2;
while(abs(f(m))>tol)
n=n+1
if(f(m)*f(a)<0)
b=m
else
a=m
end
m=(a+b)/2;
end
fprintf('The root of equation is: %f\nNumber of iterations is: %d',m,n)
sec=toc
fprintf('%f',sec)
%END


Is this modified version(using tic toc) correct?
Does this calculate the correct time? I don't think so.
 
Physics news on Phys.org
  • #2
If not, what is the correct way to do this?Yes, the modified version of your program is correct and it will calculate the correct time. The tic-toc functions are used to measure the elapsed time between two events in a MATLAB program. The tic command is used to start the timer and the toc command is used to stop the timer and return the elapsed time.
 

1. How do I calculate elapsed time for a calculation?

The elapsed time for a calculation can be calculated by subtracting the starting time from the ending time. This will give you the total amount of time that has passed during the calculation.

2. What units of time should I use for elapsed time calculation?

The units of time used for elapsed time calculation will depend on the precision needed for your calculation. For shorter time periods, you can use seconds or minutes, while for longer time periods, hours or even days may be more appropriate.

3. Is there a formula for calculating elapsed time?

Yes, the formula for calculating elapsed time is: Elapsed Time = End Time - Start Time. This will give you the total amount of time that has passed between the starting and ending times.

4. Can I calculate elapsed time for multiple calculations?

Yes, you can calculate elapsed time for multiple calculations by using the same formula of End Time - Start Time for each calculation. You can then add up the individual elapsed times to get the total elapsed time for all the calculations.

5. How can I ensure accurate elapsed time calculation?

To ensure accurate elapsed time calculation, it is important to use consistent time units and to record the starting and ending times accurately. It is also helpful to double check your calculations and make sure they make sense in relation to the actual time that has passed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
810
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
6K
  • Programming and Computer Science
Replies
12
Views
953
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
23K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top