Sunday, April 27, 2014

Chapter 3

3.1 Convert the following base-2 numbers to base-10: (a) 1011001,
(b) 110.00101 and (c) 0.01011

I wrote a program to solve this:
public class Binary {
public static void main(String[] args) {
String binary = "1101.1";
String[] halfs = binary.split("\\.");
double answer = Integer.parseInt(halfs[0], 2);
if(halfs.length > 1)
for(int i = 0; i < halfs[1].length(); i++){
if(halfs[1].substring(i, i+1).equals("1")){
answer += (1 / Math.pow(2, (i + 1))) ;
}
}
System.out.println(answer);
}
}
a) 89.0
b) 6.15625
c) 0.34375

3.2 Convert the following base-8 numbers to base 10: 71,563 and
3.14.

I made a program to do this:

public class Octal {
public static void main(String[] args) {
String octal = "3.14";
String[] halfs = octal.split("\\.");
double answer = Integer.parseInt(halfs[0], 8);
if(halfs.length > 1)
for(int i = 0; i < halfs[1].length(); i++){
answer += Integer.parseInt(halfs[1].substring(i, i+1)) *(1 / Math.pow(8, (i + 1))) ;
}
System.out.println(answer);
}
}

a) 29555.0
b) 3.1875

3.3 Compose your own program based on Fig. 3.11 and use it to
determine your computer’s machine epsilon.

I had to convert the epsilon to a string, java couldn't tell that the number was <= 1

public class Epsilon {
public static void main(String[] args) {
double epsilon = 1.0;
while(!String.valueOf(epsilon + 1.0).equals("1.0")){
epsilon /= 2;
}
epsilon = 2 * epsilon;
System.out.println(epsilon);
}
}

2.220446049250313E-16

3.5)
The infinite series converges on a value of f (n) = π4/90 as n approaches infinity.
Write a program in single precision to calculate f (n) for
n = 10,000 by computing the sum from i = 1 to 10,000. Then
repeat the calculation but in reverse order—that is, from
i = 10,000 to 1 using increments of −1. In each case, compute the
true percent relative error. Explain the results.

public static void main(String[] args) {
double t = 0;
for(int i = 1; i <= 10000; i++)
t+=s(i);
System.out.println(t);

for(int i = 10000; i >0 ;i--)
t+=s(i);
System.out.println(t);
}

public static double s(int t){
return 1 / Math.pow(t,4);
}


public static void main(String[] args) {
double a = ex1(5);
double t = 0.006737947;
System.out.println(a + "\t" + ((t - a)/ t) * 100);
a = ex2(5);
System.out.println(a + "\t" + ((t - a)/ t) * 100);
}

public static double ex1(int e){
double rv = 1.0;
for(int i = 1; i < 20; i++)
if((i&1) == 1)
rv -= (Math.pow(e,i) / fact(i));
else
rv += (Math.pow(e,i) / fact(i));
return rv;
}

public static double ex2(int e){
double rv = 1.0;
for(int i = 1; i < 20; i++)
rv += (Math.pow(e,i) / fact(i));
return 1/rv;
}

public static double fact(int t){
double rv = 1;
for(int i = 2; i <= t; i++)
rv *= i;
return rv;
}

0.00670634105421557 0.46907382596554204
0.00673794932511709 -3.450779725904994E-5


public class Chop {
static int c = 3;
public static void main(String[] args) {
System.out.println(f(0.577));
c= 4;
System.out.println(f(0.577));
}

public static double chop(double t){
String rv = Double.toString(t);
if(rv.indexOf(".") < 0) return t;
return (new Double(rv.substring(0, rv.indexOf(".") + c + 1))).doubleValue();
}

public static double f(double x){
return chop(chop(6 * x) / chop(Math.pow(chop(1 - Math.pow(chop(3 * x), 2)), 2)));
}
}

0.872
0.8689

3.8 (a) Evaluate the polynomial
y = x3 − 7x2 + 8x − 0.35
at x = 1.37. Use 3-digit arithmetic with chopping. Evaluate the
percent relative error.

public class Chop2 {
static int c = 3;
public static void main(String[] args) {
System.out.println(yc(1.37));
System.out.println(((y(1.37) - yc(1.37)) / y(1.37)) * 100);
}

public static double yc(double x){
double rv = chop(Math.pow(x,3));
rv -= chop(7 * (Math.pow(x, 2)));
rv += chop(8 * x);
rv -= 0.35;
return  rv;
}

public static double chop(double t){
String rv = Double.toString(t);
if(rv.indexOf(".") < 0) return t;
if((rv.length() - rv.indexOf(".")) <= c) return t;
return (new Double(rv.substring(0, rv.indexOf(".") + c + 1))).doubleValue();
}

public static double y(double x){
double rv = Math.pow(x,3);
rv -= (7 * (Math.pow(x, 2)));
rv += (8 * x);
rv -= 0.35;
return  rv;
}
}

(b) Repeat (a) but express y as
y = ((x − 7)x + 8)x − 0.35
Evaluate the error and compare with part (a).

public class Chop2 {
static int c = 3;
public static void main(String[] args) {
System.out.println(yc(1.37));
System.out.println(((y(1.37) - yc(1.37)) / y(1.37)) * 100);
}

public static double yc(double x){
//y = ((x − 7)x + 8)x − 0.35
double rv = chop(x - 7);
rv = chop((rv * x) + 8);
rv = chop((rv * x) - 0.38);
return  rv;
}

public static double chop(double t){
String rv = Double.toString(t);
if(rv.indexOf(".") < 0) return t;
if((rv.length() - rv.indexOf(".")) <= c) return t;
return (new Double(rv.substring(0, rv.indexOf(".") + c + 1))).doubleValue();
}

public static double y(double x){
//y = ((x − 7)x + 8)x − 0.35
double rv = x - 7;
rv = (rv * x) + 8;
rv = (rv * x) - 0.38;
return  rv;
}
}

0.011
15.728185091543626

part a had less percent relative error

3.9 Calculate the random access memory (RAM) in megabytes
necessary to store a multidimensional array that is 20 × 40 × 120.
This array is double precision, and each value requires a 64-bit word.
Recall that a 64-bit word = 8 bytes and 1 kilobyte = 2^10 bytes.
Assume that the index starts at 1.
20 x 40 x 120 x 8 = 768000 bytes = 750K = about 0.75 M


public class Cos {
public static void main(String[] args) {
double x = .3 * Math.PI, cosx = Math.cos(x);
double answer = 1.0;

System.out.println("Approximation:\t\t%error");
for(int i = 1; Double.toString(answer).length() < 10; i++){
if((i & 1) == 1)
     answer = answer - Math.pow(x, (i*2))/ fact(i * 2);
else
     answer = answer + Math.pow(x, (i*2))/ fact(i * 2);
System.out.println(answer + "\t" + ((cosx - answer)/ cosx) * 100);
}
}

public static int fact(int f){
int returnVal = 1;
for(int i = f; i > 1; i--)
returnVal *= i;
return returnVal;
}
}



3.13) The “divide and average” method, an old-time method for approximating
the square root of any positive number a, can be formulated
as: x = (x + a/x) / 2
Write a well-structured function to implement this algorithm based
on the algorithm outlined in Fig. 3.3.

Wednesday, April 23, 2014

Chapter 2

2.1)
if x >= 10 then
   do
   x = x - 5
   while x > 50
else
   if x < 5 then
      x = 5
   else
      x = 7.5
   end if
end if
exit

in matlab:
function [ output ] = two( x )
    if x >= 10
        x = x - 5;
        while x > 50
            x = x - 5;
        end
    else
        if x < 5
            x = 5;
        else
            x = 7.5;
        end
    end
    output = x;
end

no loops:
function [ output ] = two( x )
    if x >= 10
        x = x - 5;
        if x > 50
            x = 50 - (5 -(x - (floor(x / 5) * 5)));
        end
    else
        if x < 5
            x = 5;
        else
            x = 7.5;
        end
    end
    output = x;
end

2.2 Rewrite the following pseudocode using proper indentation
DO
   i = i + 1
   IF z > 50 EXIT
   x = x + 5
   IF x > 5 THEN
      y = x
   ELSE
      y = 0
   ENDIF
   z = x + y
ENDDO

2.3 Develop, debug, and document a program to determine the
roots of a quadratic equation, ax2 + bx + c, in either a high-level
language or a macro language of your choice. Use a subroutine procedure
to compute the roots (either real or complex). Perform test
runs for the cases (a) a = 1, b = 6, c = 2; (b) a = 0, b = −4,
c = 1.6; (c) a = 3, b = 2.5, c = 7.

using this formual:

I wrote the following program in java:
public static void quad(double a, double b, double c){
double tom = (b * b - 4 * a * c);
double real = -b / (2 * a);

if (tom >= 0) {
   System.out.println(Math.sqrt(tom) / (2 * a) + real);
   System.out.println(-Math.sqrt(tom) / (2 * a) + real);
} else {
   System.out.println(real + " + " + (Math.sqrt(-tom) / (2 * a)) + "i");
   System.out.println(real + " - " + (Math.sqrt(-tom) / (2 * a)) + "i");
}
}
}

The test cases output is:
-0.3542486889354093
-5.645751311064591
Infinity
NaN
-0.4166666666666667 + 1.4695993407123664i
-0.4166666666666667 - 1.4695993407123664i


matlab:
function [ output ] = quad( a,b,c )
    output(1) = (sqrt((b * b - 4 * a * c)) / (2 * a) + (-b / (2 * a)));
    output(2) = (-sqrt((b * b - 4 * a * c)) / (2 * a) + (-b / (2 * a)));
end

>> quad(1, 6, 2)

ans =

   -0.3542   -5.6458

>> quad(0, -4, 1.6)

ans =

   Inf   NaN

>> quad(3, 2.5, 7)

ans =

  -0.4167 + 1.4696i  -0.4167 - 1.4696i




a)
While i < 10
   If i is odd
      answer = answer - (x^(i*2))/(i * 2)!
   Else
      answer = answer + (x^(i*2))/(i * 2)!
   End If
   i = i + 1
   print (cos(x) - answer) / cos(x)
End While

b)

2.5) Develop, debug, and document a program for Prob. 2.4 in either a high-level language or a macro language of your choice. Employ the library function for the cosine in your computer to determine the true value. Have the program print out the series approximation and the error at each step. As a test case, employ the program to compute cos(1.25) for up to and including the term x10/10!. Interpret your results.

public class Cos {
public static void main(String[] args) {
double x = 1.25, cosx = Math.cos(x);
double answer = 1.0;
for(int i = 1; i <=5; i++){
if((i & 1) == 1)
     answer = answer - Math.pow(x, (i*2))/ fact(i * 2);
else
     answer = answer + Math.pow(x, (i*2))/ fact(i * 2);
System.out.println(((cosx - answer)/ cosx));
}
}

public static int fact(int f){
int returnVal = 1;
for(int i = f; i > 1; i--)
returnVal *= i;
return returnVal;
}
}

results:
Approximation: %error
0.21875 30.62655044877899
0.3204752604166667 -1.6341682785373326
0.3151770697699653 0.046077488510394275
0.315324898750063 -8.043688291041844E-4
0.31532233227471407 9.552305259051958E-6

for matlab:
function [ o ] = cos2( x , t)
    a = zeros(1,t);
    a(1) = 1 - (x^2)/2;
    for i = 2:t
        if (floor(i/2) * 2) == i
            a(i) = a(i-1) + (x^(i*2))/ factorial(i * 2);
        else
            a(i) = a(i-1) - (x^(i*2))/ factorial(i * 2);
        end  
    end
    o = a;
end

function [ o ] = compToCos( c )
    l = length(c);
    o(1:l) = abs(((cos(1.25) - c(1:l))/ cos(1.25)) * 100);
end


>> temp = cos2(1.25, 5)

temp =

    0.2188    0.3205    0.3152    0.3153    0.3153

>> compToCos(temp)

ans =

   30.6266    1.6342    0.0461    0.0008    0.0000

These results get more accurate the longer it runs and this happens very quickly.

2.6)

input courseNumber, name, WQ, WH, WF
for i = 1 to 5
   input quiz
   AQ = AQ + quiz
end for
AQ = AQ / 5
for i = 1 to 6
   input quiz
   AH = AH + quiz
end for
AH = AH / 5
input FE
if FE exists
   AG = ((WQ * AQ + WH * AH + WF * FE) / (WQ + WH + WF)) * 100
else
   AG = ((WQ * AQ + WH * AH) / (WQ + WH)) * 100
end if
print courseNumber, name, AG

b)
function [ o ] = grade( cn, name, WQ, WH, WF, quizes, homeworks, FE )
    AQ = sum(quizes) / length(quizes);
    AH = sum(homeworks) / length(homeworks);
    if isempty(FE)
        AG = ((WQ * AQ + WH * AH + WF * FE) / (WQ + WH + WF)) * 100;
    else
        AG = ((WQ * AQ + WH * AH) / (WQ + WH)) * 100;
    end
    o(1) = cn;
    o(2) = name;
    o(3) = AG;
end

a)
if a > 0
   tol = 10^-5
   x = a/2
   e = 0
   while e < tol
      y = (x + a/x)/ 2
      e = |(y - x) / y|
      x = y
   end while   
   squareRoot = x
else
   squareRoot = 0
end if


function [ o ] = sqrt2( a )
    if a > 0
        tol = 10^-5;
        x = a/2;
        e = 0;
        while e < tol
            y = (x + a/x)/ 2;
            e = abs((y - x) / y);
            x = y
        end    
        squareRoot = x;
    else
        squareRoot = 0;
    end
    o = squareRoot;
end


function [ o ] = intrest( p, i, n )
    rv = zeros(1,n);
    for t = 1:n
        rv(t) = p * (1 + i)^t;
    end
    o = rv;
end

>> intrest(100000, .06, 5)

ans =

   1.0e+05 *

    1.0600    1.1236    1.1910    1.2625    1.3382


function [ o ] = apay( p, i, n )
    rv = zeros(1,n);
    for t = 1:n
        rv(t) = p * ((i * (1 + i)^t) / ((1 + i)^t - 1));
    end
    o = rv;
end

>> apay(66000, 0.066, 5)

ans =

   1.0e+04 *

    7.0356    3.6302    2.4966    1.9309    1.5925


function [ o ] = temp( Tm, Tp, t, tp , w)
    l = length(t);
    T = zeros(1, l);
    for i = 1:l
        T(i) = Tm + (Tp - Tm) * cos(w * (t(i) - tp));
    end
    o = T;
end

>> temp( 22.1, 28.3, 0:59, tp, w)

ans =

  Columns 1 through 12

   16.3593   16.3198   16.2821   16.2460   16.2117   16.1792   16.1484   16.1194   16.0921   16.0667   16.0430   16.0211

  Columns 13 through 24

   16.0010   15.9827   15.9663   15.9516   15.9388   15.9278   15.9186   15.9112   15.9057   15.9021   15.9002   15.9002

  Columns 25 through 36

   15.9021   15.9057   15.9112   15.9186   15.9278   15.9388   15.9516   15.9663   15.9827   16.0010   16.0211   16.0430

  Columns 37 through 48

   16.0667   16.0921   16.1194   16.1484   16.1792   16.2117   16.2460   16.2821   16.3198   16.3593   16.4004   16.4433

  Columns 49 through 60

   16.4878   16.5340   16.5818   16.6313   16.6824   16.7351   16.7894   16.8452   16.9027   16.9616   17.0221   17.0841




>> temp( 10.7, 22.9, 180:242, tp, w)

ans =

  Columns 1 through 12

   21.7876   21.8735   21.9562   22.0355   22.1115   22.1841   22.2533   22.3190   22.3813   22.4402   22.4955   22.5474

  Columns 13 through 24

   22.5958   22.6406   22.6819   22.7197   22.7539   22.7845   22.8115   22.8350   22.8548   22.8711   22.8837   22.8928

  Columns 25 through 36

   22.8982   22.9000   22.8982   22.8928   22.8837   22.8711   22.8548   22.8350   22.8115   22.7845   22.7539   22.7197

  Columns 37 through 48

   22.6819   22.6406   22.5958   22.5474   22.4955   22.4402   22.3813   22.3190   22.2533   22.1841   22.1115   22.0355

  Columns 49 through 60

   21.9562   21.8735   21.7876   21.6983   21.6058   21.5101   21.4111   21.3090   21.2037   21.0953   20.9839   20.8694

  Columns 61 through 63

   20.7519   20.6314   20.5079


public static LinkedList<BigDecimal> parachutist(double step, double time){
LinkedList<BigDecimal> list = new LinkedList<BigDecimal>();
list.add(new BigDecimal(0));
double g = 9.8, m = 68.1, c = 12.5;
for(double i = (0.0 + step); i <= time; i += step){
double vi = list.getLast().doubleValue();
list.add( new BigDecimal(
vi + (g - ((c/m) * vi)) * step
));
}
return list;
}

matlab:
function [ output ] = eulers( s )
g = 9.8; m=68.1;c=12.5;l = length(s) - 1; v = zeros(1,l + 1);
for i = 1:l
v(2:l+1) = v(1:l) + (g-((c*v(1:l))/ m)) * (s(2) - s(1));
end
output = v;
end

a) the results for t = 10 and step size = 1
t, s v, m/s
0.0 0
1.0 9.800000000000000710542735760100185871124267578125
2.0 17.80117474302496560767394839785993099212646484375
3.0 24.3337050765372708838185644708573818206787109375
4.0 29.6671659655722805837285704910755157470703125
5.0 34.02165092049660444217806798405945301055908203125
6.0 37.57685449602953298153806827031075954437255859375
7.0 40.47948766489341920760125503875315189361572265625
8.0 42.84933207295262747038577799685299396514892578125
9.0 44.784183014040621628737426362931728363037109375
10.0 46.3638851039744253057506284676492214202880859375

in matlab:
>> eulers(0:10)

ans =

         0    9.8000   17.8012   24.3337   29.6672   34.0217   37.5769   40.4795   42.8493   44.7842   46.3639


b) results for t = 10 and step = .5:
t, s v, m/s
0.0 0
0.5 4.9000000000000003552713678800500929355621337890625
1.0 9.35029368575624175718985497951507568359375
1.5 13.3921536632015207857193672680296003818511962890625
2.0 17.06306467061694576159425196237862110137939453125
2.5 20.39707121699938596748324926011264324188232421875
3.0 23.42509331529239346991744241677224636077880859375
3.5 26.17521323863193316583419800736010074615478515625
4.0 28.67293595902180669554581982083618640899658203125
4.5 30.941425683781186961596176843158900737762451171875
5.0 33.00172068343416498237274936400353908538818359375
5.5 34.87292840338330535132627119310200214385986328125
6.0 36.57240266885840895838555297814309597015380859375
6.5 38.115904626562297607961227186024188995361328125
7.0 39.51774891560760494257920072413980960845947265625
7.5 40.79093642335286773459301912225782871246337890625
8.0 41.94727485733296390435498324222862720489501953125
8.5 42.997488251483758858739747665822505950927734375
9.0 43.95131642223598333885092870332300662994384765625
9.5 44.8176052968472191651017055846750736236572265625
10.0 45.60438895168869777307918411679565906524658203125

in matlab:
>> eulers(0:.5:10)

ans =

  Columns 1 through 12

         0    4.9000    9.3503   13.3922   17.0631   20.3971   23.4251   26.1752   28.6729   30.9414   33.0017   34.8729

  Columns 13 through 21

   36.5724   38.1159   39.5177   40.7909   41.9473   42.9975   43.9513   44.8176   45.6044




2.12 The bubble sort is an inefficient, but easy-to-program, sorting technique. The idea behind the sort is to move down through an array comparing adjacent pairs and swapping the values if they are out of order. For this method to sort the array completely, it may need to pass through it many times. As the passes proceed for an ascending-order sort, the smaller elements in the array appear to rise toward the top like bubbles. Eventually, there will be a pass through the array where no swaps are required. Then, the array is sorted. After the first pass, the largest value in the array drops directly to the bottom. Consequently, the second pass only has to proceed to the second-to-last value, and so on. Develop a program to set up an array of 20 random numbers and sort them in ascending order with the bubble sort (Fig. P2.12).

function [ o ] = sort2( a )
    m = length(a)-1;
    doswitch = 1;
    while doswitch == 1
        doswitch = 0;
        for i = 1:m
           if a(i) > a(i+1)
               temp = a(i);
               a(i) = a(i+1);
               a(i+1) = temp;
               doswitch = 1;
           end
        end
        m = m - 1;
    end
    o = a;
end

>> sort2([4, 69, 666, 82, 111, 420, 9999999, 1,0,21,13,7,7,7,7,5,4,3,1,2])

ans =

  Columns 1 through 10

           0           1           1           2           3           4           4           5           7           7

  Columns 11 through 20

           7           7          13          21          69          82         111         420         666     9999999

Chapter 1

1.1 Use calculus to solve Eq. (1.9) for the case where the initial
velocity, v(0) is nonzero.

This took a while. I asked a few math professors and googled a bunch. I am thinking I should have taken linear algebra before taking this class. This is the result from wolframalpha.com as its is the nicest output I can make.

(1.9) is: dv / dt = g − (c / m) v




1.2 Repeat Example 1.2. Compute the velocity to t = 10 s, with a
step size of (a) 1 and (b) 0.5 s. Can you make any statement regarding
the errors of the calculation based on the results?

This is the algorithm I am going to use, in the book it is annotated as (1.12)


g = 9.8 m/s
m = 68.1 kg (parachutist's mass)
c = 12.5 kg/s (drag coefficient)

I made a program that does this for me in java.

public static LinkedList<BigDecimal> parachutist(double step, double time){
LinkedList<BigDecimal> list = new LinkedList<BigDecimal>();
list.add(new BigDecimal(0));
double g = 9.8, m = 68.1, c = 12.5;
for(double i = (0.0 + step); i <= time; i += step){
double vi = list.getLast().doubleValue();
list.add( new BigDecimal(
vi + (g - ((c/m) * vi)) * step
));
}
return list;
}

I also make a function in matlab to come up with similar answers:

function [ output ] = eulers( s )
g = 9.8; m=68.1;c=12.5;l = length(s) - 1; v = zeros(1,l + 1);
for i = 1:l
v(2:l+1) = v(1:l) + (g-((c*v(1:l))/ m)) * (s(2) - s(1));
end
output = v;
end

a) the results for t = 10 and step size = 1
t, s v, m/s
0.0 0
1.0 9.800000000000000710542735760100185871124267578125
2.0 17.80117474302496560767394839785993099212646484375
3.0 24.3337050765372708838185644708573818206787109375
4.0 29.6671659655722805837285704910755157470703125
5.0 34.02165092049660444217806798405945301055908203125
6.0 37.57685449602953298153806827031075954437255859375
7.0 40.47948766489341920760125503875315189361572265625
8.0 42.84933207295262747038577799685299396514892578125
9.0 44.784183014040621628737426362931728363037109375
10.0 46.3638851039744253057506284676492214202880859375

in matlab:
>> eulers(0:10)

ans =

         0    9.8000   17.8012   24.3337   29.6672   34.0217   37.5769   40.4795   42.8493   44.7842   46.3639


b) results for t = 10 and step = .5:
t, s v, m/s
0.0 0
0.5 4.9000000000000003552713678800500929355621337890625
1.0 9.35029368575624175718985497951507568359375
1.5 13.3921536632015207857193672680296003818511962890625
2.0 17.06306467061694576159425196237862110137939453125
2.5 20.39707121699938596748324926011264324188232421875
3.0 23.42509331529239346991744241677224636077880859375
3.5 26.17521323863193316583419800736010074615478515625
4.0 28.67293595902180669554581982083618640899658203125
4.5 30.941425683781186961596176843158900737762451171875
5.0 33.00172068343416498237274936400353908538818359375
5.5 34.87292840338330535132627119310200214385986328125
6.0 36.57240266885840895838555297814309597015380859375
6.5 38.115904626562297607961227186024188995361328125
7.0 39.51774891560760494257920072413980960845947265625
7.5 40.79093642335286773459301912225782871246337890625
8.0 41.94727485733296390435498324222862720489501953125
8.5 42.997488251483758858739747665822505950927734375
9.0 43.95131642223598333885092870332300662994384765625
9.5 44.8176052968472191651017055846750736236572265625
10.0 45.60438895168869777307918411679565906524658203125

in matlab:
>> eulers(0:.5:10)

ans =

  Columns 1 through 12

         0    4.9000    9.3503   13.3922   17.0631   20.3971   23.4251   26.1752   28.6729   30.9414   33.0017   34.8729

  Columns 13 through 21

   36.5724   38.1159   39.5177   40.7909   41.9473   42.9975   43.9513   44.8176   45.6044



1.4 For the free-falling parachutist with linear drag, assume a first
jumper is 70 kg and has a drag coefficient of 12 kg/s. If a second
jumper has a drag coefficient of 15 kg/s and a mass of 75 kg, how
long will it take him to reach the same velocity the first jumper
reached in 10 s?

this was my java answer:
At t = 15.684 the second parachutist has the same velocity as the first one after 10s.
I wrote a program to do this for me:

this is my matlab answer:
>> tps(0:.01:30) ans = 15.7100 >> tps(0:.001:30) ans = 15.6840

Here is the java code:
public static void main(String[] args) {
double step = 0.001, timeTo = 10.0;
LinkedList<BigDecimal> list = new LinkedList<BigDecimal>();
LinkedList<BigDecimal> list2 = new LinkedList<BigDecimal>();
list.add(new BigDecimal(0.0));
list2.add(new BigDecimal(0.0));
list = EulersMethod.parachutist(list, 0.0, step, timeTo, 70.0, 12.0);
list2 = EulersMethod.parachutist(list2, 0.0, step, 30.0, 75.0, 15.0);

for(int i = 0; i < list2.size(); i++){
if(list2.get(i).doubleValue() >= list.getLast().doubleValue()){
System.out.println("At t = " + (i * step) + " the second"
+ " parachutist has the same velocity as the first one after 10s." );
return;
}
}
}

public class EulersMethod {
public static LinkedList<BigDecimal> parachutist(LinkedList<BigDecimal> list, double startTime, double step, double timeTo, double m, double c){
double g = 9.8;
for(double i = (startTime + step); i <= timeTo; i += step){
double vi = list.getLast().doubleValue();
list.add( new BigDecimal(
vi + (g - ((c/m) * vi)) * step
));
}
return list;
}
}

here is the matlab code:
function [ output ] = tps( s )
    l = length(s) - 1;
    %f = 1;
    v1 = zeros(1,10/s(2));
    m = 75;
    c = 15;
    v = zeros(1,l);
    fp = eulers(v1, 0:s(2):10, 70, 12, 1);
    g = 9.8;
    i = 1;
    while (fp(length(fp)) > v(i))
        v(2:l+1) = v(1:l) + (g-((c*v(1:l))/ m)) * s(2);
        i = i + 1;
    end
    output = s(i);
end

function [ output ] = eulers( v, s, m, c, f )
    g = 9.8;
    l = length(s) - 1;
    for i = f:l
        v(f+1:l+1) = v(f:l) + (g-((c*v(f:l))/ m)) * (s(2) - s(f));
    end
    output = v;
end

1.5 Compute the velocity of a free-falling parachutist using Euler’s
method for the case where m = 80 kg and c = 10 kg/s. Perform the
calculation from t = 0 to 20 s with a step size of 1 s. Use an initial
condition that the parachutist has an upward velocity of 20 m/s at
t = 0. At t = 10 s, assume that the chute is instantaneously deployed
so that the drag coefficient jumps to 50 kg/s.

I wrote a program to do this for me:
EulersMethod.parachutist is the same code as the last question.

public static void main(String[] args) {
double step = 1.0;
LinkedList<BigDecimal> list = new LinkedList<BigDecimal>();
list.add(new BigDecimal(-20.0));
list = EulersMethod.parachutist(list, 0.0, step, 9.0, 80.0, 10.0);
list = EulersMethod.parachutist(list, 0.0, step, 11.0, 80.0, 50.0);

System.out.println("t, s\tv, m/s");
for(int i = 0; i < list.size(); i++)
System.out.println(i + "\t" + list.get(i));
}

output:
t, s v, m/s
0 -20
1 -7.699999999999999289457264239899814128875732421875
2 3.0625000000000017763568394002504646778106689453125
3 12.4796875000000024868995751603506505489349365234375
4 20.719726562500003552713678800500929355621337890625
5 27.92976074218750426325641456060111522674560546875
6 34.23854064941406960542735760100185871124267578125
7 39.75872306823730895075641456060111522674560546875
8 44.58888268470764870698985760100185871124267578125
9 48.81527234911919066462360206060111522674560546875
10 28.10572713091969632159816683270037174224853515625
11 20.339647674094887719320468022488057613372802734375
12 17.4273678777855849375555408187210559844970703125
13 16.3352629541695932857692241668701171875
14 15.925723607813598192706194822676479816436767578125
15 15.77214635293010047689676866866648197174072265625
16 15.714554882348789277557443710975348949432373046875
17 15.692958080880796245537567301653325557708740234375
18 15.6848592803302988585301136481575667858123779296875
19 15.68182223012386344862534315325319766998291015625
20 15.680683336296450391955659142695367336273193359375

matlab answer:
function [ output ] = eulers( v, s, m, c, f)
    g = 9.8;
    for i = f:length(s) - 1
        v(i+1) = v(i) + (g-((c*v(i))/ m));
    end
    output = v;
end

>> v = zeros(1,20);
>> v(1) = -20;
>> eulers(eulers(v, 1:20, 80, 10, 1), 1:20, 80,50,10)

ans =

  Columns 1 through 12

  -20.0000   -7.7000    3.0625   12.4797   20.7197   27.9298   34.2385   39.7587   44.5889   48.8153   28.1057   20.3396

  Columns 13 through 20

   17.4274   16.3353   15.9257   15.7721   15.7146   15.6930   15.6849   15.6818


1.6 The amount of a uniformly distributed radioactive contaminant
contained in a closed reactor is measured by its concentration c
(becquerel/liter or Bq/L). The contaminant decreases at a decay
rate proportional to its concentration—that is
decay rate = −kc
where k is a constant with units of day−1. Therefore, according to
Eq. (1.13), a mass balance for the reactor can be written as
dc
dt
= −kc

change
in mass

=

decrease
by decay

(a) Use Euler’s method to solve this equation from t = 0 to 1 d
with k = 0.2d−1. Employ a step size of t = 0.1. The concentration
at t = 0 is 10 Bq/L.
(b) Plot the solution on a semilog graph (i.e., ln c versus t) and determine
the slope. Interpret your results.