How to resolve the algorithm Runge-Kutta method step by step in the Dart programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Runge-Kutta method step by step in the Dart programming language

Table of Contents

Problem Statement

Given the example Differential equation: With initial condition: This equation has an exact solution:

Demonstrate the commonly used explicit   fourth-order Runge–Kutta method   to solve the above differential equation.

Starting with a given

y

n

{\displaystyle y_{n}}

and

t

n

{\displaystyle t_{n}}

calculate: then:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Runge-Kutta method step by step in the Dart programming language

Source code in the dart programming language

import 'dart:math' as Math;

num RungeKutta4(Function f, num t, num y, num dt){
  num k1 = dt * f(t,y);
  num k2 = dt * f(t+0.5*dt, y + 0.5*k1);
  num k3 = dt * f(t+0.5*dt, y + 0.5*k2);
  num k4 = dt * f(t + dt, y + k3);
  return y + (1/6) * (k1 + 2*k2 + 2*k3 + k4);
}

void main(){
  num t  = 0;
  num dt = 0.1;
  num tf = 10;
  num totalPoints = ((tf-t)/dt).floor()+1;
  num y  = 1;
  Function f  = (num t, num y) => t * Math.sqrt(y);
  Function actual = (num t) => (1/16) * (t*t+4)*(t*t+4);
  for (num i = 0; i <= totalPoints; i++){
    num relativeError = (actual(t) - y)/actual(t);
    if (i%10 == 0){
      print('y(${t.round().toStringAsPrecision(3)}) = ${y.toStringAsPrecision(11)}  Error = ${relativeError.toStringAsPrecision(11)}');
    }
    y  = RungeKutta4(f, t, y, dt);
    t += dt;
  }
}


  

You may also check:How to resolve the algorithm Order two numerical lists step by step in the Arturo programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Action! programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the C++ programming language