声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1970|回复: 5

[其他相关] 各位帮忙,关于s函数的问题

[复制链接]
发表于 2009-3-29 16:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
我想用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'.

youhuax.m

2.23 KB, 下载次数: 4

youhuaxin.mdl

20.97 KB, 下载次数: 5

回复
分享到:

使用道具 举报

 楼主| 发表于 2009-3-29 20:14 | 显示全部楼层
有那位知道是什么原因的吗?下面是我程序的内容:
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
 楼主| 发表于 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)的最优值,但运行模型时出错,反复试了好多次都不行,哪位知道可以帮帮忙吗???
 楼主| 发表于 2009-3-31 14:05 | 显示全部楼层
呵呵,哪位知道??帮帮忙呗:@)
 楼主| 发表于 2009-4-1 11:47 | 显示全部楼层
哈哈,苦熬了几天,问题终于解决了。。。:lol :lol :lol
发表于 2009-4-1 13:27 | 显示全部楼层
昨晚有试了下, 但是版本问题无法读入!
LZ如何解决, 可以的话与大家共享经验吧!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-10 21:26 , Processed in 0.074073 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表