I'm getting Undefined Variable Error when the variable is defined.
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Star Strider
le 27 Août 2012
Modifié(e) : Star Strider
le 27 Août 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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!