Thursday, April 12, 2012

Homework # 3

3.2

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

To convert the numbers to base ten you must multiply each digit in the number by 8 to the power of its decimal place:

71,563 = 7*(8^4) + 1*(8^3) + 5*(8^2) + 6*(8^1)+ 3*(8^0) =
               7*(4096) + 1*(512) + 5*(64) + 6*(8)+ 3*(1) = 29,555

3.14 = 3*1 + 1*0.125 + 4*0.015625 = 3.1875

3.5

Write an algorithm that computes f(n) = 10,000 from i = 1 to 10,000


Matlab:

sum = 0;
for iter = 1 : 10000
    

    sum = sum + 1/(iter^4) ;   
    
    
end


sum2 = 1.0823;
for iter = 10000 : 1
    

    sum2 = 1/(iter^4) - sum2 ;   
    
    
end

sum
sum2




3.6
Evaluate e^(-5)
Using both of the following equations:







and compare with the true value of 6.737947 x 10^(-3).


Matlab:
function ANS = P36(iter)
% initilizes first values
x = 5;
equation1(1) = 1;
equation2(1) = 1;
equation2de(1) = 1;
Err1(1) = (exp(-5) - 1)/(exp(-5))*1;
Err2(1) = (exp(-5) - 1)/(exp(-5))*1;
%loop to calculate each value based on input values
for loop = 1:iter
   
   %retrieves each term for the iterations
   term = x^loop/factorial(loop); 
   % flips the sign
   equation2de(loop+1) = equation2de (loop) +term;
   equation2(loop + 1) = 1/equation2de(loop+1);
   if (mod(loop,2) == 1)
       term = -term;
   end
   %saves each value to the matrix
   equation1(loop+1) = equation1(loop) + term;
   Errvalue1 = (exp(-5) - equation1(loop))/(exp(-5))*1;
   Err1(loop+1) =  Errvalue1 ;
   Errvalue2 = (exp(-5) - equation2(loop))/(exp(-5))*1;
   Err2(loop+1) =  Errvalue2 ;
   
end
%prints out values and graphs
equation1
equation2
Err1
Err2
plot ((0:iter),equation1,'black');
hold on;
plot ((0:iter),equation2,'red');

plot ((0:iter),Err1,'blue');
plot ((0:iter),Err2,'green');
end




Plot:
Black is Equation 1
Red is Equation 2
Percent error:

Equation values:


No comments:

Post a Comment