3 correction of error
source | link

I believe your code is not working as you are not incrementing your iterator t. I present the following (using FoldList) as alternative iterative approach.

euler2d[f_, g_, h_, t0_, f0_, g0_, n_] :=
 FoldList[#1 + 
    h {f[#2, First@#1, Last@#1], g[#2, First@#1, Last@#1]} &, {f0, 
   g0}, Range[t0, t0 + n h , h]]

Note in this case the interval of interest: [t0,t0+n h]. You could recode as desired.

Test functions:

k[t_, x_, y_] := -y
v[t_, x_, y_] := (x - 2 y)/2

Exact solution:

sol = {x[t], y[t]} /. 
  First@DSolve[{x'[t] == -y[t], y'[t] == (x[t] - 2 y[t])/2, x[0] == 1,
      y[0] == 1}, {x[t], y[t]}, t]

Comparing for interval [0,3]:

Show[ParametricPlot[sol /. t -> u, {u, 0, 3}], 
 ListPlot[euler2d[k, v, t, x, y, 0.05, 0, 1, 1, 60], 
  PlotStyle -> Red], Frame -> True, 
 FrameLabel -> {TraditionalForm@x, TraditionalForm@y}, 
 BaseStyle -> 12]

enter image description here

I believe your code is not working as you are not incrementing your iterator t. I present the following (using FoldList) as alternative iterative approach.

euler2d[f_, g_, h_, t0_, f0_, g0_, n_] :=
 FoldList[#1 + 
    h {f[#2, First@#1, Last@#1], g[#2, First@#1, Last@#1]} &, {f0, 
   g0}, Range[t0, t0 + n h , h]]

Note in this case the interval of interest: [t0,t0+n h]. You could recode as desired.

Test functions:

k[t_, x_, y_] := -y
v[t_, x_, y_] := (x - 2 y)/2

Exact solution:

sol = {x[t], y[t]} /. 
  First@DSolve[{x'[t] == -y[t], y'[t] == (x[t] - 2 y[t])/2, x[0] == 1,
      y[0] == 1}, {x[t], y[t]}, t]

Comparing for interval [0,3]:

Show[ParametricPlot[sol /. t -> u, {u, 0, 3}], 
 ListPlot[euler2d[k, v, t, x, y, 0.05, 0, 1, 1, 60], 
  PlotStyle -> Red], Frame -> True, 
 FrameLabel -> {TraditionalForm@x, TraditionalForm@y}, 
 BaseStyle -> 12]

enter image description here

I believe your code is not working as you are not incrementing your iterator t. I present the following (using FoldList) as alternative iterative approach.

euler2d[f_, g_, h_, t0_, f0_, g0_, n_] :=
 FoldList[#1 + 
    h {f[#2, First@#1, Last@#1], g[#2, First@#1, Last@#1]} &, {f0, 
   g0}, Range[t0, t0 + n h , h]]

Note in this case the interval of interest: [t0,t0+n h]. You could recode as desired.

Test functions:

k[t_, x_, y_] := -y
v[t_, x_, y_] := (x - 2 y)/2

Exact solution:

sol = {x[t], y[t]} /. 
  First@DSolve[{x'[t] == -y[t], y'[t] == (x[t] - 2 y[t])/2, x[0] == 1,
      y[0] == 1}, {x[t], y[t]}, t]

Comparing for interval [0,3]:

Show[ParametricPlot[sol /. t -> u, {u, 0, 3}], 
 ListPlot[euler2d[k, v, 0.05, 0, 1, 1, 60], 
  PlotStyle -> Red], Frame -> True, 
 FrameLabel -> {TraditionalForm@x, TraditionalForm@y}, 
 BaseStyle -> 12]

enter image description here

2 deleted 12 characters in body
source | link

I believe your code is not working as you are not incrementing your iterator t. I present the following (using FoldList) as alternative iterative approach.

euler2d[f_, g_, t_, x_, y_, h_, t0_, f0_, g0_, n_] :=
 FoldList[#1 + 
    h {f[#2, First@#1, Last@#1], g[#2, First@#1, Last@#1]} &, {f0, 
   g0}, Range[t0, t0 + n h , h]]

Note in this case the interval of interest: [t0,t0+n h]. You could recode as desired.

Test functions:

k[t_, x_, y_] := -y
v[t_, x_, y_] := (x - 2 y)/2

Exact solution:

sol = {x[t], y[t]} /. 
  First@DSolve[{x'[t] == -y[t], y'[t] == (x[t] - 2 y[t])/2, x[0] == 1,
      y[0] == 1}, {x[t], y[t]}, t]

Comparing for interval [0,3]:

Show[ParametricPlot[sol /. t -> u, {u, 0, 3}], 
 ListPlot[euler2d[k, v, t, x, y, 0.05, 0, 1, 1, 60], 
  PlotStyle -> Red], Frame -> True, 
 FrameLabel -> {TraditionalForm@x, TraditionalForm@y}, 
 BaseStyle -> 12]

enter image description here

I believe your code is not working as you are not incrementing your iterator t. I present the following (using FoldList) as alternative iterative approach.

euler2d[f_, g_, t_, x_, y_, h_, t0_, f0_, g0_, n_] :=
 FoldList[#1 + 
    h {f[#2, First@#1, Last@#1], g[#2, First@#1, Last@#1]} &, {f0, 
   g0}, Range[t0, t0 + n h , h]]

Note in this case the interval of interest: [t0,t0+n h]. You could recode as desired.

Test functions:

k[t_, x_, y_] := -y
v[t_, x_, y_] := (x - 2 y)/2

Exact solution:

sol = {x[t], y[t]} /. 
  First@DSolve[{x'[t] == -y[t], y'[t] == (x[t] - 2 y[t])/2, x[0] == 1,
      y[0] == 1}, {x[t], y[t]}, t]

Comparing for interval [0,3]:

Show[ParametricPlot[sol /. t -> u, {u, 0, 3}], 
 ListPlot[euler2d[k, v, t, x, y, 0.05, 0, 1, 1, 60], 
  PlotStyle -> Red], Frame -> True, 
 FrameLabel -> {TraditionalForm@x, TraditionalForm@y}, 
 BaseStyle -> 12]

enter image description here

I believe your code is not working as you are not incrementing your iterator t. I present the following (using FoldList) as alternative iterative approach.

euler2d[f_, g_, h_, t0_, f0_, g0_, n_] :=
 FoldList[#1 + 
    h {f[#2, First@#1, Last@#1], g[#2, First@#1, Last@#1]} &, {f0, 
   g0}, Range[t0, t0 + n h , h]]

Note in this case the interval of interest: [t0,t0+n h]. You could recode as desired.

Test functions:

k[t_, x_, y_] := -y
v[t_, x_, y_] := (x - 2 y)/2

Exact solution:

sol = {x[t], y[t]} /. 
  First@DSolve[{x'[t] == -y[t], y'[t] == (x[t] - 2 y[t])/2, x[0] == 1,
      y[0] == 1}, {x[t], y[t]}, t]

Comparing for interval [0,3]:

Show[ParametricPlot[sol /. t -> u, {u, 0, 3}], 
 ListPlot[euler2d[k, v, t, x, y, 0.05, 0, 1, 1, 60], 
  PlotStyle -> Red], Frame -> True, 
 FrameLabel -> {TraditionalForm@x, TraditionalForm@y}, 
 BaseStyle -> 12]

enter image description here

1
source | link

I believe your code is not working as you are not incrementing your iterator t. I present the following (using FoldList) as alternative iterative approach.

euler2d[f_, g_, t_, x_, y_, h_, t0_, f0_, g0_, n_] :=
 FoldList[#1 + 
    h {f[#2, First@#1, Last@#1], g[#2, First@#1, Last@#1]} &, {f0, 
   g0}, Range[t0, t0 + n h , h]]

Note in this case the interval of interest: [t0,t0+n h]. You could recode as desired.

Test functions:

k[t_, x_, y_] := -y
v[t_, x_, y_] := (x - 2 y)/2

Exact solution:

sol = {x[t], y[t]} /. 
  First@DSolve[{x'[t] == -y[t], y'[t] == (x[t] - 2 y[t])/2, x[0] == 1,
      y[0] == 1}, {x[t], y[t]}, t]

Comparing for interval [0,3]:

Show[ParametricPlot[sol /. t -> u, {u, 0, 3}], 
 ListPlot[euler2d[k, v, t, x, y, 0.05, 0, 1, 1, 60], 
  PlotStyle -> Red], Frame -> True, 
 FrameLabel -> {TraditionalForm@x, TraditionalForm@y}, 
 BaseStyle -> 12]

enter image description here