numerical methods for engineers 5th e-solution ch 25

34
1 CHAPTER 25 25.1 The analytical solution can be derived by separation of variables Substituting the initial conditions yields C = 0. Taking the exponential gives the final result The result can be plotted as 0 1 2 0 1 2 25.2 Euler’s method with h = 0.5 x y dy/dx 0 1 -1.1 0.5 0.45 -0.3825 1 0.25875 -0.02588 1.5 0.245813 0.282684 2 0.387155 1.122749 PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw- Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Upload: mfaheem-al-hyat

Post on 06-May-2015

8.249 views

Category:

Documents


13 download

TRANSCRIPT

Page 1: Numerical methods for engineers 5th e-solution ch 25

1

CHAPTER 25

25.1 The analytical solution can be derived by separation of variables

Substituting the initial conditions yields C = 0. Taking the exponential gives the final result

The result can be plotted as

0

1

2

0 1 2

25.2 Euler’s method with h = 0.5

x y dy/dx0 1 -1.10.5 0.45 -0.38251 0.25875 -0.025881.5 0.245813 0.2826842 0.387155 1.122749

Euler’s method with h = 0.25 gives

x y dy/dx0 1 -1.10.25 0.725 -0.752190.5 0.536953 -0.456410.75 0.422851 -0.227281 0.36603 -0.03661.25 0.356879 0.1650571.5 0.398143 0.457865

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 2: Numerical methods for engineers 5th e-solution ch 25

2

1.75 0.51261 1.0059972 0.764109 2.215916

The results can be plotted along with the analytical solution as

0

1

2

0 1 2

25.3 For Heun’s method, the value of the slope at x = 0 can be computed as 1.1 which can be used to compute the value of y at the end of the interval as

The slope at the end of the interval can be computed as

which can be averaged with the initial slope to predict

This formula can then be iterated to yield

j yij a

0 0.451 0.629375 28.5%2 0.5912578 6.45%3 0.5993577 1.35%4 0.5976365 0.288%

The remaining steps can be implemented with the result

xi yi

0 1.00000000.5 0.59763651 0.45908751.5 0.62691272 2.8784358

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 3: Numerical methods for engineers 5th e-solution ch 25

3

The results along with the analytical solution are displayed below:

0

1

2

3

0 1 2

25.4 The midpoint method with h = 0.5

x y dy/dx ym dy/dx-mid0 1 -1.1 0.725 -0.752190.5 0.623906 -0.53032 0.491326 -0.264091 0.491862 -0.04919 0.479566 0.2217991.5 0.602762 0.693176 0.776056 1.523012 1.364267 3.956374 2.35336 9.32519

with h = 0.25 gives

x y dy/dx ym dy/dx-mid0 1 -1.1 0.8625 -0.935270.25 0.766182 -0.79491 0.666817 -0.639730.5 0.60625 -0.51531 0.541836 -0.384360.75 0.510158 -0.27421 0.475882 -0.159121 0.470378 -0.04704 0.464498 0.0769321.25 0.489611 0.226445 0.517916 0.4094781.5 0.59198 0.680777 0.677077 1.0431221.75 0.852761 1.673543 1.061954 2.5652822 1.494081 4.332836 2.035686 6.953139

The results can be plotted along with the analytical solution as

0

1

2

0 1 2

25.5 The 4th-order RK method with h = 0.5 gives

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 4: Numerical methods for engineers 5th e-solution ch 25

4

x y k1 ym k2 ym k3 ye k4 0 1 -1.1 0.725 -0.75219 0.811953 -0.8424 0.578799 -0.49198 -0.796860.5 0.60157 -0.51133 0.473737 -0.25463 0.537912 -0.28913 0.457006 -0.0457 -0.274091 0.464524 -0.04645 0.452911 0.209471 0.516892 0.239062 0.584055 0.671663 0.2537131.5 0.59138 0.680087 0.761402 1.494252 0.964943 1.893701 1.538231 4.460869 1.9861442 1.584452 4.594911 2.73318 10.83023 4.292008 17.00708 10.08799 51.95317 18.70378

0

1

2

0 1 2

25.6 (a) The analytical solution can be derived by separation of variables

Substituting the initial conditions yields C = 2. Substituting this value and solving for y gives the final result

The result can be plotted as

0

2

4

0 0.5 1

(b) Euler’s method with h = 0.5

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 5: Numerical methods for engineers 5th e-solution ch 25

5

x y dy/dx0 1 10.5 1.5 2.449491 2.724745 4.95204

Euler’s method with h = 0.25 gives

x y dy/dx0 1 10.25 1.25 1.6770510.5 1.669263 2.5839990.75 2.315263 3.8039971 3.266262 5.421841

The results can be plotted along with the analytical solution as

0

2

4

0 0.5 1

(c) For Heun’s method, the first step along with the associated iterations is

j yij a, %

0 1.5000001 1.862372 19.4582 1.932344 3.6213 1.945044 0.653

The remaining steps can be implemented with the result

xi yi

0 10.5 1.9450441 4.169277

The results along with the analytical solution are displayed below:

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 6: Numerical methods for engineers 5th e-solution ch 25

6

0

2

4

0.0 0.5 1.0

(d) The midpoint method with h = 0.5

x y dy/dx ym dy/dx-mid0 1 1 1.25 1.6770510.5 1.838525 2.711845 2.516487 3.965861 3.821455 5.864563 5.287596 8.048171

with h = 0.25 gives

x y dy/dx ym dy/dx-mid0 1 1 1.125 1.3258250.25 1.331456 1.730831 1.54781 2.1771930.5 1.875755 2.739164 2.21815 3.3510270.75 2.713511 4.118185 3.228284 4.9410431 3.948772 5.961455 4.693954 7.041299

The results can be plotted along with the analytical solution as

0

2

4

0 0.5 1

(e) The 4th-order RK method with h = 0.5 gives

x y k1 ym k2 ym k3 ye k4 0 1 1 1.25 1.67705 1.41926 1.78699 1.89350 2.75209 1.780030.5 1.89001 2.74956 2.57740 4.01357 2.89341 4.25251 4.01627 6.01219 4.215771 3.99784 5.99838 5.49743 8.20631 6.04942 8.60845 8.30206 11.5253 8.52554

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 7: Numerical methods for engineers 5th e-solution ch 25

7

0

2

4

0 0.5 1

25.7 The second-order ODE is transformed into a pair of first-order ODEs as in

(a) The first few steps of Euler’s method are

x y z dy/dx dz/dx0 2 0 0 -20.1 2 -0.2 -0.2 -1.950.2 1.98 -0.395 -0.395 -1.880.3 1.9405 -0.583 -0.583 -1.79050.4 1.8822 -0.76205 -0.76205 -1.68220.5 1.805995 -0.93027 -0.93027 -1.556

(b) For Heun (without iterating the corrector) the first few steps are

x y z dy/dx dz/dx yend zend dy/dx dz/dx ave slope0 2 0 0 -2 2 -0.2 -0.2 -1.95 -0.10.1 1.99 -0.1975 -0.1975 -1.94 1.97025 -0.3915 -0.3915 -1.87025 -0.29450.2 1.96055 -0.38801 -0.38801 -1.86055 1.921749 -0.57407 -0.57407 -1.77175 -0.481040.3 1.912446 -0.56963 -0.56963 -1.76245 1.855483 -0.74587 -0.74587 -1.65548 -0.657750.4 1.846671 -0.74052 -0.74052 -1.64667 1.772619 -0.90519 -0.90519 -1.52262 -0.822860.5 1.764385 -0.89899 -0.89899 -1.51439 1.674486 -1.05043 -1.05043 -1.37449 -0.97471

Both results are plotted below:

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 8: Numerical methods for engineers 5th e-solution ch 25

8

-2

0

2

4

0 2 4

y

z

Heun

Euler

x

25.8 The second-order ODE is transformed into a pair of first-order ODEs as in

The results for the 4th-order RK method are tabulated and plotted below:

x y z k11 k12 ymid zmid k21 k22 ymid zmid k31 k32 yend zend k41 k42 1 2

0 4.0000 0.0000 0.00 -32.00 4.00 -8.00 -8.00 -27.20 2.00 -6.80 -6.80 -11.92 0.60 -5.96 -5.96 -1.22 -5.93 -18.58

0.5 1.0367 -9.2887 -9.29 -2.72 -1.29 -9.97 -9.97 16.27 -1.46 -5.22 -5.22 14.78 -1.57 -1.90 -1.90 13.74 -6.93 12.18

1 -2.4276 -3.1969 -3.20 21.34 -3.23 2.14 2.14 24.53 -1.89 2.94 2.94 13.38 -0.96 3.49 3.49 5.58 1.74 17.12

1.5 -1.5571 5.3654 5.37 9.24 -0.22 7.67 7.67 -2.88 0.36 4.65 4.65 -5.68 0.77 2.53 2.53 -7.64 5.42 -2.59

2 1.1539 4.0719 4.07 -11.67 2.17 1.15 1.15 -18.07 1.44 -0.44 -0.44 -11.27 0.93 -1.56 -1.56 -6.51 0.65 -12.81

2.5 1.4810 -2.3334 -2.33 -10.45 0.90 -4.95 -4.95 -4.21 0.24 -3.39 -3.39 0.07 -0.21 -2.30 -2.30 3.08 -3.55 -2.61

3 -0.2935 -3.6375 -3.64 4.53 -1.20 -2.50 -2.50 11.13 -0.92 -0.86 -0.86 7.87 -0.72 0.30 0.30 5.59 -1.68 8.02

3.5 -1.1319 0.3723 0.37 8.83 -1.04 2.58 2.58 6.76 -0.49 2.06 2.06 2.66 -0.10 1.70 1.70 -0.22 1.89 4.58

4 -0.1853 2.6602 2.66 -0.11 0.48 2.63 2.63 -5.42 0.47 1.31 1.31 -4.56 0.47 0.38 0.38 -3.97 1.82 -4.01

4.5 0.7241 0.6564 0.66 -6.19 0.89 -0.89 -0.89 -6.57 0.50 -0.99 -0.99 -3.42 0.23 -1.05 -1.05 -1.21 -0.69 -4.56

5 0.3782 -1.6258 -1.63 -2.05 -0.03 -2.14 -2.14 1.51 -0.16 -1.25 -1.25 2.00 -0.25 -0.63 -0.63 2.34 -1.50 1.22

y

z

x

-12

-8

-4

0

4

8

0 2 4

y

z

x

-12

-8

-4

0

4

8

0 2 4

25.9 (a) The Heun method without iteration can be implemented as in the following table:

t y k1 yend k2

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 9: Numerical methods for engineers 5th e-solution ch 25

9

0 1 0 1 0.000995 0.0004980.1 1.00005 0.000995 1.000149 0.007843 0.0044190.2 1.000492 0.007845 1.001276 0.025841 0.0168430.3 1.002176 0.025865 1.004762 0.059335 0.04260.4 1.006436 0.059434 1.012379 0.11156 0.0854970.5 1.014986 0.111847 1.02617 0.184731 0.148289

2.9 3.784421 0.051826 3.789604 0.01065 0.0312383 3.787545 0.010644 3.78861 0.000272 0.005458

(b) The Ralston 2nd order RK method can be implemented as in the following table:

t y k1 yint k2 0 1 0 1 0.000421 0.000280.1 1.000028 0.000995 1.000103 0.005278 0.0038510.2 1.000413 0.007845 1.001001 0.020043 0.0159770.3 1.002011 0.02586 1.00395 0.049332 0.0415080.4 1.006162 0.059418 1.010618 0.096672 0.0842540.5 1.014587 0.111803 1.022972 0.164537 0.146959 2.9 3.785066 0.051835 3.788954 0.017276 0.0287963 3.787946 0.010646 3.788744 0.001116 0.004293

Both methods are displayed on the following plot along with the exact solution,

Note that both methods agree closely with the exact solution.

Ralston

Heun

Exact

0

2

4

0 1 2 3

25.10 The solution results are as in the following table and plot:

t y k1 k2 k3 0 1 -1 -0.6875 -0.5625 -0.718750.5 0.640625 -0.39063 0.019531 0.144531 -0.02799

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 10: Numerical methods for engineers 5th e-solution ch 25

10

1 0.626628 0.373372 0.842529 0.967529 0.785171.5 1.019213 1.230787 1.735591 1.860591 1.672292 1.855358 2.144642 2.670982 2.795982 2.6040922.5 3.157404 3.092596 3.631947 3.756947 3.5628893 4.938848 4.061152 4.608364 4.733364 4.537995

0

2

4

0 1 2 3

25.11 (a) Euler

x y z dy/dx dz/dx0 2.0000 4.0000 0.00 -10.670.2 2.0000 1.8667 -0.73 -2.320.4 1.8550 1.4021 -1.03 -1.220.6 1.6492 1.1590 -1.10 -0.740.8 1.4286 1.0113 -1.06 -0.491 1.2166 0.9139 -0.96 -0.34

(b) 4th-order RK

x y z k11 k12 k21 k22 k31 k32 k41 k42 1 2

0 2.000 4.000 0.000 -10.667 -0.381 -5.736 -0.305 -7.678 -0.603 -3.926 -0.329 -6.9030.2 1.934 2.619 -0.594 -4.423 -0.786 -2.962 -0.748 -3.338 -0.888 -2.266 -0.758 -3.2150.4 1.783 1.976 -0.884 -2.321 -0.962 -1.718 -0.947 -1.830 -0.991 -1.377 -0.949 -1.7990.6 1.593 1.617 -0.990 -1.387 -1.001 -1.087 -0.999 -1.131 -0.989 -0.897 -0.997 -1.1200.8 1.393 1.392 -0.990 -0.901 -0.963 -0.732 -0.968 -0.753 -0.928 -0.617 -0.963 -0.7481 1.201 1.243 -0.930 -0.618 -0.884 -0.515 -0.893 -0.526 -0.840 -0.441 -0.887 -0.524

Both methods are plotted on the same graph below. Notice how Euler’s method (particularly for z) is not very accurate for this step size. The 4th-order RK is much closer to the exact solution.

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 11: Numerical methods for engineers 5th e-solution ch 25

11

0

1

2

3

4

5

0 0.2 0.4 0.6 0.8 1

y

z

Euler

4th-order RK

25.12 dy

dxe y

x

10 0 6

2

2 0.075)

2

2

( )

( .

4 th -order RK method:

One step (h = 0.5): y1 = 0.3704188Two steps (h = 0.25): y2 = 0.3704096

present = 9.119106

correction = 15

6 08 10 7 .

y2 = 0.370409

dy

dx 0 3.

yscale 05 0 5 0 3 0 65. . ( . ) .

new = 0.001(0.65) = 0.00065

Since present < new, therefore, increase step.

hnew

0 50 00065

9 119 1011737

6

0.2

..

..

25.13 We will look at the first step only

present = y2 y1 = 0.24335

dy

dxe 4 05 2 30 . ( )

yscale = 2 + (2(3)) = 8

new = 0.001(8) = 0.008

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 12: Numerical methods for engineers 5th e-solution ch 25

12

Because present > new, decrease step.

25.14 The calculation of the k’s can be summarized in the following table:

x y f(x,y)k1 0 2 3k2 0.2 2.6 3.394043k3 0.3 2.98866 3.590667k4 0.6 4.154161 4.387217k5 1 6.251995 5.776166k6 0.875 5.511094 5.299464

These can then be used to compute the 4th-order prediction

along with a fifth-order formula:

The error estimate is obtained by subtracting these two equations to give

25.15Option Explicit

Sub EulerTest()Dim i As Integer, m As IntegerDim xi As Double, yi As Double, xf As Double, dx As Double, xout As DoubleDim xp(200) As Double, yp(200) As Double'Assign valuesyi = 1xi = 0xf = 4dx = 0.5xout = 0.5'Perform numerical Integration of ODECall ODESolver(xi, yi, xf, dx, xout, xp, yp, m)'Display resultsSheets("Sheet1").SelectRange("a5:b205").ClearContentsRange("a5").SelectFor i = 0 To m ActiveCell.Value = xp(i) ActiveCell.Offset(0, 1).Select ActiveCell.Value = yp(i) ActiveCell.Offset(1, -1).SelectNext iRange("a5").SelectEnd Sub

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 13: Numerical methods for engineers 5th e-solution ch 25

13

Sub ODESolver(xi, yi, xf, dx, xout, xp, yp, m)'Generate an array that holds the solutionDim x As Double, y As Double, xend As DoubleDim h As Doublem = 0xp(m) = xiyp(m) = yix = xiy = yiDo 'Print loop xend = x + xout If (xend > xf) Then xend = xf 'Trim step if increment exceeds end h = dx Call Integrator(x, y, h, xend) m = m + 1 xp(m) = x yp(m) = y If (x >= xf) Then Exit DoLoopEnd Sub

Sub Integrator(x, y, h, xend)Dim ynew As DoubleDo 'Calculation loop If (xend - x < h) Then h = xend - x 'Trim step if increment exceeds end Call Euler(x, y, h, ynew) y = ynew If (x >= xend) Then Exit DoLoopEnd Sub

Sub Euler(x, y, h, ynew)Dim dydx As Double'Implement Euler's methodCall Derivs(x, y, dydx)ynew = y + dydx * hx = x + hEnd Sub

Sub Derivs(x, y, dydx)'Define ODEdydx = -2 * x ^ 3 + 12 * x ^ 2 - 20 * x + 8.5End Sub

25.16 Example 25.1:

Example 25.4 (nonlinear model). Change time steps and initial conditions to

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 14: Numerical methods for engineers 5th e-solution ch 25

14

'Assign valuesyi = 0xi = 0xf = 15dx = 0.5xout = 1

Change Derivs Sub to

Sub Derivs(t, v, dvdt)'Define ODEdvdt = 9.8 - 12.5 / 68.1 * (v + 8.3 * (v / 46) ^ 2.2)End Sub

25.17Option Explicit

Sub Heun()Dim maxit As Integer, es As DoubleDim n As Integer, m As Integer, i As Integer, iter As IntegerDim xi As Double, xf As Double, yi As Double, h As DoubleDim x As Double, y As Double, y2 As Double, y2old As DoubleDim k1 As Double, k2 As Double, slope As DoubleDim xp(1000) As Double, yp(1000) As Double, itr(1000) As IntegerDim ea As Doublemaxit = 15: es = 0.0000001xi = 0: xf = 4: yi = 2n = 4h = (xf - xi) / nx = xiy = yim = 0xp(m) = xyp(m) = yFor i = 1 To n Call Derivs(x, y, k1) y2 = y + k1 * h iter = 0 Do y2old = y2 Call Derivs(x + h, y2, k2) slope = (k1 + k2) / 2 y2 = y + slope * h iter = iter + 1 ea = Abs((y2 - y2old) / y2) * 100 If ea < es Or iter >= maxit Then Exit Do Loop

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 15: Numerical methods for engineers 5th e-solution ch 25

15

m = m + 1 x = x + h xp(m) = x yp(m) = y2 itr(m) = iter y = y2Next iSheets("Heun").SelectRange("a5:b1005").ClearContentsRange("a5").SelectFor i = 0 To m ActiveCell.Value = xp(i) ActiveCell.Offset(0, 1).Select ActiveCell.Value = yp(i) ActiveCell.Offset(0, 1).Select ActiveCell.Value = itr(i) ActiveCell.Offset(1, -2).SelectNext iRange("a5").SelectEnd Sub

Sub Derivs(x, y, dydx)'Define ODEdydx = 4 * Exp(0.8 * x) - 0.5 * yEnd Sub

25.18Option Explicit

Sub RK4Test()Dim i As Integer, m As IntegerDim xi As Double, yi As Double, xf As Double, dx As Double, xout As DoubleDim xp(200) As Double, yp(200) As Double'Assign valuesyi = 1xi = 0xf = 4dx = 0.5xout = 0.5'Perform numerical Integration of ODECall ODESolver(xi, yi, xf, dx, xout, xp, yp, m)'Display resultsSheets("Sheet1").SelectRange("a5:b205").ClearContentsRange("a5").SelectFor i = 0 To m

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 16: Numerical methods for engineers 5th e-solution ch 25

16

ActiveCell.Value = xp(i) ActiveCell.Offset(0, 1).Select ActiveCell.Value = yp(i) ActiveCell.Offset(1, -1).SelectNext iRange("a5").SelectEnd Sub

Sub ODESolver(xi, yi, xf, dx, xout, xp, yp, m)'Generate an array that holds the solutionDim x As Double, y As Double, xend As DoubleDim h As Doublem = 0xp(m) = xiyp(m) = yix = xiy = yiDo 'Print loop xend = x + xout If (xend > xf) Then xend = xf 'Trim step if increment exceeds end h = dx Call Integrator(x, y, h, xend) m = m + 1 xp(m) = x yp(m) = y If (x >= xf) Then Exit DoLoopEnd Sub

Sub Integrator(x, y, h, xend)Dim ynew As DoubleDo 'Calculation loop If (xend - x < h) Then h = xend - x 'Trim step if increment exceeds end Call RK4(x, y, h, ynew) y = ynew If (x >= xend) Then Exit DoLoopEnd Sub

Sub RK4(x, y, h, ynew)'Implement RK4 methodDim k1 As Double, k2 As Double, k3 As Double, k4 As DoubleDim ym As Double, ye As Double, slope As DoubleCall Derivs(x, y, k1)ym = y + k1 * h / 2Call Derivs(x + h / 2, ym, k2)ym = y + k2 * h / 2Call Derivs(x + h / 2, ym, k3)ye = y + k3 * hCall Derivs(x + h, ye, k4)slope = (k1 + 2 * (k2 + k3) + k4) / 6ynew = y + slope * hx = x + hEnd Sub

Sub Derivs(x, y, dydx)'Define ODEdydx = -2 * x ^ 3 + 12 * x ^ 2 - 20 * x + 8.5End Sub

Example 25.7 a :

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 17: Numerical methods for engineers 5th e-solution ch 25

17

Example 25.7 b : Change time steps and initial conditions to

'Assign valuesyi = 2xi = 0xf = 4dx = 0.5xout = 0.5

Change Derivs Sub to

Sub Derivs(x, y, dydx)'Define ODEdydx = 4 * Exp(0.8 * x) - 0.5 * yEnd Sub

25.19Option Explicit

Sub RK4SysTest()Dim i As Integer, m As Integer, n As Integer, j As IntegerDim xi As Double, yi(10) As Double, xf As DoubleDim dx As Double, xout As DoubleDim xp(200) As Double, yp(200, 10) As Double'Assign valuesn = 2xi = 0xf = 2yi(1) = 4yi(2) = 6dx = 0.5xout = 0.5'Perform numerical Integration of ODECall ODESolver(xi, yi, xf, dx, xout, xp, yp, m, n)

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 18: Numerical methods for engineers 5th e-solution ch 25

18

'Display resultsSheets("Sheet1").SelectRange("a5:n205").ClearContentsRange("a5").SelectFor i = 0 To m ActiveCell.Value = xp(i) For j = 1 To n ActiveCell.Offset(0, 1).Select ActiveCell.Value = yp(i, j) Next j ActiveCell.Offset(1, -n).SelectNext iRange("a5").SelectEnd Sub

Sub ODESolver(xi, yi, xf, dx, xout, xp, yp, m, n)'Generate an array that holds the solutionDim i As IntegerDim x As Double, y(10) As Double, xend As DoubleDim h As Doublem = 0x = xi'set initial conditionsFor i = 1 To n y(i) = yi(i)Next i'save output valuesxp(m) = xFor i = 1 To n yp(m, i) = y(i)Next iDo 'Print loop xend = x + xout If (xend > xf) Then xend = xf 'Trim step if increment exceeds end h = dx Call Integrator(x, y, h, n, xend) m = m + 1 'save output values xp(m) = x For i = 1 To n yp(m, i) = y(i) Next i If (x >= xf) Then Exit DoLoopEnd Sub

Sub Integrator(x, y, h, n, xend)Dim j As IntegerDim ynew(10) As DoubleDo 'Calculation loop If (xend - x < h) Then h = xend - x 'Trim step if increment exceeds end Call RK4Sys(x, y, h, n, ynew) For j = 1 To n y(j) = ynew(j) Next j If (x >= xend) Then Exit DoLoopEnd Sub

Sub RK4Sys(x, y, h, n, ynew)Dim j As Integer

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 19: Numerical methods for engineers 5th e-solution ch 25

19

Dim ym(10) As Double, ye(10) As DoubleDim k1(10) As Double, k2(10) As Double, k3(10) As Double, k4(10) As DoubleDim slope(10)'Implement RK4 method for systems of ODEsCall Derivs(x, y, k1)For j = 1 To n ym(j) = y(j) + k1(j) * h / 2Next jCall Derivs(x + h / 2, ym, k2)For j = 1 To n ym(j) = y(j) + k2(j) * h / 2Next jCall Derivs(x + h / 2, ym, k3)For j = 1 To n ye(j) = y(j) + k3(j) * hNext jCall Derivs(x + h, ye, k4)For j = 1 To n slope(j) = (k1(j) + 2 * (k2(j) + k3(j)) + k4(j)) / 6Next jFor j = 1 To n ynew(j) = y(j) + slope(j) * hNext jx = x + h

End Sub

Sub Derivs(x, y, dydx)'Define ODEdydx(1) = -0.5 * y(1)dydx(2) = 4 - 0.3 * y(2) - 0.1 * y(1)End Sub

Application to Example 25.10:

25.20 Main Program:

%Damped spring mass system%mass: m=10 kg%damping: c=5,40,200 N/(m/s)%spring: k=40 N/m% MATLAB 5 version%Independent Variable t, tspan=[tstart tstop]%initial conditions [x(1)=velocity, x(2)=displacement];

tspan=[0 15]; ic=[0 1];

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 20: Numerical methods for engineers 5th e-solution ch 25

20

global cm kmm=10; c(1)=5; c(2)=40; c(3)=200; k=40;km=k/m;

for n=1:3 cm=c(n)/m; [t,x]=ode45('kc',tspan,ic); plot(t,x(:,2)); grid; xlabel('time - sec.'); ylabel('displacement - m'); title('m(d2x/dt2)+c(dx/dt)+kx=0; m=10 kg, k= 40 N/m') hold on end

Function ‘kc’:

%Damped spring mass system - m d2x/dt2 + c dx/dt + k x =0%mass: m=10 kg%damping: c=5,40,200 N/(m/s)%spring: k=40 N/m%x(1)=velocity, x(2)=displacement

function dx=kc(t,x);global cm kmdx=[-cm*x(1)-km*x(2); x(1)];

25.21 The Matlab program shown below performs the Euler Method and displays the time just before the cylindrical tank empties (y < 0).

%prob2521.mdt=0.5;t=0;y=3;i=1;while(1) y=y+dydt(t,y)*dt;

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 21: Numerical methods for engineers 5th e-solution ch 25

21

if y<0, break, end t=t+dt; i=i+1;end

function dy=dydt(t,y);dy=-0.06*sqrt(y);

The result is 56 minutes as shown below

>> prob2521

t = 56

25.22

tspan=[0,15]';x0=[6,1.5]';[t,x]=ode45('dxdt',tspan,x0);plot(t,x(:,1),t,x(:,2),'--')gridtitle('Displacement and Velocity Versus Time')xlabel('Time, t')ylabel('Displacement and Velocity')

function dx=dxdt(t,x)dx=[x(2);-5*x(1)*x(2)+(x(1)+7)*sin(1*t)];

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 22: Numerical methods for engineers 5th e-solution ch 25

22

25.23 The two differential equations to be solved are

(a) Here are the first few steps of Euler’s method with a step size of h = 0.2.

t x v dx/dt dv/dt0 1000 0 0 9.81

0.2 1000 1.962 -1.962 9.8003760.4 999.6076 3.922075 -3.92208 9.7715430.6 998.8232 5.876384 -5.87638 9.723670.8 997.6479 7.821118 -7.82112 9.6570751 996.0837 9.752533 -9.75253 9.57222

(b) Here are the results of the first few steps of the 4th-order RK method with a step size of h = 0.2.

t x v0 1000 0

0.2 999.8038 1.9613590.4 999.2157 3.9188750.6 998.2368 5.8687380.8 996.869 7.8071951 995.1149 9.730582

The results for x of both methods are displayed graphically on the following plots. Because the step size is sufficiently small the results are in close agreement. Both indicate that the

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 23: Numerical methods for engineers 5th e-solution ch 25

23

parachutist would hit the ground at a little after 20 s. The more accurate 4th-order RK method indicates that the solution reaches the ground between t = 20.2 and 20.4 s.

-500

0

500

1000

1500

0 5 10 15 20 25

25.24 The volume of the tank can be computed as

(1)

This equation cannot be solved because it has 2 unknowns: V and H. The volume is related to the depth of liquid by

(2)

Equation (2) can be differentiated to give

(3)

This result can be substituted into Eq. (1) to give and equation with 1 unknown,

(4)

The area of the orifice can be computed as

Substituting this value along with the other parameters (C = 0.55, g = 9.81, r = 1.5) into Eq. (4) gives

(5)

We can solve this equation with an initial condition of H = 2.75 m using the 4th-order RK method with a step size of 6 s. If this is done, the result can be plotted as shown,

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 24: Numerical methods for engineers 5th e-solution ch 25

24

0

1

2

3

0 2000 4000 6000 8000 10000

The results indicate that the tank empties at between t = 7482 and 7488 seconds.

25.25 The solution can be obtained with the 4th-order RK method with a step size of 1. Here are the results of the first few steps.

x y1950 25551951 2607.6761952 2661.1321953 2715.3681954 2770.381955 2826.168

The entire solution along with the data can be plotted as

0

2000

4000

6000

8000

1940 1960 1980 2000 2020

25.26 The equations to be integrated are

The solution can be obtained with Euler’s method with a step size of 1. Here are the results of the first few steps.

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 25: Numerical methods for engineers 5th e-solution ch 25

25

t v x dv/dt dx/dt0 1400 0 -9.81 14001 1390.19 1400 -9.80569 1390.192 1380.384 2790.19 -9.80141 1380.3843 1370.583 4170.574 -9.79717 1370.5834 1360.786 5541.157 -9.79296 1360.7865 1350.993 6901.943 -9.78878 1350.993

The entire solution for height can be plotted as

0

40000

80000

120000

0 50 100 150 200 250

The maximum height occurs at about 146 s.

25.27 First, we must recognize that the evaluation of the definite integral

is equivalent to solving the differential equation

for y(b) given the initial condition y(a) = 0. Thus, we must solve the following initial-value problem:

where y(0) = 0. We can do this in a number of ways. One convenient approach is to use the MATLAB function ode45 which implements an adaptive RK method. To do this, we must first set up an M-file to evaluate the right-hand side of the differential equation,

function dy = humpsODE(x,y)dy = 1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04) - 6;

Then, the integral can be evaluated as

>> [x,y] = ode45(@humpsODE,[0 0.5 1],0);

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.

Page 26: Numerical methods for engineers 5th e-solution ch 25

26

>> disp([x,y]) 0 0 0.50000000000000 21.78356481821654 1.00000000000000 29.85525185285369

Thus, the integral estimate is approximately 29.86.

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission.