Hi @acer

First of all, thanks very much for taking the time to look into this... I always find your posts very interesting and useful.

Your code when works, seems to be as much as ~70 times faster (!!) on my machine and take as much as 1/50th of the memory of th than the version I posted.

A small problem is that for some n the code does not find the "right" number of solutions. I have ran into an identical issue when I was using fsolve with the same initial guess (as you're essentially doing) for all n. A solution was to either

1) not specify the initial guess and later sort for solutions in range

2) not specify the initial guess but specify an acceptable range of solutions (i.e. theta_n=-Pi/2..Pi/2)

3) use the last solution as the guess for the next one.

All three cases showed similar timing... and seemed to work.

So while this works:

**param_n:=1000:param_omega_a:=100e9:param_v:=1e8:param_x_l:=0.3:param_C_a:=20e-14: param_Z0:=50.0:**

**ans_new:=CodeTools:-Usage(new_get_theta_n_array(param_n,param_omega_a,param_v,param_x_l,param_C_a,param_Z0) ):**

**ans:=CodeTools:-Usage( get_theta_n_array(param_n,param_omega_a,param_v,param_x_l,param_C_a,param_Z0) ):**

**plots:-display([plots:-listplot(ans), plots:-listplot(ans_new, color=red, linestyle=dash)], thickness=2);**

Changing n to say 10 or 2000 does not:

**param_n:=2000:param_omega_a:=100e9:param_v:=1e8:param_x_l:=0.3:param_C_a:=20e-14: param_Z0:=50.0:**

**ans_new:=CodeTools:-Usage(new_get_theta_n_array(param_n,param_omega_a,param_v,param_x_l,param_C_a,param_Z0) ):**

**ans:=CodeTools:-Usage( get_theta_n_array(param_n,param_omega_a,param_v,param_x_l,param_C_a,param_Z0) ):**

**plots:-display([plots:-listplot(ans), plots:-listplot(ans_new, color=red, linestyle=dash)], thickness=2);**

As far as parameters go, at least right now when I'm trying to optimize over what "works best" for the rest of the problem, I do have to be sure that whatever I'm using is robust for a huge range of paramters... Although the parameters I listed are "good candidates".

**param_n:=1000: #can vary from 100 to thousands (in principle)**

**param_omega_a:=100e9: #can vary between 1e9 and 5000e9 (likely a few hundred 1e9)**

**param_v:=1e8:param_x_l:=0.3: #can vary between 0.05 and 1**

**param_C_a:=20e-14: # can vary between 1e-14 1000e-14**

**param_Z0:=50.0: #fixed**

If you have any ideas to make your code robust against a wide range parameters (especially n), please let me know.

thanks again!

**EDIT: **

I should note that playing with the initial guess and the number of evalutations of F(n) does not seem to help. Also, I've been playing more with Mac Dude's idea above (see get_theta_n_array3) and that does seem to be a factor of a "few" faster than my original code.