%% INPUT -----------------------------------------------------------------+
a = 0; % left time interval limit
b = 20; % right time interval limit
N = 100; % number of points
tol = 1e-6; % tolerance
mu = 1; %.7; % parameter
f = @(t,x) [x(2);-x(1)-mu*(x(1)^2-1)*x(2)]; % function f
%% SOLVER ----------------------------------------------------------------+
x0 = [0.1:0.2:2;0.4:0.2:2.3];
newmap = jet(size(x0,2));
for ii = 1:size(x0,2)
    [t,x,error] = RKF45(a,b,x0(:,ii),N,tol,f); % Runge-Kutta-Fehlberg 4th order adaptative scheme

    figure(1); % time series
    plot(t,x(1,:),'Color',newmap(ii,:)); hold on;

    figure(2); % phase portrait
    plot(x(1,:),x(2,:),'Color',newmap(ii,:)); hold on;
end

x0 = [-(0.1:0.2:2);0.4:0.2:2.3];
newmap = jet(size(x0,2));
for ii = 1:size(x0,2)
    [t,x,error] = RKF45(a,b,x0(:,ii),N,tol,f); % Runge-Kutta-Fehlberg 4th order adaptative scheme

    figure(1); % time series
    plot(t,x(1,:),'Color',newmap(ii,:)); hold on;

    figure(2); % phase portrait
    plot(x(1,:),x(2,:),'Color',newmap(ii,:)); hold on;
end

x0 = [-(0.1:0.2:2);-(0.4:0.2:2.3)];
newmap = jet(size(x0,2));
for ii = 1:size(x0,2)
    [t,x,error] = RKF45(a,b,x0(:,ii),N,tol,f); % Runge-Kutta-Fehlberg 4th order adaptative scheme

    figure(1); % time series
    plot(t,x(1,:),'Color',newmap(ii,:)); hold on;

    figure(2); % phase portrait
    plot(x(1,:),x(2,:),'Color',newmap(ii,:)); hold on;
end

x0 = [0.1:0.2:2;-(0.4:0.2:2.3)];
newmap = jet(size(x0,2));
for ii = 1:size(x0,2)
    [t,x,error] = RKF45(a,b,x0(:,ii),N,tol,f); % Runge-Kutta-Fehlberg 4th order adaptative scheme

    figure(1); % time series
    plot(t,x(1,:),'Color',newmap(ii,:)); hold on;

    figure(2); % phase portrait
    plot(x(1,:),x(2,:),'Color',newmap(ii,:)); hold on;
end

figure(1);
xlabel('time');
ylabel('x');

figure(2);
xlabel('x1');
ylabel('x2');