声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 5937|回复: 6

[编程技巧] Input argument 'y' is undefined 出错求助

[复制链接]
发表于 2008-5-12 20:38 | 显示全部楼层 |阅读模式

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

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

x
%test4.m
function f=test4(t,y);
m1=1;m2=1;m3=2;k=1;
U=[0 1 0 0 0 0;
    -3 0 1 0 0 0;
    0 0 0 1 0 0;
    1 0 -2 0 1 0;
    0 0 0 0 0 1;
    0 0 1 0 -3 0];
f=U*y+[0 sin(3*t) 0 0 0 0]';

%test4Result.m
[t,y]=ode45('test4',[0:0.01:100],[0 0 0 0 0 0]');
u1=y(:,1);
u2=y(:,3);
u3=y(:,5);
以上这个程序以前运行的时候还好好的,这两天运行了几次都出现

??? Input argument 'y' is undefined.
Error in ==> C:\MATLAB6p5\work\test4.m
On line 10  ==> f=U*y+[0 sin(3*t) 0 0 0 0]';

为什么会出现这种情况????

[ 本帖最后由 eight 于 2008-5-12 21:29 编辑 ]
回复
分享到:

使用道具 举报

发表于 2008-5-12 21:01 | 显示全部楼层


没有报错呀!
另外,在Matlab R2008a版本中,显示下面代码中红色的地方是一个不必要的分号(可以去掉):
function f=test4(t,y);

function f=test4(t,y)

[ 本帖最后由 ch_j1985 于 2008-5-12 21:11 编辑 ]
发表于 2008-5-12 21:28 | 显示全部楼层
请养成编写程序时 clear all 的习惯
 楼主| 发表于 2008-5-13 02:52 | 显示全部楼层
加上clear all以后会出现以下错误

??? Error: File: C:\MATLAB6p5\work\test4.m Line: 3 Column: 1
A function declaration cannot appear within a script M-file.

为什么会这样??
发表于 2008-5-13 06:51 | 显示全部楼层
??? Input argument 'y' is undefined.
Error in ==> C:\MATLAB6p5\work\test4.m
On line 10 ==> f=U*y+[0 sin(3*t) 0 0 0 0]';

这个错误是直接运行test4.m造成的,因为test4.m文件中没有给'y'赋值,所以才会出现Input argument 'y' is undefined.
要得到结果请运行test4Result.m。

??? Error: File: C:\MATLAB6p5\work\test4.m Line: 3 Column: 1
A function declaration cannot appear within a script M-file.

(我猜测)这个错误是把clear all命令错放在test4.m中了,正确的做法是放在test4Result.m文件中。
发表于 2008-5-13 11:28 | 显示全部楼层
clear all 要放在 function 下,matlab 的 function 与其他语言的子函数无异,因此也不可能把执行语句放在function外面的
 楼主| 发表于 2008-5-13 15:43 | 显示全部楼层
多谢各位~~~~~~~~~~~~
又学到个知识
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-5-16 02:35 , Processed in 0.050338 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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