Curve fitting of a portion of a plot and linear equilibrium of both plots

2 views (last 30 days)
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

Accepted Answer

Torsten
Torsten on 1 Jan 2024
Moved: Torsten on 1 Jan 2024
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 Comments
William Rose
William Rose on 2 Jan 2024
@Torsten, no issue! I knew when I posted it that I was making a comment on your good answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!