Use of the optimisation toolbox,
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Walter Roberson
2012-3-16
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.
0 个评论
james
2012-3-16
1 个评论
Walter Roberson
2012-3-16
Your x0 does need to be as long as the maximum subscript of x that you reference.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!