% Example 12.8 nonlinear equation
% Using Heun's Method

clear all
close all
clc

% Set the initial parameters
x0 = -5;
h = 0.1;
t = [0:h:5];

% Define the ODE of the form dx/dt = f(x,t)
f = @(x,t) t.*x.^2+2.*x;

% Solve ODE using Heun's method
[tout,xout] = heun(f,t,x0);

% Exact result
xexact = @(t) -20.*exp(2.*t)./(9 - 5.*exp(2.*t) + 10.*exp(2.*t).*t);

% Plot Result
figure
hold on
fplot(xexact,[min(t),max(t)],'LineWidth',2)
plot(tout,xout,'ok','MarkerFaceColor',[0,0,0],'MarkerSize',3)
xlabel('t')
ylabel('x')
title('Example 12.8')
legend('Exact',['Heun with h = ',num2str(t(2)-t(1)),],'Location','southeast')

% Display the result as a table
fprintf('\n        t       xi\n')
disp('---------------------------')
disp([tout,xout])




