function main()
clc; clear all; close all;
% 初值
t0 = 0;
y0 = [0; 0];
xmax = 1;
h = 0.01;
% 計算
n = (xmax - t0)/h;
for i = 1 : n+1
if i == 1
t(i) = t0;
y(:, i) = y0;
else
t(i) = t0 + (i - 1)*h;
y(:, i) = y(:, i - 1) + h*test_fun(t(i-1), y(:, i-1));
end
% 繪圖
figure; hold on; box on;
plot(t, y(1, :), "r-", t, y(2, :), "g-");
grid on; legend("x", "y");
xlabel("t", "FontWeight", "Bold", "Color", "r");
ylabel("data", "FontWeight", "Bold", "Color", "r");
title("Euler法計算微分方程組 By lyqmath", "FontWeight", "Bold", "Color", "r");% 微分方程
function fxy = test_fun(t,y)
fxy = zeros(2, 1);
fxy(1) = exp(t) - 5*y(1) - y(2);
fxy(2) = y(1) + 3*y(2);結果
function main()
clc; clear all; close all;
% 初值
t0 = 0;
y0 = [0; 0];
xmax = 1;
h = 0.01;
% 計算
n = (xmax - t0)/h;
for i = 1 : n+1
if i == 1
t(i) = t0;
y(:, i) = y0;
else
t(i) = t0 + (i - 1)*h;
y(:, i) = y(:, i - 1) + h*test_fun(t(i-1), y(:, i-1));
end
end
% 繪圖
figure; hold on; box on;
plot(t, y(1, :), "r-", t, y(2, :), "g-");
grid on; legend("x", "y");
xlabel("t", "FontWeight", "Bold", "Color", "r");
ylabel("data", "FontWeight", "Bold", "Color", "r");
title("Euler法計算微分方程組 By lyqmath", "FontWeight", "Bold", "Color", "r");% 微分方程
function fxy = test_fun(t,y)
fxy = zeros(2, 1);
fxy(1) = exp(t) - 5*y(1) - y(2);
fxy(2) = y(1) + 3*y(2);結果