Solve Double Sum in MATLAB: Q=ƩƩxixjaij

  • Context: MATLAB 
  • Thread starter Thread starter adeeyo
  • Start date Start date
  • Tags Tags
    Matlab Sum
Click For Summary

Discussion Overview

The discussion revolves around solving a double summation expression in MATLAB, specifically the expression Q=ƩƩxixjaij, where the indices i and j range from 1 to n. Participants are exploring the correct implementation of this expression in MATLAB code and addressing discrepancies between manual calculations and code outputs.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant requests assistance with implementing a double summation in MATLAB, expressing confusion over the syntax and expected output.
  • Another participant questions the meaning of "solve," suggesting that the user clarify what is known and what they are trying to find.
  • A participant provides a MATLAB code snippet but notes that the output differs from their manual calculations, indicating uncertainty about the correctness of either the code or the manual expression.
  • One contributor suggests initializing Q to 0.0 before the summation and modifying the summation line to accumulate results correctly.
  • Another participant proposes an alternative approach using the repmat function to handle the summation more efficiently.
  • A participant identifies a specific error in the original code, explaining how the summation is incorrectly implemented and suggesting a correction to accumulate the results properly.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of the double summation in MATLAB, with some suggesting different coding approaches and others identifying potential errors in the original code. The discussion remains unresolved regarding the best method to achieve the desired outcome.

Contextual Notes

There are unresolved issues regarding the assumptions made in the manual calculations versus the MATLAB implementation, as well as potential dependencies on the dimensions of the matrices involved.

adeeyo
Messages
20
Reaction score
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
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.
 
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
 
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.
 
if a is nxn and x is nx1 try Q = sum(sum(a.*repmat(x,[1 n]).*repmat(x',[n 1])))
 
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
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K