5

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?

Schorsch
  • 7,761
  • 6
  • 39
  • 65
roghie
  • 59
  • 1
  • 1
  • 3
  • Related: [What's the difference between a script and a function in MATLAB?](http://stackoverflow.com/questions/1695365/whats-the-difference-between-a-script-and-a-function-in-matlab), [In MATLAB, can I have a script and a function definition in the same file?](http://stackoverflow.com/questions/5363397/in-matlab-can-i-have-a-script-and-a-function-definition-in-the-same-file) – gnovice May 12 '11 at 02:55

4 Answers4

5

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.

Quantum7
  • 3,165
  • 3
  • 34
  • 45
2

As Quantum7 pointed out, you have defined the function in the same script, which will give you an error. Regardless of whether the function is in a different file or not, what you've written there is not a valid operation with symbolic variables. If you simply comment out the second line and run it, you'll get the following error:

??? Error using ==> sym.sym>checkindex at 2697

Index must be a positive integer or logical.

which is because i-1 is zero for the first loop, and MATLAB starts counting at 1. If you try for i=2:3, the you get this error,

??? Error using ==> mupadmex

Error in MuPAD command: Index exceeds matrix dimensions.

because the symbolic variable is just a 1x1 array.

From what you've written, it seems like you have an array T1, and T2 is constructed from T1 according to the relation: T2(i)=T1(i)+2*[T1(i-1)+T1(i+1)]. I think a better way to do what you're trying is to use anonymous functions.

I'll slightly change the indexing to account for the fact that at the first and last element, you'll get an error because the index will exceed T1's bounds. Nevertheless, the answer is the same.

dummyT1=[0;T1(:);0];
f=@(i)(dummyT1(i+1)+2*(dummyT1(i)+dummyT1(i+2)));
T2=f(1:3)

If you don't want to add zeros, but instead make it circular (i.e., T1(0)=T1(3)), then you can use the same code by easily modifying the definition of f.

Community
  • 1
  • 1
abcd
  • 41,765
  • 7
  • 81
  • 98
  • If @yoda's answer doesn't work for you (and you're still working on this problem), please post more details of the problem you're trying to solve. There may be an easier way to do this, such as T2=2*[ T1(2:(end-1))+T1(1:end-2)+T1(3:end) ]? – Quantum7 May 24 '11 at 02:00
0

I think its a simple problem I solve it to push play button in editor file,thats compile your function in matlab command window then describe your inputs and give function parameters...

0

Functions within scripts are allowed from version R2016 or later

https://www.mathworks.com/help/matlab/matlab_prog/local-functions-in-scripts.html

As others have said, you need to put your functions in a different file.