0
$\begingroup$

I have solved a set of equations for a specific given data point and restricted my solutions to 100. then I did TrimmedMean to have the mean value of all 100 solutions. I need to do the same for different given data points which are all in an excel file but don't know how I put them in a loop to get 100 solutions for each set of given data.
I used the first row of this table as given data:

f             Rp    Tp
1.8*10^(12) 0.133   0.601
1.9*10^(12) 0.127   0.608
2.0*10^(12) 0.121   0.612
2.4*10^(12) 0.113   0.617 

and this is my code for the first row:

d = 2/100;
f = 1.8*10^(12);
c = 3*10^(8);
lambda = c/f;
alpha = Exp[-2*Pi*k*d/lambda];
delta = 4*Pi*n*d/lambda;
R = ((n - 1)^2 + k^2)/((n + 1)^2 + k^2);
Rp = 0.133;
Tp = 0.601;

pts = Table[
   FindRoot[{
      R (1 + (alpha)^4 - 2*(alpha)^2*Cos[delta])/(1 - 2*(alpha)^2*R*Cos[delta] + (alpha)^4*R^2) == Rp,
      ((alpha)^2*(1 - R)^2)/(1 - 2*(alpha)^2*R*Cos[delta] + (alpha)^4*R^2) == Tp
     },
    {n, RandomReal[{1.5, 1.7}]}, {k, RandomReal[{0, 0.02}]}],
  {1000}];
TrimmedMean[n /. pts]
TrimmedMean[k /. pts]

I need to do the same for all other rows and finally plot all Trimmed n's and k's.

$\endgroup$
1
$\begingroup$

Wrap your code into a function that takes {f,Rp,tp} as an argument, outputs your trimmed values and then use Map or maybe ParallelMap to evaluate this function for all input values. Use Import with proper settings to get a matrix where {f,Rp,tp} are stored in the rows. How to do this is for instance shown in this HowTo

ClearAll[trim,vals];
(* let list be of the form {f,Rp,Tp} *)
trim[list_] :=
  Module[
   {d, f, c, alpha, delta, lambda, k, n, Tp, Rp, R, pts},
   f = list[[1]];
   Rp = list[[2]];
   Tp = list[[3]];
   d = 2/100;
   f = 1.8*10^(12);
   c = 3*10^(8);
   lambda = c/f;
   alpha = Exp[-2*Pi*k*d/lambda];
   delta = 4*Pi*n*d/lambda;
   R = ((n - 1)^2 + k^2)/((n + 1)^2 + k^2);
   pts = Table[
     FindRoot[{R (1 + (alpha)^4 - 2*(alpha)^2*Cos[delta])/(1 - 
            2*(alpha)^2*R*Cos[delta] + (alpha)^4*R^2) == 
        Rp, ((alpha)^2*(1 - R)^2)/(1 - 
           2*(alpha)^2*R*Cos[delta] + (alpha)^4*R^2) == Tp}, {n, 
       RandomReal[{1.5, 1.7}]}, {k, RandomReal[{0, 0.02}]}], {1000}];
   {TrimmedMean[n /. pts], TrimmedMean[k /. pts]}
   ];

I have manually rewritten your input data (as said, use Import to get it as a matrix):

vals = {{1.8*10^(12), 0.133, 0.601}, {1.9*10^(12), 0.127, 0.608}, {2.0*10^(12), 0.121, 0.612}, {2.4*10^(12), 0.113, 0.617}};

Now use Map (/@) to get the desired output:

trim[#] & /@ vals
(* {{1.62331, 0.000223783}, {1.61242, 0.000221324}, {1.60573, 0.000221825}, {1.60296, 0.000222526}} *)

Please note that there is a bunch of warnings. However, this approach does what you requested. If you have a very big amount of input settings it might indeed be worth to use ParallelMap to gain some speedup.

Remark about Import and ParallelTable

From your OP I guess that your .xlsx file looks like this

enter image description here

Then you can Import your data as follows:

vals = Import["/path/to/file.xlsx"][[1, 2 ;;]]
(* {{1., 2., 3.}, {4., 5., 6.}, {7., 8., 9.}} *)

where the Part [[1,2;;]] mainly removes the headline which labels your columns. With that you are able to use the code presented above. As already mentioned in my comment, you may want to try ParallelMap[trim[#]&,vals] as well and check its speed (AbsoluteTiming[]) compared to the single-kernel Map approach that I have used above (for enough input data it might improve speed).

$\endgroup$
  • $\begingroup$ Thanks a lot for your help. It helped me a lot. As I have a very big excel file to import, you mentioned it's better use ParallelMap instead, but don't know how use it! $\endgroup$ – Ela Mar 8 '16 at 22:21
  • $\begingroup$ @Ela Just run ParallelMap[trim[#]&,vals]. Use AbsoluteTiming to see how fast both versions are on your machine. Also, as you mention that my answer helped you a lot, you might consider upvoting/accepting it $\endgroup$ – Lukas Mar 8 '16 at 23:12
  • $\begingroup$ I tried to upvote but it's saying I need to earn 15 reputation then my vote will be considered. In this code whenever I manually input data, it perfectly works out but when I use:vals=Import["myexcelfile.xlsx"] it doesn't. It perfectly reads my excel file and output is like {f,Rp,Tp} but doesn't work in this form! $\endgroup$ – Ela Mar 8 '16 at 23:28
  • $\begingroup$ I tried to upvote but it's saying I need to earn 15 reputation then my vote will be considered. In this code whenever I manually input data, it perfectly works out but when I use:vals=Import["myexcelfile.xlsx"] it doesn't. It perfectly reads my excel file and output is like {f,Rp,Tp} but doesn't work in this form! $\endgroup$ – Ela Mar 9 '16 at 3:27
  • $\begingroup$ @Ela Please see my updated answer. Does it now explain everything you need? $\endgroup$ – Lukas Mar 9 '16 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.