Merge pull request '第一周' (#1) from 姚安欣 into main
commit
371c0c70c3
@ -0,0 +1,40 @@
|
||||
clc; clear; close all; format long g;
|
||||
prob=optimproblem;
|
||||
x=optimvar('x',9,1,'LowerBound',0,'Type','integer');
|
||||
p=[0.25,0.35,0.50];
|
||||
r=[1.25,2.00,2.80];
|
||||
ot=[5,10,0;7,9,12;6,8,0;4,0,11;7,0,0];
|
||||
total=1000:200:3000;
|
||||
odp=[300/6000,321/10000,250/4000,783/7000,200/4000];
|
||||
|
||||
X=[];
|
||||
Q=[];
|
||||
|
||||
for i=1:length(total)
|
||||
% prob.Objective=(ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7))*odp(1)+(ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9))*odp(2)+(ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8)))*odp(3)+(ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9))*odp(4)+(ot(5,1)*(x(3)+x(6)))*odp(5)-(r(1)-p(1))*ones(1,6)*x(1:6)-(r(2)-p(2))*ones(1,2)*x(7:8)-(r(3)-p(3))*x(9);
|
||||
dp1=(ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7))*odp(1);
|
||||
dp2=(ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9))*odp(2);
|
||||
dp3=(ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8)))*odp(3);
|
||||
dp4=(ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9))*odp(4);
|
||||
dp5=(ot(5,1)*(x(3)+x(6)))*odp(5);
|
||||
dp=dp1+dp2+dp3+dp4+dp5;
|
||||
prob.Objective=dp-(r(1)-p(1))*ones(1,6)*x(1:6)-(r(2)-p(2))*ones(1,2)*x(7:8)-(r(3)-p(3))*x(9);
|
||||
prob.Constraints.con1=p(1)*ones(1,6)*x(1:6)+p(2)*ones(1,2)*x(7:8)+p(3)*x(9)+dp<=total(i);%总费用
|
||||
prob.Constraints.con2=ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7)<=6000;%A1
|
||||
prob.Constraints.con3=ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9)<=10000;
|
||||
prob.Constraints.con4=ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8))<=4000;
|
||||
prob.Constraints.con5=ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9)<=7000;
|
||||
prob.Constraints.con6=ot(5,1)*(x(3)+x(6))<=4000;
|
||||
[sol,fval,flag,out]=solve(prob);
|
||||
xx=sol.x;
|
||||
|
||||
X=[X,xx];
|
||||
Q=[Q,-fval];
|
||||
|
||||
end
|
||||
plot(total,Q,'*-');
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,61 @@
|
||||
|
||||
%27页例题
|
||||
clc; clear; close all;
|
||||
n = 10000; % 使用较小的 n 值以便更容易可视化
|
||||
x = unifrnd(0, 12, [1, n]);
|
||||
y = unifrnd(0, 9, [1, n]);
|
||||
ans=sum(y < x.^2 & x <= 3)+sum(y < 12 - x & x >= 3);
|
||||
ans=ans/n;
|
||||
% 找出满足条件的点
|
||||
condition1 = y < x.^2 & x <= 3;
|
||||
condition2 = y < 12 - x & x >= 3;
|
||||
condition_met = condition1 | condition2; % 满足任一条件的点
|
||||
condition_not_met = ~condition_met; % 不满足任何条件的点
|
||||
|
||||
% 创建图形窗口
|
||||
figure;
|
||||
hold on;%在同一张图上绘图
|
||||
|
||||
% 绘制不满足任何条件的点
|
||||
scatter(x(condition_not_met), y(condition_not_met), 'k.'); % k----黑色 .----绘制样式
|
||||
%scatter绘制散点图
|
||||
%x(condition_not_met) 会返回一个新的向量,其中只包含 x 中对应 condition_not_met 为 true 的元素。
|
||||
|
||||
% 绘制满足第一个条件的点
|
||||
scatter(x(condition1), y(condition1), 'r.'); % 红色
|
||||
|
||||
% 绘制满足第二个条件的点
|
||||
scatter(x(condition2), y(condition2), 'b.'); % 蓝色
|
||||
|
||||
% 添加图例和标签
|
||||
legend('不满足任何条件的点', '满足 y < x^2 且 x <= 3 的点', '满足 y < 12 - x 且 x >= 3 的点');
|
||||
xlabel('x');
|
||||
ylabel('y');
|
||||
title('随机生成的点和满足条件的点');
|
||||
hold off;
|
||||
|
||||
|
||||
%蒙特卡洛法求圆周率qw
|
||||
clc;clear;close all;
|
||||
|
||||
n=10^5;
|
||||
x=unifrnd(-1,1,[1,n]);
|
||||
y=unifrnd(-1,1,[1,n]);
|
||||
con1=x.^2+y.^2<=1;
|
||||
con2=~con1;
|
||||
ans=sum(x.^2+y.^2<=1);
|
||||
ans=ans/n*4;
|
||||
|
||||
figure ;
|
||||
hold on;
|
||||
|
||||
scatter(x(con1),y(con1),'r.');
|
||||
scatter(x(con2),y(con2),'k.');
|
||||
|
||||
hold off;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,3 @@
|
||||
function f=fun1(x)
|
||||
f=x(1)*x(1)+x(2)*x(2)-x(1)*x(2)-2*x(1)-5*x(2);
|
||||
end
|
@ -0,0 +1,15 @@
|
||||
function f=fun2(x)
|
||||
a=[1.25,8.75,0.5,5.75,3,7.25];
|
||||
b=[1.25,0.75,4.75,5,6.5,7.25];
|
||||
f1 = 0; % 初始化f1
|
||||
f2 = 0; % 初始化f2
|
||||
for i=1:6
|
||||
s=sqrt((a(i)-x(13))^2+(b(i)-x(14))^2);
|
||||
f1=x(i)*s+f1;
|
||||
end
|
||||
for i=7:12
|
||||
s=sqrt((a(i-6)-x(15))^2+(b(i-6)-x(16))^2);
|
||||
f2=s*x(i)+f2;
|
||||
end
|
||||
f=f1+f2;
|
||||
end
|
@ -0,0 +1,49 @@
|
||||
%整数规划
|
||||
%P23 ej2.5
|
||||
%optimproblem解法
|
||||
clc; clear; close all; format long g;
|
||||
prob=optimproblem;
|
||||
x=optimvar('x',6,'LowerBound',0,'Type','integer');
|
||||
prob.Objective=sum(x);
|
||||
cnt=[35,40,50,45,55,30];
|
||||
con=optimconstr(6);
|
||||
con(1)=x(1)+x(6)>=35;
|
||||
for i=1:5
|
||||
con(i+1)=x(i)+x(i+1)>=cnt(i+1);
|
||||
end
|
||||
prob.Constraints.con=con;
|
||||
[sol,fval,flag,out]=solve(prob);
|
||||
X=sol.x;
|
||||
|
||||
|
||||
%linprog解法
|
||||
clc; clear; close all; format long g;
|
||||
f=[1,1,1,1,1,1];
|
||||
intcon=[1:6];
|
||||
A=zeros(6,6);
|
||||
A(1,1)=-1;
|
||||
A(1,6)=-1;
|
||||
for i=1:5
|
||||
A(i+1,i)=-1;
|
||||
A(i+1,i+1)=-1;
|
||||
end
|
||||
lb=zeros(6,1);%注意不是lb=0
|
||||
b=[-35;-40;-50;-45;-55;-30];
|
||||
[x,fval]=intlinprog(f,intcon,A,b,[],[],lb);
|
||||
|
||||
|
||||
|
||||
%01规划
|
||||
%背包问题
|
||||
|
||||
f=-[540,200,180,350,60,150,280,450,320,120];
|
||||
intcon=1:10;
|
||||
lb=zeros(10);
|
||||
ub=ones(10);
|
||||
A=[6,3,4,5,1,2,3,5,4,2];
|
||||
b=30;
|
||||
[x,fval]=intlinprog(f,intcon,A,b,[],[],lb,ub);
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,140 @@
|
||||
%投资利益与风险1998A题
|
||||
|
||||
|
||||
%模型一 给定风险承受程度,求最大利益
|
||||
f=[-0.05,-0.27,-0.19,-0.185,-0.185];
|
||||
|
||||
%A矩阵
|
||||
%A=[0,0.025,0.015,0.055,0.026]; %错误
|
||||
%b=[1,1,1,1,1];
|
||||
A=[zeros(4,1),diag([0.025,0.015,0.055,0.026])];%不等式约束条件矩阵
|
||||
|
||||
%Aeq、beq
|
||||
Aeq=[1,1.01,1.02,1.045,1.065];
|
||||
beq=1;
|
||||
|
||||
%lb
|
||||
%lb=0; %错误
|
||||
lb=zeros(5,1);
|
||||
|
||||
|
||||
%可承担风险率a
|
||||
a=(0:0.001:0.05);
|
||||
|
||||
%保存最优解
|
||||
Q=zeros(1,length(a));
|
||||
xx=[];%空矩阵存放最优解对应x的值
|
||||
for i=1:length(a)
|
||||
b=a(i)*ones(4,1);
|
||||
[x,y]=linprog(f,A,b,Aeq,beq,lb);
|
||||
Q(i)=-y;%注意取负!!!
|
||||
xx=[xx;x'];
|
||||
end
|
||||
plot(a,Q,'*r');
|
||||
xlabel("风险率");
|
||||
ylabel("最大收益");
|
||||
|
||||
|
||||
%模型二 收益、风险按权重组合
|
||||
% f0=[-0.05,-0.27,-0.19,-0.185,-0.185];
|
||||
% w=(0:0.1:1);
|
||||
% Aeq=[1,1.01,1.02,1.045,1.065,0];
|
||||
% beq=1;
|
||||
% lb=0;
|
||||
% xx=[];
|
||||
% Q=zeros(1,length(w));
|
||||
% A=[zeros(5,1),diag([0.025,0.025,0.055,0.065,0])];
|
||||
% b=ones(5,1);
|
||||
% for i=1:length(w)
|
||||
% f=[-w(i)*f0,1-w(i)];
|
||||
% b=x(end)*b;
|
||||
% [x,y]=linprog(f,A,b,Aeq,beq,lb);
|
||||
% Q(i)=-y;
|
||||
% xx=[xx,x'];
|
||||
% end
|
||||
% plot(w,Q,'*r');
|
||||
|
||||
%模型二 收益、风险按权重组合
|
||||
clc; clear; close all; format long g;
|
||||
|
||||
M = 10000;
|
||||
prob = optimproblem;
|
||||
x = optimvar('x', 6, 1, 'LowerBound', 0);
|
||||
r = [0.05, 0.28, 0.21, 0.23, 0.25];
|
||||
p = [0, 0.01, 0.02, 0.045, 0.065];
|
||||
q = [0, 0.025, 0.015, 0.055, 0.026]';
|
||||
%w = 0:0.1:1;
|
||||
w = 0.7:0.03:1;
|
||||
V = [];
|
||||
Q = [];
|
||||
X = [];
|
||||
|
||||
prob.Constraints.con1 = (1 + p) * x(1:end-1) == M;
|
||||
prob.Constraints.con2 = (q(2:end).* x(2:end-1))<= x(end); %下标从1开始
|
||||
|
||||
for i = 1:length(w)
|
||||
prob.Objective = w(i) * x(end) - (1 - w(i)) * (r - p) * x(1:end-1); %注意大小写
|
||||
[sol, fval, flag, out] = solve(prob);
|
||||
xx = sol.x;
|
||||
V = [V, max(q.* xx(1:end-1))];
|
||||
Q = [Q, (r - p) * xx(1:end-1)];
|
||||
X = [X, xx];
|
||||
|
||||
plot(V, Q, '*-');
|
||||
grid on;
|
||||
xlabel('风险');
|
||||
ylabel('收益');
|
||||
end
|
||||
|
||||
|
||||
|
||||
%模型三:达到一定盈利水平,极小化风险
|
||||
|
||||
clc; clear; close all; format long g;
|
||||
M=10000;
|
||||
k=1500:100:3000;
|
||||
prob = optimproblem;
|
||||
x = optimvar('x', 5, 1, 'LowerBound', 0);%下界为0
|
||||
r = [0.05, 0.28, 0.21, 0.23, 0.25];
|
||||
p = [0, 0.01, 0.02, 0.045, 0.065];
|
||||
q = [0, 0.025, 0.015, 0.055, 0.026]';
|
||||
|
||||
V = [];
|
||||
Q = [];
|
||||
X = [];
|
||||
t = optimvar('t', 'LowerBound', 0);
|
||||
|
||||
%prob.Objective=max(q.*x);%极小化风险
|
||||
for i=1:length(k)
|
||||
prob.Objective = t; % 极小化风险
|
||||
prob.Constraints.con1 = (1 + p) * x == M;
|
||||
prob.Constraints.con2=((r-p)*x>=k(i));%达到一定盈利水平
|
||||
prob.Constraints.con3=(q.*x<=t);
|
||||
[sol,fval,flag,out]=solve(prob);
|
||||
if flag==1
|
||||
xx=sol.x;
|
||||
X=[X,xx];
|
||||
Q=[Q,(r-p)*xx];
|
||||
V=[V,fval];
|
||||
else
|
||||
xx=-1*ones(5,1);
|
||||
X=[X,xx];
|
||||
Q=[Q,-1];
|
||||
V=[V,-1];
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
plot(k,V,'*-');
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue