LINPROG requires the following inputs to be of data type double: 'f'.
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am trying to run an LP optimization.
My objective function is to estimate
that minimize
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1290490/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1290495/image.png)
where,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1290500/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1290505/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1290510/image.png)
please note , in the code my
reg1 , ...etc
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1290515/image.png)
I wrote the following code :
y= readtable('y.crra.csv'); % 249 x 1
y=table2array(y);
reg1=readtable('fitted.reg1.csv');
reg1=table2array(reg1);
trareg1=reg1.'; % 1 x 249
reg2=readtable('u.fitted.reg2.csv');
reg2=table2array(reg2);
trareg2=reg2.'; % 1 x 249
reg3=readtable('u.fitted.reg3.csv');
reg3=table2array(reg3);
trareg3=reg3.'; % 1 x 249
reg4=readtable('u.fitted.reg4.csv');
reg4=table2array(reg4);
trareg4=reg4.'; % 1 x 249
tau=.05;
yp= @(x) x*trareg1 + x*trareg2+x*trareg3 + x* trareg4;
e= @ (x) y-yp;
loss= @ (x) max(tau*(e), ( tau-1 )*(e));
obj = @(x) sum(loss);
Aeq = [1, 1, 1, 1];
lb = [0, 0, 0, 0];
beq = [1];
x = linprog(obj, [], [], Aeq, beq, lb, []);
I received the following error :LINPROG requires the following inputs to be of data type double: 'f'.
do you have any idea how to solve it?
I searched about the error and I've seen other people mentioning this error but I don't get how could work in my case
Thanks in advance.
0 Commenti
Risposta accettata
Torsten
il 9 Feb 2023
Modificato: Torsten
il 9 Feb 2023
y= readtable('y.crra.csv'); % 249 x 1
y=table2array(y);
reg1=readtable('fitted.reg1.csv');
reg1=table2array(reg1);
trareg1=reg1.'; % 1 x 249
reg2=readtable('u.fitted.reg2.csv');
reg2=table2array(reg2);
trareg2=reg2.'; % 1 x 249
reg3=readtable('u.fitted.reg3.csv');
reg3=table2array(reg3);
trareg3=reg3.'; % 1 x 249
reg4=readtable('u.fitted.reg4.csv');
reg4=table2array(reg4);
trareg4=reg4.'; % 1 x 249
tau=.05;
loss = @(x) (x(1)*trareg1 + x(2)*trareg2+x(3)*trareg3 + x(4)* trareg4).' - y;
obj = @(x) sum(max([tau*loss(x),(1-tau)*loss(x)],[],2));
Aeq = [1 1 1 1];
beq = 1;
lb = [0 0 0 0];
ub = [1 1 1 1];
sol = fmincon(obj,0.25*ones(4,1),[],[],Aeq,beq,lb,ub)
7 Commenti
Più risposte (1)
John D'Errico
il 9 Feb 2023
Apparently some or all of the arguments to LINPROG were not doubles. Which ones?
In your code, we see this:
yp= @(x) x*trareg1 + x*trareg2+x*trareg3 + x* trareg4;
e= @ (x) y-yp;
loss= @ (x) max(tau*(e), ( tau-1 )*(e));
obj = @(x) sum(loss);
So, is obj a double precision vector of numbers? (NO.) In fact, obj will return a SCALAR function of the argument vector.
obj is a function handle. While obj will return a number, if is NOT a vector of numbers. In fact, it is a NONLINEAR function of the argument, x. This is because of the max function inside the loss part of your objective.
Does LINPROG handle nonlinear objectives? (NO.)
Therefore, you cannot use LINPROG.
You may be able to use other tools like GA. But not FMINCON, because the max function inside the objective makes it not differentiable. And certainly not linprog.
4 Commenti
John D'Errico
il 10 Feb 2023
Are you saying you want to solve the problem using GA? You would need to show the code you tried. But Torsten has already shown how to solve it using fmincon.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!