Info

此问题已关闭。 请重新打开它进行编辑或回答。

Solving a first order ODE using the Euler backward method (implicit)

3 次查看(过去 30 天)
I'm trying to solve a first order ODE with the initial condition y(5) = 0. To do this, I'm using the Euler backward method, but I'm getting these two errors:
Error using fzero (line 214)
Second argument must be finite.
Error in backwardeuler (line 22)
y(i+1) = fzero(@(Y) y(i) + dt*((2*t(i+1)-4)*exp(-Y)) - Y, y(i), options);
------------------------------------------------------------------------------------
% y_true = log(t^2 -4*t-4); Initial condition y(5) = 0;
% F_ty = @(t,y) (2*t-4)*exp(-y);
dt = 0.01;
t0 = 0;
tf = 3;
t = t0:dt:tf;
y(5) = 0;
% using the formula for backward euler: y(i+1) = y(i) + dt*f(y(i+1),t(i+1))
% we get
%y(i+1) = y(i) + dt*((2*t(i+1)-4)*exp(-y(i+1)));
% setting the LHS equal to zero so we can use fsolve:
% 0 = y(i) + dt*((2*t(i+1)-4)*exp(-y(i+1))) - y(i+1);
% We define y(i+1) = Y, so that
% 0 = y(i) + dt*((2*t(i+1)-4)*exp(-Y)) - Y;
options = optimset('TolX',1e-06);
for i = 1:length(t)-1
y(i+1) = fzero(@(Y) y(i) + dt*((2*t(i+1)-4)*exp(-Y)) - Y, y(i), options);
y_exact(i+1) = log((t(i+1))^2-4*t(i+1)-4);
end
figure(1)
hold on
plot(t,y,'bo')
plot(t,y_exact,'r-')
xlabel('time')
ylabel('y(t)')
title('Backward Euler method vs exact solution')
legend('Backward Euler', 'Exact')

回答(0 个)

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by