1

enter image description here

The algorithm isn't given any output, the graph is unidentifiable. The systems of Differential Equations considered:

$$ x'[t]=-x[t]+y[t]\\ y'[t]=-x[t]-y[t]\\ x[0]=0, y[0]=4, h=0.1$$

Please how do I represent k1, l1, k2, l2, k3, l3, k4, l4, y(n+1), and x(n+1) for the simulation to run?

Here is the code:

    DSolve[{x'[t] == -x[t] + y[t], y'[t] == -x[t] - y[t], 
    x[0] == 0, y[0] == 4}, {x[t], y[t]}, t]
    {{x[t] -> 4 E^-t Sin[t], y[t] -> 4 E^-t Cos[t]}}
In[2]:= ti = 0;

tf = 10 ; m = 100; Δt = (tf - ti)/m; T = Range[ti, tf, Δt] // N; X = ConstantArray[0., m + 1]; Y = ConstantArray[0., m + 1];

In[1]:= X[[1]] = 0.; Y[[1]] = 4.; Do[ k1 = Δt ( Y[[n]] - X[[n]]); l1 = -Δt (X[[n]] + Y[[n]]); k2 = Δt ( (Y[[n]] + l1/2) - (X[[n]] + k1/2)); l2 = -Δt ( (X[[n]] + k1/2) + (Y[[n]] + l1/2)); k3 = Δt ( (Y[[n]] + k2/2) - (X[[n]] + l2/2)); l3 = -Δt ( (X[[n]] + k2/2) + (Y[[n]] + l2/2)); k4 = Δt ( (Y[[n]] + k3) - (X[[n]] + l3)); l4 = -Δt ( (X[[n]] + k3) + (Y[[n]] + l3)); X[[n + 1]] = Y[[n]] - X[[n]] + 1/6 (k1 + 2 k2 + 2 k3 + k4); Y[[n + 1]] = -X[[n]] - Y[[n]] + 1/6 (l1 + 2 l2 + 2 l3 + l4),{n, 1, m}]

I am having a hard time using this algorithm to compute for the systems of ODE. The first algorithm seems to be much. I would be glad if anyone can help out

rk4Step[{f_, t_, y_, stepSize_}] :=
 Module[{k1, k2, k3, k4, h = N[stepSize]},
  k1 = h f[N[t], y];
  k2 = h f[t + h/2, y + 1/2 k1];
  k3 = h f[t + h/2, y + 1/2 k2];
  k4 = h f[t + h, y + k3];
  {f, t + h, y + 1/6 (k1 + 2 k2 + 2 k3 + k4), h}]

RK4[f_, t0_, y0_, tEnd_, n_] := Module[{h = N[(tEnd - t0)/n]}, Most /@ Rest /@ NestList[rk4Step, {f, t0, y0, h}, n]]

Conor
  • 7,449
  • 1
  • 22
  • 46
Jimi
  • 11
  • 2
  • 1
    People here generally like users to post code as Mathematica code instead of just images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this meta Q&A helpful – Michael E2 Feb 04 '21 at 01:35
  • Do[] has no output. What happens if you execute X or Y? – Michael E2 Feb 04 '21 at 01:36
  • 1
    Possible duplicate: https://mathematica.stackexchange.com/questions/23516/solving-a-system-of-odes-with-the-runge-kutta-method – Michael E2 Feb 04 '21 at 01:39
  • You may have a typo in your expression for k2. The expression refers to a variable K1. Maybe that should be lowercase k1. – LouisB Feb 04 '21 at 01:44
  • @LouisB I changed, it's the same. – Jimi Feb 04 '21 at 02:12
  • @MichaelE2 Irrespective of that, it should give output. I tried it when simulating for Euler's method, and it gave output. – Jimi Feb 04 '21 at 02:13
  • @MichaelE2 , thanks for making the first comment – Jimi Feb 04 '21 at 02:14
  • 2
    "Irrespective of that, it should give output." No, it (the Do[] command) shouldn't. Why do you think it should? That you got output with a different code (Euler's method) does not mean this code would produce output; it just means you used a command that produced output. In changing the code to RK4, you (inadvertently) got rid of the output command. From your code above, I do get output from ListLinePlot[Transpose@{X, Y}]. – Michael E2 Feb 04 '21 at 02:44
  • 1
  • @MichaelE2 Thanks for bringing it to my notice, I just observed that I was supposed to have removed the back spacing immediately after the Do[k1... ] command. It was empty because of the spacing there. – Jimi Feb 04 '21 at 08:42

0 Answers0