Solving Diff. Eq. with Euler Method: x=0, y=5

In summary, the conversation was about using the Euler method to solve a differential equation with the initial condition x=0 and y=5. The code for Euler's method was provided, along with a question about where to input different step sizes and the values of x and y. The analytical solution for the equation was also given for comparison. The correct implementation of Euler's method was verified by running it with different step sizes and comparing the final values to the analytical solution.
  • #1
tironel
11
0

Homework Statement



Using the Euler method solve the following differential equation. At x = 0, y = 5.

y' + x/y = 0

Calculate the Numerical solution using step sizes of .5; .1; and .01




The Attempt at a Solution



From my textbook I have coded Euler's method

Code:
function [t,y] = eulode(dydt, tspan, y0, h)
%eulode: Euler ODE solver
%   [t,y] = eulode(dydt, tspan, y0, h, p1, p2,...)
%   `   uses EULER'S method to INTEGRATE an ODE
%       (uses the slope at the beginning of the stepsize to graph the
%       function.)
%Input:
%   dydt    = name of hte M-file that evaluates the ODE
%   tspan   = [ti,tf] where ti and tf = initial and final values of
%               independent variables
%   y0      = initial value of dependent variable
%   h       = step size
%   p1,p2   = additional parameter used by dydt
%Output:
%   t = vector of independent variable
%   y = vector of solution for dependent variable

if nargin<4, error('at least 4 input arguments required'), end
ti = tspan(1); tf = tspan(2);
if ~ (tf>ti), error('upper limit must be greater than lower limit'), end

t = (ti:h:tf)'; 
n = length(t);
%if necessary, add an additional value of t 
%so that range goes from t=ti to tf

if t(n)<tf
    t(n+1) = tf;
    n = n+1;
    t(n)=tf;
end

y = y0*ones(n,1); %preallocate y to improve efficiency

for i = 1:n-1 %implement Euler's Method
    y(i+1) = y(i) + dydt(t(i),y(i))*(t(i+1)-t(i));
end

plot(t,y)

I have made another m-file to run Eulode, what I am confused with is where do I input my different step sizes and where do I input x=0 and y=5. However since the analytical solution yields:
Code:
simplify(dsolve('Dy=-x/y','y(0)=5','x'))

ans =
 
(-x^2+25)^(1/2)
and when x=0 the value is 5 so I have coded my Euler's Method like the following and the final values are close to 5 so I think it is correct can someone just verify.

Code:
dydx=@(x,y) -(x/y);
[x1,y1]=eulode(dydx, [0 1],5,.5);
[x2,y2]=eulode(dydx,[0 1],5,.1);
[x3,y3]=eulode(dydx,[0 1],5,.01);
disp([x1,y1])
disp([x2,y2])
disp([x3,y3])
 
Physics news on Phys.org
  • #2
ans = 0.0000 5.00000.1000 4.85000.2000 4.70000.3000 4.55000.4000 4.40000.5000 4.25000.0000 5.00000.1000 4.97500.2000 4.95000.3000 4.92500.4000 4.90000.5000 4.87500.0000 5.00000.0100 4.99500.0200 4.99000.0300 4.98500.0400 4.98000.0500 4.9750
 

Related to Solving Diff. Eq. with Euler Method: x=0, y=5

1. What is the Euler Method for solving differential equations?

The Euler Method is a numerical method used to approximate the solution of a differential equation. It involves using small steps to calculate the derivative at a given point and using that information to estimate the value of the function at the next point.

2. How does the Euler Method work?

The Euler Method works by taking a starting point (x=0, y=5 in this case) and using the derivative at that point to estimate the value of the function at the next point. This process is repeated for multiple steps until the desired value is reached.

3. What are the limitations of using the Euler Method?

The Euler Method is an approximation and therefore may not provide an exact solution. It is also limited by the step size used - if the step size is too large, the error in the approximation may be significant. Additionally, the Euler Method may not be suitable for solving certain types of differential equations, such as those with a steep slope or high curvature.

4. How can I improve the accuracy of the Euler Method?

To improve the accuracy of the Euler Method, you can decrease the step size used. This will result in more points being calculated and a more accurate approximation. Additionally, you can use a more advanced numerical method, such as the Runge-Kutta method, which can provide a more accurate solution.

5. What are some real-world applications of using the Euler Method?

The Euler Method has many applications in physics and engineering, such as in modeling the trajectory of a projectile or the motion of a pendulum. It is also commonly used in economics and finance to model the behavior of markets and financial systems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
863
  • Engineering and Comp Sci Homework Help
Replies
6
Views
910
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Differential Equations
Replies
1
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top