You are on page 1of 2

9/21/2019 HW3

clear all
close all
global tt x_last dt
ini_t=0; % Initial time
Final_t=1; % Final time
N=10; % Number of time steps
x0=1; % Initial value x(t=0)
dt=(Final_t-ini_t)/N; % Time step
t(1)=ini_t;
x(1)=x0; x_forward(1)=x0;i=0;
x_last=x0;
for n=1:N
i=i+1;
t(i+1)=t(i)+dt;
tt=t(i+1);
fun = @Beqn; % function
x(i+1,1) = fzero(fun,x_last);
x_last=x(i+1,1);
x_forward(i+1,1)=x_forward(i,1)+dt*(x_forward(i,1)*t(i)-x_forward(i,1)^3);
end
figure
hold on
plot(t,x_forward,'o-r')
plot(t,x,'*-b')
xlabel('t');ylabel('x');legend('Euler-Forward','Euler-Backward')
function y=Beqn(x)
global tt x_last dt
y=x-x_last-dt*(tt*x-x^3);
end

file:///E:/My Drive/NumericalMethod/Prob2/html/HW3.html 1/2


9/21/2019 HW3

Published with MATLAB® R2018a

file:///E:/My Drive/NumericalMethod/Prob2/html/HW3.html 2/2

You might also like