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

采纳的回答

Walter Roberson
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 个评论
Patrick
Patrick 2012-8-27
编辑:Patrick 2012-8-27
At this time I'm not trying to pass the variable into the function, I'm defining a number of functions that are then going into a final function to calculate the maximum of a function, based off of the other functions that are a part of it.
Also, here's the error from matlab.
??? Input argument "p" is undefined.
Error in ==> ISOmax>Q1 at 13
Q1 = (p - MAXS1(1,2)) / MAXS1(1,1);
Error in ==> ISOmax at 15
Q1
I've added in the rest of the code, if that will help.
Walter Roberson
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
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by