Store result from for-loop and plot

6 ビュー (過去 30 日間)
charlotte88
charlotte88 2016 年 3 月 7 日
編集済み: Torsten 2016 年 3 月 7 日
Hi,
I have a maximization problem which I want to solve in matlab. The problem consists of several parameters, but I am mostly interested in what happens to x when alpha increases from 0 to 0.5 with increments of 0.05. I then want to plot the value of x in a figure with x on the y-axis and alpha on the x-axis
I have defined the function in one file:
function b = maximizationpb2(x, alpha)
beta1 = 6;
beta2 = 6;
s1 = 1/3;
s2 = 1/3;
s3 = 1/3;
alpha = 0.5;
b = -(x(1) - beta1*((x(2)-s2)^2+(1-x(1)-x(2)-s3)^2))^(1-alpha) * (x(2) - beta2*((x(1)-s1)^2+(1-x(1)-x(2)-s3)^2))^alpha;
And then I have used fmincon to solve the problem:
lb = [0,0];
ub = [1,1];
A = [1,1];
b = [1];
Aeq = [];
beq = [];
x0 = [1/3, 1/3];
a = fmincon(@maximizationpb2,x0,A,b,Aeq,beq,lb,ub);
x3 = 1 - a(1) - a(2);
My first difficulty is to make a for loop which goes through all values of alpha = 0:0.05:0.5. I have to admit I have no clue where to start (I was so happy last week when I finally managed to solve the problem!!). Any help would be very much appreicated!
  1 件のコメント
Stephen23
Stephen23 2016 年 3 月 7 日
You have alpha as a function input, but never use it. YOu need to change your function and learn to test your code:
function out = test
lb = [0,0];
ub = [1,1];
A = [1,1];
b = [1];
Aeq = [];
beq = [];
x0 = [1,1]/3;
s = [1,1,1]/3;
beta = [6,6];
alpha = 0:0.05:0.5;
out = NaN(numel(alpha),1);
for k = 1:numel(alpha)
a = fmincon(@(x)fun(x,alpha(k),beta,s),x0,A,b,Aeq,beq,lb,ub);
out(k) = 1 - a(1) - a(2);
end
end
%--------------------------------------------------------------------------
function b = fun(x, alpha, beta, s)
b = -(x(1) - beta(1)*((x(2)-s(2))^2+(1-x(1)-x(2)-s(3))^2))^(1-alpha) * (x(2) - beta(2)*((x(1)-s(1))^2+(1-x(1)-x(2)-s(3))^2))^alpha;
end
%--------------------------------------------------------------------------

サインインしてコメントする。

採用された回答

Torsten
Torsten 2016 年 3 月 7 日
編集済み: Torsten 2016 年 3 月 7 日
function driver
lb = [0,0];
ub = [1,1];
A = [1,1];
b = [1];
Aeq = [];
beq = [];
x0 = [1/3, 1/3];
for k=1:11
alpha(k)=0.05*(k-1);
a = fmincon(@(x)maximizationpb2(x,alpha(k)),x0,A,b,Aeq,beq,lb,ub);
M(k,1)=a(1);
M(k,2)=a(2);
M(k,3)= 1 - a(1) - a(2);
end
plot(alpha,M(:,1)) % plot a(1) over alpha
function b = maximizationpb2(x, alpha)
beta1 = 6;
beta2 = 6;
s1 = 1/3;
s2 = 1/3;
s3 = 1/3;
b = -(x(1) - beta1*((x(2)-s2)^2+(1-x(1)-x(2)-s3)^2))^(1-alpha) * (x(2) - beta2*((x(1)-s1)^2+(1-x(1)-x(2)-s3)^2))^alpha;
Best wishes
Torsten.
  1 件のコメント
charlotte88
charlotte88 2016 年 3 月 7 日
Thank you so much!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by