3
Assignment no: 8. Euler and Modified Euler equation to solve D i f f e r e n - tial Equations Method:1 Solving Differential equation by NDSolve function clear@ppD clear@ppD pp = NDSolve@8y'@tD t^2 + Hy@tDL ^2 - 1, y@- 2D - 2<, y, 8t, - 2, 2<D 88y InterpolatingFunction@88- 2., 2.<<, <>D<< plt@1D = Plot@Evaluate@y@tD. ppD, 8t, - 2, 2<, PlotStyle 8Orange, Thick<D -2 -1 1 2 -2.0 -1.5 -1.0 -0.5 0.5 clear@modiefiedeulerD clear@modiefiedeulerD modifiedeuler@f_, 8x_, x0_, xn_<, 8y_, y0_<, steps_D := Block@8xold = x0, yold = y0, sollist = 88x0, y0<<,x,y,h<,h = N@Hxn - x0L stepsD; Do@xnew = xold + h; fold = f . 8x xold, y yold<; ynew = yold + Hh 2L * HHfoldL + f . 8x xold + h, y yold + h * fold<L; sollist = Append@sollist, 8xnew, ynew<D; xold = xnew; yold = ynew, 8steps<D; Return@sollistDD

Euler Method using Mathematica

Embed Size (px)

DESCRIPTION

Euler and Modified Euler mathematica programming

Citation preview

Page 1: Euler Method using Mathematica

Assignment no: 8. Euler and Modified Euler equation to solve Differen-

tial Equations

Method:1 Solving Differential equation by NDSolve function

clear@ppDclear@ppD

pp = NDSolve@8y'@tD � t^2 + Hy@tDL^2 - 1, y@-2D � -2<, y, 8t, -2, 2<D88y ® InterpolatingFunction@88-2., 2.<<, <>D<<

plt@1D = Plot@Evaluate@y@tD �. ppD, 8t, -2, 2<, PlotStyle ® 8Orange, Thick<D

-2 -1 1 2

-2.0

-1.5

-1.0

-0.5

0.5

clear@modiefiedeulerDclear@modiefiedeulerD

modifiedeuler@f_, 8x_, x0_, xn_<, 8y_, y0_<, steps_D :=

Block@8xold = x0, yold = y0, sollist = 88x0, y0<<, x, y, h<, h = N@Hxn - x0L � stepsD;

Do@xnew = xold + h;

fold = f �. 8x ® xold, y ® yold<;

ynew = yold + Hh � 2L * HHfoldL + f �. 8x ® xold + h, y ® yold + h * fold<L;

sollist = Append@sollist, 8xnew, ynew<D;

xold = xnew;

yold = ynew, 8steps<D;

Return@sollistDD

Page 2: Euler Method using Mathematica

solution = heun@x^2 + y^2 - 1, 8x, -2, 2<, 8y, -2<, 20D

98-2, -2<, 8-1.8, -1.04<, 8-1.6, -0.537726<, 8-1.4, -0.253993<, 8-1.2, -0.1073<,

8-1., -0.0621201<, 8-0.8, -0.0973579<, 8-0.6, -0.193606<, 8-0.4, -0.327991<,

8-0.2, -0.47472<, 9-2.77556 ´ 10-16

, -0.60954=, 80.2, -0.714329<, 80.4, -0.778617<,

80.6, -0.797869<, 80.8, -0.770441<, 81., -0.694706<, 81.2, -0.566662<,

81.4, -0.377375<, 81.6, -0.108672<, 81.8, 0.276739<, 82., 0.863166<=

In[77]:= plt@3D = ListPlot@%7,

PlotStyle ® Directive@RGBColor@0., 0.95, 0.09D, [email protected], DashedD,

Joined ® TrueD

Out[77]=

-2 -1 1 2

-2.0

-1.5

-1.0

-0.5

0.5

euler@f_, 8t_, t0_, tn_<, 8y_, y0_<, steps_D :=

euler@f_, 8t_, t0_, tn_<, 8y_, y0_<, steps_D :=

Block@8told = t0, yold = y0, sollist = 88t0, y0<<, t, y, h<, h = N@Htn - t0L � stepsD;

Do@tnew = told + h;

ynew = yold + h * Hf �. 8t ® told, y ® yold<L;

sollist = Append@sollist, 8tnew, ynew<D;

told = tnew;

yold = ynew, 8steps<D;

Return@sollistDD

solution1 = euler@t^2 + y^2 - 1, 8t, -2, 2<, 8y, -2<, 20D

98-2, -2<, 8-1.8, -0.6<, 8-1.6, -0.08<, 8-1.4, 0.23328<, 8-1.2, 0.436164<,

8-1., 0.562212<, 8-0.8, 0.625428<, 8-0.6, 0.63166<, 8-0.4, 0.583459<,

8-0.2, 0.483544<, 9-2.77556 ´ 10-16

, 0.338307=, 80.2, 0.161197<, 80.4, -0.0256058<,

80.6, -0.193475<, 80.8, -0.313988<, 81., -0.36627<, 81.2, -0.33944<,

81.4, -0.228396<, 81.6, -0.0259629<, 81.8, 0.286172<, 82., 0.750551<=

2 Assignment 8.nb

Page 3: Euler Method using Mathematica

plt@2D = ListPlot@%19, PlotStyle ®

Directive@RGBColor@1., 0., 0.56D, [email protected], DashedD, Joined ® TrueD

-2 -1 1 2

-2.0

-1.5

-1.0

-0.5

0.5

In[78]:= plt11 = Show@8plt@1D, plt@2D<, plt@3DD

Out[78]=

-2 -1 1 2

-2.0

-1.5

-1.0

-0.5

0.5

From these three graphs it is evident that the modified euler’s method is perfect match to the

analytical solution obtained from Mathematica but there is significant error if soultion is obtained

from Euler’s method.

Assignment 8.nb 3