Euler's Method with Matrix

조회 수: 21 (최근 30일)
Sarah Johnson
Sarah Johnson 2020년 4월 29일
댓글: J. Alex Lee 2020년 4월 29일
I'm trying to implement Euler's Method on the following ODE, with initial condition [y1, y2] = [1, 3] and on the interval t in [0, 1]:
The exact solution is given as:
I currently have two different code as a way to go about solving this but I'm not sure which is correct/how to fix them. I'm really struggling to figure out where to go from here and any help/advice would be really appreciated!
% Code Number 1 %
function [y] = ForwardEulers(y0, t)
n = 2;
h = (t(2) - t(1)/n);
y = y0;
A = [-500.5, 499.5; 499.5, -500.5];
[V, D] = eig(A);
for t = 1:n
y(t) = y0 * (V * ((eye(n) + h * (D))^t) * inv(V));
x(t) = t * h;
end
plot(x, y)
end
% Code Number 2 %
function [y] = ForwardEulers2(y0, t)
n = 2;
h = (t(2) - t(1))/n;
A = [-500.5, 499.5; 499.5, -500.5];
for i = 1:n
y(i) = ((eye(n) + (h* A))^i) * y0;
end
end
  댓글 수: 1
J. Alex Lee
J. Alex Lee 2020년 4월 29일
I assume this is a homework problem. Neither approach looks correct. Can you start over:
  1. write down the 2 ODE's separately (not in matrix form as provided)
  2. write down what the forward euler formula looks like for each
  3. only then start thinking about the code.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by