As stated in Quantum7, you defined the function in the same script, which will give you an error. Regardless of whether the function is in another file or not, what you wrote there is not a valid operation with symbolic variables. If you just comment out the second line and run it, you will get the following error:
??? Error using ==> sym.sym> checkindex in 2697
The index must be a positive integer or logical.
because i-1 is zero for the first cycle, and MATLAB starts counting from 1. If you try for i=2:3 , you get this error,
??? Error using ==> mupadmex
Error in MuPAD command: index exceeds matrix size.
because the symbolic variable is just a 1x1 array.
From what you wrote, it seems that you have an array of T1 , and T2 built from T1 in accordance with the relation: T2(i)=T1(i)+2*[T1(i-1)+T1(i+1)] . I think the best way to do what you are trying to use anonymous functions.
I’ll change the indexing on the account a bit so that you get an error in the first and last element, because the index will exceed the T1 border. However, 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 do not want to add zeros, but instead make it circular (i.e. T1(0)=T1(3) ), then you can use the same code, easily changing the definition of f .
abcd source share