How do you solve for a double sum
18 views (last 30 days)
Show older comments
Answered: jana nassereddine on 29 Apr 2021
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!
format long g
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;
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;
H(m) = 1/(N*(N-m))*H(m);
More Answers (4)
Steven Lord on 13 Nov 2019
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.
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.
v = z(i+m, :)-z(i, :);
h = h + sum(v.^2);
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.
Yes, I wanted to keep the structure of the code closer to that of the OP. I thought he had more chances to understand. But your code is definitely better from a Matlab style point of view
hello, can anyone help me solve this,
can someone tell me why this code is not working?
f=0; G=0 ;
x= [0; 4]; y= [0; 2]; H=[0; 2]; E=[0; 3];
Term1= (y(j)-H(k))*(x(i)^2+E(l)^2)^0.5 ;
Term3= -0.25*(x(i)^2+E(l)^2-(y(j)-H(k))^2) ;
f=f + (-1)^(i+j+k+l)*G;
f12 = (1/(x(2)-x(1))*(y(2)-y(1)))*f;
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!