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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home