You are on page 1of 10

HOMEWORK # 1

Nadihah Wahi August 2, 2011

Problem # 1
x -2/3 0 2/3 uA (x) .58 1.01 .52 uB (x) .52 .94 .54 FD 5/9 1 5/9 FV 14/27 26/27 14/27

We have calculated that:

Table 1: Results of two numericals scheme compared to exact value where u(x) is the exact solution given by 1 x2 , x [1, 1], uA and uB refers to the Solution A and Solution B respectively. By using the norm denition for p = 1, 2, and to both solutions, we will get the errors as shown in the table below: Solution A B L1 .0233 .0370 L2 .0256 .0413 L .0356 .0600

Table 2: Norms comparison for F D The norms error given by solution A are the least compared to B, and hence solution A gives the best numerical scheme results for F D. Solution A B L1 .033 .015 L2 .045 .018 L .061 .023

Table 3: Norms comparison for F V Hence, for F V method, solution B is better. 1

Problem # 2

A consistent numerical scheme is where the local truncation error tends to zero as we rene the mesh size. In order to prove inconsistency, we need to show the contradiction of the denition.

2.1

Finding LTE

By working on Taylor Series separately for space and time for easiness. Space, ux x3 x2 uxx + uxxx + O(x4 ) 2! 3! x2 x3 uxx uxxx + O(x4 ) 2! 3!

un = un + xux + j+1 j un = un xux + j1 j Then

(1) (2)

un un x2 j+1 j1 = ux + uxxx + O(x4 ) 2x 6 Time, ut

(3)

The advection equation given is ut = ux . Then utt = uxt = utx = (ux )x = uxx . So, we get utt = uxx un+1 = un + tut + j j with un + un = 2un + x2 uxx + O(x4 ) j1 j+1 j Then un+1 1 (un + un ) t x2 x4 j1 j+1 j 2 = ut + uxx + O(t2 ) uxx O( ) t 2 2t t By combining equation (3) and equation (6), we get: un+1 1 (un + un ) un un 1 x2 x2 j1 j+1 j j+1 j1 2 + = ut +ux + (t )uxx + uxxx +H.O.T 1 t 2x 2 t 6 (7) From the above equation, the local truncation error is O(t, x2 ), so the numerical method given has rst order accuracy in time and second order ac2 curacy in space. Since one of the leading term in T.E. has a factor of x , t
1 Higher

t3 t2 utt + uttt + O(t4 ) 2! 3!

(4)

(5)

(6)

Order Terms

and as this ratio is to approach some constant as x and t were to approach zero, then the T.E. will not vanish under grid renement which contradict our denition. Therefore, this numerical scheme is inconsistent. 2

3
3.1

Problem #3
Choosing the values of p and
un+1 = un (1 + tp )

In order for the numerical method (8)

to perform well, we need to have stability and consistency. This can be achieved by applying the right choices of p and , which correspond to stability and consistency respectively. The correct choice of can be veried by initially xing t = 0.1 and p = 1, and varying the value of . The table shows how the errors behave with the given value of a and . a\ 2 1/2 -1/2 2 16.26 35.62 37.97 1/2 51.94 .065 2.28 -1/2 54.24 2.36 .0094

Table 4: Error values with varying The table shows that the least error is obtained only when = a compared to any other choices. Another approach is by looking at the plot of the exact solution compared to the calculated scheme. The value of a = 0.5 and three dierent values of are plotted against the exact solution. The graph shows that when = 2, its numerical result blows up almost instantly, but as approaches the same value as a the result is closely following the exact solution. Therefore, we have veried that must be of the same value as a. Next step is to verify the correct choice of p. The numerical scheme gives an intuition that the value of p must be positive, otherwise we will have larger time step instead of keeping it smaller and this will lead to instability. Furthermore, by using Taylor Series expansion of the method, the value of p is positive and unity. Using the same technique, the time step t is xed at 0.1 and the values of a are taken to be -1/2 and 1/2. By pluging in dierent values for p, the errors of the scheme are tabulated below: The table above shows that the error is the least when using p = 1 and hence veried that the right choice of p is 1.

Figure 1: This plot shows that at = a, the scheme is closer to the exact solution p -2 -1 -0.5 0 0.5 1 2 E1/2 6.348e10 .3977 .5747 .3977 .00714 .3442 E1/2 1.3819e14 .8283 663.875 .8283 .02855 .771

Table 5: Errors resulted by varying p

3.2

Varying t

Now that we have settled with the choices of p and , we move on to answer the question of what will happen to the result if we took smaller and smaller values of t. We hunching that the smaller the step is, the closer we are to the exact solution. Three values of t are selected starting from 1. The result is given in gures below. From Figure(2)and(3), both show that by reducing the time step, the scheme behaves almost exactly as the true solution.

(a) t = 1

(b) t = .5

Figure 2: The plot of varying t, at a = 0.5

Figure 3: At t = 0.01, the numerical result seems to be indistinguishable from the exact solution

3.3

Runge-Kutta of Second Order


t ) 2

Our next method is consists of an additional intermediate step given by: u = un (1 + a where un+1 = un + atu (10) (9)

and this will lead to second order accuracy in our new method. By eliminating the u for un+1 , we will get: un+1 = un + un at + un Using Taylor series for un+1 : un+1 = un + tun + t t2 un t3 un tt ttt + + O(t4 ) 2 6 (12) a2 t 2 (11)

From our ODE: ut = au (ut ) = utt = aut = a2 u Hence our equation(12) became: un+1 = un + tun + t un+1 t2 un a2 t3 un a2 + + O(t4 ) 2 6 +
t3 un a2 6

(13)

+ O(t3 ) t By plug-in equation(11) into (14) we will be left with: a3 t2 un + O(t3 ) 6 Hence the L.T.E of this scheme is: un = un t t Lh (P (uh )) L(u) = a3 t2 un + O(t3 ) 6

un = t

t2 un a2 2

(14)

(15)

(16)

The leading term in L.T.E is of O(t2 ), thus it is proved that this method is second order. 2 In order to assure that both of the numerical schemes are rst order and second order, we can check at the error ratio when we plug in dierent time step. In other words, for the rst order, if we reduce the time step by a half, the error will also be reduced by a half, but for the second order, it will be reduce to a quarter. Two separate tables were generated for both orders, and the error ratio is calculated by taking the latter time step to the former one. The error ratios given by the rst order in the table show that as we reduce the time step a by half, the error is also reduced by a half. The second order error ratios also present the correct order of the numerical scheme. 6

t 1 0.5 0.25 0.1 0.05

Error .0753286 .0364609 .0179748 .00713835 .00356131

Error Ratio 2.066 2.028 2.004

Table 6: The error ratio of the 1st Order method t 1 0.5 0.25 0.1 0.05 Error .0135098 .00318529 .000770193 .000120433 2.9868e-5 Error Ratio 4.241 4.136 4.032

Table 7: Error ratio of the 2nd Order method

3.4

Computational Cost

From equation(8)and(9), the cost for each numerical method are 3 and 7 respectively. Hence the overall cost to achieve the accuracy of 0.1% and 0.01% for each method is given by the table below: Accuracy 0.1% 0.01% Order 1st 2nd 1st 2nd Cost 30 56 3000 140

Table 8: Cost of numerical scheme needed to obtain some accuracy Table(8) suggests that, if we want a lesser accuracy, it is better to use the Euler rst order method since we will have less work to obtain it. However, when accuracy is our main goal, it is better to use the second order. The second order method for higher accuracy is in this case is much cheaper as well since it requires less time step which reduces the computational cost.

3.5

Another Second Order Method


du dt

If we approximate

= au by : a un+1 un = (un+1 + un ) t 2 (17)

Then rearrange equation(17) to get un+1 :

un+1 = un (

1 + at/2 ) 1 at/2

(18)

We can check the order using the error ratio as before, and given in the table(9). By rounding the error ratio, we can assure that this is a second order method. t 1 0.5 0.25 0.1 0.05 Error .00461786 .00132648 .00035097 5.7948e-5 1.47352 Error Ratio 3.481 3.780 3.93

Table 9: Second order method

Figure 4: Comparison of all methods

Figure(4) shows that both Runge-Kutta and this method are almost exactly equal to the exact solution. Plot Error vs. time step. the slope is the order orf accuracy

C++ Source Code

#include <cstdlib> #include <iostream> #include <math.h> #include <fstream> #include <iomanip> /* NADIHAH WAHI : HOMEWORK #1 PROBLEM #3. Main */ using namespace std; int main(int argc, char *argv[]) { int i, p=1; float a=-.5, tn, dt; float e1=0, e2=0, e3=0, er1, er2, er3,nstep; float t[500]={0}; /*Array size should be the num of step, but as novice, I put some big num.*/ float u[500]={1,0}; float ustar[500]={0}; float urk[500]={1,0}; float uc[500]={1,0}; float ue[500]={0}; tn = 2; //Terminal time cout<< "Enter : dt "<< endl; //dt is between 0 and 1 cin >> dt; nstep = (tn-0)/dt; for(i=0;i<nstep;i++) { t [i+1]= t[i] + dt; ustar[i] = urk[i]*(1+a*dt/2); //2nd order urk[i+1] = urk[i] + a*dt*ustar[i]; uc[i+1] = uc[i]*(1+a*dt/2)/(1-a*dt/2); ue[i] = exp(a*t[i]); //exact solution //Finding the error at each time step: e1 += pow(u[i]-ue[i],2); e2 += pow(urk[i]-ue[i],2); e3 += pow(uc[i]-ue[i],2); ue[i] = exp(a*t[i]); //exact solution } //write output t and u--------------------------------------------------/*ofstream myfile; myfile.open("extra.txt"); for(i=0;i<=nstep;i++) 9

{ myfile<< t[i]<<setw(12)<<u[i]<<setw(12)<<urk[i]<<setw(12)<<ue[i] << endl; } myfile.close(); */ //-----------------------------------------------------------------------

//Using norm 2 error def. er1 = sqrt(e1/nstep); er2 = sqrt(e2/nstep); er3 = sqrt(e3/nstep); cout<<"First order error is " << er1 << endl; cout<<"Second order error is "<< er2 <<endl; cout<<"Other method "<< er3 <<endl;

system("PAUSE"); return EXIT_SUCCESS; }

10

You might also like