# Heun's method for a system of ODE's [closed]

I need to solve a system of two ODE's using improved Euler's (Heun) method. Can any one help as I am pretty bad at Mathematica. This is what I've come up with so far

f[t_, x_, y_] = -4 x - 2 y + cos (t) + 4 sin (t);
g[t_, x_, y_] = 3 x + y - 3 sin (t);
xlist = {0};
ylist = {-1};
h = .1;
n = 10;
For[i = 1, i <= n, i++,
{
k1 = h*f[ h*(i - 1), xlist[[i]], ylist[[i]] ],
m1 = h*g[ h*(i - 1), xlist[[i]], ylist[[i]] ],
k2 = h*f[h*(i - 0.5), xlist[[i], ylist[[i]] + 0.5 k1],
m2 = h*g[h*(i - 0.5), xlist[[i], ylist[[i]] + 0.5 m1],
k3 = h*f[h*(i - 0.5), xlist[[i]], ylist[[i]] + 0.5 k2]],
m3 = h*g[h*(i - 0.5), xlist[[i], ylist[[i]] + 0.5 m2],
k4 = h*f[ h*i, xlist[[i]], ylist[[i]] + k3]
m4 = h*g[ h*i, xlist[[i]], ylist[[i]] + m3]
AppendTo[xlist, xlist[[i]] + 1/6*(k1 + k2 + k3 + k4)],
AppendTo[ylist, ylist[[i]] + 1/6*(m1 + m2 + m3_m4)]
]]}]
xlist
ylist


## closed as off-topic by bbgodfrey, Michael E2, happy fish, Kuba♦May 7 '17 at 8:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – bbgodfrey, Michael E2, happy fish, Kuba
If this question can be reworded to fit the rules in the help center, please edit the question.

• One implementation of Heun's method can be found here: mathematica.stackexchange.com/questions/45436/… – Michael E2 May 6 '17 at 21:30
• You should take some time to learn the syntax, how function names are capitalized, when to use brackets, braces, and parentheses. Perhaps start by searching the documents for sin. – Michael E2 May 6 '17 at 21:41
• – Michael E2 May 7 '17 at 2:38

Toooooo many mistakes and syntax errors

f[t_, x_, y_] := -4 x - 2 y + Cos [t] + 4 Sin [t];
g[t_, x_, y_] := 3 x + y - 3 Sin [t];
xlist = {0};
ylist = {-1};
h = .1;
n = 10;
For[i = 1, i <= n,
i++, {k1 = h*f[h*(i - 1), xlist[[i]], ylist[[i]]];
m1 = h*g[h*(i - 1), xlist[[i]], ylist[[i]]];
k2 = h*f[h*(i - 0.5), xlist[[i]], ylist[[i]] + 0.5 k1];
m2 = h*g[h*(i - 0.5), xlist[[i]], ylist[[i]] + 0.5 m1];
k3 = h*f[h*(i - 0.5), xlist[[i]], ylist[[i]] + 0.5 k2];
m3 = h*g[h*(i - 0.5), xlist[[i]], ylist[[i]] + 0.5 m2];
k4 = h*f[h*i, xlist[[i]], ylist[[i]] + k3];
m4 = h*g[h*i, xlist[[i]], ylist[[i]] + m3] ;
AppendTo[xlist, xlist[[i]] + 1/6*(k1 + k2 + k3 + k4)],
AppendTo[ylist, ylist[[i]] + 1/6*(m1 + m2 + m3 + m4)]}]
xlist
ylist


{0, 0.193667, 0.373655, 0.540596, 0.694934, 0.836957, 0.966821, \ 1.08458, 1.19021, 1.28362, 1.36471}

{-1, -1.08051, -1.14683, -1.20053, -1.24296, -1.27526, -1.2984, \ -1.31319, -1.32032, -1.32039, -1.31388}

done

EDIT
plus a bonus in order to "see" the result

ListPlot[Table[{xlist[[t]], ylist[[t]]}, {t, 1, Length@xlist}]]


and for n=1000 you get a nice attractor

• I hope you get an A on this assignment, but I'm afraid that this use of For and AppendTo is bad practice. – Michael E2 May 7 '17 at 2:36