lsqcurvefit with custom equation

2 views (last 30 days)
I am trying to fit my equation to data obtained to find Dab but for some reason it is not recognizing the function correctly . The custom function I am trying to fit is the following:
the code I am using is the following:
%Calling data from excel
filename = 'Data Sheet.xlsx'; % Call the file we are using
sheet = 'Sheet1'; % Call the sheet we are using
xlRange = 'A2:A12'; % Call x-values within sheet
x2Range = 'B2:B12'; % Call y-values within sheet
t = xlsread(filename, sheet, xlRange); % Reads x-axis specified with above variables
c = xlsread(filename, sheet, x2Range); % Reads y-axis specified with above variables
Cal = f(t,c);
plot(t,c,t,Cal)
x0 = [1];
t = lsqcurvefit(f(t,c), x0, t, c);
function Cal = f(t,c)
n = 1:250; % Number of sumations
Cao = 1.87; % Initial concentration of drug inside patch (mg/cm^3)
L = .01; % Distance from middle of patch to surface (cm)
Vp = 1*1*2*L; % Volume of patch (cm^3) <--same as mL <-- Vp=1*1*2L
Vl = 40; % Volume of liquid reservoir (cm^3)
Dab = c(1); % Fitting values
sum_parts = ((1./((2.*n + 1).^2)).*exp(-(((2.*n + 1).^2)*(pi.^2).*Dab.*t)./(4.*(L.^2)))); %Summation
Cal = ((Cao*Vp)/Vl)*(1-((8/(pi^2)))*sum(sum_parts,2)); %Final Function
end
Could someone please help me in finding what I am doing wrong?

Accepted Answer

Star Strider
Star Strider on 15 Sep 2022
There is a problem with ‘f’ since it is not obvioul what parameters are to be estimated. The function should be:
f(parameter_vector, independent_variable, other_parameters)
with ‘other_parameters’ being an optional list of additional parameters to be passed to the function, if any.
In this instance, it also needs to be referred to as a function handle, here ‘@f’ . (See What Is a Function Handle? if you are not familiar with them.)
%Calling data from excel
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1126535/Data%20Sheet.xlsx'; % Call the file we are using
sheet = 'Sheet1'; % Call the sheet we are using
xlRange = 'A2:A12'; % Call x-values within sheet
x2Range = 'B2:B12'; % Call y-values within sheet
% t = xlsread(filename, sheet, xlRange); % Reads x-axis specified with above variables
t = readmatrix(filename, 'Sheet',sheet, 'Range',xlRange);
t = 11×1
0 2 4 6 8 10 15 20 30 45
% c = xlsread(filename, sheet, x2Range); % Reads y-axis specified with above variables
c = readmatrix(filename, 'Sheet',sheet, 'Range',x2Range);
c = 11×1
0 0.0032 0.0041 0.0045 0.0048 0.0050 0.0054 0.0056 0.0057 0.0057
% Cal = f(t,c);
figure
plot(t,c,'p')
hold on
x0 = 100;
Dabv = lsqcurvefit(@f, x0, t, c);
Initial point is a local minimum. Optimization completed because the size of the gradient at the initial point is less than the value of the optimality tolerance.
plot(t,f(Dabv,t),'-r')
hold off
grid
function Cal = f(Dab,t)
n = 1:250; % Number of sumations
Cao = 1.87; % Initial concentration of drug inside patch (mg/cm^3)
L = .01; % Distance from middle of patch to surface (cm)
Vp = 1*1*2*L; % Volume of patch (cm^3) <--same as mL <-- Vp=1*1*2L
Vl = 40; % Volume of liquid reservoir (cm^3)
% Dab = c(1); % Fitting values
sum_parts = ((1./((2.*n + 1).^2)).*exp(-(((2.*n + 1).^2)*(pi.^2).*Dab.*t)./(4.*(L.^2)))); %Summation
Cal = ((Cao*Vp)/Vl)*(1-((8/(pi^2)))*sum(sum_parts,2)); %Final Function
end
It now runs without error, however it needs your help in order that it actually fits the ‘Dab’ parameter correctly. (I used readmatrix because it runs here and xlsread does not. Use whatever works best for you.)
.
  2 Comments
Daniel Alejandro Diaz
Daniel Alejandro Diaz on 21 Sep 2022
Great! Thank you! Now i just need to figure out the limit issue which I believe is from how I might have derived the equation
Star Strider
Star Strider on 21 Sep 2022
As always, my pleasure!
Consider adding the limit as an additional parameter in the objective function and let lsqcurvefit estimate it as well.

Sign in to comment.

More Answers (1)

Torsten
Torsten on 15 Sep 2022
Edited: Torsten on 15 Sep 2022
The saturation concentration for your model is 9.35e-4. So you will never reach 5.6e-3 as in your data.
Note that the infinite sum starts at 0, not at 1.
M = [0 0
2 0.0032484191
4 0.004080132
6 0.0045066096
8 0.0047778558
10 0.0050474681
15 0.005395513
20 0.0055932286
30 0.0056978055
45 0.0057223157
60 0.005640615];
t = M(:,1);
c = M(:,2);
Dab0 = 1e-5;
options = optimset('TolX',1e-10,'TolFun',1e-10);
Dab = lsqnonlin(@(Dab)f(Dab,t,c), Dab0, [],[],options);
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
plot(t,[c,f(Dab,t,c)+c])
function Cal = f(Dab,t,c)
n = 0:250; % Number of sumations
Cao = 1.87; % Initial concentration of drug inside patch (mg/cm^3)
L = .01; % Distance from middle of patch to surface (cm)
Vp = 1*1*2*L; % Volume of patch (cm^3) <--same as mL <-- Vp=1*1*2L
Vl = 40; % Volume of liquid reservoir (cm^3)
sum_parts = ((1./((2.*n + 1).^2)).*exp(-(((2.*n + 1).^2)*(pi.^2).*Dab.*t)./(4.*(L.^2)))); %Summation
Cal = ((Cao*Vp)/Vl)*(1-((8/(pi^2)))*sum(sum_parts,2))-c; %Final Function
end

Categories

Find more on Particle & Nuclear Physics 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!