用Euler法求下列微分方程初值问题的数值解,要求有matlab程序(能看懂的最好有备注),谢谢
发布网友
发布时间:2022-05-14 05:44
我来回答
共1个回答
热心网友
时间:2023-10-05 04:02
做个测试,希望有所帮助。代码% By lyqmath
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);结果