Solve in Matlab In class we considered the equation y y2 3x
Solve in Matlab:
In class we considered the equation y = y2 3x, where y(0) = 1.
a. Use an Euler approximation with a step size of 0.1 to approximate y(2).
b. Use a Runge-Kutta approximation with a step size of 0.5 to approximate y(2).
c. Graph both approximation functions in the same window as a slope field for the differential equation.
d. Find a formula for the actual solution (not by hand!) and compute its value at x = 2. (You can use vpa(...) to compute the result as a decimal expression.) Is the explicit formula useful? Is the computed value of y(2) useful? In your comments, explai
Solution
clear all
 f = @(x,y) (y^2 - 3*x);
 x = 0;
 i = 1;
 y(1) = 1;
 for i = 2: 21
 y(i) = y(i-1) + 0.1*(f( x(i-1), y(i-1)));
 x(i) = x(i-1) + 0.1;
 end
 y(i)
disp(\'rk4\')
 %rk4%
 z(1) = 1;
 for i = 2:21
 k1 = f(x(i-1), z(i-1));
 k2 = f(x(i-1) + 0.05, z(i-1) + 0.05*k1);
 k3 = f(x(i-1) + 0.05, z(i-1) + 0.05*k2);
 k4 = f(x(i-1) + 0.1, z(i-1) + 0.1*k3);
 x(i) = x(i-1) + 0.1;
 z(i) = z(i-1) + 0.1/6*(k1 + 2*k2 + 2*k3+ k4);
 end
 z(i)
y is the euler approximation at 2 and z(i) is the rk4 approximatiom for 2.

