I'm getting Undefined Variable Error when the variable is defined.
3 次查看(过去 30 天)
显示 更早的评论
Here's the code in question. I'm taking inputs from another function, which calls this one. The variable p is "undefined, despite it being defined in line 10, with the error on line 13. Can anyone give some advise to fix this?
function [ p ] = ISOmax( MAXS1, MAXS2, TAB)
%Maximize ISO function
%values for consumer bids
D1 = [-.52 100];
D2 = [-.65 100];
%variable calculations
PA = 1;
p = 1;
%Production by S1
function [Q1] = Q1 (p, MAXS1)
Q1 = (p - MAXS1(1,2)) / MAXS1(1,1);
end
Q1
%Production by S2
function [Q2] = Q2 (p, MAXS2)
Q2 = (p - MAXS2(1,2)) / MAXS2(1,1);
end
Q2
%total production at A
function [QA] = QA (Q1, Q2)
QA = Q1 + Q2;
end
QA
%Consumption by D1
function [d1] = d1 (p)
d1 = (p - 100) / -.52;
end
d1
%Consumption by D2
function [d2] = d2 (p)
d2 = (p - 100) / -.65;
end
d2
%total consumption at B
function [DB] = DB (d1, d2)
DB = d1 + d2;
end
DB
%consumer Surpluses
function [CS1] = CS1 (p, d1)
CS1 = .5 * (100 - p)* d1;
end
CS1
function [CS2] = CS2 (p, d2)
CS2 = .5 * (100 - p)* d2;
end
CS2
%Supplier Surplus
function [PS1] = PS1 (p, MAXS1, Q1)
PS1 = .5 * (p - MAXS1(1,2)) * Q1;
end
PS1
function [PS2] = PS2 (p, MAXS2, Q2)
PS2 = .5 * (p - MAXS2(1,2)) * Q2;
end
PS2
%Benefit to consumers
function [BB] = BB (CS1, p, d1, CS2, d2)
BB = CS1 + p * d1 + CS2 + p * d2;
end
BB
% Cost to Producers
function [CA] = CA ( p, Q1, PS1, Q2, PS2)
CA = p * Q1 - PS1 + p * Q2 - PS2;
end
CA
%Total Welfare Formula
function [TW] = TW (BB, CA)
TW = BB - CA;
end
%Negative TW Formula, for Matlab maximazation function
function [TWA] =TWA (TW)
TWA = -1*TW;
end
p = fminbnd(TWA,0,1000)
p
end
0 个评论
采纳的回答
Walter Roberson
2012-8-27
You need to add "end" to the file, or else the file will not be interpreted as having nested functions.
4 个评论
Walter Roberson
2012-8-27
Your lines
function [Q1] = Q1 (p, MAXS1)
Q1 = (p - MAXS1(1,2)) / MAXS1(1,1);
end
define the function Q1 as being a function with two parameters, the first of which is named "p".
The next line in your code is
Q1
which is a call to Q1 with no parameters. The two input arguments to Q1 are then undefined, so you get an error when the code attempts to use them.
Remember, function name with no parameters is a call to the function.
Note: do not name your output parameter with the same name as the function.
更多回答(1 个)
Star Strider
2012-8-27
编辑:Star Strider
2012-8-27
I think I see the problem. When you call Q1 (and for that matter many of your other functions) as you do here:
%Production by S1
function [Q1] = Q1 (p, MAXS1)
Q1 = (p - MAXS1(1,2)) / MAXS1(1,1);
end
you have a self-referential call,because MATLAB is getting confused by all the Q1 references. When you assign Q1 in the function, it is probably interpreting that as a call to Q1 without any arguments. I suggest you either change the name of the function:
%Production by S1
function [Q1] = FunctionQ1 (p, MAXS1)
Q1 = (p - MAXS1(1,2)) / MAXS1(1,1);
end
or (probably preferably) change them all to anonymous functions, e,g,:
Q1 = @(p, MAXS1) (p - MAXS1(1,2)) / MAXS1(1,1);
along with the others.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!