here's the bisection, i changed it a bit since the last time.
Code:
N = 100;
p = 0;
i = 1;
s0 = 100;
c = 4;
vm = 3;
k = 0.02;
a = input('Please enter starting endpoint "a" for the Bisection Method ');
b = input('Please enter starting endpoint "b" for the Bisection Method ');
Fa = (vm - c) / k * (1 - exp(-k*a)) + s0*exp(-k*a);
FA = Fa;
FP = 0;
while i <= N
p1 = p;
p = a + (b - a) / 2;
FP = (vm - c) / k * (1 - exp(-k*p)) + s0*exp(-k*p);
if abs(p - p1) / abs(p) < 10^-4;
break;
else i = i + 1;
if FA * FP > 0;
a = p;
FA = FP;
else b = p;
end
end
end
disp(sprintf('The Bisection Method: %5.10f', p));
disp(sprintf('Total Number of Iterations = %1.1f', i));
here's the newton's method
Code:
N = 200;
p0 = 0;
TOL = 0;
N0 = 0;
p = 0;
i = 1;
s0 = 100;
c = 4;
vm = 3;
k = 0.02;
p0 = input('Please enter initial approximation "p0" for Newtons Method ');
while i <= N;
Fp0 = ((vm - c) / k) * (1 - exp(-k*p0)) + s0*exp(-k*p0);
q0 = ((vm - c) / k) * (k * exp(-k*p0)) - s0*k*exp(-k*p0);
p1 = p;
p = p0 - Fp0 / q0;
if abs(p - p1) / abs(p) < 10^-4
break;
else i = i + 1;
p0 = p;
end
end
disp(sprintf('Newtons Method: %5.10f', p));
disp(sprintf('Total Number of Iterations = %1.1f', i));
here's the secant method:
Code:
N = 200;
p0 = 0;
p1 = 0;
TOL = 0;
N0 = 0;
p = 0;
i = 2;
s0 = 100;
c = 4;
vm = 3;
k = 0.02;
p0 = input('Please enter initial approximation "p0" for the Secant Method ');
p1 = input('Please enter initial approximation "p1" for the Secant Method ');
while i <= N
q0 = (vm - c) / k * (1 - exp(-k*p0)) + s0*exp(-k*p0);
q1 = (vm - c) / k * (1 - exp(-k*p1)) + s0*exp(-k*p1);
r = p;
p = p1 - q1 * (p1 - p0) / (q1 - q0);
if abs(p - r) / abs(p) < 10^-4
break;
else i = i + 1;
p0 = p1;
p1 = p;
end
end
disp(sprintf('The Secant Method: %5.10f', p));
disp(sprintf('Total Number of Iterations = %1.1f', i));
and finally, here's the method of false position
Code:
N = 200;
p0 = 0;
p1 = 0;
p = 0;
i = 2;
s0 = 100;
c = 4;
vm = 3;
k = 0.02;
p0 = input('Please enter initial approximation "p0" for the Method of False Position ');
p1 = input('Please enter initial approximation "p1" for the Method of False Position ');
while i <= N
q0 = (vm - c) / k * (1 - exp(-k*p0)) + s0*exp(-k*p0);
q1 = (vm - c) / k * (1 - exp(-k*p1)) + s0*exp(-k*p1);
FP = 0;
r = p;
p = p1 - q1 * (p1 - p0) / (q1 - q0);
FP1 = FP;
if abs(p - r) / abs(p) < 10^-4
break;
else i = i + 1;
q = (vm - c) / k * (1 - exp(-k*p)) + s0*exp(-k*p);
end
if q * q1 < 0
p0 = p1;
q0 = q1;
else p1 = p;
q1 = q;
end
end
disp(sprintf('The Method of False Position: %5.10f', p));
disp(sprintf('Total Number of Iterations = %1.1f', i));
oh yeah, forgive me for the lack of comments. i know it's bad, but i was tired