Thread: Matlab
View Single Post
Old 07-28-2003, 11:43 AM   #35 (permalink)
slant eyes
Psycho
 
Location: cali
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
slant eyes is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73