TP3 ::Error
TP3 Error and
andApproximation
Approximation
Find the error of the equation below
1. ex , x = 0.1
π π π π
2. sinx , x = ( , , , )
8 6 5 2
π π π π
3. cosx , x = ( , , , )
8 6 5 2
4. e 0.1x
,x=(1,2,3,4)
5. ex+0.1 , x = ( 0.1 , 0.02 , 0.01 )
6. ex + sinx , x = ( 0.1 , 0.2 , 0.3 )
1
7. ex + , x = ( 0.1 , 0.2 , 0.3 )
cos2x
Solution
1.Find the error of ex, x=0.1 by using matlab
𝐚𝐚𝟐𝟐 𝐚𝐚𝟑𝟑 𝐚𝐚𝐧𝐧
Taylor’s series of 𝐞𝐞𝐱𝐱 = 1 + a + 𝟐𝟐! + + … + 𝐧𝐧! + 𝐑𝐑 𝐧𝐧
𝟑𝟑!
Code
n=5
a=0.1
vec=(1:n)
terms=a.^vec./cumprod(vec)
expval=1+cumsum(terms)
trueval=exp(a)
err=abs(trueval-expval)
err =
0.0052 0.0002 0.0000 0.0000 0.0000
2. Find the error of ex,x=(0.1,0.05,0.002,0.01) and plot the
graphic by using matlab.
𝐚𝐚𝟐𝟐 𝐚𝐚𝟑𝟑 𝐚𝐚𝐧𝐧
Taylor’s series of 𝐞𝐞𝐱𝐱 = 1 + a + 𝟐𝟐! + + … + 𝐧𝐧! + 𝐑𝐑 𝐧𝐧
𝟑𝟑!
Code
n=5
t=[0.1,0.05,0.02,0.01]
vec=(1:n)
err=[]
for i=1:length(t)
a=t(i)
terms=a.^vec./cumprod(vec)
expval=1+cumsum(terms)
trueval=exp(a)
err=[err;abs(trueval-expval)]
end
figure
plot(t,err)
legend('n=1','n=2','n=3','n=4','n=5')
err =
0.0052 0.0002 0.0000 0.0000 0.0000
0.0013 0.0000 0.0000 0.0000 0.0000
0.0002 0.0000 0.0000 0.0000 0.0000
0.0001 0.0000 0.0000 0.0000 0.0000
3. Find the error of ex, x=(0.1,0.05,0.002,0.01) and plot the
graphic (style loglog) by using matlab.
𝐚𝐚𝟐𝟐 𝐚𝐚𝟑𝟑 𝐚𝐚𝐧𝐧
Taylor’s series of 𝐞𝐞𝐱𝐱 = 1 + a + 𝟐𝟐! + + … + 𝐧𝐧! + 𝐑𝐑 𝐧𝐧
𝟑𝟑!
Code
n=5
t=[0.1,0.05,0.02,0.01]
vec=(1:n)
err=[]
for i=1:length(t)
a=t(i)
terms=a.^vec./cumprod(vec)
expval=1+cumsum(terms)
trueval=exp(a)
err=[err;abs(trueval-expval)]
end
figure
loglog(t,err)
legend('n=1','n=2','n=3','n=4','n=5')
err =
0.0052 0.0002 0.0000 0.0000 0.0000
0.0013 0.0000 0.0000 0.0000 0.0000
0.0002 0.0000 0.0000 0.0000 0.0000
0.0001 0.0000 0.0000 0.0000 0.0000
π π π π
4. Find the error of sinx, , ( x = , , , ) and plot the graphic
8 6 5 2
(style loglog) by using matlab.
𝐚𝐚𝟑𝟑 𝐚𝐚𝟓𝟓 𝐚𝐚𝟕𝟕
Taylor’s series of sinx = a - + - 𝟕𝟕! + …
𝟑𝟑! 𝟓𝟓!
Code
n=5
t=[pi/8,pi/6,pi/5,pi/2]
vec=(1:n)
err=[]
for i=1:length(t)
a=t(i)
terms=(-1).^(vec+1).*a.^(2.*vec-1)./cumprod(2.*vec-1)
expval=a+cumsum(terms)
trueval=sin(a)
err=[err;abs(trueval-expval)]
end
figure
loglog(t,err)
legend('n=1','n=2','n=3','n=4','n=5')
err =
0.0100 0.0102 0.0095 0.0096 0.0096
0.0236 0.0243 0.0216 0.0217 0.0217
0.0405 0.0422 0.0356 0.0360 0.0360
0.5708 0.7211 0.0836 0.3083 0.2467
π π π π
5. Find the error of cosx, , ( x = , , , ) and plot the graphic
8 6 5 2
(style loglog) by using matlab.
𝐚𝐚𝟐𝟐 𝐚𝐚𝟒𝟒 𝐚𝐚𝟔𝟔
Taylor’s series of cosx = 1 - + 𝟒𝟒! - +…
𝟐𝟐! 𝟔𝟔!
Code
aAll=[pi/8,pi/6,pi/5,pi/2]
vec=(1:n)
err=[]
for i=1:length(aAll)
a=aAll(i)
terms=(-1).^vec.*a.^(2*vec)./cumprod(2*vec)
expval=1+cumsum(terms)
trueval=cos(a)
err=[err;abs(trueval-expval)]
end
figure
loglog(aAll,err)
legend('n=1','n=2','n=3','n=4','n=5')
err =
0.0010 0.0020 0.0019 0.0019 0.0019
0.0031 0.0063 0.0059 0.0059 0.0059
0.0064 0.0131 0.0118 0.0119 0.0119
0.2337 0.5273 0.2144 0.3109 0.2871
6. Find the error of e0.1x, x=(1,2,3,4) and plot the graphic (style
loglog) by using matlab.
(𝟎𝟎.𝟏𝟏)𝟐𝟐𝐚𝐚𝟐𝟐 (𝟎𝟎.𝟏𝟏)𝟑𝟑𝐚𝐚𝟑𝟑 (𝟎𝟎.𝟏𝟏)𝐧𝐧 𝐚𝐚𝐧𝐧
Taylor’s series of 𝐞𝐞𝟎𝟎.𝟏𝟏𝟏𝟏 = 1 + 0.1a + + +…+ + 𝐑𝐑 𝐧𝐧
𝟐𝟐! 𝟑𝟑! 𝐧𝐧!
Code
n=5
t=[1,2,3,4]
vec=(1:n)
err=[]
for i=1:length(t)
a=t(i)
terms=((0.1*a).^vec)./cumprod(vec)
expval=1+cumsum(terms)
trueval=exp(0.1*a)
err=[err;abs(trueval-expval)]
end
figure
loglog(t,err)
legend('n=1','n=2','n=3','n=4','n=5')
err =
0.0052 0.0002 0.0000 0.0000 0.0000
0.0214 0.0014 0.0001 0.0000 0.0000
0.0499 0.0049 0.0004 0.0000 0.0000
0.0918 0.0118 0.0012 0.0001 0.0000
7. Find the error of e0.1+x, x=(0.1,0.2,0.01) and plot the graphic
(style loglog) by using matlab.
(𝟎𝟎.𝟏𝟏)𝟐𝟐𝐚𝐚𝟐𝟐 (𝟎𝟎.𝟏𝟏)𝟑𝟑𝐚𝐚𝟑𝟑 (𝟎𝟎.𝟏𝟏)𝐧𝐧 𝐚𝐚𝐧𝐧
Taylor’s series of 𝐞𝐞𝟎𝟎.𝟏𝟏𝟏𝟏 = 1 + 0.1a + + +…+ + 𝐑𝐑 𝐧𝐧
𝟐𝟐! 𝟑𝟑! 𝐧𝐧!
Code
n=5
t=[0.1,0.2,0.01]
vec=(1:n)
err=[]
for i=1:length(t)
a=t(i)
terms=(a).^(vec)./cumprod(vec)
expval=(1+cumsum(terms))*exp(0.1)
trueval=exp(a+0.1)
err=[err;abs(trueval-expval)]
end
figure
loglog(t,err)
legend('n=1','n=2','n=3','n=4','n=5')
err =
0.0057 0.0002 0.0000 0.0000 0.0000
0.0237 0.0016 0.0001 0.0000 0.0000
0.0001 0.0000 0.0000 0.0000 0.0000
8. Find the error of ex+sinx, x=(0.1,0.2,0.3) and plot the graphic
(style loglog) by using matlab.
𝐚𝐚𝟐𝟐 𝐚𝐚𝟑𝟑 𝐚𝐚𝐧𝐧 𝐚𝐚𝟑𝟑 𝐚𝐚𝟓𝟓 𝐚𝐚𝟕𝟕
Taylor’s series of 𝐞𝐞𝐱𝐱 + sinx = ( 1 + a + 𝟐𝟐! + + … + 𝐧𝐧! + 𝐑𝐑 𝐧𝐧 ) + ( a - 𝟑𝟑! + - +…)
𝟑𝟑! 𝟓𝟓! 𝟕𝟕!
Code
n=5
t=[0.1,0.2,0.3]
vec=(1:n)
err=[]
for i=1:length(t)
a=t(i)
terms=a.^(vec)./cumprod(vec)
terms1=(-1).^(vec+1).*a.^(2.*vec-1)./cumprod(2.*vec-1)
expval=1+cumsum(terms)
sinval=cumsum(terms1)
totalval=expval+sinval
trueval=exp(a)+sin(a)
err=[err;abs(trueval-totalval)]
end
figure
loglog(t,err)
legend('n=1','n=2','n=3','n=4','n=5')
err =
0.0050 0.0003 0.0002 0.0002 0.0002
0.0201 0.0027 0.0014 0.0013 0.0013
0.0454 0.0094 0.0047 0.0044 0.0044
9.Find the error of ex+(1/cos(2x)) (-1), x=(0.1,0.2,0.3) and plot the
graphic (style loglog) by using matlab.
𝟏𝟏 𝐚𝐚𝟐𝟐 𝐚𝐚𝐧𝐧 𝟏𝟏
Taylor’s series of 𝐞𝐞𝐱𝐱 + = ( 1 + a + 𝟐𝟐! + … + 𝐧𝐧! + 𝐑𝐑 𝐧𝐧 ) + 𝟒𝟒𝟒𝟒𝟐𝟐 𝟏𝟏𝟏𝟏𝐚𝐚𝟒𝟒 𝟔𝟔𝟔𝟔𝐚𝐚𝟔𝟔
𝐜𝐜𝐜𝐜𝐜𝐜(𝟐𝟐𝟐𝟐) ( 𝟏𝟏 − + − +… )
𝟐𝟐! 𝟒𝟒! 𝟔𝟔!
Code
n=5
t=[0.1,0.2,0.3]
vec=(1:n)
err=[]
for i=1:length(t)
a=t(i)
terms=a.^(vec)./cumprod(vec)
terms1=(-1).^(vec).*(2*a).^(2*vec)./cumprod(2*vec)
expval=1+cumsum(terms)
cosval=1./1+cumsum(terms1)
totalval=expval+cosval
trueval=exp(a)+1./cos(2*a)
err=[err;abs(trueval-totalval)]
end
figure
loglog(t,err)
legend('n=1','n=2','n=3','n=4','n=5')
err =
0.0455 0.0403 0.0401 0.0401 0.0401
0.1871 0.1639 0.1627 0.1626 0.1626
0.4415 0.3803 0.3768 0.3764 0.3764