MATLAB: Hyperbolic Functions series convergence issue

hyperbolic function series convergence

I'am trying to run a code which has attached with this i'am getting answer as NAN i think the reason behind this is because of term e in this it contains sinh terms as n increases sinh values increases but if i keep x=0 then also it is coming NAN if you see the equation as x=0 then e will be 2*B1*sin(k2*(di+dd-y))/di which i have written as uu in code now after convergence it comes to 1.002(Vbi) which is correct by physics
but again e term is coming NAN because of this phi is coming NAN same goes to x=L or any value of 0<x<L
see the code below
how this can be remove ?

Best Answer

You have
L=120e-9;
[...]
di=5e-9;
[...]
k2=(2*n-1)*pi/(2*di)
[...]
e=e+2*sin(k2*(di+dd-y))*(B1*sinh(k2*(L-x))+B2*sinh(k2*x))/(di*sinh(k2*L));
With di being 5E-9, 2*di is 1E-8, so k2 = 1E8 * (2*n-1) * pi . Multiply that by L = 1.2E-7 and you get 12 * (2*n-1) * pi . When you reach n = 10, that leads you to calculating sinh(12 * 19 * pi), which overflows to infinity. That happens on both the numerator and denominator, leading you to infinity / infinity, which is NaN. That NaN then pollutes the rest of the calculation.
By the time of the overflow, you are probably pretty safe making some approximations on the (B1*sinh(k2*(L-x))+B2*sinh(k2*x))/(di*sinh(k2*L)) . For example you could taylor it, and then get rid of the elements that are going to have no effect. For example,
exp((1199/25)*n*Pi-(1199/50)*Pi)+exp((24*n-12)*Pi)-exp((599/25)*n*Pi-(599/50)*Pi)-1
is completely dominated by the first term, so you can reduce the complexity of the taylored expression.