Implementing double series in MATLAB

In summary, Jason's code calculates the sum of two numbers, but the values of e1 and e2 always remain zero. He is not able to solve this problem.
  • #1
Atr cheema
69
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
  • #2
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
 
  • #3
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.
 
  • #4
First of all, you have some code errors in there, you are not calculating what you posted. secondly, the erfc function is [itex] ~10^{-100} [/itex] for [itex] x > 5 [/itex] 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 Atr cheema
  • #5
@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:
  • #6
e2Arg: the denominator should have a factor of 2 not 1 and this will run faster if you use vectors not loops...
 
  • #7
@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:
  • #8
Look at my original reply, you are taking the difference between two extremely small numbers...
 
  • #9
@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.
 

Related to Implementing double series in MATLAB

1. How do I define a double series in MATLAB?

To define a double series in MATLAB, you can use the "symsum" function. This function allows you to specify the variables, starting and ending values, and the expression for the series. For example, to define a double series for the expression (x^2 + y^2)/n^2, you can use the following code: symsum((x^2 + y^2)/n^2, n, 1, Inf).

2. Can I use nested loops to implement a double series in MATLAB?

Yes, you can use nested loops to implement a double series in MATLAB. However, this approach may not be as efficient as using the "symsum" function. It is recommended to use the "symsum" function for more complex double series.

3. How can I plot a double series in MATLAB?

To plot a double series in MATLAB, you can use the "ezsurf" function. This function allows you to specify the expression for the series and the range of values for the variables. For example, to plot the double series for the expression (x^2 + y^2)/n^2, you can use the following code: ezsurf((x^2 + y^2)/n^2, [-10, 10]).

4. Can I use symbolic variables in a double series in MATLAB?

Yes, you can use symbolic variables in a double series in MATLAB. This allows you to work with the variables as algebraic expressions instead of numerical values. You can use the "syms" function to define symbolic variables and then use them in your double series expression.

5. How do I evaluate a double series in MATLAB?

To evaluate a double series in MATLAB, you can use the "vpa" function. This function allows you to specify the expression for the series and the precision of the result. For example, to evaluate the series for the expression (x^2 + y^2)/n^2 at x = 2, y = 3 with a precision of 5 decimal places, you can use the following code: vpa((x^2 + y^2)/n^2, 5).

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
978
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
Back
Top