Thursday, May 8, 2014

Chapter 5

5.1 Determine the real roots of f (x) = −0.6x2 + 2.4x + 5.5:
(a) Graphically.
(b) Using the quadratic formula.
(c) Using three iterations of the bisection method to determine
the highest root. Employ initial guesses of xl = 5 and xu = 10.
Compute the estimated error εa and the true error εt after each
iteration.

a) it appears to be -1.5 and 5.5

x = -3:.1:7;
y = -0.6.*x.^2 + 2.4.*x + 5.5;
plot(x,y, x, 0)

matlab:


excel:
The formula I used is at the top of the picture below, right after Fx.


b) I wrote a program to do this:
public static void main(String[] args) {
quad(-0.6, 2.4, 5.5);
}
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");
}
}

-1.6285901761795403
5.62859017617954


in matlab:

function [ output ] = quadr( 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


>> quadr(-0.6,2.4,5.5)

ans =

   -1.6286    5.6286

c)
f = @(x) -0.6.*x.^2 + 2.4.*x + 5.5;

%first guess

%ps = positive side ; ns = negitive side

f(5) % = 2.5 so ps = 5;
f(10)% = -30.5 so ns = 10;

%the first guess will be 10 - 5 / 2 = 7.5
%we know the root is really at: 5.6286 so the % error is
root = 5.6286;
abs((root - 7.5)/root) * 100 
% 33.2481%

f(7.5)  % = -10.25 so ns is now = 7.5
% the second guess is going to be 7.5 - 5 / 2 = 6.25

abs((root - 6.25)/root) *100
% 11.04%

f(6.25) % = -2.9375 so ns is now = 6.25
% the third and final guess will be 6.25 - 5 / 2 = 5.625

abs((root - 5.625)/root) *100
% 0.064%

5.2 Determine the real root of f (x) = 4x3 − 6x2 + 7x − 2.3:
(a) Graphically.
(b) Using bisection to locate the root. Employ initial guesses of
xl = 0 and xu = 1 and iterate until the estimated error εa falls
below a level of εs = 10%.

a)
f = @(x) 4.*x.^3 - 6.*x.^2 + 7.*x - 2.3;
x = -.4:.8;
plot(x, f(x), x, 0)


b)
%b)
f(0) % = -2.3 so ns = 0;
f(1) % = 2.7 so ps = 1;

%first guess will be 1 - 0 / 2 = .5
root = 0.450124;
abs((root - .5)/root)*100
% 11.0805%

f(.5) % = .2 so ps = .5 and the second guess will be .5 - 0 / 2 = .25
abs((root - .25)/root)*100
% 44.4597%

f(.25) % = -0.8625 so ns = .25 and the third guess will be .5 - .25 / 2 = 0.3750
abs((root - 0.3750)/root)*100
% 16.6896%

f(0.3750) % = -0.3078 so ns = .375 and the forth guess will be 0.4375
abs((root - 0.4375)/root)*100
% 2.8046% and this is where we stop since its less than 10%

5.3 Determine the real root of f (x) = −26 + 85x − 91x2 +
44x3 − 8x4 + x5 :
(a) Graphically.
(b) Using bisection to determine the root to εs = 10%. Employ initial
guesses of xl = 0.5 and xu = 1.0.
(c) Perform the same computation as in (b) but use the falseposition
method and εs = 0.2 %.

a)
% a)
f = @(x) -26 + 85.*x -91.*x.^2 + 44.*x.^3 - 8.*x.^4 + 5.*x;
x = -.5:.01:1.5;
plot(x, f(x), x, 0)


b)
root = 0.557026;
f(.5) % = -1.2188 so ns = .5
f(1)  % = 5 sp ps = 1 which means the first guess is .75
abs((root - .75)/root)*100
% 34.6436%

f(.75) % = 2.8311 so ps = .75 and next guess is 0.6250
abs((root - 0.6250)/root) * 100
% 12.2030%

f(0.6250) % = 1.1950 so ps - .625 and next guess is 0.5625
abs((root - 0.5625)/ root) *100
% 0.9827% so we can stop here because its under 10%


Tuesday, May 6, 2014

Chapter 4


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; (Math.abs(((cosx - answer)/ cosx) * 100)) > 0.01; 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" + Math.abs((cosx - answer)/ cosx) * 100);
}
System.out.println("cos(x) = " + cosx);
}

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

output:
Approximation: %error
0.5558678019509788 5.430120986705642
0.5887433701749547 0.1630047502458982
0.5877699636164597 0.0026010649218886303
cos(x) = 0.5877852522924731

function [ o ] = cos2( x )
    o(1) = 1 - (x^2)/2;
    i = 2;
    cosx = cos(x);
    while abs(((cosx - o(i-1))/ cosx) * 100) > 0.01
        if (floor(i/2) * 2) == i
            o(i) = o(i-1) + (x^(i*2))/ factorial(i * 2);
        else
            o(i) = o(i-1) - (x^(i*2))/ factorial(i * 2);
        end  
        i = i + 1;
    end
end

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

>> cos2(pi/3)

ans =

    0.4517    0.5018    0.5000

>> compToCos(cos2(pi/3))

ans =

    9.6623    0.3592    0.0071

error graph:



public class Sin {
public static void main(String[] args) {
double x = (Math.PI / 3.0), sinx = Math.sin(x);
double answer = x;

System.out.println("Approximation:\t\t%error");
for(int i = 1; (Math.abs(((sinx - answer)/ sinx) * 100)) > 0.01 ; i++){
if((i & 1) == 1)
     answer = answer - ((Math.pow(x, (i * 2) + 1)/ fact((i * 2) + 1)));
else
     answer = answer + ((Math.pow(x, (i * 2) + 1)/ fact((i * 2) + 1)));
System.out.println(answer + "\t" + Math.abs((sinx - answer)/ sinx) * 100);
}
System.out.println("sin(x) = " + sinx);
}
public static int fact(int f){
int returnVal = 1;
for(int i = f; i > 1; i--)
returnVal *= i;
return returnVal;
}
}

output:
Approximation: %error
0.8558007815651173 1.1806376781374763
0.8662952837868347 0.03116305840646154
0.8660212716563725 4.7713705025174E-4
sin(x) = 0.8660254037844386

matlab:
function [ o ] = sin2( x )
    o(1) = x;
    i = 1;
    sinx = sin(x);
    while abs(((sinx - o(i))/ sinx) * 100) > 0.01
        if (floor(i/2) * 2) == i
            o(i+1) = o(i) + (x^((i*2)+1))/ factorial((i * 2)+1);
        else
            o(i+1) = o(i) - (x^((i*2)+1))/ factorial((i * 2)+1);
        end  
        i = i + 1;
    end
end

function [ o ] = compToSin( c )
    l = length(c);
    o(1:l) = abs(((sin(pi/3) - c(1:l))/ sin(pi/3)) * 100);
end

>> sin2(pi/3)

ans =

    1.0472    0.8558    0.8663    0.8660

>> compToSin(sin2(pi/3))

ans =

   20.9200    1.1806    0.0312    0.0005

error graph:




a) khan academy explains this better then I can with the told available to me. https://www.khanacademy.org/math/integral-calculus/sequences_series_approx_calc/maclaurin_taylor/v/taylor-series-at-0--maclaurin--for-e-to-the-x



because the derivative of e is e.. as shown above this comes in handy.

b)

this java part was written with the wrong formula, the correct formula is underneath in matlab:
public class Exp {
public static void main(String[] args) {
double x = -0.2, expe = Math.exp(x);
double answer = 0.0;
System.out.println("Approximation:\t\t%error");
for(int i = 0; i < 4; i++){
answer = answer + ((Math.pow(x, i) / fact(i)) );
System.out.println(answer + "\t" + Math.abs((expe - answer)/ expe) * 100);
}
System.out.println("e^-x = " + expe);
}

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

Approximation: %error
1.0 22.140275816016988
0.8 2.2877793471864036
0.8200000000000001 0.1550261691339386
0.8186666666666668 0.007827531954079692
e^-x = 0.8187307530779818

matlab:
function [ o ] = expe( n )
    Xi = .2; Xi1 = 1; h = Xi1 - Xi;
    o = (((-1).^n) .* exp(-Xi) .* (h.^n)) ./ factorial(n);
end

function [ o ] = compToE( l )
    o = abs(((exp(-1) - sum(expe(0:l)))/ exp(-1)) * 100);
end

>> for i = 0:3
e(i+1) = sum(expe(0:i));
er(i+1) = compToE(i);
end
>> plot(e)


graph of expe:

graph of error:



public class Log {
public static void main(String[] args) {
double x = 2.5, logx = Math.log(x);
double answer = 0.0;
//System.out.println(x - 1);
System.out.println("Approximation:\t\t%error");
for(int i = 1; i < 5; i++){
answer += ((Math.pow(-1,  i +1) * Math.pow(x-1, i))/i);
   System.out.println(answer + "\t" + Math.abs((logx - answer)/ logx) * 100);
}
System.out.println("ln(x) = " + logx);
}

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

Approximation: %error
1.5 63.70350019059371
0.375 59.07412495235157
1.5 63.70350019059371
0.234375 74.42132809521974
ln(x) = 0.9162907318741551

these results are all over the place.

4.5)Use zero- through third-order Taylor series expansions to predict f (3) for
f (x) = 25x3 − 6x2 + 7x − 88
using a base point at x = 1. Compute the true percent relative error εt for each approximation.

function [ o ] = poly(  )
    %f(x) = 25x^3 - 6x^2 + 7x -88
 
    o(1) = (25 - 6 + 7 - 88) * 2^0;
    o(2) = o(1) + (75 - 12 + 7) * 2^1;
    o(3) = o(2) + ((6*(25 - 2))/factorial(2)) * 2^2;
    o(4) = o(3) + (150/factorial(3)) * 2^3;
end

function [ o ] = compToPoly( c )
    x = 3;
    true = 25*(x)^3 - 6*(x)^2 +7*(x) -88
 
    l = length(c);
    o(1:l) = abs(((true - c(1:l))/ true) * 100);
end

>> compToPoly(poly)

true =

   554


ans =

  111.1913   85.9206   36.1011         0

error graph:

4.6 Use forward and backward difference approximations of O(h) and a centered difference approximation of O(h2) to estimate the first derivative of the function examined in Prob. 4.5. Evaluate the derivative at x = 2 using a step size of h = 0.2. Compare your results with the true value of the derivative. Interpret your results on the basis of the remainder term of the Taylor series expansion.

This was a fun one.
this is forward difference approximation:

function [ o ] = forw( x, s )
    o = (f(x + s) - f(x))/ s;
end

function [ o ] = f( x )
    o = 25.*(x).^3 - 6.*(x).^2 +7.*(x) -88;
end

>> forw(2,.2)

ans =  312.8000

real value is:

function [ o ] = fp( x )
    o = 75.*x.^2 - 12.*x + 7;
end

>> fp(2)

ans =  283


here is a graph of the estimated fprime at 2 against f (black is f)

for backwards i used the same functions with different inputs:
>> forw(1.8,.2)

ans =  255.2000

real value
>> fp(2)

ans =  283


for center i used the same function with different inputs, the result was a lot more acurate.

>> forw(1.8,.4)

ans =

  284.0000

>> fp(2)

ans =

   283



4.7 Use a centered difference approximation of O(h2) to estimate the second derivative of the function examined in Prob. 4.5. Perform the evaluation at x = 2 using step sizes of h = 0.25 and 0.125. Compare your estimates with the true value of the second derivative. Interpret your results on the basis of the remainder term of the Taylor series expansion.

function [ o ] = forw( x, s )
    o = (f(x + s) - f(x))/ s;
end

this is f double prime
function [ o ] = fp( x )
    o = 150 .* x - 12;
end

this is f prime
function [ o ] = f( x )
    o = 75.*x.^2 - 12.*x + 7;
end

>> forw(1.75, .5)

ans =

   288

>> fp(2)

ans =

   288

the estimation is dead on accurate.



>> forw(1.875, .25)

ans =

   288

>> fp(2)

ans =

   288

as you can see we get the same result with a smaller step size.


A = .15;
e = .9;
o = 5.67 * 10^-8;
T = 650 + (-20:20);
T2= 650 + (-40:40);

exact = abs(4*A*e*o*T.^3) .* (T - 650);
H = A*e*o*T.^4;
exact2 = abs(4*A*e*o*T2.^3) .* (T2 - 650);
H2 = A*e*o*T2.^4;
plot(T, H, T, exact, T2, H2, T2, exact2);

%plot(T2, H2, T2, exact2)

I am not sure i did this correct. however this is the graph. I thought I would see that the error is greater at the extremes but it seems to be pretty even