关于s函数程序的问题

[复制链接]
查看11 | 回复3 | 2013-8-4 16:34:47 | 显示全部楼层 |阅读模式
各位帮忙,关于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'.
有那位知道是什么原因的吗?下面是我程序的内容:
function [sys,x0,str,ts] = youhua(t,x,u,flag)
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,

[sys,x0,str,ts]=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 [sys,x0,str,ts]=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=[1,1,1,1];
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
其实我就是想在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'求最优值,并输出y(1)到y(4),输入为u(1)到u(4),搭建模型时,输入为4个正弦波,如何在s函数中得到输出???为什么总出错呢????各位帮帮忙啊!

回复

使用道具 举报

千问 | 2013-8-4 16:34:47 | 显示全部楼层
你这问题比我的还难一样。同是新手。。。。...
回复

使用道具 举报

千问 | 2013-8-4 16:34:47 | 显示全部楼层
s...
回复

使用道具 举报

千问 | 2013-8-4 16:34:47 | 显示全部楼层
不会哈...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行