dadaya 发表于 2009-3-29 16:24

各位帮忙,关于s函数的问题

我想用fminunc函数求解非线性无约束规划的最优解,输入4个,输出也是四个,程序以及模型见附件,可当我把fminunc函数编入到s函数中,再在模型中运行时,就会出现如下的错误,哪位高手可以帮我看一下我的程序哪里出错了呢?


Error in 'youhuaxin/S-Function1' while executing M-File S-function 'youhuax', flag = 3 (output), at time 0. MATLAB error message:
Error using ==> inlineeval at 15
Error in inline expression ==> ((y(1)^2)/(u(1)^2))+((y(2)^2)/(u(2)^2))+((y(3)^2)/(u(3)^2))+((y(4)^2)/(u(4)^2))+16*(y(1)^2+y(2)^2+y(3)^2+y(4)^2)/(y(1)+y(2)+y(3)+y(4))^2
??? Error using ==> eval
Undefined function or method 'y' for input arguments of type 'double'.

dadaya 发表于 2009-3-29 20:14

有那位知道是什么原因的吗?下面是我程序的内容:
function = youhua(t,x,u,flag)
switch flag,

%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
    =mdlInitializeSizes;

%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
    sys=mdlOutputs(t,x,u,flag);

%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case {1, 2, 4, 9 },
    sys = [];

%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
    error(['Unhandled flag = ',num2str(flag)]);

end
% end csfunc

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function =mdlInitializeSizes

sizes = simsizes;
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs   = 4;
sizes.NumInputs      = 4;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;

sys = simsizes(sizes);
x0= [];
str = [];
ts= [-1 0];

% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%


% end mdlDerivatives
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u,flag)

y0=;
sys=fminunc('((y(1)^2)/(u(1)^2))+((y(2)^2)/(u(2)^2))+((y(3)^2)/(u(3)^2))+((y(4)^2)/(u(4)^2))+16*(y(1)^2+y(2)^2+y(3)^2+y(4)^2)/(y(1)+y(2)+y(3)+y(4))^2',y0)
sys(1) =y(1)
sys(2) =y(2)
sys(3) =y(3)
sys(4) =y(4)

% end mdlOutputs

dadaya 发表于 2009-3-30 10:31

其实就是在s函数中对f=((y(1)^2)/(u(1)^2))+((y(2)^2)/(u(2)^2))+((y(3)^2)/(u(3)^2))+((y(4)^2)/(u(4)^2))+16*(y(1)^2+y(2)^2+y(3)^2+y(4)^2)/(y(1)+y(2)+y(3)+y(4))^2)来求最优值,u(1)到u(4)为输入,(见模型),想在s函数中求出y(1)到y(4)的最优值,但运行模型时出错,反复试了好多次都不行,哪位知道可以帮帮忙吗???

dadaya 发表于 2009-3-31 14:05

呵呵,哪位知道??帮帮忙呗:@)

dadaya 发表于 2009-4-1 11:47

哈哈,苦熬了几天,问题终于解决了。。。:lol :lol :lol

ChaChing 发表于 2009-4-1 13:27

昨晚有试了下, 但是版本问题无法读入!
LZ如何解决, 可以的话与大家共享经验吧!
页: [1]
查看完整版本: 各位帮忙,关于s函数的问题