I need to program Euler's method to solve a system of two diffferential equations of first order.
Fist, I have programmed the Euler's method for just one differential equation:
euler[f_, ini_, int_, h_] :=
Module[{x, y, l}, l = {ini};
y = ini[[2]];
For[x = int[[1]], x <= int[[2]] - h, x = x + h,
y = y + h*f[x, y]; l = AppendTo[l, {x + h, y}]];l]
And it works fine.
f[x_, y_] := 3 (1 - y);
euler[f, {0, 0.01}, {0, 10}, 1]
{{0, 0.01}, {1, 2.98}, {2, -2.96}, {3,8.92}, {4, -14.84}, {5, 32.68}, {6, -62.36}, {7,127.72}, {8, -252.44}, {9, 507.88}, {10, -1012.76}}
Now, changing a little the previous function I want to solve a system of two differential equations of first order. I programmed the following:
euler2[f_, g_, inif_,inig_, int_, h_] :=
Module[{x, y, t, list1, list2}, list1 = {inif}; list2 = {inig}; x = inif[[2]];
y = inig[[2]]; For[t = int[[1]], t <= int[[2]] - h, t = t + h,
x = x + h*f[x, y, t]; y = y + h*g[x, y, t]];
list1 = AppendTo[list1, {t + h, x}];
list2 = AppendTo[list2, {t + h, y}]; {list1, list2}]
But I got an error. What am I doing wrong?
For example, I define f anf g in this way:
f[x_, y_, t_] := y; g[x_, y_, t_] :=0.5x+0.5y;
inif= is a set of the initial value of x (x(0)=1$\to$ inif={0,1}) and inig= is a set of the initial value of y (y(0)=1 $\to$ inig={0,1})
int is the interval where I want to calculate the solution int={0,10} and h the lenght of each step h=1
Thank you for your help.

f[x_, y_, t_] := y;why would you pass inxandtto the functionfif they are not used in the body of the function?g[x_, y_, t_] :=0.5x+0.5yand here,tis not used in the body of the function, so why is it passed? if you have the problem description you are trying to solve from the source, can you post it? I am having hard time seeing what you are trying to do. – Nasser Mar 08 '14 at 11:58