lsqcurvefit with custom equation
Show older comments
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
More Answers (1)
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);
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 Bodies 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!
