如何更正“提示或脚本中不允许使用函数定义” [英] How to correct "Function definitions are not permitted at the prompt or in scripts"
问题描述
我想写下这个方程的代码: 我想编码产生 在提示符或脚本中不允许使用函数定义 如何解决此问题? Matlab期望函数在它们自己的文件中。将上面的代码复制到一个文件'stat.m'中,它应该可以工作。 这个策略的确会导致不必要的短文件数量,但是它是必需的,因为方式matlab处理变量范围。每个文件都有自己的作用域,命令提示符下的所有变量都具有全局作用域。 I want to write the code for this equation: i want to code produce T2(i)= T1(i)+ 2 * [T1(i-1)+ T1(i + 1)] $ c
$ p $ syms T1 T2
函数[T2] = stat(T1)
$ b $ (i)= T1(i-1,)+ T1(i + 1,)] * 2
end
T2(111)= T1(111)+ 2 * [ T1(011)+ T(211)]
,循环继续。
,但matlab给出这个错误
lockquote
blockquote
T2(i)=T1(i)+2*[T1(i-1)+T1(i+1)]
syms T1 T2
function [T2] = stat(T1)
for i=1:3
T2(i)=T1(i)*2+[T1(i-1,)+T1(i+1,)]*2
end
T2(111)=T1(111)+2*[T1(011)+T(211)]
and the loop go on .
but matlab giving this error
"Function definitions are not permitted at the prompt or in scripts"
How can I solve this problem?
Matlab expects functions to be in their own file. Copy the above code to a file 'stat.m' and it should work.
This policy does cause an unnecessary number of short files, but it is required because of the way matlab handles variable scope. Each file gets its own scope, and all variables in the command prompt have global scope.
这篇关于如何更正“提示或脚本中不允许使用函数定义”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!