(* EULER'S ALGORITHM 5.1 * * To approximate the solution of the initial value problem: * Y' = F(T,Y), A <= T <= B, Y(A) = ALPHA, * at n+1 equally spaced points in the interval [A,B]. * * INPUT: endpoints a, b; initial condition ALPHA; integer n. * * OUTPUT: Approximation W to Y at the (n+1) values of T. *) TEMP = Input["This is Euler's Method\n Input the function F(T,Y) in terms of t and y\n \n For example: y-t^2+1\n"]; F[t_,y_] :=Evaluate[TEMP]; OK = 0; While[OK == 0, A = Input["Input the left endpoint\n"]; B = Input["Input the right endpoint\n"]; If[A >= B, Input["Left endpoint must be less than right endpoint\n \n Press 1 [enter] to continue\n"], OK = 1; ]; ]; ALPHA = Input["Input thr initial condition\n"]; OK = 0; While[OK == 0, n = Input["Input a positive integer for the number of\n subintervals\n"]; If[n <= 0, Input["Number must be a positive integer\n \n Press 1 [enter] to continue\n"], OK = 1; ]; ]; If[OK == 1, FLAG = Input["Select output destination\n 1. Screen\n 2. Text file\n Enter 1 or 2\n"]; If[FLAG == 2, NAME = InputString["Input the file name\n For example: output.dta\n"]; OUP = OpenWrite[NAME,FormatType->OutputForm], OUP = "stdout"; ]; Write[OUP,"Euler's Method\n"]; Write[OUP,"t w \n"]; Write[OUP,"\n"]; (* Step 1 *) H = N[(B-A)/n]; T = A; W = ALPHA; Write[OUP,SetPrecision[T,9]," \n",SetPrecision[W,9]]; (* Step 2 *) For[i = 1, i <= n, i++, (* Step 3 - Compute W(I) *) W = W+H*F[T,W]; (* Compute T(I) *) T = A+i*H; (* Step 4 *) Write[OUP,SetPrecision[T,9]," \n",SetPrecision[W,9]]; ]; (* Step 5 *) If[OUP == "OutputStream[",NAME," 3]", Print["Output file: ",NAME," created successfully\n"]; Close[OUP] ]; ]; Quit[];