Use of the optimisation toolbox,
Show older comments
I have the code to display 2 functions in a graphical form I need to find the minimum point with several constraints, my code to display the equations is
x = 0.1:.01:2.1;
y = 0:.05:10;
[X,Y] = meshgrid(x,y);
Z = meshgrid(0.125:.025:5.125);
C = (1.10471.*((Z).^2) .* ((X).^2))+(0.0481.*(Y.*Z)*14.*X);
surfc(X,Y,Z,C);
axis([0 2.1 0 10 0 5.125]);
hold on;
R = 6000./sqrt(2.*Z.*X);
A = 6000*(14+0.5*X)*sqrt(0.25*(X).^2 +(Z+Y).^2);
B= 2*0.707*Z.*X.*(((X).^2)/(12+0.25*(Z+Y)^2));
S =A./B;
T=sqrt((((R).^2)+((S).^2)+X.*R.*S)./sqrt(0.25*((X).^2)+(Z+Y).^2));
surfc(X,Y,Z,T);
I'm trying to now use the function fminunc to minimalise the term for C whereby T is less than 13600 so far I thought it best to do firstly minimise the innital equation before inputing the futher constraints so my code is as follows,
function f = myfun(x)
f = (1.10471*(x(1)^2) * (x(1)^2))+(0.0481*(x(2)*x(3))*14*x(1));
x0 = [1,1];
[x,fval] = fminunc(@myfun,x0);
however when run this returns the statement
??? Input argument "x" is undefined.
Error in ==> myfun at 2
f = (1.10471*(x(1)^2) * (x(1)^2))+(0.0481*(x(2)*x(3))*14*x(1));
>>
Any clues as to where I'm going wrong?
Many thanks
Answers (2)
Walter Roberson
on 16 Mar 2012
Split your code like
function [x, fval] = minimize_myfun
x0 = [1,1];
[x,fval] = fminunc(@myfun,x0);
function f = myfun(x)
f = (1.10471*(x(1)^2) * (x(1)^2))+(0.0481*(x(2)*x(3))*14*x(1));
It is important that you do not have the call to fminunc within the myfun function.
james
on 16 Mar 2012
0 votes
1 Comment
Walter Roberson
on 16 Mar 2012
Your x0 does need to be as long as the maximum subscript of x that you reference.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!