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?

조회 수: 3 (최근 30일)
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
Jan
Jan 2017년 9월 25일
편집: Jan 2017년 9월 25일
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?

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

채택된 답변

Jan
Jan 2017년 9월 25일
편집: Jan 2017년 9월 25일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by