matlab fmincon 精度,fmincon与quadprog误差

2022-06-29 10:26:33 浏览数 (2)

大家好,又见面了,我是你们的朋友全栈君。

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

有没有人能帮我看看这两个程序的运行结果为什么会不一样啊??困扰了好久……

程序一:

%text1.m

clear all;clc;

a = [0.058,0.075,0.092,0.111,0.136,0.092]’;

b = [0.062,0.085,0.128,0.149,0.164,0.148]’;

alpha = [0.054,0.075,0.096,0.123,0.148,0.081]’;

beta = [0.058,0.085,0.112,0.138,0.168,0.101]’;

pk = [0.005,0.005,0.005,0.005]’;

pn = [0,0,0,0,0.005,0.005]’;

qb = [0.008,0.008,0.008,0.008]’;

n = 16;

m = 24;

q = 5;

c0 = -((a’ b’) / 2 (beta’ – alpha’) / 4) pn’;

t = 0.15;

c = t * [c0,-0.01,0.03,pk’,qb’]’

eta1 = beta b – a;

eta2 = beta alpha 2 * (b – a);

bigc0 = (beta * beta’ alpha * alpha’ 3 * eta1 * eta2′)/24;

bigc0( : ,7:n) = 0;

bigc0( 7:n , : ) = 0;

bigc = 0.5 * (bigc0 bigc0′)

A = [-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0;

0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0;0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0;

0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0;

1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0;

0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0;0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0;

0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0;

0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0;

0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0;

0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0;

0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0;

0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0;0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0;

0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0;0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1;

];

B = [0,0,0,0,0,0,0.35,0.35,0.35,0.35,0.35,0.35,0,1,0,0.5,0,0,0,0,0,0,0,0]’;

Aeq = [1,1,1,1,1.005,1.005,1,-1,0.005,0.005,0.005,0.005,0.008,0.008,0.008,0.008;

1,0,0,0,0,0,0,0,-1,0,0,0,1,0,0,0;0,1,0,0,0,0,0,0,0,-1,0,0,0,1,0,0;

0,0,1,0,0,0,0,0,0,0,-1,0,0,0,1,0;0,0,0,1,0,0,0,0,0,0,0,-1,0,0,0,1];

Beq = [1,0.15,0.1,0.2,0.25]’;

x0 = [0.15,0.1,0.2,0.25,0,0,0.3,0,0,0,0,0,0,0,0,0]’;

[x,fval] = quadprog(bigc,c,A,B,Aeq,Beq,[],[],x0)

程序二:(有两个m文件)

%text2.m

clear all;clc;

A = [-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0;

0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0;0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0;

0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0;

1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0;

0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0;0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0;

0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0;

0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0;

0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0;

0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0;

0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0;

0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0;0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0;

0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0;0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1;

];

B = [0,0,0,0,0,0,0.35,0.35,0.35,0.35,0.35,0.35,0,1,0,0.5,0,0,0,0,0,0,0,0]’;

Aeq = [1,1,1,1,1.005,1.005,1,-1,0.005,0.005,0.005,0.005,0.008,0.008,0.008,0.008;

1,0,0,0,0,0,0,0,-1,0,0,0,1,0,0,0;0,1,0,0,0,0,0,0,0,-1,0,0,0,1,0,0;

0,0,1,0,0,0,0,0,0,0,-1,0,0,0,1,0;0,0,0,1,0,0,0,0,0,0,0,-1,0,0,0,1];

Beq = [1,0.15,0.1,0.2,0.25]’;

x0 = [0.15,0.1,0.2,0.25,0,0,0.3,0,0,0,0,0,0,0,0,0]’;

[x,fval] = fmincon(@myfun,x0,A,B,Aeq,Beq)

%myfun.m

function f = myfun(x)

a = [0.058,0.075,0.092,0.111,0.136,0.092]’;

b = [0.062,0.085,0.128,0.149,0.164,0.148]’;

alpha = [0.054,0.075,0.096,0.123,0.148,0.081]’;

beta = [0.058,0.085,0.112,0.138,0.168,0.101]’;

pk = [0.005,0.005,0.005,0.005]’;

pn = [0,0,0,0,0.005,0.005]’;

qb = [0.008,0.008,0.008,0.008]’;

n = 16;

m = 24;

q = 5;

c0 = -((a’ b’) / 2 (beta’ – alpha’) / 4) pn’;

t = 0.15;

c = t * [c0,-0.01,0.03,pk’,qb’]’

eta1 = beta b – a;

eta2 = beta alpha 2 * (b – a);

bigc0 = (beta * beta’ alpha * alpha’ 3 * eta1 * eta2′)/24;

bigc0( : ,7:n) = 0;

bigc0( 7:n , : ) = 0

bigc = 0.5 * (bigc0 bigc0′)

f = c’ * [x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10),x(11),x(12),x(13),x(14),x(15),x(16)]’ 0.5 * [x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10),x(11),x(12),x(13),x(14),x(15),x(16)] * bigc * [x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10),x(11),x(12),x(13),x(14),x(15),x(16)]’;

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/132331.html原文链接:https://javaforall.cn

0 人点赞