用Euler法求下列微分方程初值问题的数值解,要求有matlab程序(能看懂的最好有备注),谢谢
1个回答
2013-05-31
展开全部
做个测试,希望有所帮助。代码% 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);结果
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);结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询