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

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

问题描述

我想写这个方程的代码:T2(i)=T1(i)+2*[T1(i-1)+T1(i+1)]

syms T1 T2函数 [T2] = 统计(T1)对于 i=1:3T2(i)=T1(i)*2+[T1(i-1,)+T1(i+1,)]*2结尾

我想编码产生 T2(111)=T1(111)+2*[T1(011)+T(211)] 并且循环继续.但是matlab给出了这个错误

<块引用><块引用>

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

我该如何解决这个问题?

解决方案

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天全站免登陆