0
$\begingroup$
μ = 0.15;
λ = 0.4;
ρ = 1;
r = 0.04;
i = 3;
θ = 1;
κ = 0.1;
σ = 0.15;
γ[κ_, μ_, λ_, ρ_, σ_, r_] = 
  (2 κ - 2 μ + 2 λ ρ σ + σ^2 + Sqrt[8 r σ^2 + (-2 κ + 2 μ - 2 λ ρ σ - σ^2)^2])/(2 σ^2);
b[κ_, μ_, λ_, ρ_, σ_, r_] = 
  2 - 2 γ[κ, μ, λ, ρ, σ, r] + 2 (κ - μ + λ ρ σ)/σ^2;
B[κ_, μ_, λ_, ρ_, σ_, r_] = 
  -Gamma[1 - γ[κ, μ, λ, ρ, σ, r] - b[κ, μ, λ, ρ, σ, r]]*
   Gamma[b[κ, μ, λ, ρ, σ, r]]/
   Gamma[-γ[κ, μ, λ, ρ, σ, r]]/
   Gamma[2 - b[κ, μ, λ, ρ, σ, r]];
h[V] = Hypergeometric1F1[-γ[κ, μ, λ, ρ, σ, r], b[κ, μ, λ, ρ, σ, r], (2 κ θ)/(σ^2 V)];
g[V] = 
  Hypergeometric1F1[
    1 - γ[κ, μ, λ, ρ, σ, r] - b[κ, μ, λ, ρ, σ, r], 2 - b[κ, μ, λ, ρ, σ, r], 
    (2 κ θ)/(σ^2 V)];
F[A, V] = 
  A*(h[V] + B[κ, μ, λ, ρ, σ, r]*((2 κ θ)/(σ^2 V))^(1 - b[κ, μ, λ, ρ, σ, r])*g[V])*
  V^γ[κ, μ, λ, ρ, σ, r];
x = V /. FindRoot[{F[A, V] == V - i, D[F[A, V], V] == 1}, {A, 0.01}, {V, 1.01}]

I think this should be an easy question, but I am new to Mathematica and can not figure it out.

Variable x is what I would like to obtain, and given the parameters values at the beginning of the code, the value of x can be found. It should be 6.3632.

Here is the question: how can I change the code and establish a loop to evaluate the values of x with σ taking different values, like ranging from 0.1 to 0.2 in step of 0.01. Thus, σ is no longer a constant input as presented in my code but a iterating variable. Also, if it is convenient, how can I plot x against the values of σ?

$\endgroup$
  • $\begingroup$ Do you really need to introduce x at all? Don't you really want V as a function of σ? $\endgroup$ – m_goldberg Jul 4 '13 at 14:27
  • $\begingroup$ values = Table[V /. ...., {\[Sigma], 0.1, 0.2, 0.01}] is a good way to get a list of values, but you may need to define your 'functions' using :=... $\endgroup$ – cormullion Jul 4 '13 at 14:30
  • $\begingroup$ Another piece of advice, rewrite your auxiliary expression as functions of σ. For example; With[{κ = κ, μ = μ, ..., r = r}, b[σ_] := 2 - 2 γ[κ, μ, λ, ρ, σ, r] + 2 (κ - μ + λ ρ σ)/σ^2 $\endgroup$ – m_goldberg Jul 4 '13 at 14:37
1
$\begingroup$

Easiest way -> take out $\sigma$, put everything in a Table and use $\sigma$ as the loop variable. It will make a Table of {$\sigma$,x}.

Table[μ = 0.15;
λ = 0.4;
ρ = 1;
r = 0.04;
i = 3;
θ = 1;
κ = 0.1;
                 (*no sigma*)  
γ[κ_, μ_, λ_, ρ_, σ_, r_] = (2 κ - 2 μ + 2 λ ρ σ + σ^2 + Sqrt[8 r σ^2 + (-2 κ + 2 μ - 2 λ ρ σ - σ^2)^2])/(2 \σ^2);

b[κ_, μ_, λ_, ρ_, σ_, r_] = 2 - 2 γ[κ, μ, λ, ρ, σ, r] + 2 (κ - μ + λ ρ σ)/σ^2;

B[κ_, μ_, λ_, ρ_, σ_,r_] = -Gamma[1 - γ[κ, μ, λ, ρ, σ, r] - b[κ, μ, λ, ρ, σ, r]]*Gamma[b[κ, μ, λ, ρ, σ, r]]/Gamma[-γ[κ, μ, λ, ρ, σ, r]]/Gamma[2 - b[κ, μ, λ, ρ, σ, r]];

h[V] = Hypergeometric1F1[-γ[κ, μ, λ, ρ, σ, r],    b[κ, μ, λ, ρ, σ,r], (2 κ θ)/(σ^2 V)];

g[V] = Hypergeometric1F1[1 - γ[κ, μ, λ, ρ, σ,r] - b[κ, μ, λ, ρ, σ, r], 2 - b[κ, μ, λ, ρ, σ, r], (2 κ θ)/(σ^2 V)];

F[A, V] = A*(h[V] + B[κ, μ, λ, ρ, σ, r]*((2 κ θ)/(σ^2 V))^(1 - b[κ, μ, λ, ρ, σ, r])*g[V])*   V^γ[κ, μ, λ, ρ, σ, r];

x = V /.FindRoot[{F[A, V] == V - i, D[F[A, V], V] == 1}, {A, 0.01}, {V, 1.01}], {σ, 0.1, 0.2, 0.01}];

ListPlot[%]

Another way is to define x as a function of $\sigma$. In that way you can use x[$\sigma$] in your calculation.

μ = 0.15;
λ = 0.4;
ρ = 1;
r = 0.04;
i = 3;
θ = 1;
κ = 0.1;

γ[κ_, μ_, λ_, ρ_, σ_, r_]:= (2 κ - 2 μ + 2 λ ρ σ + σ^2 + Sqrt[8 r σ^2 + (-2 κ + 2 μ - 2 λ ρ σ - σ^2)^2])/(2 \σ^2);

b[κ_, μ_, λ_, ρ_, σ_, r_]:= 2 - 2 γ[κ, μ, λ, ρ, σ, r] + 2 (κ - μ + λ ρ σ)/σ^2;

B[κ_, μ_, λ_, ρ_, σ_,r_] := -Gamma[1 - γ[κ, μ, λ, ρ, σ, r] - b[κ, μ, λ, ρ, σ, r]]*Gamma[b[κ, μ, λ, ρ, σ, r]]/Gamma[-γ[κ, μ, λ, ρ, σ, r]]/Gamma[2 - b[κ, μ, λ, ρ, σ, r]];

h[V_, σ_] := Hypergeometric1F1[-γ[κ, μ, λ, ρ, σ, r],    b[κ, μ, λ, ρ, σ,r], (2 κ θ)/(σ^2 V)];

g[V_, σ_] := Hypergeometric1F1[1 - γ[κ, μ, λ, ρ, σ,r] - b[κ, μ, λ, ρ, σ, r], 2 - b[κ, μ, λ, ρ, σ, r], (2 κ θ)/(σ^2 V)];

F[A_, V_, σ_] = A*(h[V, σ] + B[κ, μ, λ, ρ, σ, r]*((2 κ θ)/(σ^2 V))^(1 - b[κ, μ, λ, ρ, σ, r])*g[V, σ])*   V^γ[κ, μ, λ, ρ, σ, r];

x[σ_] := V /.FindRoot[{F[A, V, σ] == V - i, D[F[A, V, σ], V] == 1}, {A, 0.01}, {V, 1.01}];

Now

Plot[x[σ],{σ,0.1,0.2}]

Or put it in a table.

Notebook File

Considering the length, I don't feel very confident about my typing. You can use this Nick.nb file if you face any problem in executing what I have written above.

$\endgroup$
0
$\begingroup$

You could try this:

ClearAll["Global`*"]
μ = 0.15;
λ = 0.4;
ρ = 1;
r = 0.04;
i = 3;
θ = 1;
κ = 0.1;
γ[κ_, μ_, λ_, ρ_, σ_, r_] =
  (2 κ - 2 μ + 2 λ ρ σ + σ^2 + Sqrt[8 r σ^2 + (-2 κ + 2 μ - 2 λ ρ σ - σ^2)^2])/(2 σ^2);
b[κ_, μ_, λ_, ρ_, σ_, r_] = 
  2 - 2 γ[κ, μ, λ, ρ, σ, r] + 2 (κ - μ + λ ρ σ)/σ^2;
B[κ_, μ_, λ_, ρ_, σ_, r_] =
  -Gamma[1 - γ[κ, μ, λ, ρ, σ, r] - b[κ, μ, λ, ρ, σ, r]]*
  Gamma[b[κ, μ, λ, ρ, σ, r]]/Gamma[-γ[κ, μ, λ, ρ, σ, r]]/Gamma[
  2 - b[κ, μ, λ, ρ, σ, r]];
h[V] =
  Hypergeometric1F1[-γ[κ, μ, λ, ρ, σ, r], b[κ, μ, λ, ρ, σ, r], (2 κ θ)/(σ^2 V)];
g[V] =
  Hypergeometric1F1[1 - γ[κ, μ, λ, ρ, σ, r] - b[κ, μ, λ, ρ, σ, r], 2 - b[κ, μ, λ, ρ, σ, 
  r], (2 κ θ)/(σ^2 V)];
F[A, V] =
  A*(h[V] + B[κ, μ, λ, ρ, σ, r]*((2 κ θ)/(σ^2 V))^(1 - b[κ, μ, λ, ρ, σ, r])*g[V])*
  V^γ[κ, μ, λ, ρ, σ, r];

See that I've defined all variables, expect σ.

Now you can solve it through this command:

x = V /. FindRoot[{F[A, V] == V - i /. σ -> #, 
      D[F[A, V], V] == 1 /. σ -> #}, {A, 0.01}, {V, 1.01}] & /@ Range[.1, .2, .01]

{8.65089, 7.9194, 7.36981, 6.94866, 6.62103, 6.3632, 6.15855, 5.99512, 5.86412, 5.75898, 5.67468}

Now you can plot it:

ListLinePlot[x, PlotRange -> All]

enter image description here

$\endgroup$

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.