function return wrong output

1 回表示 (過去 30 日間)
xueqi
xueqi 2013 年 3 月 12 日
Hi fellows, I have encounterred a super bizzare situation. I have a function called maxmin3. When I call it, it gives me the wrong output. But when I get rid of the function definition and make it like a normal script file, it works perfectly. Is anyone could help me out?
Here is the input for the function
if true
% sub=[0.1,0.3,0.2,0.04];
d1=[1.2,2,0.7];
d2=[0.8,1.1,1.2];
edw=100
end
and here is the code for maxmin3
if true
% function [ PF,EU] = maxmin3(sub,d1,d2,edw)
lb1=sub(1);
lb2=sub(2);
lb3=sub(3);
r=sub(4);
Dm1=[d1(1,1),d2(1,1);d1(1,2),d2(1,2);d1(1,3),d2(1,3)];
for j=1:7
if (j==1)
p1=1-lb2-lb3;
p2=lb2;
p3=lb3;
f=@(x) p1*(d1(1,1)-d2(1,1))*exp(-r*(x*(d1(1,1)-d2(1,1))+edw*(d2(1,1)+1)))...
+p2*(d1(1,2)-d2(1,2))*exp(-r*(x*(d1(1,2)-d2(1,2))+(d2(1,2)+1)*edw))...
+p3*(d1(1,3)-d2(1,3))*exp(-r*(x*(d1(1,3)-d2(1,3))+(d2(1,3)+1)*edw));
[x, fval, exitflag] = fzero(f, 1);
if exitflag < 0
PF1(1,1) = 0;
else
PF1(1,1) = x;
end
PF1(1,2)=edw-PF1(1,1);
w1(1,:)=PF1*[d1(1,:);d2(1,:)];
if (w1(1,1)==min(w1,[],2))
EU1= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF1'+edw));exp(-r*(Dm1(2,:)* PF1'+edw));exp(-r*(Dm1(3,:)* PF1'+edw))];
else
EU1=-Inf;
end
elseif (j==2)
p1=lb1;
p2=1-lb1-lb3;
p3=lb3;
[x, fval, exitflag] = fzero( @(x) p1*(d1(1,1)-d2(1,1))*exp(-r*(x*(d1(1,1)-d2(1,1))+edw*(d2(1,1)+1)))...
+p2*(d1(1,2)-d2(1,2))*exp(-r*(x*(d1(1,2)-d2(1,2))+(d2(1,2)+1)*edw))...
+p3*(d1(1,3)-d2(1,3))*exp(-r*(x*(d1(1,3)-d2(1,3))+(d2(1,3)+1)*edw)), 1);
if exitflag < 0
PF2(1,1) = 0;
else
PF2(1,1) = x;
end
PF2(1,2)=edw-PF2(1,1);
w2(1,:)=PF2*[d1(1,:);d2(1,:)];
if (w2(1,2)==min(w2(1,:),[],2))
EU2= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF2'+edw));exp(-r*(Dm1(2,:)* PF2'+edw));exp(-r*(Dm1(3,:)* PF2'+edw))];
else
EU2=-Inf;
end
elseif (j==3)
p1=lb1;
p2=lb2;
p3=1-lb1-lb3;
[x, fval, exitflag] = fzero( @(x) p1*(d1(1,1)-d2(1,1))*exp(-r*(x*(d1(1,1)-d2(1,1))+edw*(d2(1,1)+1)))...
+p2*(d1(1,2)-d2(1,2))*exp(-r*(x*(d1(1,2)-d2(1,2))+(d2(1,2)+1)*edw))...
+p3*(d1(1,3)-d2(1,3))*exp(-r*(x*(d1(1,3)-d2(1,3))+(d2(1,3)+1)*edw)), 1);
if exitflag < 0
PF3(1,1) = 0;
else
PF3(1,1) = x;
end
PF3(1,2)=edw-PF3(1,1);
w3(1,:)=PF3*[d1(1,:);d2(1,:)];
if (w3(1,3)==min(w3(1,:),[],2))
EU3= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF3'+edw));exp(-r*(Dm1(2,:)* PF3'+edw));exp(-r*(Dm1(3,:)* PF3'+edw))];
else
EU3=-Inf;
end
elseif (j==4) %w1=w2
PF4(1,1)=edw*(d2(1,2)-d2(1,1))/(d1(1,1)-d2(1,1)-d1(1,2)+d2(1,2));
PF4(1,2)=edw-PF4(1,1);
w4(1,:)=PF4*[d1(1,:);d2(1,:)];
if (w4(1,1)==min(w4(1,:),[],2)||w4(1,2)==min(w4(1,:),[],2))
p1=1-lb2-lb3;
p2=lb2;
p3=lb3;
EU4= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF4'+edw));exp(-r*(Dm1(2,:)* PF4'+edw));exp(-r*(Dm1(3,:)* PF4'+edw))];
elseif((w4(1,3)==min(w4(1,:),[],2)))
p1=lb1;
p2=lb2;
p3=1-lb1-lb2;
EU4= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF4'+edw));exp(-r*(Dm1(2,:)* PF4'+edw));exp(-r*(Dm1(3,:)* PF4'+edw))];
end
elseif(j==5) %w2=w3
PF5(1,1)=edw*(d2(1,3)-d2(1,2))/(d1(1,2)-d2(1,2)-d1(1,3)+d2(1,3));
PF5(1,2)=edw-PF5(1,1);
w5(1,:)=PF5*[d1(1,:);d2(1,:)];
if (w5(1,2)==min(w5(1,:),[],2)||w5(1,3)==min(w5(1,:),[],2))
p1=lb1;
p2=1-lb1-lb3;
p3=lb3;
EU5= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF5'+edw));exp(-r*(Dm1(2,:)* PF5'+edw));exp(-r*(Dm1(3,:)* PF5'+edw))];
elseif((w5(1,1)==min(w5(1,:),[],2)))
p1=1-lb2-lb3;
p2=lb2;
p3=lb3;
EU5= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF5'+edw));exp(-r*(Dm1(2,:)* PF5'+edw));exp(-r*(Dm1(3,:)* PF5'+edw))];
end
elseif(j==6) %w1=w3
PF6(1,1)=edw*(d2(1,3)-d2(1,1))/(d1(1,1)-d2(1,1)-d1(1,3)+d2(1,3));
PF6(1,2)=edw-PF6(1,1);
w6(1,:)=PF6*[d1(1,:);d2(1,:)];
if (w6(1,1)==min(w6(1,:),[],2)||w6(1,3)==min(w6(1,:)))
p1=1-lb2-lb3;
p2=lb2;
p3=lb3;
EU6= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF6'+edw));exp(-r*(Dm1(2,:)* PF6'+edw));exp(-r*(Dm1(3,:)* PF6'+edw))];
elseif((w6(1,2)==min(w6(1,:),[],2)))
p1=lb1;
p2=1-lb1-lb3;
p3=lb3;
EU6= -(1/r)*[p1,p2,p3]*[exp(-r*(Dm1(1,:)* PF6'+edw));exp(-r*(Dm1(2,:)* PF6'+edw));exp(-r*(Dm1(3,:)* PF6'+edw))];
end
elseif(j==7)
PF7(1,1)=0;
PF7(1,2)=edw;
EU7=(-1/r)*exp(-r*edw);
end
end
[value,st]=max([EU1;EU2;EU3;EU4;EU5;EU6;EU7]);
EU=value;
pftotal=[PF1(1,1);PF2(1,1);PF3(1,1);PF4(1,1);PF5(1,1);PF6(1,1);PF7(1,1)];
PF(1,1)=pftotal(st);
PF(1,2)=edw-PF(1,1);
if (PF(1,1)<0||PF(1,1)>edw||PF(1,2)<0||PF(1,2)>edw)
EU=-Inf;
end
end
end
  1 件のコメント
xueqi
xueqi 2013 年 3 月 12 日
when I call the function ([PF,EU]=maxmin3(sub,d1,d2,edw);)the result I get is PF=[44.4,55.6] which is right. But EU=-0.0068 which is wrong. And if I write maxmin3 in the form of scripts then I can get perfectly right answers PF=[44.4,55.6] and EU=-0.3689.

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

回答 (2 件)

Image Analyst
Image Analyst 2013 年 3 月 12 日
If you step through it with the debugger you will undoubtedly discover where and why EU gets assigned to the wrong value. See this for a good tutorial: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/

Jan
Jan 2013 年 3 月 12 日
Of course you cannot expect, that we understand your uncommented code or spend some hours to try this. Therefore it would be a good idea to narrow the problem down.
Where are the function or script file stored respectively? If they are not in the same folder, the subfolder \private might contain functions, which shadow builtin functions. Or another function in the same folder is preferred in one case, but not in the other.
But this is a pure guessing only.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by