How can Euler's method be adapted for two-dimensional systems?

Click For Summary
SUMMARY

This discussion focuses on adapting Euler's method for two-dimensional systems represented by the equations x' = f(x, y, t) and y' = g(x, y, t). The proposed implementation involves defining y as a vector and modifying the function f to return a vector of derivatives. The key code structure includes a loop that updates both x and y in parallel, ensuring that the independent variable t is also incremented appropriately. This adaptation allows for the effective numerical solution of systems of differential equations.

PREREQUISITES
  • Understanding of Euler's method for numerical integration
  • Familiarity with MATLAB programming and syntax
  • Basic knowledge of differential equations
  • Concept of vector operations in MATLAB
NEXT STEPS
  • Research "MATLAB vectorization techniques" to optimize performance
  • Learn about "Runge-Kutta methods" for improved accuracy in solving differential equations
  • Explore "MATLAB plotting functions" for visualizing multi-dimensional data
  • Study "systems of ordinary differential equations (ODEs)" for broader applications
USEFUL FOR

Mathematicians, engineers, and computer scientists interested in numerical methods for solving two-dimensional systems of differential equations, particularly those using MATLAB for simulations and modeling.

grimster
Messages
39
Reaction score
0
this is the classic euler's method, but i'd like to modify it so that it can handle two-dimensional systems on the form
x'=...
y'=...

what needs to be done?

function [X,Y] = euler1(x,y,x1,n)
h=(x1-x)/n;
X=x;
Y=y;
for i=1:n
y=y+h*f(x,y);
x=x+h;
X=[X;x];
Y=[Y;y];
end
X
Y
plot(X,Y,x,y)

function yp = f(x,y)
yp=y*sin(x);
 
Physics news on Phys.org
grimster said:
this is the classic euler's method, but i'd like to modify it so that it can handle two-dimensional systems on the form
x'=...
y'=...

what needs to be done?

function [X,Y] = euler1(x,y,x1,n)
h=(x1-x)/n;
X=x;
Y=y;
for i=1:n
y=y+h*f(x,y);
x=x+h;
X=[X;x];
Y=[Y;y];
end
X
Y
plot(X,Y,x,y)

function yp = f(x,y)
yp=y*sin(x);

If you want to solve a system of equations, say [y1' ; y2'] = f(x,[y1;y2]), just define y as a vector to start and make your function f return a vector [yp1;yp2].
 
grimster said:
this is the classic euler's method, but i'd like to modify it so that it can handle two-dimensional systems on the form
x'=...
y'=...

what needs to be done?

function [X,Y] = euler1(x,y,x1,n)
h=(x1-x)/n;
X=x;
Y=y;
for i=1:n
y=y+h*f(x,y);
x=x+h;
X=[X;x];
Y=[Y;y];
end
X
Y
plot(X,Y,x,y)

function yp = f(x,y)
yp=y*sin(x);

Of course, you now have 3 variables: 2 dependent and 1 independent. I would use "x" and "y" as the dependent variables, "t" as the independent variable. Just do x and y "in parallel": to solve x'= f(x,y,t), y'= g(x,y,t), xnew= xold+ hf(xold,yold,told), ynew= yold+ h*g(xold,yold,told), tnew= told+ h.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 65 ·
3
Replies
65
Views
9K
  • · Replies 0 ·
Replies
0
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
22K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K