Solving a first order ODE using the Euler backward method (implicit)
Información
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
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')
Respuestas (0)
La pregunta está cerrada.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!