Compute double sum or double integral throug sum

조회 수: 5 (최근 30일)
F.O
F.O 2017년 10월 30일
답변: Roger Stafford 2017년 10월 30일
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.
  댓글 수: 2
Adam
Adam 2017년 10월 30일
You want to sum from y = 1 up to the number of elements in y even though y begins at -10 and runs to 5? How is that expected to work exactly?
F.O
F.O 2017년 10월 30일
편집: F.O 2017년 10월 30일
Here we are dealing with the position of the elements not the elements themseves. Maybe to use for loop command here. IT is actually a double integral which I should integrate with respect to from y= -10to y=5in the first step then with respect to x from x=-20 to x=10 I have dx=0.2 dy=0.2

댓글을 달려면 로그인하십시오.

채택된 답변

Honglei Chen
Honglei Chen 2017년 10월 30일
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
  댓글 수: 2
F.O
F.O 2017년 10월 30일
Could you explain more why we should use transpose here and what this the sum here works?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2017년 10월 30일
[X,Y] = meshgrid(x,y);
M = -3*exp(-sqrt(X.^2+Y.^2)/2);
S = sum(M(:))*.2*.2; % The desired sum

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by