Question

In matlab, I implemented the sum of series (x^k) / (2*k) when k is from 1 to 6, as following:

syms x;
syms k real;
symsum(x^k/(2*k), k, 1, 6)

The above sum has only one symbolic variable (x). Now I want to implement the following sum in Matlab (alpha and n are constant). As you can see, depending on the value of n, we will have different number of symbolic variables. For example, if n=2 then we have 2 symbolic variables x1 and x2. If n=4 then we have 4 symbolic variables x1,x2,x3 and x4. How can I implement this in Matlab?

enter image description here

Was it helpful?

Solution

You can create a 1xN vector of symbolic variables, by using

A=sym('A',[1 N]);

and then the i-th element is accessed using A(i).

See here for more detail.

As for writing the sum, I can't really help. I think you may to use a for loop and specify the indices, i.e.

S=0;
for i=1:N/2
    S=S+alpha*(x(2*i)-x(2*i-1)^2)+(1-x(2*i-1))^2;
end

but that doesn't simplify the answer at all. I'm not aware of a better method though.

OTHER TIPS

If Mupad solution is admissible for you, try

sum(alpha*(x(2*k)-x(2*k-1)^2)+(1-x(2*k-1))^2, k=1..n/2)

of course you should state alpha and n

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top