Group Abstract Group Abstract

Message Boards Message Boards

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

Posted 3 years ago
POSTED BY: Magdy Ismail
4 Replies
Posted 3 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
Posted 3 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 3 years ago

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

POSTED BY: Magdy Ismail
Posted 3 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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard