35 views (last 30 days)

x=[-20:0.2:10];

y=[-10:0.2:5];

u2=-3*exp(-sqrt(x.^2+y.^2)/2)

I want to sum this function from y=1 to the number of elements of y vector then the sum of this result from x=1 to the number of elements in x. IT is actually a double integral in form of sum.

Honglei Chen
on 30 Oct 2017

If the ultimate goal is to compute the integral, you can consider using integral2

If you have to do it with sum, then since you already computed u2 for all values, it is just

x=[-20:0.2:10]; y=[-10:0.2:5]'; u2=-3*exp(-sqrt(x.^2+y.^2)/2);

sum(u2(:))*0.2*0.2

Note that you have to transpose x or y so it forms a grid

HTH

Roger Stafford
on 30 Oct 2017

[X,Y] = meshgrid(x,y);

M = -3*exp(-sqrt(X.^2+Y.^2)/2);

S = sum(M(:))*.2*.2; % The desired sum

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/364111-compute-double-sum-or-double-integral-throug-sum#comment_498966

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/364111-compute-double-sum-or-double-integral-throug-sum#comment_498966

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/364111-compute-double-sum-or-double-integral-throug-sum#comment_498972

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/364111-compute-double-sum-or-double-integral-throug-sum#comment_498972

Sign in to comment.