Implementing double series in MATLAB

  • Thread starter Thread starter Atr cheema
  • Start date Start date
  • Tags Tags
    Matlab Series
Click For Summary

Discussion Overview

The discussion revolves around implementing a double series summation in MATLAB, focusing on the calculation of specific mathematical functions using the complementary error function (erfc). Participants are addressing coding issues, mathematical dependencies, and the behavior of the output results based on varying parameters.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant presents their MATLAB code for calculating a double sum but encounters issues with the values of the complementary error function (e1 and e2) remaining zero, possibly due to the term (p-m).
  • Another participant notes that e1 and e2 do not depend on deltaH, suggesting that the participant's expectations regarding these values are misplaced.
  • Concerns are raised about the rapid decay of the erfc function for large positive arguments, which could lead to e1 and e2 being effectively zero.
  • There are corrections suggested regarding the implementation of the summation and the proper calculation of e1 and e2, including adjustments to parentheses and denominators.
  • A participant expresses the desire to run the summation for multiple values of p, aiming for a bell-shaped behavior in the output array, but encounters issues with the output increasing unboundedly.
  • Further suggestions are made to optimize the code by using vectorized operations instead of loops to improve performance.
  • Participants discuss the implications of taking the difference between two very small numbers, which may affect the results.
  • One participant indicates that their inner loop setup was incorrect, leading to unexpected results in the output array.

Areas of Agreement / Disagreement

Participants express differing views on the reasons behind the output behavior and the correctness of the code implementation. There is no consensus on the resolution of the issues raised, and multiple competing perspectives on the problem remain present.

Contextual Notes

Participants highlight potential limitations in the code related to the handling of mathematical functions and the structure of loops, as well as the dependence on specific parameter values that may not yield expected results.

Atr cheema
Messages
67
Reaction score
0
Mod note: Moved from a technical forum section, so missing the homework template
I want to write code for this double sum in MATLAB and I have written following code:
x = 100; % to calculate omega and u
l = 300; % to calculate omega
p = 10;
omegaa= x/l; deltaH = 200;
deltat = 5; % to calculate u
v = 140; % to calculate u
u = x/sqrt(v*deltat); suma = 0;
for m = 1:p;
for n = 1:1000;

e1 = erfc(2*(n-1)*u + u*omega / (2*omega*sqrt(p-m)));
e2 = erfc(2*n*u - u*omega/(1*omega*sqrt(p-m)));
totalerror = e1*e2;
f = (-1)^n-1 * deltaH * totalerror;
summ = suma + f;
end
end​
My problem is no matter what value of deltaH I use e1 and e2 values remain zero, may be because of p-m. How can I resolve this issue?
 
Last edited by a moderator:
Physics news on Phys.org
Several things:

1. e1 and e2 have no dependence on deltaH, so you shouldn't expect e1 and e2 to change with deltaH
2. You are not implementing the sum in the link. take another look and check your code. There are several errors on your part.
3. erfc(x) falls off very fast for positive x. for example, erfc(4) is about 1.5e-8, erfc(6) is about 2e-17. So if you give it large positive arguments you will get zero.

jason
 
Yes the actual problem is the values of e1 and e2 always remaining constant.
'Summ' should be 'suma' but it won't solve problem of e1 and e2 being always zero.
Value of x is measured from field. It can not be changed to other value.
 
First of all, you have some code errors in there, you are not calculating what you posted. secondly, the erfc function is ~10^{-100} for x > 5 so it doesn't surprise me that it come out to be 0. I inserted some parentheses and fixed it according to your image of the equation...

Code:
x = 100; % to calculate omega and u
l = 300; % to calculate omega
p = 10;
omega= x/l; deltaH = 200;
deltat = 5; % to calculate u
v = 140; % to calculate u
u = x/sqrt(v*deltat); suma = 0;
for m = 1:p
  for n = 1:1000
  e1Arg = (2*(n-1)*u + u*omega) / (2*omega*sqrt(p-m));
  e1 = erfc(e1Arg);
  e2Arg = (2*n*u - u*omega)/(1*omega*sqrt(p-m));
  e2 = erfc(e2Arg);
  totalerror = e1-e2;
  f = (-1)^(n-1) * deltaH * totalerror;
  suma = suma + f;
  end
end
 
  • Like
Likes   Reactions: Atr cheema
@Dr Transport Thank you very much. One more thing. I want MATLAB to run summation for every single value of 'p', every time with a different value of 'deltaH'. It means if 'p=100' then my goal will be to calculate sum first for p=1, than for p=2 and so on till p=100, thus 100 values in total. I have added an outer for loop but final array results in an ever increasing values which should not be the case. The elements of outer array should behave somehow similar to that of deltaH. i.e bell shaped
Code:
x = 100; % to calculate omega and u
l = 300; % to calculate omega
p = 100;
omega= x/l; deltaH =sin(linspace(0.3,2.6,100));
deltat = 5; % to calculate u
v = 140; % to calculate u
u = x/sqrt(v*deltat); suma = 0;
for i=1:p
for m = 1:i
  for n = 1:1000
  e1Arg = (2*(n-1)*u + u*omega) / (2*omega*sqrt(p-m));
  e1 = erfc(e1Arg);
  e2Arg = (2*n*u - u*omega)/(1*omega*sqrt(p-m));
  e2 = erfc(e2Arg);
  totalerror = e1-e2;
  f = (-1)^(n-1) * deltaH(i) * totalerror;
  suma(i) = suma(i) + f;
  end
end
 
Last edited:
e2Arg: the denominator should have a factor of 2 not 1 and this will run faster if you use vectors not loops...
 
@Dr Transport Having changed the denominator the result still looks unrealistic as here http://[PLAIN]https://www.dropbox.com/s/glaq5av7fzzlhaj/11.png?dl=0 . It still seems that I am adding more in every element of the output array `suma`.
 
Last edited by a moderator:
Look at my original reply, you are taking the difference between two extremely small numbers...
 
@Dr Transport It seems I am not able to clear my point. You said in your initial reply that erfc is very small for x>5 but here problem is that output array "suma" is getting larger and larger which means I am adding more in every successive element of output array "suma".
 
  • #10
I'll have to interrogate it further, kinda tied up right now...
 
  • #11
I had to rework the inner loop, that is the reason your suma is increasing. I remember that your inner loop on m was not set up correctly, I fixed that in my version. when I run it on my machine, suma hits 74.59 in about 2 iterations and doesn't change.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 15 ·
Replies
15
Views
1K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K