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
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
Torsten 2024년 1월 2일
Sorry, it's your work that should have been accepted as answer here.
William Rose
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 CenterFile 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!

Translated by