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:
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
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;
}
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:
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:
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
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
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home