You are on page 1of 15

1 Problem 1

Solution:

We will to integrate Z 2 p
1 + x3 dx
0
using Gaussian quadrature with n = 4 ,

Listing 1: Solution Problem 1


1 clc
2 clear all
3 %S o l u t i o n Q u e s t i o n 1
4 format long
5 w=[(18+ s q r t ( 3 0 ) ) /36 (18+ s q r t ( 3 0 ) ) /36 (18− s q r t ( 3 0 ) ) /36 (18− s q r t ( 3 0 ) ) / 3 6 ] %Weights
6 x=[ s q r t (3/7 −(2/7) ∗ s q r t ( 6 / 5 ) ) −s q r t (3/7 −(2/7) ∗ s q r t ( 6 / 5 ) ) s q r t ( 3 / 7 + ( 2 / 7 ) ∗ s q r t ( 6 / 5 ) ) −s q r t
( 3 / 7 + ( 2 / 7 ) ∗ s q r t ( 6 / 5 ) ) ] %p o i n t
7
8 % limits
9 a =0;
10 b=2;
11 %f u n c t i o n t o be i n t e g r a t e d
12 f=@( x ) s q r t (1+x . ˆ 3 ) ;
13
14 S o l u t i o n = 0. 5∗( b−a ) ∗w∗ f ( 0 . 5 ∗ ( b−a ) ∗x+(a+b ) / 2 ) '
15
16 %Comparing my answer
17 E r r o r=abs (3.241309265 − S o l u t i o n )

We obtain the following output:

Listing 2: Solution Problem 1


1 %Part 1
2 Solution =
3
4 3.241105700699591
5
6 %Part 2
7 Error =
8
9 2 . 0 3 5 6 4 3 0 0 4 0 9 0 2 0 4 e −04

2 Problem 2
Solution:

We will to find a zero of


  2
x + y2 − 4
 
f1 (x, y)
f (x, y) = = x2 2 =0 (1)
f2 (x, y) 9 +y −1
Using Newton’s method, the iteration formula is given by:
−1 " #
Xn2 + Yn2 − 4
    
Xn+1 Xn 2X 2Yn
= − 2Xnn Xn2
2
(2)
Yn+1 Yn 9 2Yn 9 + Yn − 1

Where    
X0 1
= (3)
Y0 1
In orden to find the first, second, third, fourth, and fifth approximation to a solution of f (x, y) = 0 and
the relative error, we make the following script:

Listing 3: Solution Problem 2


1 clc
2 clear all
3 format long
4
5 f=@( x , y ) [ xˆ2+y ˆ2 −4; xˆ2/9+y ˆ 2 − 1 ] ;%F u n c t i o n t o f i n d z e r o
6 J=@( x , y ) [ 2 ∗ x 2∗ y ; ( 2 / 9 ) ∗x 2∗ y ] ; %J a c o b i a n o f t h e f u n c t i o n
7
8 TrueSolution =[1.83711730708738;0.790569415042095];
9 N=6;
10 Xold = [ 1 ; 1 ] ;
11 RE=norm ( T r u e S o l u t i o n −Xold ) /norm ( T r u e S o l u t i o n ) ;
12 fprintf ( 't Solution R e l a t i v e E r r o r \n ' )
13 fprintf ( '1 (%1.14 f , % 1 . 1 5 f ) %1.14 f \n ' , Xold ( 1 ) , Xold ( 2 ) ,RE)
14 f o r k =2:N
15 Xnew=Xold−J ( Xold ( 1 ) , Xold ( 2 ) ) \ f ( Xold ( 1 ) , Xold ( 2 ) ) ;
16 RE=norm ( T r u e S o l u t i o n −Xnew) /norm ( T r u e S o l u t i o n ) ;
17 f p r i n t f ( '%d (%1.14 f , % 1 . 1 5 f ) %1.14 f \n ' , k , Xnew ( 1 ) ,Xnew ( 2 ) ,RE)
18 Xold=Xnew ;
19 end

and we obtain the following results:

Listing 4: Solution Problem 2


1 t Solution Relative Error
2 1 (1.00000000000000 ,1.000000000000000) 0.43145873375708
3 2 (2.18750000000000 ,0.812500000000000) 0.17553417192195
4 3 (1.86517857142857 ,0.790865384615385) 0.01403141256623
5 4 (1.83732839533611 ,0.790569470423194) 0.00010554412800
6 5 (1.83711731921321 ,0.790569415042097) 0.00000000606291
7 6 (1.83711730708738 ,0.790569415042095) 0.00000000000000

3 Problem 3
Solution:

We obtain the following table:


x0 = −1 f [x0 ] = 0
f [x0 , x1 ] = f 0 (x0 ) = 0
0.5−0 1
x1 = −1 f [x1 ] = 0 f [x0 , x1 , x2 ] = 1−(−1) = 4
1−0 1 0.25−0.25
f [x1 , x2 ] = 1−(−1) = 2 f [x0 , x1 , x2 , x3 ] = 1−(−1) =0
1−0.5 1
x2 = 1 f [x2 ] = 1 f [x1 , x2 , x3 ] = 1−(−1) = 4
f [x2 , x3 ] = f 0 (x2 ) = 1
x3 = 1 f [x3 ] = 1
So f [−1] = 0, f [−1, −1] = f 0 (−1) = 0; f [−1, −1, 1] = 14 ; f [−1, −1, 1, 1] = 0

The Hermite interpolating polynomial of degree ≤ 3 for f at the values -1,-1,1,1 is:

P (x) f [x0 ] + f [x0 , x1 ](x − x0 ) + f [x0 , x1 , x2 ](x − x0 )(x − x1 ) + f [x0 , x1 , x2 , x3 ](x − x0 )(x − x1 )(x − x2 )
=
1
P (x) = 0 + 0(x + 1) + (x + 1)2 + 0(x + 1)2 (x − 1)
4
1
P (x) = (x + 1)2
4
4 Problem 4
Solution:

We will to find the first three iterates of the Jacobi method of the following system:
     
1.0 0.5 0 x 1
0 1.0 0.5 · y  = 0 (4)
0.5 0.0 1.0 z 0

We have performed a function with the Jacobi method

Listing 5: The Jacobi method


1 f u n c t i o n [ X, i t e r ]= J a c o b i ( a , b , x0 , nmax , t o l l )
2 [ n , n]= s i z e ( a ) ;
3 iter = 0;
4 r = b−a ∗ x0 ;
5 r 0 = norm ( r ) ;
6 e r r =norm ( r ) ;
7 x = x0 ;
8 fprintf ( ' Iter Solution E r r o r \n ' )
9 f p r i n t f ( '%d (%1.4 f , % 1 . 4 f , % 1 . 4 f ) %1.4 f \n ' , i t e r , x0 ( 1 ) , x0 ( 2 ) , x0 ( 3 ) , e r r )
10 w h i l e e r r > t o l l && i t e r < nmax
11 iter = iter + 1;
12 f o r i =1:n
13 s = 0;
14 f o r j = 1 : i −1
15 s=s+a ( i , j ) ∗x ( j ) ;
16 end
17 f o r j = i +1:n
18 s=s+a ( i , j ) ∗x ( j ) ;
19 end
20 X( i ) =(b ( i )−s ) / a ( i , i ) ;
21 end
22 x=X ' ;
23 r = b−a ∗x ;
24 e r r = norm ( r ) / r 0 ;
25 f p r i n t f ( '%d (%1.4 f , % 1 . 4 f , % 1 . 4 f ) %1.4 f \n ' , i t e r , x ( 1 ) , x ( 2 ) , x ( 3 ) , e r r )
26 end

and with the following script we obtain the desired results

Listing 6: Solution Problem 4


1 clc
2 clear all
3
4 A=[1 0 . 5 0 ; 0 1 0 . 5 ; 0 . 5 0 1 ] ;
5 b=[1 0 0 ] ' ;
6 x0 =[0 0 0 ] ' ;
7 nmax=3;
8 t o l l =e p s ;
9 [ x , i t e r ]= J a c o b i (A, b , x0 , nmax , t o l l ) ;

We obtain the following results:

Listing 7: Solution Problem 4


1 Iter Solution Error
2 0 (0.0000 ,0.0000 ,0.0000) 1.0000
3 1 (1.0000 ,0.0000 ,0.0000) 0.5000
4 2 (1.0000 ,0.0000 , −0.5000) 0.2500
5 3 (1.0000 ,0.2500 , −0.5000) 0.1250
5 Problem 5
Solution:

We will to find the first three iterates of the Gauss Seidel method of the following system:
     
1.0 0.5 0 x 1
0 1.0 0.5 · y  = 0 (5)
0.5 0.0 1.0 z 0

We have performed a function with the Gauss Seidel method

Listing 8: The Gauss Seidel method


1 f u n c t i o n [ x , i t e r ]= G a u s s S e i d e l ( a , b , x0 , nmax , t o l l )
2 [ n , n]= s i z e ( a ) ;
3 iter = 0;
4 r = b−a ∗ x0 ;
5 r 0 = norm ( r ) ;
6 e r r = norm ( r ) ;
7 x o l d = x0 ;
8 fprintf ( ' Iter Solution E r r o r \n ' )
9 f p r i n t f ( '%d (%1.4 f , % 1 . 4 f , % 1 . 4 f ) %1.4 f \n ' , i t e r , x0 ( 1 ) , x0 ( 2 ) , x0 ( 3 ) , e r r )
10
11 w h i l e e r r > t o l l && i t e r < nmax
12 iter = iter + 1;
13 f o r i =1:n
14 s = 0;
15 f o r j = 1 : i −1 ,
16 s=s+a ( i , j ) ∗x ( j ) ;
17 end
18 f o r j = i +1:n
19 s=s+a ( i , j ) ∗ x o l d ( j ) ;
20 end
21 x ( i ) =(b ( i )−s ) / a ( i , i ) ;
22 end
23 X = x';
24 x o l d = X;
25 r = b−a ∗X;
26 e r r = norm ( r ) / r 0 ;
27 f p r i n t f ( '%d (%1.4 f , % 1 . 4 f , % 1 . 4 f ) %1.4 f \n ' , i t e r , x ( 1 ) , x ( 2 ) , x ( 3 ) , e r r )
28 end
29 end

and with the following script we obtain the desired results

Listing 9: Solution Problem 5


1 clc
2 clear all
3
4 A=[1 0 . 5 0 ; 0 1 0 . 5 ; 0 . 5 0 1 ] ;
5 b=[1 0 0 ] ' ;
6 x0 =[0 0 0 ] ' ;
7 nmax=3;
8 t o l l =e p s ;
9 [ x , i t e r ]= G a u s s S e i d e l (A, b , x0 , nmax , t o l l ) ;

We obtain the following results:

Listing 10: Solution Problem 5


1 Iter Solution Error
2 0 (0.0000 ,0.0000 ,0.0000) 1.0000
3 1 (1.0000 ,0.0000 , −0.5000) 0.2500
4 2 (1.0000 ,0.2500 , −0.5000) 0.1250
5 3 (0.8750 ,0.2500 , −0.4375) 0.0313

6 Problem 6
Solution:

We will to find the first four iterates to approximates the largest eigenvalue of
 
1.0 0.5 0
0.5 1.0 0.5 (6)
0 0.5 1.0

We have performed a function with the Power method

Listing 11: the Power method with Infinity Norm


1 f u n c t i o n lam=powerm (A, z0 , t o l l , nmax )
2 %Approximation o f E i g e n v a l u e s and E i g e n v e c t o r s
3 q=z0 /norm ( z0 , i n f ) ;
4 q2=q ;
5 r e s= t o l l +1;
6 n i t e r =0;
7 z=A∗q ;
8 l am ol d=norm ( z , i n f ) ;
9 w h i l e ( r e s >= t o l l && n i t e r <= nmax )
10 q=z /norm ( z , i n f ) ;
11 z=A∗q ;
12 lam=norm ( z , i n f ) ;
13 x1=q ;
14 z2=q2 ' ∗A;
15 q2=z2 /norm ( z2 , i n f ) ;
16 q2=q2 ' ;
17 y1=q2 ;
18 c o s t h e t a=abs ( y1 ' ∗ x1 ) ;
19 i f ( c o s t h e t a >= 5 e −2) ,
20 n i t e r=n i t e r +1;
21 r e s=norm ( z−lam ∗q , i n f ) / c o s t h e t a ;
22 f p r i n t f ( '%d y=[% f %f %f ] mu=%f \n ' , n i t e r , q ( 1 ) , q ( 2 ) , q ( 3 ) , la mo ld )
23 l am ol d=lam ;
24 else
25 disp ( ' Multiple e i g e n v a l u e ' ) ; break ;
26 end
27
28 end
29 end

and with the following script we obtain the desired results

Listing 12: Solution Problem 6


1 clc
2 clear all
3
4 A=[1 0 . 5 0 ; 0 . 5 1 0 . 5 ; 0 0 . 5 1 ] ;
5 x0 = [ 0 . 9 4 9 6 3 9 1 4 3 5 0 . 5 0 7 4 0 6 2 9 7 9 1 ] ' ;
6 nmax=3;
7 t o l l =e p s ;
8 lam=powerm (A, x0 , t o l l , nmax ) ;
9 Realeig =1.707106781;
10 e r r o r=norm ( R e a l e i g −lam , i n f )

We obtain the following results:


Listing 13: Solution Problem 6
1 1 y =[0.811848 1.000000 0.845825] mu=1.482226
2 2 y =[0.717313 1.000000 0.735891] mu=1.828836
3 3 y =[0.705034 1.000000 0.715794] mu=1.726602
4 4 y =[0.704528 1.000000 0.710819] mu=1.710414
5
6 error =
7
8 5 . 6 6 3 1 e −04

7 Problem 7
Solution:

We have to use the Taylor method of order 2, which iteration formula is given by:
 
h 0
yn+1 = yn + h f (tn , yn ) + f (tn , yn )
2
To solve

y 0 = y 3 f or t ∈ [0, 1] with h = 0.2, y(0) = 0.5


We have performed a function with Taylor method of order 2:

Listing 14: Taylor method of order 2


1 f u n c t i o n [ x , s o l ] = T a y l o r 2 ( f , df , i n t e r v a l , N, I n i t i a l )
2
3 a=i n t e r v a l ( 1 ) ;
4 b=i n t e r v a l ( 2 ) ;
5 h=(b−a ) /N;
6 x=a : h : b ;
7 y= I n i t i a l ;
8 t=a ;
9 s o l ( 1 )=y ;
10 f o r k =1:N
11 y=y+h ∗ ( f ( t , y ) +(h / 2 ) ∗ d f ( t , y ) ) ;
12 t=t+h ;
13 s o l ( k+1)=y ;
14 end
15 end

and with the following script we obtain the desired results

Listing 15: Solution Problem 7


1 clc
2 clear all
3
4 f a c t u a l=@( t ) 1 . / s q r t (4 −2∗ t ) ;
5 f=@( t , y ) y . ˆ 3 ;
6 d f=@( t , y ) 3∗ y . ˆ 2 ∗ y . ˆ 3 ;
7 i n t e r v a l =[0 1 ] ;
8 I n i t i a l =0.5;
9 h=0.2;
10 N=( i n t e r v a l ( 2 )− i n t e r v a l ( 1 ) ) /h ;
11 [ x , y ] = T a y l o r 2 ( f , df , i n t e r v a l , N, I n i t i a l ) ;
12
13 A c t u a l e r r o r=abs ( y−f a c t u a l ( x ) ) ;
14 disp ( 'x T a y l o r Order 2 Error ' )
15 f p r i n t f ( '%f %f %f \n ' , [ x ; y ; A c t u a l e r r o r ] )
16 figure
17 p l o t ( x , y , ' g ' , ' LineWidth ' , 3 )
18 h o l d on
19 p l o t ( x , f a c t u a l ( x ) , ' r ' , ' LineWidth ' , 3 )
20 xlabel ( ' t ' )
21 ylabel ( 'y ' ' ( t ) ' )
22 t i t l e ( ' T a y l o r o r d e r 2 Method ' )
23 l e g e n d ( ' T a y l o r Order 2 ' , ' Exact S o l u t i o n ' )
24 g r i d on
25 p r i n t −d j p e g −r 5 0 0 F i g u r e 1 . j p e g

We obtain the following results:

Listing 16: Solution Problem 7


1 x T a y l o r Order 2 Error
2 0.000000 0.500000 0.000000
3 0.200000 0.526875 0.000171
4 0.400000 0.558563 0.000454
5 0.600000 0.596679 0.000936
6 0.800000 0.643703 0.001794
7 1.000000 0.703678 0.003429

and the following plot show us the comparative between Exact Solution and the solution performed by
Taylor method of order 2

Figure 1: Plot of Taylor method of order 2.

8 Problem 8
Solution:

We have the following initial value problem:


y 000 + sin(t)y 00 + 4y 0 + cos(t)y = et ; 0 ≤ t ≤ 4
or

y 000 = et − sin(t)y 00 − 4y 0 − cos(t)y; 0 ≤ t ≤ 4


and y(0) = 0, y 0 (0) = 1, y 00 (0) = 2
The ordinary differential equation as a 1st order system of ordinary differential equations is given by:
Let be

x1 = y (7)
x2 = y0 (8)
00
x3 = y (9)

Then the equivalent system of 3 first order ODEs is:

x01 = x2 (10)
x02 = x3 (11)
x03 = et − sin(t)x3 − 4x2 − cos(t)x1 (12)

We now convert the initial conditions:

y(0) = x1 (0) = 0 (13)


0
y (0) = x2 (0) = 1 (14)
00
y (0) = x3 (0) = 2 (15)

9 Problem 9
Solution:

We have to use the shooting method (with Runge Kutta method of order 4) to solve

y 00 = y + x2 0 ≤ x ≤ 2
with boundary values y(0) = −2.0; y(2) = −6.0
In order to find the solution we will rewrite the equation as follows:
Let be

u = y (16)
0
v = y (17)

Then the equivalent system of 2 first order ODEs is:

u0 = v (18)
0 2
v = u+x (19)
(20)

We now convert the boundary conditions:


y(0) = u(0) = −2 (21)
y 0 (0) = v(0) = a (22)

Note that this system is a initial value problems, so it is not necessary satisfy the value of y(2) = −6 (
Unless we are incredibly lucky), but will help in the following way:
To solve this system we will use the Runge Kutta method of order 4:

Listing 17: Runge Kutta method of order 4


1 f u n c t i o n [ y , z , t ] = RK4System ( f , g , a , b , N, y0 )
2 h=(b−a ) /N;
3 t=a : h : b ;
4 y=z e r o s ( l e n g t h ( t ) , 1 ) ;
5 z=z e r o s ( l e n g t h ( t ) , 1 ) ;
6 y ( 1 )=y0 ( 1 ) ;
7 z ( 1 )=y0 ( 2 ) ;
8 f o r i =1:N
9 k1=f ( t ( i ) , y ( i ) , z ( i ) ) ;
10 l 1=g ( t ( i ) , y ( i ) , z ( i ) ) ;
11
12 k2=f ( t ( i )+h / 2 , y ( i ) +0.5∗ h∗k1 , z ( i ) +0.5∗ h∗ l 1 ) ;
13 l 2=g ( t ( i )+h / 2 , y ( i ) +0.5∗ h∗k1 , z ( i ) +0.5∗ h∗ l 1 ) ;
14
15 k3=f ( t ( i )+h / 2 , y ( i ) +0.5∗ h∗k2 , z ( i ) +0.5∗ h∗ l 2 ) ;
16 l 3=g ( t ( i )+h / 2 , y ( i ) +0.5∗ h∗k2 , z ( i ) +0.5∗ h∗ l 2 ) ;
17
18 k4=f ( t ( i )+h , y ( i )+h∗k3 , z ( i )+h∗ l 3 ) ;
19 l 4=g ( t ( i )+h , y ( i )+h∗k3 , z ( i )+h∗ l 3 ) ;
20
21 y ( i +1)=y ( i ) +(1/6) ∗ ( k1+2∗k2+2∗k3+k4 ) ∗h ;
22 z ( i +1)=z ( i ) +(1/6) ∗ ( l 1 +2∗ l 2 +2∗ l 3+l 4 ) ∗h ;
23 end
24 end

and with the following script we obtain two solution of the above system:

• First Solution:

u0 = v (23)
0 2
v = u+x (24)
(25)

where

u(0) = −2 (26)
v(0) = −2 (27)

• Second Solution

u0 = v (28)
0 2
v = u+x (29)
(30)

with
u(0) = −2 (31)
v(2) = 2 (32)

Then using the shooting method we obtain the desired solution:

Listing 18: Solution Problem 9


1 clc
2 clear all
3
4 f=@( x , y , z ) z ;
5 g=@( x , y , z ) y+x ˆ 2 ;
6 f a c t u a l=@( x ) −2−x . ˆ 2 ;
7
8 %F i r s t a p r o x i m a t i o n
9 a =0; b=2;
10 y01=[−2 − 2 ] ;
11 h=0.2;
12 N=(b−a ) /h ;
13 [ y1 , z1 , t 1 ] = RK4System ( f , g , a , b , N, y01 ) ;
14
15 %Second a p r o x i m a t i o n
16 y02=[−2 2 ] ;
17 h=0.2;
18 N=(b−a ) /h ;
19 [ y2 , z2 , t 2 ] = RK4System ( f , g , a , b , N, y02 ) ;
20
21
22 %S h o o t i n g Method
23 b e t a =−6;
24 y1n=y1 ( end ) ;
25 y2n=y2 ( end ) ;
26 y0=[−2 y01 ( 2 ) +( beta−y1n ) ∗ ( y02 ( 2 )−y01 ( 1 ) ) / ( y2n−y1n ) ] ;
27
28 a =0; b=2;
29 h=0.2;
30 N=(b−a ) /h ;
31 [ y , z , t ] = RK4System ( f , g , a , b , N, y0 ) ;
32
33 t r u e s o l=f a c t u a l ( t ) ;
34
35 f p r i n t f ( ' Time S h o o t i n g Method Exact S o l u t i o n \n ' )
36 f p r i n t f ( '%f , %f , %f \n ' , [ t ; y ' ; t r u e s o l ] )
37
38 figure
39 p l o t ( t , y1 , ' ko− ' , ' LineWidth ' , 3 )
40 h o l d on
41 p l o t ( t , y2 , ' bo− ' , ' LineWidth ' , 3 )
42 h o l d on
43 p l o t ( t , y , ' ro− ' , ' LineWidth ' , 3 )
44 h o l d on
45 plot (t , truesol , ' go− ' , ' LineWidth ' , 2 )
46
47 l e g e n d ( ' F i r s t a p r o x i m a t i o n ' , ' Second a p r o x i m a t i o n ' , ' S h o o t i n g Method ' , ' Exact S o l u t i o n ' )
48 t i t l e ( ' S h o o t i n g Method : y”=y+x ˆ 2 , y ( 0 ) =−2,y ( 2 )=−6 ' )
49 p r i n t −d j p e g −r 5 0 0 F i g 2 . j p e g

We obtain the following results:

Listing 19: Solution Problem 9


1 Time S h o o t i n g Method Exact S o l u t i o n
2 0.000000 , −2.000000 , −2.000000
3 0.200000 , −2.040004 , −2.040000
4 0.400000 , −2.160008 , −2.160000
5 0.600000 , −2.360010 , −2.360000
6 0.800000 , −2.640011 , −2.640000
7 1.000000 , −3.000012 , −3.000000
8 1.200000 , −3.440011 , −3.440000
9 1.400000 , −3.960010 , −3.960000
10 1.600000 , −4.560008 , −4.560000
11 1.800000 , −5.240004 , −5.240000
12 2.000000 , −6.000000 , −6.000000

In the following plot we can see the two solution explain above and the solution given by the Shooting
method

Figure 2: Plot Shooting Method.

10 Problem 10
Solution:

We have to use finite difference to solve

y 00 = y + x2 0 ≤ x ≤ 2
with boundary values y(0) = −2.0; y(2) = −6.0
note that:
y(x−∆x)−2y(x)+y(x+∆x)
• y 00 (x) ≈ ∆x2

so for xj = j × h, with 0 ≤ j ≤ 10
y 00 (xj ) − y(xj ) = x2j (33)
y(xj − ∆x) − 2y(xj ) + y(xj + ∆x)
− y(xj ) = x2j (34)
∆x2
y(xj − ∆x) − (2 + ∆x2 )y(xj ) + y(xj + ∆x)
= x2j (35)
∆x2
(36)
So we have the following scheme
1
yj−1 − (2 + ∆x2 )yj + yj+1 = x2j

∆x 2

where,
• j = 2, · · · , N − 1
• y1 = −2 and yN = −6
• x = 0 : ∆x : 2
We can rewrite equation in a matrix form as follows:

1
−(2 + ∆x2 ) x22 − ∆x
    
1 y2 2 y1
 1 −(2 + ∆x2 ) 1    y3 x3 2 
1  1 −(2 + ∆x2 ) 1
  
y4 x24

=
    
2
∆x 
 
.. ..

..    
 .    . . 
2 1
1 −(2 + ∆x ) yN −1 x2N −1 − ∆x2 yN
(37)
We do this using the following matlab code

Listing 20: Solution Problems 10


1 clc
2 clear all
3 format long
4 f e x a c=@( x ) −2−x . ˆ 2 ;
5 h = 0 . 2 ;%s t e p S i z e
6 a=−2;%t h e t a ( 0 )=−2
7 b=−6;%t h e t a ( 2 )=−6
8
9 x =0:h : 2 ;
10 N=l e n g t h ( x ) ;
11
12 B=d i a g ( −(hˆ2+2) ∗ o n e s (N−2 ,1) )+d i a g ( o n e s (N−3 ,1) , 1 )+d i a g ( o n e s (N−3 ,1) , −1) ;
13 B=(1/h ˆ 2 ) ∗B ;
14
15 q=z e r o s (N−2 ,1) ;
16 q ( 1 )=x ( 2 ) ˆ2−a /h ˆ 2 ;
17 q (N−2)=x (N−1)ˆ2−b/h ˆ 2 ;
18 q ( 2 : N−3)=x ( 3 : N−2) . ˆ 2 ;
19
20 S o l u t i o n=B\q ;
21 S o l u t i o n =[ a S o l u t i o n ' b ] ;
22
23 figure
24 p l o t ( x , S o l u t i o n , ' go− ' , ' LineWidth ' , 5 )
25 hold
26 p l o t ( x , f e x a c ( x ) , ' ko− ' , ' LineWidth ' , 3 )
27 l e g e n d ( ' t h e f i n i t e d i f f e r e n c e method ' , ' Exact S o l u t i o n ' )
28 t i t l e ( ' y ' '−y=x ˆ2 ' )
29 g r i d on
30 p r i n t −d j p e g −r 5 0 0 F i g 3 . j p e g
We obtain the following results:

Listing 21: Solution Problem 10


1 Solution Exact S o l u t i o n
2 −2.000000 , −2.000000
3 −2.040000 , −2.040000
4 −2.160000 , −2.160000
5 −2.360000 , −2.360000
6 −2.640000 , −2.640000
7 −3.000000 , −3.000000
8 −3.440000 , −3.440000
9 −3.960000 , −3.960000
10 −4.560000 , −4.560000
11 −5.240000 , −5.240000
12 −6.000000 , −6.000000

In the following plot we can see the solution by the finite difference method

Figure 3: Plot Shooting Method.

11 Problem 11
Solution:

We have to use finite difference to solve

∂2u ∂2u
+ 2 = 0 0 ≤ x ≤ 1; 0 ≤ y ≤ 1
∂x2 ∂y
with u(x, y) = x3 − xy 2 on boundary of the region
note that:
∂2u u(x−∆x,y)−2u(x,y)+u(x+∆x,y)
• ∂x2 (x, y) ≈ ∆x2

∂2u u(x,y−∆x)−2u(x,y)+u(x,y+∆x)
• ∂y 2 (x, y) ≈ ∆y 2

We do this using the following matlab code

Listing 22: Solution Problems 11


1 clc
2 clear all
3 %%%%% I n i t i a l i z a t i o n %%%%%%%%%%%%%
4 a =0;
5 b=1;
6 c =0;
7 d=1;
8 Dx = 0 . 2 5 ; %f i r s t d i s c r e t i s e d s p a c e
9 Dy = 0 . 2 5 ; %f i r s t d i s c r e t i s e d s p a c e
10 SpaceX =(b−a ) /Dx ; %Equal t o i n i t i a l i s e d a r r a y
11 SpaceY =(d−c ) /Dy ; %Equal t o i n i t i a l i s e d a r r a y
12 T = z e r o s ( SpaceX +1 , SpaceY+1) ;
13
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 xx=a : Dx : b ;
16 yy=c : Dy : d ;
17
18 U=@( x , y ) x .ˆ3 −3∗ x . ∗ y . ˆ 2 ;
19 T s o l E x a c t = z e r o s ( SpaceX +1 , SpaceY+1) ;
20 T s o l E x a c t ( : , 1 )=U( xx ( 1 ) , yy ) ;
21 T s o l E x a c t ( : , 2 )= f l i p l r (U( xx ( 2 ) , yy ) ) ;
22 T s o l E x a c t ( : , 3 )= f l i p l r (U( xx ( 3 ) , yy ) ) ;
23 T s o l E x a c t ( : , 4 )= f l i p l r (U( xx ( 4 ) , yy ) ) ;
24 T s o l E x a c t ( : , 5 )= f l i p l r (U( xx ( 5 ) , yy ) ) ;
25 %%%%%%% Boundary C o n d i t i o n s %%%%%%
26 T( end , : ) = xx . ˆ 3 ;
27 T ( 1 , : )= xx .ˆ3 −3∗ xx ;
28 T ( : , end )= f l i p l r (1 −3∗ yy . ˆ 2 ) ;
29
30 % % %F i n a l l y , run a l o o p o v e r t h e e q u a t i o n i n t h e time a s w e l l a s t h e s p a c e domains t o g e t
the s o l u t i o n .
31 % %%%%%%%%% S o l u t i o n %%%%%%%%%%%%%%%
32 N=10;
33 f o r t =1:N
34 Told=T ;
35 f o r j =2: SpaceY %o v e r spaceX
36 f o r i =2: SpaceX %o v e r spaceY
37 T( i , j ) = (Dyˆ 2 ∗ ( Told ( i −1 , j )+Told ( i +1 , j ) )+Dxˆ 2 ∗ ( Told ( i , j −1)+Told ( i , j +1) ) ) / ( 2 ∗ (Dyˆ2+Dxˆ 2 ) ) ;
38 end
39 end
40
41 end
42 Tsol Exact
43 T
44 Error INFINITY NORM=norm ( T s o l E x a c t −T, i n f )

We obtain the following results in one step time:

Listing 23: Solution Problem 11


1
2 Tsol Exact =
3
4 0 −0.734375000000000 −1.375000000000000 −1.828125000000000 −2.000000000000000
5 0 −0.406250000000000 −0.718750000000000 −0.843750000000000 −0.687500000000000
6 0 −0.171875000000000 −0.250000000000000 −0.140625000000000 0.250000000000000
7 0 −0.031250000000000 0.031250000000000 0.281250000000000 0.812500000000000
8 0 0.015625000000000 0.125000000000000 0.421875000000000 1.000000000000000
9
10
11 T =
12
13 0 −0.734375000000000 −1.375000000000000 −1.828125000000000 −2.000000000000000
14 0 −0.183593750000000 −0.343750000000000 −0.628906250000000 −0.687500000000000
15 0 0 0 0.062500000000000 0.250000000000000
16 0 0.003906250000000 0.031250000000000 0.308593750000000 0.812500000000000
17 0 0.015625000000000 0.125000000000000 0.421875000000000 1.000000000000000
18
19
20 Error INFINITY NORM =
21
22 0.812500000000000

We obtain the following results in 100 step time:

Listing 24: Solution Problem 11


1
2
3 Tsol Exact =
4
5 0 −0.734375000000000 −1.375000000000000 −1.828125000000000 −2.000000000000000
6 0 −0.406250000000000 −0.718750000000000 −0.843750000000000 −0.687500000000000
7 0 −0.171875000000000 −0.250000000000000 −0.140625000000000 0.250000000000000
8 0 −0.031250000000000 0.031250000000000 0.281250000000000 0.812500000000000
9 0 0.015625000000000 0.125000000000000 0.421875000000000 1.000000000000000
10
11
12 T =
13
14 0 −0.734375000000000 −1.375000000000000 −1.828125000000000 −2.000000000000000
15 0 −0.406250000000000 −0.718750000000000 −0.843750000000000 −0.687500000000000
16 0 −0.171875000000000 −0.250000000000000 −0.140625000000000 0.250000000000000
17 0 −0.031250000000000 0.031250000000000 0.281250000000000 0.812500000000000
18 0 0.015625000000000 0.125000000000000 0.421875000000000 1.000000000000000
19
20
21 Error INFINITY NORM =
22
23 7 . 7 7 1 5 6 1 1 7 2 3 7 6 0 9 6 e −16

You might also like