%% Using dsolve to solve first order ODEs %% Section 2.2 #9 % Solve $x^2y'=y \ln y - y'$. % % I solve it using *dsolve*. The equation has to be enclosed in single % quotes and so does the independent variable. (If you leave out the % argument for the independent variable, MATLAB assumes it is $t$, which % isn't what you want here!) syms x dsolve('x^2*Dy = y*log(y) - Dy','x') %% % You could also define the equation in MATLAB and give it a name, then use % that in *dsolve*. eqn = 'x^2*Dy = y*log(y) - Dy' dsolve(eqn,'x') %% Solving an inital value problem % Solve the same problem with $y(0)=2$. % % I add the initial value as the second argument. The independent variable % has to be the last argument. dsolve('x^2*Dy = y*log(y) - Dy','y(0)=2','x') %% % I could also combine the equation and initial value into one argument. dsolve('x^2*Dy = y*log(y) - Dy, y(0)=2','x') %% Solving with a general initial value $y(0)=c$ Y= dsolve('x^2*Dy = y*log(y) - Dy','y(0)=c','x') %% % This is messy. I'll simplify it. Y = simplify(Y) %% % That helps a little. %% % You could define the initial value % problem in MATLAB, give it a name and use it in *dsolve*. IVP = 'x^2*Dy = y*log(y) - Dy, y(0)=c' dsolve(IVP,'x') %% Plotting the solutions for several initial values % Plot the solutions with initial values 0,1,2,3,4 figure; hold on for j = 0:4 ezplot(subs(Y,'c',j),[0 1]) end hold off axis([0 1 -1 25]) title('Solutions with initial value 0,...,4') %% % The examples in _Differential Equations with MATLAB_ use the *figure* % command. At least here you can omit it.