Fitting exponential function, coefficients and errors
이전 댓글 표시
Dear experts,
I have experimental data for exponential distribution a*exp(b*x). I need to find coefficients a, b and their errors. I have used function fit(B,C, 'exp1') and I got some results. I have problem because some data points in my file have higher error rate because of the nature of experiment.
1.) Which algorithm or function in Matlab can give the smallest error ? 2.) How can I use/adopt some function in Matlab to put smaller weight (when I calculate coefficients) on data that drastically differ from exponential function ?
Thank you.
채택된 답변
추가 답변 (1개)
Amar Yimam
2025년 11월 8일
0 개 추천
%% COF_ExponentialFit_All.m
% Exponential least-squares fitting for all four lubricants
% Time range: 2400–3600 s, with extrapolation to 5000 s
clc; clear; close all;
%% --- 1. Load Excel Data ---
filename = 'Lubricants_COF.xlsx';
data = readtable(filename);
t_all = data.Time;
cof_data = {data.Luba, data.Lubb, data.Lubc, data.Lubd};
lubricants = {'Luba','Lubb','Lubc','Lubd'};
%% --- 2. Select Final Phase (2400–3600 s) ---
idx_final = (t_all >= 2400 & t_all <= 3600);
t_final = t_all(idx_final);
for i = 1:length(cof_data)
cof_data{i} = cof_data{i}(idx_final);
end
%% --- 3. Normalize COF (optional for better stability) ---
cof_norm = cell(size(cof_data));
for i = 1:length(cof_data)
cof_min = min(cof_data{i});
cof_max = max(cof_data{i});
cof_norm{i} = (cof_data{i} - cof_min) / (cof_max - cof_min);
end
%% --- 4. Define exponential model: y = a * exp(k * t)
expModel = @(p,t) p(1) * exp(p(2) * t);
results = struct();
%% --- 5. Fit exponential model for each lubricant ---
for i = 1:length(lubricants)
name = lubricants{i};
y = cof_norm{i};
t = t_final;
% Normalize time to reduce numerical range
t_scaled = t / 1000;
% Initial guesses (like Mathematica)
p0 = [0.01, 1e-6];
lb = [0, -1]; % allow slow decay or growth
ub = [10, 1];
opts = optimoptions('lsqcurvefit','Display','off');
[p_fit,~,resid] = lsqcurvefit(expModel,p0,t_scaled,y,lb,ub,opts);
% Fitted curve
t_smooth = linspace(min(t_scaled), 5, 500);
y_fit = expModel(p_fit,t_smooth);
% Goodness of fit (R²)
SSE = sum(resid.^2);
SST = sum((y - mean(y)).^2);
R2 = 1 - SSE/SST;
% Store
results.(name).params = p_fit;
results.(name).R2 = R2;
results.(name).t_smooth = t_smooth * 1000;
results.(name).y_fit = y_fit;
% Display
fprintf('\n=== %s ===\n', name);
fprintf('a = %.6f, k = %.6e\n', p_fit(1), p_fit(2));
fprintf('R² = %.4f\n', R2);
% Plot
figure('Color','w'); hold on;
scatter(t, y, 40, 'filled', 'MarkerFaceColor',[0.85 0.33 0.10], ...
'MarkerEdgeColor','k', 'DisplayName','Measured COF');
plot(t_smooth*1000, y_fit, 'LineWidth',2, 'Color',[0 0.45 0.74], ...
'DisplayName','Exponential Fit');
xlabel('Time (s)');
ylabel('Normalized COF');
title(sprintf('Exponential Fit – %s (R² = %.4f)', name, R2));
legend('Location','best'); grid on; hold off;
endtimes = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,fun(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')
카테고리
도움말 센터 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!