Search code examples
arraysmatlabsymbolic-math

Subscript indices error while indexing array within symbolic function symsum


I'm trying to solve an easy recursive equation, but I'm encountered with very rudimentary problems that I think a MATLAB expert can easy resolve. So here is the short version of my code:

clear all
%%%INPUT DATA
gconst = [75 75];
kconst = [200 200];
tau = [.01667 .14153];
%%% TIME Span
t = [0 .001 .002 .003 .004 .005];
%%% Definition of the functions g(x) and k(y)
syms g(x) k(y)
g(x) = gconst(1)*exp(-x/tau(1))+gconst(2)*exp(-x/tau(2));
k(y) = kconst(1)*exp(-y/tau(1))+kconst(2)*exp(-y/tau(2));
%%% Defining initial conditons
nu = zeros(1,7);
nu(1)= 3.64e-1;
nu(2)= 3.64e-1;
%%% nu(3) is required
int(sym('i'))
nu(3)=nu(1)*(3*k(t(3)-t(2))+g(t(3)-t(2))-g(t(3)))...
     +symsum(nu(i)*(3*k(t(3)-t(i+1))-3*k(t(3)-t(i-1))... %symsum line 1
     +g(t(3)-t(i+1))-g(t(3)-t(i-1))), i, 1, 3))... %symsum line 2
     /(3*k(0)+g(0));

You can ignore the whole symsum part, because without, the code still doesn't work. It is a very straightforward code, but after running it, I get this error:

Subscript indices must either be real positive integers or logicals.

This error is found in the line where I defined nu(3). I'd like to hear your comments.

EDIT 1: k(y) instead of k(x).

EDIT 2: zeros(1,7) instead of zeros(7).

NOTE 1: The code works without the symsum part and after EDIT 1.


Solution

  • What you want can't be done.

    The reason is, that you are indexing an array t = [0 .001 .002 .003 .004 .005] with the symbolic summation index i.

    So while

    syms i
    S1 = symsum( i, i, 1,3)
    

    works

    syms t i
    t = [1 2 3];
    S1 = symsum( t(i), i, 1,3)
    

    won't work, and there is no way around it, because the values 1 ... 3 are evaluated after indexing. You need to rethink your approach completely.


    Apart from that you probably want k(y) instead of k(x). That was the reason why the code didn't work without the symsum part neither.

    Using i as a variable name is not an issue anymore, but shouldn't be used to avoid misunderstandings.