Message Boards Message Boards

Making a variable an input to a function to change its value?

Posted 2 years ago

I would like to put random values like s = 0.1, 0.7, 0.4, 0.15, and 0.18 to get the output results which are ( X and Rn ) for these values in different Excel files or in the same Excel file, instead of putting them manually for each single code. How can I do that? Because i will have a lot of s values so basically the only that changes is s which gives different solutions in each case, Kindly help if you can

ClearAll["Global"]
ClearAll[My`Print]
ClearAll[X1]
SetAttributes[My`Print, HoldAll]
My`Print[args___] := 
 Do[Print[Extract[Hold[args], i, HoldForm], "=", {args}[[i]]], {i, 
   Length[{args}]}]
s = 0.15; c = 2; k = (1/2)*(1 - a^2)*s^2; h = 10; n = 0.1; a = 
 n/(1 - n); 
p = -((Sqrt[(a*(1 - c)*y - 1)^2 - 2*(1 - 2*c)*(-(a*y) - k + y^2/2)] + 
       a*(1 - c)*y - 1)/(1 - 2*c)); 
f := (-(c*p + 1)^(-(1/c)))*(c*y + 1)^((1 - c)/c); NIntegrate[f, {y, 
  s, -s}]; 
\[Rho] = h/
  NIntegrate[
   f, {y, s, -s}]; rn = \[Rho]*(c*s + 1)^(1/c); rp = \[Rho]*(1 - 
     c*s)^(1/c); 
\[Delta] = rp - rn; My`Print[\[Rho], rn, rp, \[Delta]]
g = 
     -(1 - (c*(Sqrt[((a*(1 - c)*((y/\[Rho])^c - 1))/c - 1)^2 - 
             2*(1 - 2*c)*(-((a*((y/\[Rho])^c - 1))/c) + 
                             (1/2)*(((y/\[Rho])^c - 1)/c)^2 - 
                k)] + (a*(1 - c)*((y/\[Rho])^c - 1))/c - 1))/(1 - 
         2*c))^(-c^(-1)); 
R = 20; X = ({#1, 
     NIntegrate[g, {y, rn, (#1*\[Delta])/R + rn}] - h/2} & ) /@ 
  Range[R]; 
Rn = ({#1, (#1*\[Delta])/R + rn} & ) /@ Range[R]; 
XvsRn = Transpose[{Last /@ X, Last /@ Rn}]
ListLinePlot[(Callout[#1, Round[#1, 0.001] -> Point] & ) /@ XvsRn, 
 Mesh -> All, 
   MeshStyle -> Directive[PointSize[Medium], Red], 
 FrameLabel -> (Style[#1, Black, 14] & ) /@ {"X", "Rn"}, 
 ImageSize -> 600, 
   PlotTheme -> "Detailed"]
(Export["mfile1.xlsx", #1] & )[Prepend[{"X", "Rn"}][XvsRn]]
POSTED BY: Magdy Ismail
4 Replies
Posted 2 years ago

Wrap your code in a function that takes s as an argument

xrn[s_] := 
 Module[{c = 2, h = 10, n = 0.1, a, k, p, f, \[Rho], rn, rp, \[Delta], g, R, Rn, X},
  a = n/(1 - n);
  k = (1/2)*(1 - a^2)*s^2; 
  p = -((Sqrt[(a*(1 - c)*y - 1)^2 - 
          2*(1 - 2*c)*(-(a*y) - k + y^2/2)] + a*(1 - c)*y - 1)/(1 - 
        2*c)); f := (-(c*p + 1)^(-(1/c)))*(c*y + 1)^((1 - c)/c); 
  NIntegrate[f, {y, s, -s}]; \[Rho] = h/NIntegrate[f, {y, s, -s}]; 
  rn = \[Rho]*(c*s + 1)^(1/c); 
  rp = \[Rho]*(1 - c*s)^(1/c); \[Delta] = rp - rn; 
  g = -(1 - (c*(Sqrt[((a*(1 - c)*((y/\[Rho])^c - 1))/c - 1)^2 - 
              2*(1 - 
                 2*
                  c)*(-((a*((y/\[Rho])^c - 1))/c) + (1/
                    2)*(((y/\[Rho])^c - 1)/c)^2 - k)] + (a*(1 - 
                 c)*((y/\[Rho])^c - 1))/c - 1))/(1 - 2*c))^(-c^(-1)); 
  R = 20;
  X = ({#1, NIntegrate[g, {y, rn, (#1*\[Delta])/R + rn}] - h/2} &) /@ 
    Range[R]; Rn = ({#1, (#1*\[Delta])/R + rn} &) /@ Range[R]; 
  Transpose[{Last /@ X, Last /@ Rn}]]

Verify that the function returns the right result for s=0.15

xrn[0.15]

Use Table to generate results for various values of s

Table[xrn[s] // Prepend[s], {s, { 0.1, 0.7, 0.4, 0.15, 0.18}}]

Note that for s=0.7 the result is complex so it cannot be plotted with ListLinePlot.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thank you so much you solved my problem. I really appreciate your effort.

POSTED BY: Magdy Ismail
Posted 2 years ago

One more question. Can I export to the same Excel file values of (Delta, Rho, rn, rp) that are associated with each value of s if possible? How can I do that please?

POSTED BY: Magdy Ismail
Posted 2 years ago

You can return any values you want from the function e.g. Change the last line to

{s, \[Delta], \[Rho], rn, rp} // Append[Transpose[{Last /@ X, Last /@ Rn}]

Then

Table[xrn[s], {s, {0.1, 0.7, 0.4, 0.15, 0.18}}]

As far as exporting to Excel, since the result includes a list of pairs {X, Rn} you will have to decide how you want that to be represented in the file.

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract