Store result from for-loop and plot

3 views (last 30 days)
charlotte88
charlotte88 on 7 Mar 2016
Edited: Torsten on 7 Mar 2016
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 Comment
Stephen23
Stephen23 on 7 Mar 2016
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
%--------------------------------------------------------------------------

Sign in to comment.

Accepted Answer

Torsten
Torsten on 7 Mar 2016
Edited: Torsten on 7 Mar 2016
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.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!