In this block we will use three different methods to solve six differential equations. The first method we will be using is Euler’s method, just as we did in block 1. The next two methods, the ode45 equation solver and the dsolve function in MATLAB will both be used an compared to Euler’s method.
PROBLEM 1. : dy/dx=x^4*y ; y(1)=1; expression solution $ latex y= \frac {1}{sin(x)+1} $
In these problems, using Euler’s method, i will be using the MATLAB code that i created in the previous block. The written m file was as follows,
function [x,y] = euler (f,x_range, y_initial, nstep)
dx = (x_range(2) – x_range(1)) / nstep;
x = 1;
y = y_initial;
for i = 1 :nstep
x(i+1) = x(i) + dx;
y(i+1) = y(i) + dx * feval( f, x(i), y(i) );
plot(x,y)
xlabel(‘x’)
ylabel(‘y’)
end
When i entered the first problem into the code,
exmplepm.m :
function yprime = pmexmple(x,y)
yprime = (x^4*y)
end
In the command screan, entered
>> eulerpm(‘pmexample’,[0,1],1,100);
MATLAB then yielded the following graph,

The next step was to use the ode45 method for the same problem. Since ode45 is already programed in MATLAB all that is necessary to yield a graph the following code in the command screan,
>>ode45(‘pmexample,1,100);
The following graph is created,

After getting both graphs, in order to get a better comparison, the next step is to overlay the two. We use the following code,
>> eulerpm(‘pmexample’,[0,1],1,100);
>> hold on
>> [x,y]=ode45(‘pmexample’,[0,1],1);
>> hold off
the overlay graph:

As you can see, the two graphs were so similar, they overlap each other making it seem as though there is only one equation line. This shows that both methods yielded the same answer to the equation.
The MATLAB solution using the dsolve method which evaluates differential equations used the following code,
dsolve(‘DY=x^4*y’,'x’)
the answer = 1/5*x^5y+C1
PROBLEM 2. : dy/dx = (x^4)*(y)
Using the same MATLAB codes as in the first problem, all that was changed were the codes to fit this problem. The same m files were used in this problem as in the first.
Euler’s Method produces the following graph:

By using a smaller step size, we can see how inaccurate Euler’s method is. As you can see, this method uses a series of lines instead of curves to approximate the solution.
The ode45 method, using the same code as problem 1 with a slight change then yields,

This graph is much more fluid in its curves and more accurate than the first. Now we can look at the over lay of the two graphs,

As we can see, the approximations are close to one another, however they are not equal. The Euler’s approximation is nice, but not quite as accurate as the ode 45 method.
Our next step is to look at this equation using the dsolve method in MATLAB
dsolve(’DY=Y*X^4′,’Y(1)=1′,’X’)
ans =
exp(X^5/5)/exp(1/5)
PROBLEM 3. : dy/dx = x + y ; y(0) = 0 expression solution y = 1 – x + e^x
Euler’s Method:

The graph displays the function as a slowly upward sloping curve. The graph looks as though it is a pretty good depiction, however, let’s take a look at the graph produced by the ode 45 method,
Ode45 Method:

Once again, we see a similar, yet smoother and more accurate graph produced by the ode45 method as opposed to Euler’s method.
The Overlay Graph:

The two curves of this function are very close to each other, but are not equal to each other. The ode45 method has produced the more accurate graph yet again.
The MATLAB dsolve method of solving this problem has given the following solution,
dsolve(’DY=x+y’,’x’)
ans =
1/2*x^2+y*x+C1
PROBLEM 4. : dy/dx = x*y^2 ; y(0)=1
Again, by changing the examplepm to fit the equation we can plot the graphs as follows,
Eulers Method:

The graph shows a curve function that looks similar to the curve in problem one. This is because the only difference in the two equations is the exponential power of x. This creates the graph to rise at a steeper pace than the previous curve. The next step is to look at the ode45 method and compare the graphs,
ode45 Method:

The graph is much similar to that of Euler’s method. In order to get a better visual understanding of the relations between the two, we must overlay the two functions on one graph. As it has been in the previous problems, I believe the ode45 method has produced the more accurate depiction of the equation.
The overlay graph of the two functions looks as follows,

As you can see, the ode 45 method, just as it has been in the previous problems, produces a more accurate graph of the function. Euler’s method produces a graph that is very similar but not quite as accurate as the ode45 method.
The dsolve method in MATLAB yields,
dsolve(’DY=x^2*y’,’x’)
ans =
(y*x^3)/3 + C2
PROBLEM 5. : dy/dx = e ^ -x ; y(0)=1
For this equation, the first method i used to solve the equation was the MATLAB dsolve method, which yielded the following,
dsolve(’DY=exp(-x)’,’x’)
ans =
-exp(-x)+C1
The next step was to change the Euler’s method equation to fit the problem, after which, we can see the graph of the function as follows,
Euler’s Method:

As you can see, the curve of the line looks like a reasonable solution to the problem. However, our next step is to use the ode45 method once again, and then compare the two graphical solutions.
Ode45 Method:

This graph looks pretty similar to that of Euler’s method, but when we overlay them for a closer look,

We can see that while the curves are very close in this particular problem, as we have found throughout this block, the ode45 method has once again produced the smoother, more accurate solution curve
PROBLEM 6. : dy/dx = x^3 * y – x^2 * y^2 ; y(-1) = 1
When we take a look at Euler’s method for this problem, the graph yielded is,

As you can see, the Euler’s method has produced a very jagged graph of this problem. The next step we took the ode45 method and graphed the equation which produced a much better graph. I skipped right to overlaying them and they look as follows,

The ode45 method has produced the much smoother graph of the line once again which leads us to the conclusion of this block. As you can see throughout the problems above, the euler method is not a very accurate way t solve problems. The ode45 method or Runge-Kutta method is a much more smooth and accurate depiction of the problems.