0

我尝试使用 Matlab 编写欧拉方法的“愚蠢”版本,但我总是一无所获。我的代码是垃圾:-(

请参阅此方法的伪代码:

‘set integration range
xi = 0
xf = 0
‘initialize variables
x = xi
y = 1
‘set step size and determine
‘number of calculation steps
dx = 0.5
nc = (xf – xi) / dx
‘ output initial condition
PRINT x, y
‘Loop to implement Euler’s method
‘and display results
DOFOR I = 1, nc
dydx = -(2X**3) + (12X**2) - (20X) + 8.5
y = y + dydx . dx
x = x + dx
PRINT x, y
END DO

我很确定这个伪代码是我需要实现的,但我未能将其转换为 Matlab 代码。请问有什么帮助吗?

4

1 回答 1

1

我不确定你在哪里失败了,知道你是如何失败的或者有什么并发症真的很有帮助。否则,这就像在做你的功课。

这是我对 MATLAB 代码的尝试。(注:我这台电脑上没有MATLAB,也没有测试过)

i = 0;
stepsize = .1; % Define as what you want it to be
y = 1; % Initial value condition given 
t = 0; % Initial time value
yout = [zeros(1,20)]; % Assuming you want 20 outputs, can change
fvec = [zeros(1,20)];

for i = 1:20 % Time range, can change to correspond to what you want
fvec(i) = 2 - exp(-4*t) - 2*yout(i); % Just loops calculating based on Euler's method
yout(i+1) = yout(i) + stepsize*fvec(i)
t = t+stepsize % Time has passed, increment the time.
end

我不完全确定这段代码,但它应该给你一个如何去做的例子。如果有问题,请发表评论。

于 2011-06-03T16:16:56.177 回答