# SECTION 5.3# Example 2with(linalg);x := vector(2);f := vector(2);f := x -> [x[2],-4*x[2]-3*x[1]];x0 := vector(2); xnew := vector(2);x0[1] := 1.5; x0[2] := -2.5;h := .1;x := evalm(x0);# First step:x := evalm(x+h*f(x));# Second step:x := evalm(x + h*f(x));# Now take all ten steps:# First check syntax of "for" loop:?fort := 0;x := evalm(x0);# Initialization of t and xfor n from 1 to 10 dot := t + h;x := evalm(x+h*f(x));# Next t and xend do;# Example 3k := 'k'; n := 'n';# We will put the above loop inside another loop that changes the step sizes h.# Also, we'll only display h and the approximation for y at t=1:print('h','y(1,h)');h := .1: N := 10: # Initial step size and number of stepsfor k from 1 to 5 dot := 0:x := evalm(x0): # Initialization of t and xfor n from 1 to N dot := t + h: x := evalm(x+h*f(x)):# Next t and xend do:print(h,x[1]);h := h/2: N := 2*N: end do:# Example 4f := x -> [2*x[1]-2*x[1]*x[2],x[1]*x[2]-x[2]];x0 := [1,3];h := 1.; x := evalm(x0); # Initializationk1 := evalm(h*f(x));k2 := evalm(h*f(evalm(x+.5*k1)));k3 := evalm(h*f(evalm(x+.5*k2)));k4 := evalm(h*f(evalm(x+k3)));x := evalm(x + (k1+2*(k2+k3)+k4)/6.); # The above represents a single Runge-Kutta step. It will be# convenient to make it into a "procedure" called rk4. First, we review the appropriate syntax:? procrk4 := proc(x,h)local k1, k2, k3, k4;k1 := evalm(h*f(x));k2 := evalm(h*f(evalm(x+.5*k1)));k3 := evalm(h*f(evalm(x+.5*k2)));k4 := evalm(h*f(evalm(x+k3)));x := evalm(x + (k1+2*(k2+k3)+k4)/6.);end proc;# Next, we'll test the procedure to see if it gives the same result that we got before:x := evalm(x0); h := 1.;xnew := rk4(x,h);# Next, we create the loops to change h, and to approximate x at t=1 for each h:n := 'n'; k := 'k';print('h','x(1,h)');h := 1.: N := 1: # Initial step size and number of stepsfor k from 1 to 5 dot := 0:x := evalm(x0): # Initialization of t and xfor n from 1 to N dox := rk4(x,h):end do:print(h,x);h := h/2: N := 2*N: end do: