lsqcurvefit residuals don't agree with the manual calculation
3 views (last 30 days)
Show older comments
Charles Dorchester
on 19 Jan 2022
Answered: Charles Dorchester
on 21 Jan 2022
Hello,
I'm using lsqcurvefit to fit experimental modeling data to the analytic solution of a solute transport equation. Unfortunately, the calculation of the residuals via lsqcurvefit, do not match my manual calculation of the residuals between the best-fit curve and experimental modeling data.
I call lsqcurvefit like this:
dt = table2array(outletConcentrations(:,1)); % first column of this table is a time vector
allconcs = [concA, concB, concC]; concAvg = mean(allconcs,2); % these columns are concentration vectors
xmids = [0.01999, 0.01999, 0.01999]; % [m] mid point fluid sampling format [centerA, CenterB, CenterC]
hardparams = [xmids(1), V_out2p2p, 480]; % these are intentionally hardwired parameters
initialparams = [2e-1, 0.1/0.5]; % alpha, beta -- these are the parameters I'd like to optimize using lsqcurvefit
[params_est, resnorm,residual,exitflag,output] =...
lsqcurvefit(@(initialparams,dt) soln1dNoDisp(initialparams,hardparams,dt),initialparams,dt,concAvg);
% calling the function sol1dnodisp as such, produces the output from sol1dnodisp which has the following syntax
% function [c,cim,alpha,beta]=soln1dNoDisp(initialparams,hardparams,dt)
% Also, note that c is the array being fit to concAvg by adjusting alpha
% and beta.
I have lsqcurvefit outputting the residuals for error checking, however when integrating the vector of residuals I get pretty large numbers (-135 mol/m^3). So I decided to check the work with a manual calculation of the residuals and their displacement as follows:
LDresiduals = c - concAvg; % The help menu of lsqcurvefit indicates the residuals are calculated as:
% FUN(X,XDATA)-YDATA. I believe this is exactly what I have done manually here.
displaced = LDresiduals - mean(LDresiduals-residual);
You can see in the attached image that the residuals seem to be shifted in magnitude. The 'residuals, displaced' data is my checking to see if they are displaced, which seems likely true within the error produced by machine precision (mean(displaced-residual) = 1.99e-17). Is anyone able to explain why I'm seeing this?
I've attached the experimental model data and best fit curve data for reference; 'c mobile' is being fit to 'average outlet'
3 Comments
Torsten
on 20 Jan 2022
Does that mean the following variables should translate from the example to my problem? :
xdata -> time
x -> concentration
ydata -> concentration from the best fit curve
No.
xdata -> time
x -> parameters supplied from lsqcurvefit to myfun which are to be used to calculate the simulated concentrations by using ode15s
ydata -> concentrations from measurements which are to be reproduced by your model
Accepted Answer
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!