PDA

查看完整版本 : [求助]function 问题


xiaotie
2008-03-30, 16:49
我编了一个如下
function [y,n]=jacobi(A,b,x0,eps)
回车时系统
??? function [y,n]=jacobi(A,b,x0,eps)
|
Error: Function definitions are not permitted at the prompt or in scripts.
打入
HELP function
回车时系统
Warning: Could not find an exact (case-sensitive) match for 'HELP'.
I:\Program Files\MATLAB71\toolbox\matlab\helptools\help.m is a case-insensitive match and will be used instead.
You can improve the performance of your code by using exact
name matches and we therefore recommend that you update your
usage accordingly. Alternatively, you can disable this warning using
warning('off','MATLAB:dispatcher:InexactMatch').
FUNCTION Add new function.
New functions may be added to MATLAB's vocabulary if they
are expressed in terms of other existing functions. The
commands and functions that comprise the new function must
be put in a file whose name defines the name of the new
function, with a filename extension of '.m'. At the top of
the file must be a line that contains the syntax definition
for the new function. For example, the existence of a file
on disk called STAT.M with:

function [mean,stdev] = stat(x)
%STAT Interesting statistics.
n = length(x);
mean = sum(x) / n;
stdev = sqrt(sum((x - mean).^2)/n);

defines a new function called STAT that calculates the
mean and standard deviation of a vector. The variables
within the body of the function are all local variables.
See SCRIPT for procedures that work globally on the work-
space.

A subfunction that is visible to the other functions in the
same file is created by defining a new function with the FUNCTION
keyword after the body of the preceding function or subfunction.
For example, avg is a subfunction within the file STAT.M:

function [mean,stdev] = stat(x)
%STAT Interesting statistics.
n = length(x);
mean = avg(x,n);
stdev = sqrt(sum((x-avg(x,n)).^2)/n);

%-------------------------
function mean = avg(x,n)
%AVG subfunction
mean = sum(x)/n;

Subfunctions are not visible outside the file where they are defined.
Normally functions return when the end of the function is reached.
A RETURN statement can be used to force an early return.

See also script, return, varargin, varargout, nargin, nargout,
inputname, mfilename.

Reference page in Help browser
doc function

xiaotie
2008-03-30, 16:49
怎么解决呢:frown:
,我是看网上资料,依样画葫芦的,准备学了,解方程组

dnping
2008-03-30, 17:02
首先:
HELP function的时候不能用大写,matlab对变量和函数区分大小写的;
对于你的function问题,请楼主先编好整个函数之后再回车,仅仅写了代码function [y,n]=jacobi(A,b,x0,eps)根本就不能运行得到结果,肯定会出错,其非C,可以进行随时的调试,matlab只能等你编好了整个函数程序之后才能输入变量进行运算。

建议楼主先找本最基础的书察看……

xiaotie
2008-03-30, 19:47
首先:
HELP function的时候不能用大写,matlab对变量和函数区分大小写的;
对于你的function问题,请楼主先编好整个函数之后再回车,仅仅写了代码function [y,n]=jacobi(A,b,x0,eps)根本就不能运行得到结果,肯定会出错,其非C,可以进行随时的调试,...

我是将整个命令复制了下来,粘到MATLAB上去的,我拿上来求帮助时,栽了,你能不能给我编一个,没有错误的方程组,用function编一个4组方程组,让我学习下好不好,谢了,我刚学,而且我那里很少用MATLAB,书店也买不到,只能上网,谢了,随便编个:ft:

xiaotie
2008-03-31, 09:31
以下是我的另一个命令,你看看

dnping
2008-03-31, 10:43
我不知道你的所说的四组方程组是什么意思,你要我解决一个什么问题?你想要做什么东西?
楼上的好像什么都没有哈……

xiaotie
2008-03-31, 10:44
Warning: MATLAB Toolbox Path Cache is out of date and is not being used.
Type 'help toolbox_path_cache' for more info.

To get started, select MATLAB Help or Demos from the Help menu.

>> function [y,n]=jacobi(A,b,x0,eps)
if nargin==3
eps=1.0e-6;
elseif nargin<3
error
return
end
D=diag(diag(A)); %求A的对角矩阵
L=-tril(A,-1); %求A的下三角阵
U=-triu(A,1); %求A的上三角阵
B=D\(L+U);
f=D\b;
y=B*x0+f;
n=1; %迭代次数
while norm(y-x0)>=eps
x0=y;
y=B*x0+f;
n=n+1;
end
??? function [y,n]=jacobi(A,b,x0,eps)
|
Error: Function definitions are not permitted at the prompt or in scripts.

>> A=[10,-1,0;-1,10,-2;0,-2,10]

A =

10 -1 0
-1 10 -2
0 -2 10

>> b=[9,7,6]'

b =

9
7
6

>> [x,n]=jacobi(A,b,[0,0,0]',1.0e-6)
??? Undefined command/function 'jacobi'.

>> A=[10,-1,0;-1,10,-2;0,-2,10];
b=[9,7,6]';
[x,n]=jacobi(A,b,[0,0,0]',1.0e-6)
??? Undefined command/function 'jacobi'.

>>

xiaotie
2008-03-31, 10:46
四组如:
X+Y+Z+C=1
X+Y+Z-C=2
X+Y^Z+C=3
X^Y+Z+C=4

dnping
2008-03-31, 11:24
对于你的问题,上面那个undefinded function出错是因为你的那个函数不在matlab的当前目录下,你需要将其置于你的matlab的当前目录下方可运行。

你的函数本身没有错误,是因为上面的原因所以才会运行不了。

第二个出错是由于你将函数在命令窗口编辑运行了,这个是不允许的,除非你有确切的数值供计算方可,可以自己写一个.m文件:就是打开一个空白的.m编辑器,在里面将你的代码拷贝进去之后保存到当前目录下,在命令窗口运行:
>>[x,n]=jacobi(A,b,[0,0,0]',1.0e-6)
x =

0.9958
0.9579
0.7916


n =

11
其实程序没有问题,就是编写程序和运行程序的时候一些规范没有遵守。

建议楼主下一些基本的资料看一下,我们论坛中就有很多基本的资料,我在上面也发布了两个资料:精通matlab6.5 以及精通matlab7,可以下下来仔细看看。