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.