why "load" doen't work in my function ?

i used "load" in my function like:
function y = ob0218(x)
load try1.mat
y = A*x(1:5)'+B*x(6:10)'+C*x(6:10)'+D*(x(11:60).*x(61:110))'+N*(x(11:60).*x(61:110))'+O*(x(11:60).*x(61:110))'+Q*(x(11:60).*x(61:110))'+P*sqrt(x(61:110)')
end
after i run GA solver, it shows:
M L can't be recoginized
i checked the work space to find that try1.mat can't be openned in the work space. But when i type: load try1.mat in command window, it did open try1.mat in the work space, why i can't use "load" in my function?
Thanks for any help!

 采纳的回答

When you have an "end" matching "function" then you create a sealed workspace. You need to use the command form of load.
T = load('try1.mat');
y = T.A*x(1:5)' + T.B*x(6:10)' + T.C*x(6:10)' + T.D*(x(11:60).*x(61:110))' + T.N*(x(11:60).*x(61:110))' + T.O*(x(11:60).*x(61:110))' + T.Q*(x(11:60).*x(61:110))' + T.P*sqrt(x(61:110)');
end
However you should not do the load() inside the function:

10 个评论

Thank you Walter!
now my objectfunction looks like:
it did get the GA toolbox running ,however, the result is wierd . both population and scores keeps its initial value in 51 iterations, is there any problem?
"now my objectfunction looks like"
Avoid slow EVALIN. Much better ways of passing parameters to the objective function:
  • using an anonymous function (possibly with the parameters in structure)
  • using a nested function.
By the way, shared variables are much slower passing in variables.
Store the following few lines in ob0218_internal.m
function y = ob0218_internal(x, T)
y = T.A*x(1:5)'+T.B*x(6:10)'+T.C*x(6:10)'+T.D*(x(11:60).*x(61:110))'+T.N*(x(11:60).*x(61:110))'+T.O*(x(11:60).*x(61:110))'+T.Q*(x(11:60).*x(61:110))'+T.P*sqrt(x(61:110)');
end
then before starting the ga tool, execute
T = load('try1.mat');
ob0218 = @(x) ob0218_internal(x, T);
Inside the tool, where it asks for function, instead of using @ob0218 use just ob0218 by itself without the @
Thanks for your advices!
I tried but still wierd result: M L F1 can't be recognized, these are what i fill in for A , b, lower, in the GA toolbox.
Slow is not really my concern, my big problem is, why it successfully runs 51 iterations then stop, but all 51 population and scores in each iteration keep the same? !
and the choice "use random state from previous runs" in the left part of GA toolbox is grey and can't be click. so where is the problem?
Please help!
Use T.M, T.L, T.F1 in the dialog instead of M, L, F1
If you attach your try1.mat then I will experiment.
Thank you Walter, please experiment for me.
it is a location problem, x1-x5 represent wheather one city will be chosen as distribution center.
Which MATLAB release are you using? It looks like the optimization tool you are using has been removed in newer releases.
i found some mistakes in M and L, now i correct them and replace M , L with MM and LL,
ga gives: Could not find a feasible initial point.
Since in MM and LL, I used like forms like a*x<=b and a*x.>=b for a*x=b to let x1 x2 x3 x4 x5 to be interger,
now i tried give up interger request, and seperate unequaliity constraint and equality constraint as MM1 LL1, and MM2 LL2, then i get:
The equality constraints are dependent.
The system of equality constraints is not consistent.
The inequality constraints may or may not be satisfied.
There is no feasible solution.
How can i fix this problem? Please help!
I do not know what code you are using now; I do not know which .mat file you are using; I do not know what MATLAB release you are using.
Your MM and LL arrays must not have relational operators: they need to be numeric arrays.
For integer variables, you need to code lower bounds and upper bounds, and you need to fill in "integer variables indices"

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by