I'm getting Undefined Variable Error when the variable is defined.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 27 Aug. 2012
You need to add "end" to the file, or else the file will not be interpreted as having nested functions.
4 Kommentare
Walter Roberson
am 27 Aug. 2012
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.
Weitere Antworten (1)
Star Strider
am 27 Aug. 2012
Bearbeitet: Star Strider
am 27 Aug. 2012
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 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!