1
$\begingroup$

I have written a code. I want to change y from (*10^-4 to 10^-6) and want to find the corresponding minimum and then need to export the value of y and corresponding minimum in an excel sheet. I know that probably this is very trivial. But any help will be highly appreciated.

   y = 5*10^-8;
S = ( (3*10^14 - y/(4*10^-12) - x^2) + (I*x (7*10^7)))/(3*10^14 - y/(
     4*10^-12) - x^2 - (I*x*(9*10^7)));
U = ( Abs[S])^2;
minim = x /. Last[FindMinimum[U, {x, 5.8 10^7}]]
$\endgroup$
4
  • $\begingroup$ See Table and lookup Export/xls. $\endgroup$ – Kuba Apr 26 '18 at 20:16
  • $\begingroup$ @Kuba: Thanks for your reply. But I also need to increment my "y" and find the minimum corresponding to that "y". So how can be that done simultaneously? $\endgroup$ – Raj Apr 26 '18 at 20:23
  • $\begingroup$ Why not with Table? $\endgroup$ – Kuba Apr 26 '18 at 20:24
  • $\begingroup$ @Kuba:Table[x /. Last[FindMinimum[U, {x, 5.8 10^7}]], {y, 5*10^-9, 5*10^-7, 1.98*10^-8}]. I tried something like this $\endgroup$ – Raj Apr 26 '18 at 21:37
2
$\begingroup$

You could do it like this:

We build a function f that has both x and y as arguments (don't assign a value to y). Next we define the function minimum in y that returns the corresponding minimal point x. Afterwards, we use Table to do the calculations for various y = 10^z. I use 10^z in order to get a nicer stepping. Finally, we add some column titles with Join and export everything to the xls file "a.xls". The file should appear in Directory[]. Of course, you can specify another file name and a whole path as first argument of Export.

ClearAll[y];
S = ((3 10^14 - y/(4 10^-12) - x^2) + (I x (7 10^7)))/(3 10^14 - y/(4 10^-12) - x^2 - (I x (9 10^7)));
U = (Abs[S])^2;
f = {x, y} \[Function] Evaluate[U // ComplexExpand // Simplify];
minimum = y \[Function] x /. Last[FindMinimum[f[x, y], {x, 58 10^6}]];
data = Table[{10^z, minimum[10^z]}, {z, -6, -4, 1/100}];
Export["a.xls", Join[{{"y", "minimum[y]"}}, data]]
$\endgroup$
8
  • $\begingroup$ I tried this in the real program (I just made up the numbers in the above program). Once I run it, it's giving me an output error which tells me that "encountered a gradient that is effectively zero. The result returned may not be maximum. It may be a maximum or a saddle point". $\endgroup$ – Raj Apr 26 '18 at 22:16
  • $\begingroup$ Yes, I also observed that. You can use minimum = y \[Function] Evaluate[x /. Last[Simplify[Minimize[f[x, y], x], 10^-6 <= y <= 10^-4]]] instead. This utilizes that this is one of the rare cases which as analytical solutions. $\endgroup$ – Henrik Schumacher Apr 26 '18 at 22:21
  • $\begingroup$ Thanks for the reply. I tried what you said.That doesn't seem to work. My step size is actually very small (of the order of 10^-8). $\endgroup$ – Raj Apr 27 '18 at 15:26
  • $\begingroup$ So, what exactly went wrong? It works on my machine, you know. Of course you have to adjust the step size in Table. You can also use data = Table[{y, minimum[y]}, {z, 10^-6,10^ -4, 10^-8}];. $\endgroup$ – Henrik Schumacher Apr 27 '18 at 15:28
  • $\begingroup$ My real code is as follows.a = 300*10^-6; n = 1.44; c = 3*10^8; [Lambda] = 1550*10^-9; m = 0.96;p = (2/m - 1) + Sqrt[(2/m - 1)^2 - 1] ; Q1 = 8*10^6; Q2 = 1.98*10^8; [CapitalKappa]1 = ([Pi]*c)/([Lambda]*Q1); [CapitalKappa]2 = ([Pi]*c)/([Lambda]*Q2); [Tau] = (2*[Pi]*a*n)/c; T1 = (2*[CapitalKappa]1*[Tau])/(1 + 1/p); $\endgroup$ – Raj Apr 27 '18 at 15:37

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.