Posted By:
31 Mar, 2013 8:21 pm PDT

Solving ODE using Euler forward and backward method in Matlab

I have an ordinary differential equation y' = -2x -y, with initial condition y(0) = -1.
I want to use Euler forward and Euler backward difference methods to solve this ODE. My step size is 0.01.

how can I do it in Matlab? also how the solution vary if i change the step size?

Attachment:None


Euler forward is an explicit method, while euler backward is an implicit method that requires solving algebraic equations Matlab Code for the above program:

clc
clear all
close all
% y' =-2x-y
% initial condition
x(1) = 0;
y(1) = -2;
h = 0.1;% step size


%Euler Forward
%%%%%%%%%%%%%%
for i = 1:10
    y(i+1) = y(i)+h*(-2*x(i)-y(i));
    x(i+1) = x(i)+h;
end

% plot
figure
set(gca,'Fontsize',15)
plot(x,y,'+-', 'Linewidth', 1.5)
xlabel('x')
ylabel('y')




%Euler Backward
%%%%%%%%%%%%%%
% yn+1 = (yn -2hxn+1)/(1+h)
for i = 1:10
        x(i+1) = x(i)+h;
    y(i+1) = (y(i)-2*h*x(i+1))/(1+h);
end

% plot
hold on
plot(x,y,'+-', 'Linewidth', 1.5, 'color', 'red')
xlabel('x')
ylabel('y')
legend('forward','backward')

Please watch the video for details**********

Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud