I need to use Mathematica to solve the following DE by the fourth-order Runge-Kutta method and find $y(10)$: $$y'(x)=\frac{1}{2y}, y(0)=1$$ The following is the code I have so far (with step size h=0.5):
Clear[x, y, n, h, S1, S2, S3, S4, i];
y[0] = 1; y[1] = Sqrt[2]; h = 0.5; n = 20;
Do[x[i] = 0 + (i - 1)*h, {i, 1, n + 1}]
y'[x_] = f[x_, y_] = 1/(2*y);
Do[{S1 = f[x[i], y[i]],
S2 = f[x[i] + h/2, y[i] + h*S1/2],
S3 = f[x[i] + h/2, y[i] + h*S2/2],
S4 = f[x[i] + h, y[i] + h*S3],
y[i + 1] = y[i] + (S1 + 2 S2 + 2 S3 + S4)*h/6}, {i, 1, n}]
Do[Print[x[i], " ", y[i]], {i, 1, n + 1}]
This code results in an approximation of $y(10)=3.4641$, which is fine; however, when I change the step size and the corresponding $n$ value I still get $y(10)=3.4641$ regardless of the value of the step size, which doesn't make sense to me. I'm almost certain that the smaller the step size one chooses, the better the approximation should be, so we shouldn't observe the same value in error. I'm not sure how to proceed from here, but I'm sure there is an error in my code somewhere. Any help is greatly appreciated.
y[1] = Sqrt[2]? – Michael E2 May 10 '17 at 03:05