clc
clear
t = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4];
v = [100, 62, 38, 21, 13, 7, 4, 2, 3];
exp_fit = @(p, t) p(1) * exp(-p(2) * t);
fit_error = @(p) sum((exp_fit(p, t) - v).^2);
initial_guess = [100, 0.5];
params = fminsearch(fit_error, initial_guess);
V0 = params(1);
k = params(2);
t_fit = linspace(min(t), max(t), 100);
v_fit = exp_fit(params, t_fit);
figure;
scatter(t, v, 'b', 'DisplayName', 'Data');
hold on;
plot(t_fit, v_fit, 'r-', 'DisplayName', sprintf('Fit: V(t) = %.1f e^{-%.2f t}', V0, k));
title('Capacitor Voltage Decay');
xlabel('Time (s)');
ylabel('Voltage (V)');
legend
grid on
To embed this project on your website, copy the following code and paste it into your website's HTML: