clear

%% parameter values

% system parameters
mus=1;
mud=0.5;
v0=0.5;
zeta1=0.05;

vcr=1.829;

num_sampl=500;
% v=linspace(1.3,2.1,num_sampl);
v=linspace(1.3,3,num_sampl);

% range of initial conditions for x0 and x0d
x10_range=[2, 3];
x1d0_range=[1, 2];

% time span and time interval
tfinal=1000;
t_steps=1000;
tspan=linspace(0,tfinal,t_steps);

%% generate random initial conditions

x0=zeros(num_sampl,2);
x0(:,1)=rand(num_sampl,1)*(x10_range(2)-x10_range(1))+x10_range(1);
x0(:,2)=rand(num_sampl,1)*(x1d0_range(2)-x1d0_range(1))+x1d0_range(1);


%% perform simulations
options = odeset('RelTol', 1e-8, 'AbsTol', 1e-8);
x=cell(length(v),1);
for i=1:num_sampl
    i
    [~, x{i}] = ode45(@(t,xt) system_MOB(t,xt,zeta1,v(i),v0,mus,mud),tspan,x0(i,:),options);
end

%% save data
clear("i");
% save MOB_4incremental_testing_raw_simulation_data.mat

%% normalize data polar_01_log_mvm

clear

load MOB_4incremental_testing_raw_simulation_data.mat

rho1=cell(length(x),1);
for i=1:length(x)
    polar_pure1=sqrt(x{i}(:,1).^2+x{i}(:,2).^2);
    max_pol1=max(polar_pure1);
    polar_011=polar_pure1/max_pol1;
    polar_01_log1=log(polar_011);
    for j=20:length(polar_01_log1)
        if max([polar_01_log1(j-19:j)])<-6
            polar_01_log1=polar_01_log1(1:j);
            break
        end
    end
    polar_01_log_resumpled1=spline(linspace(1,length(polar_01_log1),length(polar_01_log1)), polar_01_log1, linspace(1,length(polar_01_log1),1000));
    rho1{i}=movmean(polar_01_log_resumpled1,round(length(polar_01_log_resumpled1)/20));
end


% save normalized data
clear("x");
clear("i");
save MOB_4incremental_testing_polar_01_log_mvm.mat


%% normalize data minmax

clear

load MOB_4incremental_testing_raw_simulation_data.mat

rho1=cell(length(x),1);
for i=1:length(x)
    massimo=max(x{i}(:,1));
    minimo=min(x{i}(:,1));
    minmax=zeros(length(x{i}(:,1)),1);
    for j=1:length(x{i})
        if x{i}(j,1)>0
            minmax(j)=x{i}(j,1)/massimo;
        else
            minmax(j)=x{i}(j,1)/abs(minimo);
        end
    end
    for j=50:length(minmax)
        if max([minmax(j-49:j)])<1e-8
            minmax=minmax(1:j);
            break
        end
    end
    minmax_resumpled=spline(linspace(1,length(minmax),length(minmax)), minmax, linspace(1,length(minmax),1000));
    rho1{i}=minmax_resumpled;
end

% save normalized data
clear("x");
clear("i");
save MOB_4incremental_testing_minmax.mat


%% normalize data polar [0 1]

clear

load MOB_4incremental_testing_raw_simulation_data.mat

rho1=cell(length(x),1);
for i=1:length(x)
    polar_pure=sqrt(x{i}(:,1).^2+x{i}(:,2).^2);
    max_pol=max(polar_pure);
    polar_01=polar_pure/max_pol;
    for j=20:length(polar_01)
        if max([polar_01(j-19:j)])<1e-8
            polar_01=polar_01(1:j);
            break
        end
    end
    polar_01_resumpled=spline(linspace(1,length(polar_01),length(polar_01)), polar_01, linspace(1,length(polar_01),1000));
    rho1{i}=polar_01_resumpled;
end

% save normalized data
clear("x");
clear("i");
save MOB_4incremental_testing_polar_01.mat


%% normalize data polar [0 1] mvm

clear

load MOB_4incremental_testing_raw_simulation_data.mat

rho1=cell(length(x),1);
for i=1:length(x)
    polar_pure=sqrt(x{i}(:,1).^2+x{i}(:,2).^2);
    max_pol=max(polar_pure);
    polar_01=polar_pure/max_pol;
    for j=20:length(polar_01)
        if max([polar_01(j-19:j)])<1e-8
            polar_01=polar_01(1:j);
            break
        end
    end
    polar_01_resumpled=spline(linspace(1,length(polar_01),length(polar_01)), polar_01, linspace(1,length(polar_01),1000));
    rho1{i}=movmean(polar_01_resumpled,round(length(polar_01_resumpled)/20));
end

% save normalized data
clear("x");
clear("i");
save MOB_4incremental_testing_polar_01_mvm.mat



%% normalize data polar_01_log

clear

load MOB_4incremental_testing_raw_simulation_data.mat

rho1=cell(length(x),1);
for i=1:length(x)
    polar_pure1=sqrt(x{i}(:,1).^2+x{i}(:,2).^2);
    max_pol1=max(polar_pure1);
    polar_011=polar_pure1/max_pol1;
    polar_01_log1=log(polar_011);
    for j=20:length(polar_01_log1)
        if max([polar_01_log1(j-19:j)])<-6
            polar_01_log1=polar_01_log1(1:j);
            break
        end
    end
    polar_01_log_resumpled1=spline(linspace(1,length(polar_01_log1),length(polar_01_log1)), polar_01_log1, linspace(1,length(polar_01_log1),1000));
    rho1{i}=polar_01_log_resumpled1;
end


% save normalized data
clear("x");
clear("i");
save MOB_4incremental_testing_polar_01_log.mat


















