0
$\begingroup$

Thank you for your comments, I will discribe the question in more detail.

I have a piecewise function as below and I'm looking for the parameters :J0, C1, C2, a, Rp, R0, Z0, K, \[Delta], A

    J[R_, Z_, J0_, C1_, C2_, a_, Rp_, R0_, Z0_, K_, \[Delta]_, A_] = Piecewise[{
{J0 (1 - \[Rho]s[R, Z, a, Rp, R0, Z0, K, \[Delta], A]^2)^A, 0 <= \[Rho]s <= 0.7},
{J0 (C1 (1 - \[Rho]s[R, Z, a, Rp, R0, Z0, K, \[Delta], A])^2 + C2 (1 - \[Rho]s[R, Z, a, Rp, R0, Z0, K, \[Delta], A])^3), 0.7 < \[Rho]s <= 1},
{0, 1 < \[Rho]s}
    }];

And \[Rho]s[R, Z, a, Rp, R0, Z0, K, \[Delta], A] is shown as below:

x[R_, a_, Rp_] := (R - Rp)/a;
y[Z_, a_, Z0_] := (Z - Z0)/a;
\[Zeta][Z_, a_, Z0_, K_] := y[Z, a, Z0]/K;
\[Sigma][a_, Rp_, R0_] := (Rp - R0)/a;
\[Rho]s[R_, Z_, a_, Rp_, R0_, Z0_, K_, \[Delta]_, A_] :=
   1/(1 - \[Sigma][a, Rp, R0]^2) (((x[R, a, Rp] + \[Delta] \[Zeta][Z, a, 
           Z0, K]^2)^2 + (1 - A^2) \[Zeta][Z, a, 
         Z0, K]^2)^0.5 + \[Sigma][a, Rp, 
       R0] (x[R, a, Rp] + \[Delta] \[Zeta][Z, a, Z0, K]^2));

Then I want to time another function M and integrate it in range {R, 0.2, 1, d}, {Z, -1, 1, d} . Here d was set as 0.05 to reduce the amount of calculation.

Therefore, the code is

   Msum = 0;
   Do[{
   Msum = J[R, Z, J0, C1, C2, a, Rp, R0, Z0, K, \[Delta], A] d^2 CP[Rphi, Zphi, R, Z] + Msum;   
   }, {R, 0.2, 1, d}, {Z, -1, 1, d}];

Here the function CP[Rphi, Zphi, R, Z] is the response function of an unit Helmholtz current:

   CP[Rphi_, Zphi_, R_, Z_]:= 4 Pi 10^-7 (( 2 R Rphi)/Sqrt[(Zphi - Z)^2 + 
   (Rphi + R)^2](2/((4 R Rphi)/((Zphi - Z)^2 + (Rphi + R)^2)) 
   (EllipticK[((4 R Rphi)/((Zphi - Z)^2 + (Rphi + R)^2))] -  
   EllipticE[((4 R Rphi)/((Zphi - Z)^2 + (Rphi + R)^2))]) - 
   EllipticK[((4 R Rphi)/((Zphi - Z)^2 + (Rphi + R)^2))]));

Then the final function Msum is used for fitting the measured data. The measureddata, Rphi and Zphi is shown below.

Rphi https://drive.google.com/file/d/1PUB1A9ap_MqztbLtWcYCsTjrhuIjKuIA/view?usp=sharing

Zphi https://drive.google.com/file/d/1FXYvlQI2tYHwKfw2Xm_bV5cRiBHf_wOO/view?usp=sharing

measureddata https://drive.google.com/file/d/1I6E6vZhVUuH0l9pjltK0-LwGCij8-g8N/view?usp=sharing

I tried to use the FindFit :

data = Table[i + j, {i, 1, 64}, {j, 1, 3}];
Do[{
  data[[n, 1]] = Rphi[[n]] 10^-3; data[[n, 2]] = Zphi[[n]] 10^-3;  
  data[[n, 3]] = measureddata[[n]];
  }, {n, 1, 64}];
model = Msum;
FindFit[data, model, {J0, C1, C2, a, Rp, R0, Z0, K, \[Delta], A}, {Rphi, Zphi}

But it didn't work. Only FindFit[...1...] was shown in Out.

Then I tried to reduce number of parameters that only A is unknown. Additionally, the piecewise function was also simplified.

J0 = -500;
K = 1; 
\[Delta] = 0; 
Rp = 0.5;  
Z0 = 0.17; 
R0 = 0.6;  
a = 0.2;
J[R_, Z_, A_] = Piecewise[{
    {J0 (1 - \[Rho]s[R, Z, a, Rp, R0, Z0, K, \[Delta], A]^2)^A, 0 <= \[Rho]s <= 1},
    {0, 1 < \[Rho]s}
    }];

Here \[Delta] is ellipticity, hence the figure of \[Rho]s should be an eccentric circle.

However, FindFit:The Jacobian is not a matrix of real numbers at {A} = {1.}` was get.

What I'm confused is that the magnetidue of function\[Rho]s is decided on the parameters I'm looking for. I have no idea about how to conduct the fitting using such a piecewise function.

It will be appreciated if someone can help to improve this code. If the calculation take times you can reduce the number of parameters.

$\endgroup$
4
  • 1
    $\begingroup$ You ask, you don't explain. $\endgroup$
    – user64494
    Commented Jan 18, 2023 at 20:56
  • 1
    $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, $\endgroup$
    – Dunlop
    Commented Jan 19, 2023 at 4:58
  • 1
    $\begingroup$ 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – Dunlop
    Commented Jan 19, 2023 at 4:58
  • $\begingroup$ Without your data it is very difficult to be able to help you. What do you mean when you say the fits don't work? Did you get an error or did nothing happen? Please be more specific. $\endgroup$
    – Dunlop
    Commented Jan 19, 2023 at 5:00

0

Your Answer

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

Browse other questions tagged or ask your own question.