Error with OPTIMOPTIONS and lsqcurvefit

62 views (last 30 days)
Peter Alpert
Peter Alpert on 25 May 2023
Edited: dpb on 26 May 2023
I want to use lsqcurvefit. The first two matlab examples work, but the third does not on my computer. When I run it online and in this description it works, but I get an error about optimoptions on my computer below.
Error using lsqcurvefit
Invalid datatype. Options argument must be created with OPTIMOPTIONS.
Error in test (line 21)
[x,res] = lsqcurvefit(@myfun,startpt,xdata,ydata,lb,ub,A,b,Aeq,beq)
Of course, optimoptions it is not used. Instead I find, when i comment out the linear constraints, the code works again on my computer. What could be wrong here? My optimization toolbox installation? I have one of these huge matlab packages from my academic institution.
Optimization Toolbox Version 9.3 (R2022a)
clear all
close all
clc
a = 2; % x(1)
b = 4; % x(2)
t0 = 5; % x(3)
c = 1/2; % x(4)
xdata = linspace(2,7);
ydata = a + b*atan(xdata - t0) + c*xdata + 1/10*randn(size(xdata));
plot(xdata,ydata,'ro')
lb = zeros(4,1);
ub = 7*ones(4,1);
A = [-1 -1 1 1];
b = 0;
startpt = [1 2 3 1];
Aeq = [];
beq = [];
[x,res] = lsqcurvefit(@myfun,startpt,xdata,ydata,lb,ub,A,b,Aeq,beq)
% [x,res] = lsqcurvefit(@myfun,startpt,xdata,ydata,lb,ub)
plot(xdata,ydata,'ro',xdata,myfun(x,xdata),'b-')
function F = myfun(x,xdata)
a = x(1);
b = x(2);
t0 = x(3);
c = x(4);
F = a + b*atan(xdata - t0) + c*xdata;
end

Answers (1)

dpb
dpb on 25 May 2023
Edited: dpb on 26 May 2023
Have you checked the documentation for the installed version (R2022a) for allowable syntax?
I have R2020b and R2023a and the equality constraints option (and thus, the Aeq, beq arguments) was added between the two releases. I didn't go look at the release notes to try to find which specific release it was.
Two possibilities at least -- R2022b doesn't actually support the equality constraints and you picked up the example from a later release, like online, or it does support it internally, but there's a bug that the input parser is still checking earlier syntax that requires an options struct at that point in the argument list if it goes that far.
Unless you need the feature, just remove the two arguments from the call as they're not being used in the example, anyway, and go ahead.
I did notice a typo in the documentation for the input arguments in R2023a -- the Aeq section has a couple instances of "inequalities" that got missed turning into "equalities" when the A, b arguments sections got copied over to be added as the equalities...that's worthy of a note to support to fix.
Aeq encodes the Me linear equalities
Aeq*x = beq,
where x is the column vector of N variables x(:), and beq is a column vector with Me elements.
For example, consider these inequalities:
x1 + 2x2 + 3x3 = 10
2x1 + 4x2 + x3 = 20,
Specify the inequalities by entering the following constraints.
"Oops!" :)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!