Laboratory Exercise 4 Naval.nb

DERIVATIVE
Exercise 4 - 1
Theoretical solution
f@x_D := Sech@xD
th1 = D@f@xD, xD . x ® 1;
th2 = D@D@f@xD, xD, xD . x ® 1;
Print@"1st Derivative : ", D@f@xD, xDD
Print@"1st Derivative at x=1 : ", N@th1, 7DD
Print@"2nd Derivative : ", Simplify@D@D@f@xD, xD, xDDD
Print@"2nd Derivative at x=1 : ", N@th2, 7DD
1st Derivative : -Sech@xD Tanh@xD
1st Derivative at x=1 : -0.4935543
H-3 + Cosh@2 xDL Sech@xD3
2
2nd Derivative at x=1 : 0.1037219
2nd Derivative :
1
Numerical solution
z1 = Hf@1 + 0.1D - f@1DL  H0.1L;
z2 = Hf@1 + 0.1D - f@1 - 0.1DL  H2 ´ 0.1L;
z3 = Hf@1 + 0.1D - 2 f@1D + f@1 - 0.1DL  H0.1L ^ 2;
Print@"1st derivative with h=0.1 at x=1 forward : ",
z1, "
error : ", Abs@th1 - z1DD
Print@"1st derivative
central : ",
z2, "
error : ", Abs@th1 - z2DD
Print@"2nd derivative
: ",
z3, "
error : ", Abs@th2 - z3DD
2
Laboratory Exercise 4 Naval.nb
1st derivative with h=0.1 at x=1 forward :
-0.4872021
error : 0.006352217
1st derivative
-0.4923029
central :
error : 0.001251445
2nd derivative
0.1020154
error : 0.001706506
:
Laboratory Exercise 4 Naval.nb
INTEGRAL
Exercise 4.2
.1
Theoretical solution
f@x_D := x ^ 2 Sqrt@1 + x ^ 2D
Print@"Theoretical solution : ",
Integrate@f@xD, xDD
Print@"Theoretical value
: ",
Integrate@f@xD, 8x, 0.4, 1<DD
Plot@f@xD, 8x, 0.4, 1<,
AxesOrigin ® 80, 0<, PlotRange ® All,
BaseStyle ® 8FontFamily ® "Arial", FontSize ® 12<,
AxesLabel ® 8"x", "fHxL"<,
PlotStyle ® 8Red, [email protected]<D
3
4
Laboratory Exercise 4 Naval.nb
Theoretical solution :
Theoretical value
1 + x2 Ix + 2 x3 M - ArcSinh@xD
1
8
: 0.3978286
fHxL
1.4
1.2
1.0
0.8
0.6
0.4
0.2
0.2
0.4
0.6
0.8
1.0
x
Numerical solutions
Composite Trapezoidal
f@x_D := x ^ 2 Sqrt@1 + x ^ 2D; a = 0.4; b = 1; h = 0.1; n =
b-a
h
S0 = N@f@aDD + N@f@bDD; S1 = 0;
Print@"i=", 0, " , ", " x", 0, "= ", a, "
Do@y = a + i * h; S1 += N@f@yDD; Print@"i=", i, "
,
,
", N@f@
", "
Print@"i=", n, " ,
,
", N@f@
", "
x", n, "= ", b, "
Print@"Composite Trapezoidal IHfL=", SD
tv = NIntegrate@f@xD, 8x, 0.4, 1<D; Print@"Theoretical value
Laboratory Exercise 4 Naval.nb
i=0
,
x0=
0.4
,
0.1723253
i=1
,
x1=
0.5
,
0.2795085
i=2
,
x2=
0.6
,
0.4198285
i=3
,
x3=
0.7
,
0.5981212
i=4
,
x4=
0.8
,
0.8195999
i=5
,
x5=
0.9
,
1.089744
,
1.414214
i=6. ,
x6.= 1
Composite Trapezoidal IHfL=0.4000071
Theoretical value=0.3978286
,
error=0.002178486
5
6
Laboratory Exercise 4 Naval.nb
Composite Simpson
f@x_D := x ^ 2 Sqrt@1 + x ^ 2D; a = 0.4; b = 1; h = 0.1; n =
b-a
h
S0 = N@f@aDD + N@f@bDD; S1 = 0; S2 = 0;
Do@y = a + i * h; If@EvenQ@iD, S2 += N@f@yDD, S1 += N@f@yDDD
1
S=
h HS0 + 2 S2 + 4 S1L;
3
Print@"IHfL=", SD
tv = NIntegrate@f@xD, 8x, 0.4, 1<D; Print@"Theoretical value
IHfL=0.3978296
Theoretical value=0.3978286
,
error=9.994546 ´ 10-7
Laboratory Exercise 4 Naval.nb
7
Composite Simpson 3  8
Clear@xD; f@x_D := x ^ 2 Sqrt@1 + x ^ 2D; a = 0.4; b = 1; h = 0.1
S0 = N@f@aDD + N@f@bDD; S1 = 0; S2 = 0; S3 = 0;
Print@"i=", 0, " , ", " x", 0, "= ", a, "
Do@y = a + i * h; S1 += N@f@yDD; Print@"i=", i, "
Do@y = a + i * h; S2 += N@f@yDD; Print@"i=", i, "
Do@y = a + i * h; S3 += N@f@yDD; Print@"i=", i, "
Print@"i=", n, " , ", " x", n, "= ", b, "
3
S=
h HS0 + 3 S1 + 3 S2 + 2 S3L;
8
Print@"Composite 38 Simpson IHfL=", N@SD, "
i=0
,
x0=
0.4
,
0.1723253
i=1
,
x1=
0.5
,
0.2795085
i=4
,
x4=
0.8
,
0.8195999
i=2
,
x2=
0.6
,
0.4198285
i=5
,
x5=
0.9
,
1.089744
i=3
,
x3=
0.7
,
0.5981212
,
1.414214
i=6. ,
x6.= 1
Composite 38 Simpson IHfL=0.3978309
Absolute error
=2.225646 ´ 10-6
,
,
,
,
,
", N@f@
", "
", "
", "
", N@f@
Absolute
8
Laboratory Exercise 4 Naval.nb
NATURAL CUBIC SPLINE
In[22]:=
f@x_D := x ^ 2 Sqrt@1 + x ^ 2D;
x0 = 0.4;
x1 = 0.7;
x2 = 1;
y0 = f@x0D;
y1 = f@x1D;
y2 = f@x2D;
s@x_D := a0 + a1 x + c0 If@x < x0, 0, Hx - x0L ^ 3D +
c1 If@x < x1, 0, Hx - x1L ^ 3D + c2 If@x < x2, 0, Hx - x2L ^ 3D
y = c0 + c1 + c2;
z = c0 x0 + c1 x1 + c2 x2;
sol = Solve@8s@x0D Š y0, s@x1D Š y1, s@x2D Š y2, y Š 0, z Š 0<,
8a0, a1, c0, c1, c2<D
s1@x_D := s@xD . sol
tv = Integrate@f@xD, 8x, x0, x2<D;
nv = Integrate@s1@xD, 8x, x0, x2<D;
Print@"Spline sHxL=", s1@xDD
Print@"Approximation of I using sHxL : ",
nv, ",
error : ", Abs@tv - nvDD
data = 88x0, y0<, 8x1, y1<, 8x2, y2<<;
fgr1 = ListPlot@data, PlotStyle ® Red, PlotMarkers ® "æ"D;
fgr2 = Show@Plot@s1@xD, 8x, x0 + 0.005, x1 - 0.005<,
PlotStyle ® 8Blue, [email protected]<D,
Plot@s1@xD, 8x, x1 + 0.005, x2 - 0.005<, PlotStyle ®
8Green, [email protected]<D, AxesOrigin ® 80, 0<D;
fgr3 = Plot@f@xD, 8x, x0, x2<, PlotStyle ®
8Brown, [email protected]<, AxesLabel ® 8"x", "fHxL"<,
BaseStyle ® 8FontFamily ® "Arial", FontSize ® 14<,
AxesOrigin ® 80, 0<D
fgr = Show@fgr1, fgr2, PlotRange ® All,
AxesLabel ® 8"x", "sHxL"<,
BaseStyle ® 8FontFamily ® "Arial", FontSize ® 14<,
AxesOrigin ® 80, 0<D
Laboratory Exercise 4 Naval.nb
Out[32]=
88a0 ® -0.2653039, a1 ® 1.094073,
c0 ® 3.613855, c1 ® -7.227711, c2 ® 3.613855<<
Spline sHxL=9-0.2653039 +
1.094073 x + 3.613855 IfAx < 0.4, 0, Hx - x0L3E 7.227711 IfAx < 0.7, 0, Hx - x1L3E +
3.613855 IfAx < 1, 0, Hx - x2L3E=
Approximation of I using sHxL :
80.4027811<,
error : 80.004952452<
fHxL
1.4
1.2
1.0
Out[41]=
0.8
0.6
0.4
0.2
0.2
0.4
0.6
0.8
sHxL
1.4
1.0
x
æ
1.2
1.0
Out[42]=
0.8
0.6
æ
0.4
0.2
æ
0.2
0.4
0.6
0.8
1.0
x
9