Skip navigation

Whats up party people.  As engineers we usually need to visually interpret data in order to fully understand and believe in it.  This is why I’m sure many of us weren’t to surprised upon discovering santa clause wasn’t real and is why in order to learn about differential equations we began with graphical analysis.  The concept of a solution is slighty different in regard to differential equations.  More commonly a solution is arriving at a single point or quantity.  Differential equations pose solutions of much more information.  This is why we need to analyze them graphically by plotting what is called a directional field using MATLAB.  This will show overall trends of the equations solution.  To further this understanding we  will then use a technique of numerical approximation known as Eulers (pronounced “oilers”) Method.  This generates a single trend line of the solution.  When placed over the directional field this line should follow the general flow of the graph…hopefully.


 I’ll begin with problem 18 from chapter two in “A Course in Ordinary Differential Equations”, our classes text.  The following code was influenced by Gary Davis’s famous “first block details” blog.

Problem 18(p. 94): dy/dx=x+y

>> [X,Y]=meshgrid(-2:0.4:2,-2:0.4:2);

>> DY=X+Y;

>> DX=ones(size(DY));

>> DW=sqrt(DX.^2+DY.^2);

>> quiver(X,Y,DX./DW,DY./DW,0.5,’.’);

>> xlabel(‘x’);

>> ylabel(‘y’);


You can see where each value passes either of the x=0 axis or y=0 axis the field lines shift from negative slope to postive slope.  I feel like this trend continues so I decided to expand the dimensions of the plot.  You can see this change in the MATLAB code, first line after “meshgrid”. 


>> [X,Y]=meshgrid(-5:0.25:3,-1:0.25:4);
>> DY=X+Y;
>> DX=ones(size(DY));
>> DW=sqrt(DX.^2+DY.^2);
>> quiver(X,Y,DX./DW,DY./DW,0.5,’.’);
>> xlabel(‘x’);
>> ylabel(‘y’)


Here you can see how as you move away from zero, the field lines gradually move to a vertical position.  Next we will use Eulers Method of numerical approximation.  To do this we’ll set up an m-file in MATLAB which contains the code for Eulers Method.  By doing this we’ll be able to just enter a reference to the m-file instead of having to type it each time it is used. The code for the m-file is as follows:

function [ x, y ] = euler ( f, x_range, y_initial, nstep )
dx = ( x_range(2) – x_range(1) ) / nstep;

for i = 1 : nstep
x(i+1) = x(i) + dx;
y(i+1) = y(i) + dx * feval ( f, x(i), y(i) );


  After saving that we then made another smaller m-file to specify what equation was being used:

function yprime = example1(x,y)
yprime = x+y;

Now that you have these two m-files you implement them in the MATLAB command window by entering:

 >> euler(’example1′,[0,6],1,60)

 This tells the program to use Eulers method on problem 18(saved as ‘example 1’ ) with an x-range from zero to six, a y-intial of one and a step size of sixty.  Below is the graphical result of this process.  As you can see the line transistions to a vertical postition as is moves away from the origin. 




 Next we’ll take this trendline and post it into the directional field. I feel that Emile Tayeh from section 1 portrayed it best on his first block post when he generated this graph:


Notice how the line follows the trend of the field?  The field lines run tangent to the numerical approximation.  This makes sense as differentiating an equation yields the slope of that equations tangent line.  Whether this is an accurate assumption or not it seems to me that this numerical line is the directional field’s integral.  The MATLAB code to produce this representation is as follows.


>> [X,Y]=meshgrid(0:0.5:12,0:0.5:18);

>> DY=x+y;
>> DW=sqrt(DX.^2+DY.^2);
>> quiver(X,Y,DX./DW,DY./DW,0.5);
>> hold on
>> plot(x,y)

>> hold off
>> xlabel(’x’)
>> ylabel(’y’)

As you can see this is just combining the two code sequences we have already explored.

Now to ensure that we understand what we just did, Gary assigned another equation, equation.  We will use the same techniques used in the previous example except now the equation is in terms of “t” instead of “x”.  This change of variable for our present purposes is meaningless.  Though this was a simple change you can view the error I intitially made when assigning axis labels.  I entered “tlabel” where “xlabel” was needed.  I was just worried about changing all the x’s to t’s, not realizing that such code was refering to the cartesian coordinate system where x is horizontal and y is vertical. 

>> [T,Y]=meshgrid(-4:.5:4,-4:.5:4);
>> DY=(Y.^2+T);
>> DT=ones(size(DY));
>> DW=sqrt(DT.^2+DY.^2);
>> quiver(T,Y,DT./DW,DY./DW,0.5,’.’);
>> tlabel(‘t’);
??? Undefined function or method ‘tlabel’ for input arguments of type ‘char’.

>> xlabel(‘t’)

>> ylabel(‘y’)


This plot may seem slightly more random then the last so lets continue and apply the Euler Method of numerical approximation. In only modifying the variables and intervals of the previously used MATLAB code we get:


 As you may notice, the intervals presented in the last graph were much smaller then that of the directional field, this is to best view the trend of the solution.  Keeping these smaller intervals, the trend line matches up beautifully with the directional plot.  Look at the point (-1,-1).  This is a perfect representation of the field line being tangent to the approximation. 


 In the end of it all I have learned some basics on the operation of MATLAB, how to plot differential equations and what Eulers Method is.  Personally I don’t feel Eulers Method is affective at all.  Most likely it may be myself missing an essential piece of information, but it seems like the trends created are almost to specific and are not the best representation of the entire field.  The two problems I explored were very simple .  I’m almost positive that presented with a more complex equation Eulers Method would be even less accurate.  But hey I don’t mean to come off as unappreciative, from what I’ve heard he seemed like a good fellow.  Anyway thats enough for now, take it easy.



  1. Nick,

    nicely done, well and fully explained. The blue on black is pretty crap, and I cannot see your code unless I run the cursor over it!


  2. Nick,

    where is block 2?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: