1
$\begingroup$

I'm trying to fit a function to data, but to make it go quicker I want to specify a starting point for Mathematica to start looking for the best fitting parameters. The equation I want to fit is:

SigmaBar[A_, Rs_][r_] :=  4 r NIntegrate[rho[A, Rs][u r] u^2, {u, 0, 1}] + 4 NIntegrate[rho[A, Rs][u] u/(u + Sqrt[u^2 - r^2]), {u, r, Infinity}]

where

 rho[A_, Rs_][r_] = (A 10^-3) r^(-1) (r + Rs)^(-2) Rs^(3);

I want to obtain starting values for two variables, $A$ and $R_s$. I have a list of values from my data for $\bar{\Sigma}$ and $r$, so I thought I could fill in two sets of those values (like $\bar{\Sigma}(r2)$, $r2$ and $\bar{\Sigma}(r4)$, $r4$), to solve for the $A$ and $R_s$. However, if I just use

Solve[{SigmaBar2 == SigmaBar[A, Rs][r2], SigmaBar4 == SigmaBar[A, Rs][r4]}, {A, Rs}]

I get an error saying that the integral in SigmaBar evaluated to non-numerical values (because A and Rs are not specified). Does anyone have an idea of how I can solve this?

$\endgroup$
4
  • 1
    $\begingroup$ I get an error saying that the integral in SigmaBar evaluated to non-numerical values (because A and Rs are not specified) That is correct. Does anyone have an idea of how I can solve this?. Yes. Give numerical values for A and Rs and r so that NIntegrate can work. Numerical integration can't integrate integrand which has unknown numerical values in them. Otherwise try Integrate instead and see if that will work. $\endgroup$
    – Nasser
    May 15, 2020 at 8:05
  • 1
    $\begingroup$ For example ClearAll[a,x]; NIntegrate[a*Sin[x], {x, -Pi, Pi}] will give same error you had because a has no numerical value. $\endgroup$
    – Nasser
    May 15, 2020 at 8:08
  • $\begingroup$ Yes, but the thing is that I want Mathematica to give me numerical values for A and Rs, based on the numerical values for SigmaBar and r, so I can't fill them in beforehand. $\endgroup$
    – Katja
    May 15, 2020 at 8:44
  • $\begingroup$ Change your function definitions to SigmaBar[A_?NumericQ, Rs_?NumericQ][r_?NumericQ] :=... and rho[A_?NumericQ, Rs_?NumericQ][ r_?NumericQ] = (A 10^-3) r^(-1) (r + Rs)^(-2) Rs^(3); $\endgroup$ May 15, 2020 at 9:08

1 Answer 1

5
$\begingroup$

Not a full answer, but a hint. You can get analytical solution for integrals with assumptions (fill in your special needs). This should be more easy to handle.

rhoA[A_, Rs_][r_] = (A 10^-3) r^(-1) (r + Rs)^(-2) Rs^(3);

Sig[A_, Rs_][r_] = 
    4 r Integrate[rhoA[A, Rs][u r] u^2, {u, 0, 1}, 
Assumptions -> 0 < A < 2 && 0 < Rs < 3 && 0 < r < 4] + 
    4 Integrate[
rhoA[A, Rs][u] u/(u + Sqrt[u^2 - r^2]), {u, r, Infinity}, 
Assumptions -> 0 < A < 2 && 0 < Rs < 3 && 0 < r < 4
 ]

(*   -((A Rs^3 (r + (r + Rs) Log[Rs/(r + Rs)]))/(250 r^2 (r + Rs))) + (
 A Rs^3 (r Sqrt[(r - Rs) (r + Rs)] + (r + 
   Rs) (Sqrt[r^2 - Rs^2] Log[r/(2 (r + Rs))] + 
   I Rs (Log[-((2 I r)/Sqrt[(r - Rs) (r + Rs)])] - 
      Log[2 - (2 I Rs)/Sqrt[r^2 - Rs^2]]))))/(
 250 r^2 Sqrt[r - Rs] (r + Rs)^(3/2))   *)
$\endgroup$
1
  • $\begingroup$ You get a simpler form if {A > 0, r > Rs > 0} $\endgroup$
    – Bob Hanlon
    May 16, 2020 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.