MATLAB: ODE45 Not Plotting

exact solutionnumerical solutionodeode45plot

I need to solve the equation y''+(1/x)y'-(1/x^2)y=0 with the initial conditions y(1)=0, y'(1)=0.01 usig ode45. I also have to plot the exact solution. When I run the code it plots the exact solution, but does not plot the numerical solution. Why is it not plotting?
function Untitled2
clc;clear all;clf
%Enter initial condition matrix
yo=[0,0.01];
[x,y]=ode45(@DE2,(0:0.1:5),[);
plot(x,y(:,1),'k-','Linewidth',1.5)
xlabel('r'),ylabel('u'),grid on
title('Numerical Solution of u(r)""+1/u(r)"-1/u(r)^2=0')
hold on
% Plot Exact Solution
X=0:.5:5;Y=-.002.*X+0.012./X;
plot(X,Y,'or','markerfacecolor','r')
legend('Numerical Solution','Exact Solution')
function dydx=DE2(x,y)
%Computes Derivatives of Each Equation
dydx=[y(2);((-1/x)*y(2)+(1/x^2)*y(1))];

Best Answer

The initial conditions are for y(1) and y'(1) which are for y(x) and y'(x) with x=1. However, your configuration is over x=0:.1:5 which starts at x=0 not x=1
The first entry in the tspan parameter must always be the point for which the initial conditions hold. From there the entries in tspan would typically increase, but it is also valid for the entries to decrease instead, for initial value problems. I would suggest to you that you should not be expecting to solve at x=0 as the equation divides by x.
Related Question