2

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.

Jay
  • 23
  • 4
  • I do not have the symbolic math toolbox, what is the value of `int(sym('i'))`? – sco1 Sep 16 '15 at 12:35
  • You don't declare `i` . So for example nu(i) will give you the classical `Subscript indices must either be real positive integers or logicals.` – obchardon Sep 16 '15 at 12:41
  • Just don't use `i` as a variable, see [here](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab) – Adriaan Sep 16 '15 at 13:19
  • At least one issue is that `nu` is a numeric array, but you're trying to assign a symbolic expression to it, which won't work. Define `nu` as symbolic: `nu = zeros(7,'sym')`, or in older versions of Matlab: `nu = sym(zeros(7))`. – horchler Sep 16 '15 at 22:06
  • You also declare `k(y)` as a symbolic function but never define it (instead you define `k(x)`). – horchler Sep 16 '15 at 22:12
  • @horchler the same would apply for `t` - but in Matlab 2015 it doesn't seem to make any difference anymore? – Robert Seifert Sep 17 '15 at 14:35
  • I already have declared `i`and I even changed it to `ii` as the link suggests, but that's not the issue. @horchler a symbolic `nu` returns a massive equation, how can I change it to a single number, and honestly `nu` as an array does the job as well (without the `symsum` part though). And `k(y)` was mistakenly written as `k(x)` and I edited that. – Jay Sep 17 '15 at 15:13

1 Answers1

2

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.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • It is possible to get around the order of evaluation "issue" in your second answer by using the old string notation (slower and not recommended by The MathWorks) and `eval`: `S1 = eval(symsum('sym(t(i))', i, 1,3))` (the added `sym` is so the sum is evaluated symbolically). I would rewrite the code rather than use this though. One could also call to the MuPAD engine as well, but it doesn't seem worth it here. – horchler Sep 16 '15 at 22:30
  • @horchler It does not work, I tried a hundred other things as well. I could imagine to create the whole expression within `symsum` as string, e.g. with a loop and evaluate that. – Robert Seifert Sep 17 '15 at 05:55
  • @thewaywewalk `k(x)` was a typo. Now the only problem is the `symsum` part. And as you suggested for a _string and loop_ instead of `symsum`, that is not feasible, because it's a recursive equation, i.e. in order to solve the equation for `nu(3)` you need `nu(1)` and `nu(2)`, and I want this for say `nu(70)`. And as far as I know, one can not use a loop inside an equation. – Jay Sep 17 '15 at 15:38
  • 1
    I was able to write the `symsum` part with two rather easy `for` commands combined with an `if`. – Jay Oct 08 '15 at 09:56