Trying to code a tricky double summation

So I'm trying to write code that calculates the following:
I have two functions of the same two variables, call them D(x,y) and T(x,y)
I'm trying to write a function that accepts inputs x,y,z and outputs:
So, there are two summations, the outer one takes the sum from i=0 up to min(x,y), and the inner sum takes the summation of terms from k=0 up to x-i
The inner sum isn't as complicated as it looks, it has a (-1)^k at the start and then just multiples the functions D(a,b) and T(a,b)^(z/2) where a and b depend on both where you are in the indexing of your outer summation and inner summation
This is a much harder proram than I have written before, so tips, insights, general advice, and even completed code are welcome!! Thanks for your time

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 24 日

1 投票

Something like this should work
s = 0;
for i=0:min(x,y)
for k=0:x-1
s = s + (-1)^k * D(2*x-2*k-2*i, 2*y+2-2*i) * T(2*x-2*k-2*i, 2*y+2-2*i)^(z/2)
end
end

5 件のコメント

Michael Vaughan
Michael Vaughan 2020 年 9 月 24 日
Wow, so simple!! Thank you so much.
one thing though: in the second "for", it should be k=0:x-i I believe and not k=0:x-1
because the top summation bound depends on i and is not just x-1, right?
Michael Vaughan
Michael Vaughan 2020 年 9 月 24 日
How come when I run the code:
function sum = Sum(x,y,z)
s = 0;
for i=0:min(x,y)
for k=0:x-i
s = s + (-1)^k * QuantumDimension(2*x-2*k-2*i, 2*y+2-2*i) * Twist(2*x-2*k-2*i, 2*y+2-2*i)^(z/2);
end
s
end
I get the output:
s =
((1/q^7 - q^7)*(1/q^(7/2) - q^(7/2))^2)/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - ((1/q^4 - q^4)*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))) + ((1/q^5 - q^5)*(1/q^(3/2) - q^(3/2))*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - ((1/q^6 - q^6)*(1/q^(5/2) - q^(5/2))*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - ((1/q^8 - q^8)*(1/q^(7/2) - q^(7/2))*(1/q^(9/2) - q^(9/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2)
s =
((1/q^3 - q^3)*(1/q^(5/2) - q^(5/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))) - ((1/q^4 - q^4)*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))) + ((1/q^5 - q^5)*(1/q^(5/2) - q^(5/2))^2)/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) + ((1/q^7 - q^7)*(1/q^(7/2) - q^(7/2))^2)/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - ((1/q^4 - q^4)*(1/q^(3/2) - q^(3/2))*(1/q^(5/2) - q^(5/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) + ((1/q^5 - q^5)*(1/q^(3/2) - q^(3/2))*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - (2*(1/q^6 - q^6)*(1/q^(5/2) - q^(5/2))*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - ((1/q^8 - q^8)*(1/q^(7/2) - q^(7/2))*(1/q^(9/2) - q^(9/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2)
s =
((1/q^3 - q^3)*(1/q^(5/2) - q^(5/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))) - ((1/q^2 - q^2)*(1/q^(3/2) - q^(3/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))) - ((1/q^4 - q^4)*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))) + ((1/q^3 - q^3)*(1/q^(3/2) - q^(3/2))^2)/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) + ((1/q^5 - q^5)*(1/q^(5/2) - q^(5/2))^2)/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) + ((1/q^7 - q^7)*(1/q^(7/2) - q^(7/2))^2)/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - (2*(1/q^4 - q^4)*(1/q^(3/2) - q^(3/2))*(1/q^(5/2) - q^(5/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) + ((1/q^5 - q^5)*(1/q^(3/2) - q^(3/2))*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - (2*(1/q^6 - q^6)*(1/q^(5/2) - q^(5/2))*(1/q^(7/2) - q^(7/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2) - ((1/q^8 - q^8)*(1/q^(7/2) - q^(7/2))*(1/q^(9/2) - q^(9/2)))/((q - 1/q)*(1/q^(1/2) - q^(1/2))^2)
I would much rather just get one output..... why is it giving me s 3 times??
Ameer Hamza
Ameer Hamza 2020 年 9 月 24 日
Yes, it is correct. k should be from 0 to x-i. How are you calling this function? I think that these outputs are displayed on the command window because you have put s after inner for-loop.
function sum = Sum(x,y,z)
s = 0;
for i=0:min(x,y)
for k=0:x-i
s = s + (-1)^k * QuantumDimension(2*x-2*k-2*i, 2*y+2-2*i) * Twist(2*x-2*k-2*i, 2*y+2-2*i)^(z/2);
end
s % remove this from here
end
Michael Vaughan
Michael Vaughan 2020 年 9 月 24 日
Thank you very much. I'm now trying to give this function a call handle,
I type S1 = @Sum1.m
Where Sum1.m is the code:
function sum = Sum1(x,y,z)
s = 0;
for i=0:min(x,y)
for k=0:x-i
s = s + (-1)^k * QuantumDimension(2*x-2*k-2*i, 2*y+2-2*i) * Twist(2*x-2*k-2*i, 2*y+2-2*i)^(z/2);
end
end
s
When I type in S1(3,2,1) I get:
Undefined function 'Sum1.m' for input arguments of type 'double'.
What is going wrong here?? THanks for your patience
Ameer Hamza
Ameer Hamza 2020 年 9 月 24 日
To make a function handle type this
S1 = @Sum1
S1(3,2,1)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by