Automation
This approach can be automated. Let us create a function which returns, for a given guess, the value at
:
>
> RK4sys2:=proc(f, g, x0, alpha, beta, h, N)
> local n,ku1,ku2,ku3,ku4,kw1,kw2,kw3,kw4;
> x(0):=x0:u(0):=alpha:w(0):=beta:
> for n from 0 to N-1 do
> ku1:=h*f(x(n),u(n),w(n)):
> kw1:=h*g(x(n),u(n),w(n)):
> ku2:=h*f(x(n)+h/2,u(n)+ku1/2,w(n)+kw1/2):
> kw2:=h*g(x(n)+h/2,u(n)+ku1/2,w(n)+kw1/2):
> ku3:=h*f(x(n)+h/2,u(n)+ku2/2,w(n)+kw2/2):
> kw3:=h*g(x(n)+h/2,u(n)+ku2/2,w(n)+kw2/2):
> ku4:=h*f(x(n)+h,u(n)+ku3,w(n)+kw3):
> kw4:=h*g(x(n)+h,u(n)+ku3,w(n)+kw3):
> u(n+1) := u(n) + (1/6)*(ku1+2*ku2+2*ku3+ku4):
> w(n+1) := w(n) + (1/6)*(kw1+2*kw2+2*kw3+kw4):
> x(n+1) := x(n) + h:
> od:
> # print(x(N),u(N),w(N));
> end:
(This version suppresses the printout)
>
> guproc:=proc(beta)
> RK4sys2(f,g,1.,1.,beta,0.01,100):
> u(100);
> end:
> gu:=t->guproc(t);
> gu(0.);
> betaf:=solve(gu(t)=2,t);
>
Indeed,
> RK4sys2(f,g,1.,1.,betaf,0.01,100);u(100);
>
Theory can provide more intellectually appealing ways to solve boundary value problems using initial value problems for certain classes of problems. Also, other approaches, using finite differences or finite elements can be used to solve Boundary Value Problems directly.
>