Curve fitting of a portion of a plot and linear equilibrium of both plots
2 ビュー (過去 30 日間)
古いコメントを表示
How do I curve fit of after equilbrium points in electron temperature profile so that they both equilibriate in a single line ?? MY code:
data1 = readmatrix('lat vs time.xlsx');
x1 = data1(:,1);
y1 = data1(:,2);
data2 = readmatrix('elec vs time.xlsx');
x2 = data2(:,1);
y2 = data2(:,2);
[k, yInf, y0, yFit] = fitExponential(x1, y1);
figure(1);
plot(x1,y1,'g',x2,y2,'r','linewidth',1.5);
hold on
plot(x1,yFit,'k','linewidth',2.5);
hold off
% apply corrective factor on fitted curve to math the other curve asymptote
y2_asymp = mean(y2(round(end/2):end));
correction_factor = y2_asymp/yFit(end);
yFit = yFit*correction_factor;
figure(2);
plot(x1,yFit,'g','linewidth',4);
hold on;
plot(x2,y2,'r','linewidth',2);
hold off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [k, yInf, y0, yFit] = fitExponential(x, y)
% FITEXPONENTIAL fits a time series to a single exponential curve.
% [k, yInf, y0] = fitExponential(x, y)
%
% The fitted curve reads: yFit = yInf + (y0-yInf) * exp(-k*(x-x0)).
% Here yInf is the fitted steady state value, y0 is the fitted initial
% value, and k is the fitted rate constant for the decay. Least mean square
% fit is used in the estimation of the parameters.
%
% Outputs:
% * k: Relaxation rate
% * yInf: Final steady state
% * y0: Initial state
% * yFit: Fitted time series
%
% improve accuracy by subtracting large baseline
yBase = y(1);
y = y - y(1);
shafin = @(param) norm(param(2)+(param(3)-param(2))*exp(-param(1)*(x-x(1))) - y, 2);
initGuess(1) = -(y(2)-y(1))/(x(2)-x(1))/(y(1)-y(end));
initGuess(2) = y(end);
initGuess(3) = y(1);
param = fminsearch(shafin,initGuess);
k = param(1);
yInf = param(2) + yBase;
y0 = param(3) + yBase;
yFit = yInf + (y0-yInf) * exp(-k*(x-x(1)));
end
採用された回答
Torsten
2024 年 1 月 1 日
移動済み: Torsten
2024 年 1 月 1 日
How do I curve fit of after equilbrium points in electron temperature profile so that they both equilibriate in a single line ??
What are "both" ?
If you want to prescribe yInf, just remove param(2) from the parameters to be fitted and fix it as "equilibrium value - y(1)".
5 件のコメント
Torsten
2024 年 1 月 2 日
Sorry, it's your work that should have been accepted as answer here.
William Rose
2024 年 1 月 2 日
@Torsten, no issue! I knew when I posted it that I was making a comment on your good answer.
その他の回答 (0 件)
参考
カテゴリ
Help Center および 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!



