Using Euler's method in MatLab to solve a system of ODEs

I have a system of first order ODEs that I need to use Euler's method on. I have a general code for using Euler's Method in MATLAB for one equation, but I'm not sure how to apply it to a system.
I have x'(t) and y'(t), the input is supposed to involve h, t0, x0, y0, and tf. The output is supposed to generate vectors. How do I go about writing this?

Answers (1)

You can express your system of ODEs directly (1), using anonymous function (@) (2), writing a function file (3) or inline function (4). E.g.
dx = 3x-y
dy = 3x+5y
that can be expressed directly (1):
df = ([3*x(1,:) - x(2, :); 3*x(1,:)+5*x(2,:)]);
Or can be also expressed via anonymous function (2):
df = @(x1, x2)([([3*x1-x2; 3*x1+5*x2])])
etc.
Note where x1 represents x and x2 represents y. Note also how you write the given ICs: [x0; y0]
Good luck.

Products

Asked:

on 10 May 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!