Solve Double Sum in MATLAB: Q=ƩƩxixjaij

  • MATLAB
  • Thread starter adeeyo
  • Start date
  • Tags
    Matlab Sum
In summary, a MATLAB user is seeking help with a double summation expression (Q=ƩƩxixjaij) that they cannot solve using the available syntax. They provide a manual calculation and their attempted code, but are getting different answers. The expert advises them to set Q to 0.0 before starting and provides a correction to their code for the desired result.
  • #1
adeeyo
20
0
Hi,
I have this double summation expression to solve as part of MATLAB code I am writing. I have searched MATLAB no syntax that can do it. Please assist.

Q=ƩƩxixjaij i.e double sum of xi xj aij, i=1:n, j=1:n

Please assist me

Thanks
adeeyo
 
Physics news on Phys.org
  • #2
What do mean by solve? What you have written is simply an expression. To solve something you need to describe what is known and what you are trying to find.
 
  • #3
Thanks Mathman,

What I mean is this. I wrote a MATLAB code for that expression and manual as seen below but got different answer. I don't know what is wrong either with MATLAB code or manual expression or both.

Q=∑∑xixjaij the first sigma has i=1:n, the second sigma j=1:n

for i=1:n
for j=1:n
Q=sum(sum(x(i).*x(j).*(a(i, j))));
end
end



Mannual

Q=x(1)*x(1)*(a(1)*a(1))+x(1)*x(2)*(a(1)*a(2))+x(1)*x(3)*a(1)*a(3)+x(2)*x(1)*a(2)*a(1))+x(2)*x(2)*a(2)*a(2))+x(2)*x(3)*a(2)*a(3)*+x(3)*x(1)*a(3)*a(1))+x(3)*
x(2)*a(3)*a(2))+x(3)*x(3)*a(3)*a(3));

Thanks
 
  • #4
From what I recall about coding: You need to set Q = 0.0 before starting. Then the operating instruction should be Q=Q+x(i)*x(j)*a(i, j). To save a little time you could multiply by x(i) outside the j loop.
 
  • #5
if a is nxn and x is nx1 try Q = sum(sum(a.*repmat(x,[1 n]).*repmat(x',[n 1])))
 
  • #6
Q=sum(sum(x(i).*x(j).*(a(i, j))));

This line is what is wrong with your code. Let's consider what happens on each loop:
i=1, j=1:
sum(sum(x(1)*x(1)*a(1,1))) = x(1)*x(1)*a(1,1) so Q=x(1)*x(1)*a(1,1)

i=1,j=2:
sum(sum(x(1)*x(2)*a(1,2))) = x(1)*x(2)*a(1,2) so Q=x(1)*x(2)*a(1,2) NOT x(1)*x(1)*a(1,1)+x(1)*x(2)*a(1,2)

If you replace the line with
Q=Q+x(i)*x(j)*a(i,j)
you should get the right answer
 

FAQ: Solve Double Sum in MATLAB: Q=ƩƩxixjaij

1. What does the double sum notation mean in MATLAB?

The double sum notation in MATLAB, represented by ƩƩ, indicates that there are two nested summation operations being performed. This means that the values of two variables, xi and xj, are being added together and multiplied by aij.

2. How do I solve a double sum in MATLAB?

To solve a double sum in MATLAB, you can use the built-in function "sum" and specify the range of the variables using a for loop. For example, if the double sum is represented as ƩƩxixjaij, you can use the code "sum(sum(xi.*xj.*aij))" to solve it.

3. Can I use a different variable name instead of "i" and "j" in the double sum expression?

Yes, you can use any variable names that are not already used in your code. However, it is a common convention to use "i" and "j" as the variables in a double sum expression.

4. How can I optimize the performance of solving a double sum in MATLAB?

To optimize the performance of solving a double sum in MATLAB, you can use vectorization instead of a for loop. This means that instead of using a for loop to iterate through the range of the variables, you can use a single vector operation to perform the summation.

5. What are some common errors that occur when solving a double sum in MATLAB?

One common error is forgetting to include the multiplication symbol "*" between the variables and the coefficients in the double sum expression. Another error can occur if the range of the variables is not specified correctly, resulting in incorrect summation. It is also important to ensure that the dimensions of the variables and coefficients match for proper summation.

Similar threads

Replies
10
Views
2K
Replies
2
Views
3K
Replies
10
Views
2K
Replies
6
Views
2K
Replies
1
Views
1K
Replies
2
Views
3K
Replies
3
Views
4K
Replies
6
Views
1K
Back
Top