clc , clear, close all, format long g
% Model three \ mathematics modeling
% some amount to be used write them in patterns of columns;
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]';
M = 10000;
prob = optimproblem('ObjectiveSense', 'min','Description','The min optim');
% Define the decision variables
% because the x_{n+1} equal x_{5} when n equal 4, so the decision variables
% has the structure of 6*1;
x = optimvar('x',6,1,'LowerBound', 0);
%w = 0:0.1:1;
w =[0.766,0.767,0.810,0.811,0.824,0.825,0.962,0.963,1.0];
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);% I think if not begin from '2',the result is ok ??????????????
% To solve the problem, we need a circle of 'for'
for i=1:length(w)
prob.Objective = w(i)*x(end)-(1-w(i))*(r-p)*x(1:end-1,:); % i represent the index, if i ==0.2, so w =0.2.
[sol, fval] =solve(prob);
% some data
xx=sol.x;
V=[V;max(q.*xx(1:end-1))];% the function 'max' is useful , and the decision variables begin from index '1'from 'end-1',begin x_{5} is useless
Q=[Q,(r-p)*xx(1:end-1)];X=[X;xx'];
% plot(V,Q,'*-');grid on;
% xlabel('Risk/yuan');ylabel('Profit/yuan');
end
% The method is also useful in this case, and what we need to keep
% attention is that we should ensure that the elemenets in function 'V';
% Ususally , one symbol error can make whole formula uncorrect!!! I will
% add this error to my 优快云,
% extract values is in Q where at last
ind = find(V<=59.4, 1);
sx = X(ind, 1:end-1)
q= V(ind)
% V,Q,X
find函数用在了这里,用于查找x的值。
还有一点需要注意, 那就是for循环与w的结合, 这一点可以借鉴,对于掌握不熟练的我来说是很有帮助的,