Hi, i need to write a sum of serie :
with b_k= (k+1)^2-(k)^2 and u^(m-k) is the numerical solution approximation u(x,t_m-k) for k=1,...m-1 and m=1,....,20
for m=1:20
syms k u
b(k)=(k+1)^2-k^2;
F=symsum((b(k)-b(k+1))*u(:,m-k),k,1,m-1)
end
F
but i get error
Necessary I need u(:,m-k)
Please help me

 Accepted Answer

Walter Roberson
Walter Roberson on 16 Dec 2020

0 votes

It is never possible to use a symbolic variable as an index in MATLAB.
You need to create a definite list of values and sum() the list. Or loop adding on to a total.
Note by the way that and so b(k) - b(k+1) = (2*k+1) - (2*(k+1)+1) = -2

5 Comments

Ok I understand. What if b(k)=(k+1)^(1-alpha)-k^(1-alpha) with alpha=0.6; It's correct ?
alpha=0.2;
for m=1:20
sum=U(:,m)
for k=1:m-1
b(k)=(k+1)^(1-alpha)-k^(1-alpha);
sum=sum+(b(k)-b(k+1))*U(:,m-k)
end
end
We recommend against using sum as the name of a variable, as it is quite common to need to use sum() as a function in the same routine as using sum as a variable. And also using sum as a variable tends to confuse readers.
You need to assign to b(k+1) before you can use it, so it would make the most sense to initialize b(1) and then inside the function have the appropriate calculation to assign to b(k+1) .
Or just vectorize the assignment to b.
K = 0:20;
b = (K+1).^(1-alpha) - K.^(1-alpha);
and now b is fully initialized and does not need to be calculated inside the loops.
@Walter Roberson, Aah! Now, I understand why sum tends to confus readers, but i don't know how to fix my problem.
How can i get S as a sum of matrix ? because it relates to U(:,k) numerical solution of pde (=U(x,y,t(k)) in maths). Please, just see the next figure :
.
alpha=0.2;
for m=1:2
k=1:m
b=(k+1).^(1-alpha)-k.^(1-alpha);
S(:,k)=b*(U(:,m-k+1)-U(:,m-k)); % U is a matrix, numerical solution of 2D
end
% S=S(:,1)+....+S(:,m)??
Sorry, you are changing equations too much. Your equation here is quite different than your equation in your Question, and it is not really reasonable to answer both at the same time as they are too different. What is the question you need answered?
Note: using U(:,m-k) is not equivalent to as t is obviously a vector of values that as outside observers we need to assume is a real number in or possibly even in and cannot assume that t just happens to be linear indices . At the very least it is likely that t starts from 0, and so is not suitable for indexing. Indeed, since then k can equal m, and then would be 0, and that is not a valid matrix index.
Yamina chbak
Yamina chbak on 17 Dec 2020
Edited: Yamina chbak on 18 Dec 2020
You are right !, I'm sorry @Walter Roberson. I just asked my first question in a simple formule of b but your answer is not i want. In fact, it's the same problem with different formule, it's all about calculate the sum of series matrix.
I have a matrix U(:,k). My final question : What is the best way to write the code to calculate sum of series matrix :?
This what i did before
alpha=0.2;b(0)=1.0;c(0)=2.^(1-alpha)-1;
S=U(:,1); %initialize of S
for m=2:20
for k=1:m
b(k)=(k+1).^(1-alpha)-k.^(1-alpha); c(k)=(k+2).^(1-alpha)-(k+1).^(1-alpha);
S=(b(k)-c(k))*U(:,m-k);
end
end
S
I'm sorry again, but I learned a lot from you.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!