How do I use an integral in lsqcurvefit whose bounds involve the xdata parameter?
4 views (last 30 days)
Show older comments
Aditya Desai
on 22 Aug 2023
Answered: Walter Roberson
on 22 Aug 2023
In my code, I have a set of x and y data points, called xdata and ydata respectively. I am trying to use lsqcurvefit to find the value for a quantity in the model that should reproduce the data. The problem is that the model equation includes an integral, and the xdata values serve as the upper bounds for that integral. The error I am receiving is that "Limits of integration must be double or single scalars" which I am guessing is because of xdata being an array. Is there a way to work around this?
Note: The model currently only has one parameter, but this is just a first step. I'm just trying to get the basics working before adding in additional parameters.
Relevant code:
xdata = [1 2 3 4 5 6 7 8]
ydata = [101 102 103 104 105 106 107 108] %this is all fake numbers, just an fyi
alpha = @(n)n^2; %not sure what the functional form of this is yet, so this is just a temporary fill-in
fit_equation = @(n,xdata)n(1)-n(1)*integral(alpha,0,xdata);
options = optimoptions('lsqcurvefit', 'Display','iter', 'MaxFunctionEvaluations', 10000, 'MaxIterations',10000);
guess = [1];
[bestn,~,bestresidual,~,~,~,bestjacobian] = lsqcurvefit(fit_equation, guess, xdata, ydata, [], [], options);
display(bestn)
fitted = bestn(1)-bestn(1)*integral(alpha,0,xdata);
scatter(xdata,ydata);
plot(xdata,fitted);
0 Comments
Accepted Answer
Walter Roberson
on 22 Aug 2023
xdata = [1 2 3 4 5 6 7 8]
ydata = [101 102 103 104 105 106 107 108] %this is all fake numbers, just an fyi
alpha = @(n)n.^2; %not sure what the functional form of this is yet, so this is just a temporary fill-in
fit_equation = @(n,xdata)n(1)-n(1)*arrayfun(@(UB)integral(alpha,0,UB), xdata);
options = optimoptions('lsqcurvefit', 'Display','iter', 'MaxFunctionEvaluations', 10000, 'MaxIterations',10000);
guess = [1];
[bestn,~,bestresidual,~,~,~,bestjacobian] = lsqcurvefit(fit_equation, guess, xdata, ydata, [], [], options);
display(bestn)
fitted = bestn(1)-bestn(1)*arrayfun(@(UB)integral(alpha,0,UB), xdata)
plot(xdata, ydata, 'o', xdata, fitted);
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!