1

I want to do the following in Matlab:

equation

i is the imaginary unit

r is a vector of length n: [r(1),...,r(n)]

phi is a 1x300 double, i.e. [phi(1),...,phi(300)]

sum(r(1:n).*(1i.^(1:n))./factorial(1:n))

This would work if there was no phi. But how can I implement the phi here?

sum(r(1:n).*((phi*1i).^(1:n))./factorial(1:n))

results in:

Matrix dimensions must agree.

The expected output is the same size as phi. This code would achieve what I want but I want n to be dynamic so the looping is not feasible:

if n==1
    R = r(1) * ( i * phi )
elseif n==2
    R = r(1) * ( i * phi ) + r(2) * ( i * phi ).^2 / 2;
elseif n==3
    R = r(1) * ( i * phi ) + r(2) * ( i * phi ).^2 / 2 + r(3) * ( i * phi ).^3 / 6;
...
Wolfie
  • 27,562
  • 7
  • 28
  • 55
user914822
  • 13
  • 3
  • Welcome to StackOverflow! What is the size of `n`? If it isn't 300, then there is a mismatch between `r` and `phi`. I also notice that `phi` is not indexed in your equation, so is `phi ` a constant or a vector? – Lucas Mar 02 '22 at 10:18
  • @Lucas Yes I mistakenly wrote c instead of r. I corrected it. n can be arbitrary (n is given as an input for my function). I understand that my code does not work because the size of n isn't 300 - I'm looking for another way so that the sum can be computed for arbitary n (the size of phi is 300 and it is fixed, but n can vary) – user914822 Mar 02 '22 at 10:23
  • 1
    the dot-product between `r` and `phi` will only work if they are the same size. So it won't work for arbitrary `n`. – Lucas Mar 02 '22 at 10:25
  • Please include a [mcve], say for `n=3`. How do you expect the terms of `phi` to be collected? Do you expect a 300-element output, with one sum per value of `phi`? Please also include your MATLAB version, it looks like you're straying towards requiring implicit expansion which is version-specific (although now not all that new) – Wolfie Mar 02 '22 at 10:36
  • 1
    @Wolfie I added it above. Yes I expect a 300 element output (the code I added above has a 1x300 double as output. That's exactly what I want, but I want to get rid of all the if's and instead program it as sum from 1 going to n - so that I dont have to check if n==1, if n==2 and so on) I use MATLAB R2021b. – user914822 Mar 02 '22 at 10:46

2 Answers2

0

You need to transpose phi, and then transpose your result back at the end, this looks something like

s = sum(r(1:n).*((phi.'*1i).^(1:n))./factorial(1:n),2).'

note the .' after phi and at the end to transpose. I've also included ,2 in the sum to sum along the 2nd dimension.

This relies on implicit expansion to create an intermediate matrix.

i.e. the operations between phi.' (a column array) and your row arrays (r(1:n), (1:n), and factorial(1:n)) are evaluated element-wise, making a matrix which is 300 x n. Then we sum in the 2nd dimension (sum(__,2)) to get a 300x1 output, which is finally transposed back to a 1x300 output to match your original phi.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
-1

Given your example, you could do

ind_end = min(n, length(phi))
sum(r(1:ind_end).*((phi(1:ind_end)*1i).^(1:ind_end))./factorial(1:ind_end))

This works for any n by avoiding the indexing to go beyond the length of phi.

Lucas
  • 362
  • 2
  • 12
  • This avoids throwing an error but also does not give the correct result I think, the desired output would compute the series for each value of `phi`, outputting something the same size as `phi`, which this does not do – Wolfie Mar 02 '22 at 14:36