2 views (last 30 days)

Dear everyone：

I was obsessed with a problem for a long time，here is my equation and date.txt.

My calculation result is quite different from the result in the literature. There must be a problem in my code but I don't know what happened. I'm a newbie, somebody please help me. thanks a lot advance!

clear;

format long g

z=load('d:\20191106\a\a1.txt');

n=length(z);

h=0;

i=0.0195314;

m=100

for m=1:512

for j=1:n

for i=1:n-m

h=h+(z(i+m,j)-z(i,j))^2;

end

end

h=1/(n*(n-m))*h;

rr(m)=log(m*i);

hh(m)=log(h);

end

plot(rr,hh,'bo')

My calculations

literatures

Fabio Freschi
on 13 Nov 2019

There are some things that are vgue, like the x-axis, in any case, this code shows something similar to the desired result

clear variables, close all;

z=load('a1.txt');

N = size(z,2);

mMax = size(z,1)-1;

H = zeros(mMax,1);

for m = 1:mMax

for j = 1:N

for i = 1:N-m

H(m) = H(m)+(z(i+m,j)-z(i,j))^2;

end

end

H(m) = 1/(N*(N-m))*H(m);

end

figure,semilogx(1:mMax,log10(H),'bo')

Steven Lord
on 13 Nov 2019

h=0;

i=0.0195314;

m=100

for m=1:512

for j=1:n

for i=1:n-m

h=h+(z(i+m,j)-z(i,j))^2;

end

end

h=1/(n*(n-m))*h;

rr(m)=log(m*i);

hh(m)=log(h);

end

Let's take a look at this section of your code. When m becomes 2, what value should h contain? What value does h contain?

You want to put the "h = 0;" line inside your loop over m.

You also want to handle the case where n is equal to m. In that case, your loop over i executes no times and i is []. This causes problems on your line where you assign to rr(m). You probably want to use (n-m) [the last value i would take when the loop executed, which it doesn't when n is equal to m.]

But anyway, we can probably simplify that code a bit. Since the limits of the loop over i don't depend on j at all, we can swap the order of the loops.

for i=1:n-m

for j=1:n

h=h+(z(i+m,j)-z(i,j))^2;

end

end

If we use array operations and sum rather than operating on each scalar element of z independently and using plus, we can eliminate the innermost loop entirely. We do need to be a little careful, since you used length on a matrix and that may not do what you expect if z is taller than it is wide. Instead you should use size to get the number of rows and columns.

for i=1:n-m

v = z(i+m, :)-z(i, :);

h = h + sum(v.^2);

end

You probably can even eliminate that loop if you think of using array operations on submatrices rather than on vectors, but I'll leave that as an exercise for the reader.

Fabio Freschi
on 13 Nov 2019

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://au.mathworks.com/matlabcentral/answers/490799-how-do-you-solve-for-a-double-sum#comment_766728

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/490799-how-do-you-solve-for-a-double-sum#comment_766728

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/490799-how-do-you-solve-for-a-double-sum#comment_766804

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/490799-how-do-you-solve-for-a-double-sum#comment_766804

Sign in to comment.