 
% Finite-Difference Simulation of SpO2 Levels Over Time

% Parameters
time_steps = 100;       % number of time steps
dt = 0.1;               % time step size
SpO2_initial = 98;      % initial SpO2 level (%)
diffusion_rate = 0.05;  % rate of change (modifiable)

% Initialize SpO2 array
SpO2_levels = zeros(1, time_steps);
SpO2_levels(1) = SpO2_initial;

% Finite-difference simulation
for t = 2:time_steps
    % Simple model: SpO2 moves toward 90% baseline
    SpO2_levels(t) = SpO2_levels(t-1) - diffusion_rate * (SpO2_levels(t-1) - 90) * dt;
end

% Time array
time = linspace(0, time_steps*dt, time_steps);

% Plot
figure;
plot(time, SpO2_levels, 'LineWidth', 2);
xlabel('Time (minutes)');
ylabel('SpO2 Level (%)');
title('Finite-Difference Simulation of SpO2 Levels Over Time');
grid on;
legend('SpO2 Level');
