My Modified regula falsi code has the same number of iterations and error value as the regular falsi code that I made. Why is this?

2 vues (au cours des 30 derniers jours)
This is my modified regula falsi code
clear all; clear clc;
g = 9.81; %acceleration m/s^2
m = 68.1; %mass, kg
t = 20; % time, seconds
f = @(c) g*m./c.*(1-exp(-c/m*t))-93.71;
c = 2:20;
plot (c, f(c))
xlabel('c = 2:20')
ylabel('Velocity formula in terms of c')
title('Plot of velocity function with respect to c')
a = 4; b = 8;
error = 1;
mold = 1;
SF = 6;
i = 1;
F = f(a);
G = f(b);
while (1)
m = (a*G - b*F)/(G-F);
if F*f(m) < 0
b = m;
G = f(m);
if F*f(m) > 0
F = F/2;
end
else
a = m;
F = f(m);
if f(a)*f(m) > 0
G = G/2;
end
end
error = abs ((m-mold)/m);
if error < 0.5*10^-SF
break
end
mold = m;
i = i + 1; %iteration counter
end
verse3 = [num2str(m(end)),' is our (c) value.'];
disp (verse3)
verse = ['There were ' num2str(i), ' iterations done'];
disp (verse)
verse2 = ['The error is ', num2str(error(end))];
disp (verse2)
Here is my regula falsi code
clear all; clear clc;
g = 9.81; %acceleration m/s^2
m = 68.1; %mass, kg
t = 20; % time, seconds
f = @(c) g*m./c.*(1-exp(-c/m*t))-93.71;
c = 2:20;
plot (c, f(c))
xlabel('c = 2:20')
ylabel('Velocity formula in terms of c')
title('Plot of velocity function with respect to c')
a = 4; b = 8;
error = 1;
mold = 1;
SF = 6;
i = 1;
while (1)
m = (a*f(b)-b*f(a))/(f(b)-f(a));
mbis(i) = m;
if f(a)*f(m)<0;
b = m;
else
a = m;
end
error = abs ((m-mold)/m);
if error < 0.5*10^-SF;
break
end
mold = m;
i = i + 1; %iteration counter
end
verse3 = [num2str(m(end)),' is our (c) value.'];
disp (verse3)
verse = ['There were ' num2str(i), ' iterations done'];
disp (verse)
verse2 = ['The error is ', num2str(error(end))];
disp (verse2)
  1 commentaire
Jan
Jan le 25 Sep 2017
Modifié(e) : Jan le 25 Sep 2017
Let me ask the other way around: Why do you assume that the number of iterations must be different?
Did you use the debugger already to compare the evaluations in the codes line by line?

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 25 Sep 2017
Modifié(e) : Jan le 25 Sep 2017
Let me ask the other way around: Why do you assume that the number of iterations must be different?
Did you use the debugger already to compare the evaluations in the codes line by line?
The only difference is inside this block (and the corresponding block in the other branch):
if F*f(m) > 0
F = F/2;
end
And does this condition occur at all?
Note: Do not shadow the important function "error" by a variable, because this leads to unexpected behavior frequently.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by