- #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.
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.