3
$\begingroup$

I try to solve an equation. Normally, Mathematica code works but the problem is that it takes a huge amount of time.

The code I use is :

paramFinal2 = {ρ -> 0.05, γ -> 0.5, ω -> 0.8, ψ -> 15.5, α -> 0.3, β -> 0.06, λ -> 0.7, 
   Subscript[γ, 2] -> -0.6, Subscript[γ, 1] -> 0.9, η -> 4.25, χ -> 2, μ -> 0.1};

and

{power} = {1/(1 - α) (1 + Subscript[γ, 2]/Subscript[γ, 1] ) - 1 /. paramFinal2}
{power1} = {1 + Subscript[γ, 2]/Subscript[γ, 1]  η /. paramFinal2}

I have an integral to solve ;

solint[i_] := Integrate[(a^power/(1 + i/χ a^power1)^(α/(1 - α))), {a, 0, 1}] /. paramFinal2

The equation I try to solve is

msolres1[n_,i_] := (ρ + θ) + λ n - ((1 - n) (1 - ω) ψ θ)/(λ (1 - α)) - 
    (α Subscript[γ, 1])/(λ (1 - Subscript[γ, 1]) ) ((1 - n)/solint[i]) 
    ((χ + i)^(-(α/(1 - α)))/χ^(α/(1 - α)))

And finally, in order to have the results ;

solres1[i_] := NSolve[msolres1[n, i] /. paramFinal2, n] 
tabres1 = Table[solres1[i], {i, 0.01, 1.5, 0.1}] /. θ -> 0.01
solutionres1 = solres1[i];

How can I modify this code in a more efficient way to have results in a quicker way?

$\endgroup$
4
$\begingroup$

It seems that you can calculate the integral and solutions to the equation in your problem symbolically, so you don't need to compute them over and over numerically for every value of $i$.

Your assignments to power and power2 seem to contain extraneous lists as well, which I removed below.

paramFinal2 = {ρ -> 5/100, γ -> 5/10, ω -> 8/10, ψ -> 155/10, α -> 3/10, β -> 6/100, 
    λ -> 7/10, Subscript[γ, 2] -> -6/10, Subscript[γ, 1] -> 9/10, η -> 425/100, 
    χ -> 2, μ -> 1/10};
power = (1/(1 - α) (1 + Subscript[γ, 2]/Subscript[γ, 1]) - 1) /. paramFinal2
power1 = (1 + Subscript[γ, 2]/Subscript[γ, 1] η) /. paramFinal2

The following are more substantial changes. Notice that the value of solint is assigned imemdiately, and not through SetDelayed (:=), so as to avoid recalculations.

solint[i_] = With[
   {integrand = (a^power/(1 + i/χ a^power1)^(α/(1 - α)) /. paramFinal2)},
   Integrate[integrand, {a, 0, 1}, Assumptions -> 0.01 <= i <= 1.5]
];

Without assumptions, Integrate would generate a conditional expression; this can be avoided by explicitly providing assumptions specifying that $i$ is real and non-zero.

msolres1[n_, i_] := (ρ + θ) + λ n - ((1 - n) (1 - ω) ψ θ)/(λ (1 - α)) -
   (α Subscript[γ, 1])/(λ (1 - Subscript[γ, 1])) ((1 - n) / solint[i]) *
   ((χ + i)^(-(α/(1 - α)))/χ^(α/(1 - α)))

solres1 = Solve[msolres1[n, i] == 0, n] /. paramFinal2 /. θ -> 0.01;
tabres1 = Table[solres1, {i, 0.01, 1.5, 0.1}]

(* Out:
{{{n -> 0.624808}}, {{n -> 0.672363}}, {{n -> 0.689797}}, {{n -> 0.700922}}, 
 {{n -> 0.709031}}, {{n -> 0.715345}}, {{n -> 0.720463}}, {{n -> 0.724729}}, 
 {{n -> 0.728358}}, {{n -> 0.731495}}, {{n -> 0.734239}}, {{n -> 0.736666}}, 
 {{n -> 0.73883}}, {{n -> 0.740774}}, {{n -> 0.742532}}}
*)

The calculation now only takes a few seconds.

$\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.