The Matlab code is given below:
Matlab
Code:
%Start of code
xfinal = 20; % Final value of x
xspan = [0 xfinal];
y0 = 2; % Initial value of y
F = @(x,y) (x^2/y); % Differential function
[X,Y] = ode45(F,xspan,y0); % Solve the equation
%Results at specified vlaues of x
Y0 = Y(X == 0)
Y0_analytical = ((2*0.^3/3)+4).^0.5
Y20 = Y(X == 20)
Y20_analytical = ((2*20.^3/3)+4).^0.5
% Plot both the solutions
plot(X,Y,'.')
hold on
plot(X,((2*X.^3/3)+4).^0.5,'r')
xlabel('X')
ylabel('Y')
legend('ode45','analytical solution'
% End of code
As it can be noticed, the solution obtained using ode45 is close
to the analytical solution