In: Mechanical Engineering
Show to use ode45 built-in function in MATLAB to solve the following ODE problem:
dydx=x^2 / y y(0)=2
Calculate y(x) for 0 ? x ? 20 and compare the results to analytical solution y = sqrt((2x^3 / 3) + 4)
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