Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ENGR 516 Assignment 1 DuongLe

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

ENGR 516 Computational Methods for Graduate Students

Assignment#1
Name: DUONG LE
Due: 02/18/2013

PROBLEM 2: Solution by Iteration:


1/ f(x) = tan(pi - x) - x = 0

a/ Solve by Newton Raphson Method:

( )

( )

))

XK+1 = xK f(xK)/f(xK)
XK+1= xK [tan(pi-x) x] / [-2 tan2(pi-x)]

(1)

The condition of equation (1) is x /2 +k2


There are 2 separate areas. One is from - /2 to + /2, the other one is from /2 to 3/2

For the first area (from - /2 to /2): To find the root, let x0 = 1, the condition is |f(x)| < 10-10
Using Matlab to obtain these following data
With x0=1
K
x
1
1
2
0.422122506263781
3
0.0264103193380855
4
6.14172788135359e-06
5
2.11419423634673e-18
f(x) meet the condition when x = 2.11419423634673e-18 0

|f(x)|
2.55740772465490
0.871243263107487
0.0528267808328284
1.22834557629346e-05
1.24578874151082e-16

With x0=-1
K
x
1
-1
2
-0.422122506263781
3
-0.026410319338086
4
-6.141727881506243e-06
5
-2.009068977263002e-16
f(x) meet the condition when x = -2.009068977263002e-16 0

|f(x)|
2.55740772465490
0.871243263107486
0.0528267808328282
1.22834557628423e-05
7.84422178115649e-17

For the second area (from /2 to 3/2): To find the root, let x0 = 3, the condition is |f(x)| < 10-10
With x0=3
K
x
1
3
2
1.58564279909040
3
1.60013441776625
4
1.62805477384048
5
1.67968726477802
6
1.76685216638210
..
..
11
2.02875783811043
f(x) meet the condition when x = 2.0287

|f(x)|
2.85745345692572
65.7654764727369
32.4754663652182
15.8175269586881
7.46748768365328
3.26821548564776
..
1.56050861122026e-09

With x0=-3
K
x
1
-3
2
-1.58564279909040
3
-1.60013441776625
4
-1.62805477384048
5
-1.67968726477802
6
-1.76685216638210
..
..
11
-2.02875783785528
f(x) meet the condition when x = -2.0287

Result: f(x) has 3 roots: 0, 2.0287

|f(x)|
2.85745345692572
65.7654764727380
32.4754663652187
15.8175269586883
7.46748768365342
3.268215485647831
..
1.56050949939868e-09

Matlab code (Change the value of x(1) at line 3 to 1 and 3 for each case of x0)
clear;
clc;
x(1)= 1;
Epsilon = 10^(-10);
k=1;
delta(k) = abs(tan(pi-x(k))-x(k));
while(true)
k= k+1;
x(k) = x(k-1) - (tan(pi-x(k-1))-x(k-1))/(-2 - (tan(pi-x(k-1)))^2);
delta(k) = abs(tan(pi-x(k))-x(k));
if delta(k)< Epsilon
break;
end
end

b/ Solve by Regula-Falsi Method:


( )
( )

(
(

)
)

Let =10-10
For the first area (from - /2 to /2): choose two initial values of x(1) = -1.5 and x(2) =1
k
1
2
3
4
5
.
110

x
-1.5
1
0.647911229337960
0.470479978993301
0.354126331991277
.
4.173476495865219e-11
In the first area, the roots is approximately 0

f(x)
15.601419947171696
-0.557407724700000
-1.404824955284764
-0.979049851103723
-0.723838055822700
.
-8.346950318662128e-11

For the second area (from /2 to 3/2):


Choose two initial values of x(1) = 2 and x(2) = 3
k
1
2
3
4
5
..
11

x
2
3
2.06081849450047
2.03032146541832
2.02883519360745
..
2.02875783811157

f(x)
0.185039863261518
-2.857453457
-0.186111258588269
-0.00953763433657784
-0.00047303316844793
..
-6.96642743491793e-12

Choose two initial values of x(1) = -3 and x(2) = -2


k
x
1
-3
2
-2
3
-2.06081849450047
4
-2.03032146541832
5
-2.02883519360745
..
..
11
-2.02875783811157
In the second area, the roots are 2.0287

f(x)
2.857453457
-0.185039863261520
0.186111258588268
0.00953763433657962
0.000473033168448822
..
6.96775970254748e-12

Result: f(x) has 3 roots: 0, 2.0287

Matlab code (change the value of x(1) and x(2) at line 3 and 4 for desired initial numbers)
clear;
clc;
x(1) = -1.5;
x(2) = 1;
Epsilon = 10^(-10);
f(1) = tan(pi-x(1))-x(1);
f(2) = tan(pi-x(2))-x(2);
if f(1)*f(2) > 0
break;
end;
for k=2:1000
x(k+1)= (f(2)*x(1)- f(1)*x(2))/(f(2)-f(1));
f(k+1) = tan(pi-x(k+1))-x(k+1);
if f(k+1)==0,
break;
elseif f(2)*f(k+1)>0
x(2)=x(k+1);
f(2)=f(k+1);
else
x(1)=x(k+1);
f(1)=f(k+1);
end
if abs(f(k+1))<Epsilon,
break,
end
end

2/ f(x) = f(x) = (1/125) (x2 - 25)(x -10) 5

a/ Solve by Newton Raphson Method:

f(x) = (1/125) (x2 - 25)(x -10) 5


f(x) = (1/125) [2x(x-10) + (x2 -25)] = (1/125) ( 3x2 20x -25)

XK+1 = xK f(xK)/f(xK)
Let =10-10
K
1
2
3
4
5
6
7

x
10
18.3333333333333
15.1426426426426
13.9604605349711
13.7898198718246
13.7863959715875
13.7863959715875

|f(x)|
5
15.7407407407408
3.40511975669093
0.382882379195851
2.94165558756276e-06
8.76683055349542e-08
4.68070027181966e-13

Result: The root of f(x) is 13.78639


Matlab Code:
clear;
clc;
x(1)= 10;
Epsilon = 10^(-10);
k=1;
delta(k) = abs((1/125)*(x(k)*x(k)-25)*(x(k)-10)-5);
while(true)
k= k+1;
x(k) = x(k-1) - ((1/125)*(x(k-1)*x(k-1)-25)*(x(k-1)-10)5)/((1/125)*(3*x(k-1)*x(k-1) -20*x(k-1)-25));
delta(k) = abs((1/125)*(x(k)*x(k)-25)*(x(k)-10)-5);
if delta(k)< Epsilon
break;
end
end

b/ Solve by Regula-Falsi Method:


( )
( )

(
(

)
)

Let =10-10
Choose two initial values of x(1) = 10 and x(2) =20
K
1
2
3
4
5
..
36

x
10
20
11.666666666666666
12.694805194805193
13.252086742951052
..
13.786394606985908

f(x)
-5
25
-3.518518518518519
-2.064644012590549
-1.081422715779407
..
-5.632116995002434e-11

Result: The root of f(x) is 13.78639


Matlab Code:
clear;
clc;
x(1) = 10;
x(2) = 20;
Epsilon = 10^(-10);
f(1) = (1/125)*(x(1)*x(1) -25)*(x(1)-10) -5;
f(2) = (1/125)*(x(2)*x(2) -25)*(x(2)-10) -5;
if f(1)*f(2) > 0
break;
end;
for k=2:1000
x(k+1)= (f(2)*x(1)- f(1)*x(2))/(f(2)-f(1));
f(k+1) = (1/125)*(x(k+1)*x(k+1) -25)*(x(k+1)-10) -5;
if f(k+1)==0,
break;
elseif f(2)*f(k+1)>0
x(2)=x(k+1);
f(2)=f(k+1);
else
x(1)=x(k+1);
f(1)=f(k+1);
end
if abs(f(k+1))<Epsilon,
break,
end
end

PROBLEM 3: Cubic Spline:


The input data:
xj
-5.8 -5.0 -4.0 -2.5 -1.5 -0.8 0
0.8
1.5
2.5 4.0 5.0 5.8
fj
0
1.5
1.8
2.2
2.7
3.5
3.9 3.5
2.7
2.2 1.8 1.5 0
Because this graph is symmetry with x= 0 being the center, so I just need to do with the right
hand side, the left hand side is similar
Data point of the right hand side is:
i
xj
fj

1
0
3.9

2
0.8
3.5

3
1.5
2.7

4
2.5
2.2

5
4.0
1.8

6
5.0
1.5

7
5.8
0

I use the following equation from the book (equation 9 page 823 from the book):
(
( )

Where

) and

( ) and

With j = 1, 2, 3, 4, 5, 6
The matrix form of above equation is: A.K=B
With A is the coefficient matrix of K and B is the matrix of right hand side
Note: With the normal Cubic Spline method:
A(1, 2:7)= 0 and A(1,1)=1 .
At j=7, A(7, 2:6)=0 and A(7,7)=1, B(1) = 0 and B(7) = 0.
Using Matlab to get the result of A, B and K
Matrix A:
j=1
j=2
j=3
j=4
j=5
j=6
j=7

1
1.2500
0
0
0
0
0

0
5.3571
1.4286
0
0
0
0

0
1.4286
4.8571
1
0
0
0

0
0
1
3.3333
0.6667
0
0

0
0
0
0.6667
3.3333
1
0

0
0
0
0
1
4.50000
0

0
0
0
0
0
1.25000
1

Matrix B:
j=1
j=2
j=3
j=4
j=5
j=6
j=7

0
-6.7730
-6.3980
-2.0333
-1.4333
-7.9313
0

Matrix K:
j=1
0
j=2
-1.0124
j=3
-0.9447
j=4
-0.3633
j=5
0.1837
j=6
-1.8033
j=7
0
The cubic polynomial form (equation 12 page 823 from the book):
( )

With j = 1, 2, 3, 4, 5, 6, 7. Using Taylors formula, we obtain:

Using Matlab to calculate the cubic polynomials coefficient

j=1
j=2
j=3
j=4
j=5
j=6

3.9000
3.5000
2.7000
2.2000
1.8000
1.5000

-1.5265e-15
-1.0124
-0.9447
-0.3633
0.1837
-1.8033

-0.6095
-0.6559
0.7527
-0.1713
0.5360
-2.5230

-0.0193
0.6708
-0.3080
0.1572
-1.0197
3.0417

The graph of right hand side:


4

3.5

2.5

1.5

0.5

Repeat the same process for the left hand side of graph:
Data point of the left hand side is:
i
xj
fj

1
-5.8
0

2
-5.0
1.5

3
-4.0
2.8

4
-2.5
2.2

5
6
-1.5 -0.8
2.7 3.5

7
0
3.9

Using Matlab to calculate the cubic polynomials coefficient

j=1
j=2
j=3
j=4
j=5
j=6

0
1.5000
1.8000
2.2000
2.7000
3.5000

1.2096e-15
1.8033
-0.1837
0.3633
0.9447
1.0124

4.7771
-2.5230
0.5360
-0.1713
0.7527
-0.6559

-3.0417
1.0197
-0.1572
0.3080
-0.6708
0.0193

The graph of left hand side:

3.5

2.5

1.5

0.5

0
-6

-5

-4

-3

-2

-1

-4

-2

Entire graph
4

3.5

2.5

1.5

0.5

0
-6

Matlab code
(Change the X and Y value (at line 3 and 4) to calculate right hand side and left hand side)
clear;
clc;
X= [0 0.8 1.5 2.5 4 5 5.8];
Y= [3.9 3.5 2.7 2.2 1.8 1.5 0];
%X= [-5.8 -5.0 -4.0 -2.5 -1.5 -0.8 0];
%Y= [0 1.5 1.8 2.2 2.7 3.5 3.9];
n= length(X); K=zeros(n,1);
A= zeros(n);
B= zeros(n,1);
h= zeros(n-1,1);
c= zeros(n-1,1);
para= zeros(n-1,4);
for k=1:(n-1)
h(k)= X(k+1)-X(k);
end
c= 1./h;
A(1,1)= 1;
A(1,2)= 0;
B(1)= 0;
A(n,n-1)= 0;
A(n,n)= 1;
B(n)= 0;
for i=2:(n-1)
A(i,i-1)= c(i-1);
A(i,i)= 2*(c(i-1) + c(i));
A(i,i+1)= c(i);
B(i)= 3*(c(i-1)^2*(Y(i)-Y(i-1))+ c(i)^2*(Y(i+1)-Y(i)));
end
K= pinv(A)*B;
para(:,1)= Y(1:n-1)';
para(:,2)= K(1:n-1);
for j=1:(n-1)
para(j,3)= 3*(Y(j+1)-Y(j))/h(j)^2 - (K(j+1)+2*K(j))/h(j);
para(j,4)= 2*(Y(j)-Y(j+1))/h(j)^3 + (K(j+1)+K(j))/h(j)^2;
end
plot(X,Y,'r*');
hold on;
for k=1:(n-1)
x1= X(k):0.05:X(k+1);
n1= length(x1);
y1= zeros(4,n1);
y1(1,:)= 1;
y1(2,:)= (x1-X(k))';
y1(3,:)= ((x1-X(k)).*(x1-X(k)))';
y1(4,:)= ((x1-X(k)).*(x1-X(k)).*(x1-X(k)))';
y1plot= para(k,:)*y1;
plot(x1,y1plot);
end

PROBLEM 1: Data Handling:


Important properties of Ammonia (NH3):
-

Melting point (1.013 bar): 195.15 K


Boiling point (1.013 bar): 239.65 K
Latent heat of vaporization (1.013 bar at boiling point): 1371.2 kJ/kg
Critical temperature: 405.55 K
Critical pressure: 112800 kPa
Triple point temperature : 195.4 K

The Clausius-Clapeyron Equation for transitions between a gas and a condensed phase:

With:

L: latent heat (J/kg) = 1371.2 kJ/kg


R: the specific gas constant (287.04 Jkg-1K-1).
T: Temperature of gas (K)
P: Pressures of gas (Pa)

I. Curve t the data using the Clausius - Clapeyron Equation:


a/ The temperature range 283 K to 311 K in 2 K increments:
In this temperature range, NH3 is in compressible liquid or vapor state.
Ammonia Latent heat of vaporization (1.013 bar at boiling point 239.65K) = 1371.2 kJ/kg
So: 101.3x103 = C x e-1371200 / (287.04 x 239.65) => C = 4.598 x 1013
P = 4.598x1013 x e-1371200 / 287.04T
MATLAB CODE:
clear;
clc;
T= 283:2:311;
P= 4.598*10^13*exp((-1371200/287.04)./T);
figure;
plot(T, P);
xlabel('Temperature (K)');
ylabel('Pressure (Pa)');
title('The relationship between temperature and pressure');

10

The relationship between temperature and pressure

x 10

Pressure (Pa)

8
7
6
5
4
3
2
280

285

290

295
300
Temperature (K)

305

310

315

b/ The temperature range 283 K to 388 K in 2 K increments:


Similar to part a, the only change is the range of temperature is from 283K to 388K
MATLAB CODE:
clear;
clc;
T= 283:2:388;
P= 4.598*10^13*exp((-1371200/287.04)./T);
figure;
plot(T, P);
xlabel('Temperature (K)');
ylabel('Pressure (Pa)');
title('The relationship between temperature and pressure');

2.5

The relationship between temperature and pressure

x 10

Pressure (Pa)

1.5

0.5

0
280

300

320

340
Temperature (K)

360

380

400

II. Polynomial t:
a/ The temperature range 283 K to 311 K in 2 K increments:
In this temperature range, NH3 is in compressible liquid or vapor state.
With equation:
P(T) = P0 + T (1)
Using the temperature-vapor pressure data for ammonia (NH3), I have: P(273) = 435083 and
P(283) = 622893.7
Solving equation (1) => P0 = -4692149.11 and = 18781.07
So: P(T) = -4692149.11 + 18781.07 T
MATLAB CODE:
T= 283:2:311;
P= -4692149.11 + 18781.07.*T;
c = polyfit(T,P,3);
f = polyval(c,T);
figure;
plot(T, f);
xlabel('Temperature (K)');
ylabel('Pressure (Pa)');
title('The Relationship between Temperature and Pressure');

12

The Relationship between Temperature and Pressure

x 10

11

Pressure (Pa)

10

6
280

285

290

295
300
Temperature (K)

305

310

315

b/ The temperature range 283 K to 388 K in 2 K increments:


Similar to part a, the only change is the range of temperature is from 283K to 388K
MATLAB CODE:
T= 283:2:388;
P= -4692149.11 + 18781.07.*T;
c = polyfit(T,P,3);
f = polyval(c,T);
figure;
plot(T, f);
xlabel('Temperature (K)');
ylabel('Pressure (Pa)');
title('The Relationship between Temperature and Pressure');

2.6

x 10

The Relationship between Temperature and Pressure

2.4
2.2

Pressure (Pa)

2
1.8
1.6
1.4
1.2
1
0.8
0.6
280

300

320

340
Temperature (K)

360

380

400

III. Exponential power Equation:


a/ The temperature range 283 K to 311 K in 2 K increments:
In this temperature range, NH3 is in compressible liquid or vapor state.
With Equation:
P(T) = P0 x eT

=> = (ln(P(T) ln(P0))/T

Using the temperature-vapor pressure data for ammonia (NH3), I have: P(273) = 435083 and
P(283) = 622893.7
= (ln(622893.7) ln(435028))/ (283-273) = 0.035897
So: P(T) = Po x e0.035897(T-T0) with P0=622893.7 Pa and T0 = 283K

MATLAB CODE:
clear;
clc;
T= 283:2:311;
P= 622893*exp(0.035897.*(T-283));
figure;
plot(T, P);
xlabel('Temperature (K)');
ylabel('Pressure (Pa)');
title('The Relationship between Temperature and Pressure');
6

1.8

x 10

The Relationship between Temperature and Pressure

1.6

Pressure (Pa)

1.4

1.2

0.8

0.6
280

285

290

295
300
Temperature (K)

305

310

315

b/ The temperature range 283 K to 388 K in 2 K increments:


Similar to part a, the only change is the range of temperature is from 283K to 388K
MATLAB CODE:
clear;
clc;
T= 283:10:388;
P= 622893*exp(0.035897.*(T-283));
figure;
plot(T, P);
xlabel('Temperature (K)');
ylabel('Pressure (Pa)');
title('The Relationship between Temperature and Pressure');

2.5

x 10

The Relationship between Temperature and Pressure

Pressure (Pa)

1.5

0.5

0
280

300

320

340
Temperature (K)

360

380

400

IV. Log curve ts:


a/ The temperature range 283 K to 311 K in 2 K increments:
In this temperature range, NH3 is in compressible liquid or vapor state.
With Equation: P(T) = P0 x T => = ln(P)/ ln(T)
Using the temperature-vapor pressure data for ammonia (NH3), I have: P(273) = 435083 and
P(283) = 622893.7
= (ln(622893.7) ln(435028))/ (ln283-ln273) = 9.97816
So: P(T) = P0 x T9.97816 with P0= 2.1384377e-19 Pa
MATLAB CODE:
clear;
clc;
T = 283:2:311;
P = 2.134377*10^(-19).*(T.^(9.97816));
figure;
plot(T, P);
xlabel('Temperature (K)');
ylabel('Pressure (Pa)');
title('The Relationship between Temperature and Pressure');

1.6

x 10

The Relationship between Temperature and Pressure

1.5
1.4

Pressure (Pa)

1.3
1.2
1.1
1
0.9
0.8
0.7
0.6
280

285

290

295
300
Temperature (K)

305

310

315

b/ The temperature range 283 K to 388 K in 2 K increments:


Similar to part a, the only change is the range of temperature is from 283K to 388K
MATLAB CODE:
clear;
clc;
T = 283:2:388;
P = 2.134377*10^(-19).*(T.^(9.97816));
figure;
plot(T, P);
xlabel('Temperature (K)');
ylabel('Pressure (Pa)');
title('The Relationship between Temperature and Pressure');

15

x 10

The Relationship between Temperature and Pressure

Pressure (Pa)

10

0
280

300

320

340
Temperature (K)

360

380

400

You might also like