如何更正“提示或脚本中不允许使用函数定义” [英] How to correct "Function definitions are not permitted at the prompt or in scripts"

查看:493
本文介绍了如何更正“提示或脚本中不允许使用函数定义”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写下这个方程的代码: T2(i)= T1(i)+ 2 * [T1(i-1)+ T1(i + 1)]
$ 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

在提示符或脚本中不允许使用函数定义



如何解决此问题?

解决方案

Matlab期望函数在它们自己的文件中。将上面的代码复制到一个文件'stat.m'中,它应该可以工作。



这个策略的确会导致不必要的短文件数量,但是它是必需的,因为方式matlab处理变量范围。每个文件都有自己的作用域,命令提示符下的所有变量都具有全局作用域。


I want to write the code for this equation: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

i want to code produce 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆