-1
$\begingroup$
rData={{5.3571429,0.096854535},{10.714286,0.055104186},{16.071429,0.042811499},{21.428571,0.024825886},{26.785714,0.023279183},{32.142857,0.016328542},{37.5,0.0092185037},{42.857143,0.0075624777},{48.214286,0.0023514323},{53.571429,0.001637045},{58.928571,-0.0024887011},{64.285714,-0.0034741333},{69.642857,-0.0056340032},{75,-0.0040906991},{80.357143,-0.0039738424},{85.714286,-0.0044593789},{91.071429,-0.0054884315},{96.428571,-0.0037277341},{101.78571,-0.0041691748},{107.14286,-0.0039292558},{112.5,-0.0037408923},{117.85714,-0.0040700255},{123.21429,-0.0028904555},{128.57143,-0.0022557232},{133.92857,-0.0020756487},{139.28571,-0.0020739949},{144.64286,-0.0015149035},{150,-0.0019796368},{155.35714,-0.00068430865},{160.71429,-0.00060721168},{166.07143,-0.00055972397},{171.42857,-0.0011788755},{176.78571,-0.00090675531},{182.14286,-0.00060012026},{187.5,7.6071311*10^-6}};

tData={{5.3571429,0.081473653},{10.714286,-0.0076210718},{16.071429,-0.038565046},{21.428571,-0.014000405},{26.785714,-0.042161254},{32.142857,-0.071404281},{37.5,-0.066992712},{42.857143,-0.031355057},{48.214286,-0.02043848},{53.571429,-0.025259291},{58.928571,-0.019615094},{64.285714,-0.015185751},{69.642857,-0.012213914},{75,-0.0047624032},{80.357143,-0.00041652762},{85.714286,0.0028162852},{91.071429,0.00979253},{96.428571,0.0080315783},{101.78571,0.0034739882},{107.14286,0.0021786814},{112.5,0.0043349925},{117.85714,0.0053397331},{123.21429,0.0061087654},{128.57143,0.0028425693},{133.92857,0.002129577},{139.28571,0.0068534431},{144.64286,0.0071201038},{150,0.0099290536},{155.35714,0.0089545127},{160.71429,0.0079282308},{166.07143,0.0075533041},{171.42857,0.01092774},{176.78571,0.012219652},{182.14286,0.01013098},{187.5,0.0096622622}};

(*Extract x and y values*)
rXValues = rData[[All, 1]];
rYValues = rData[[All, 2]];

tXValues = tData[[All, 1]];
tYValues = tData[[All, 2]];

(*Define the equations*)
\[Alpha][\[Mu]_, \[Lambda]_] := 1/(2*\[Mu] + \[Lambda]);
eqns[\[Mu]_, \[Lambda]_, ke_, 
   ko_] := {x^2*r''[x] + x*r'[x] - 
     r[x] + \[Alpha][\[Mu], \[Lambda]]^-1*ke^2*x^2*
      r[x] - \[Alpha][\[Mu], \[Lambda]]^-1*ko^2*x^2*\[Theta][x] == 0, 
   x^2*\[Theta]''[x] + 
     x*\[Theta]'[x] - \[Theta][x] + \[Mu]^-1*ko^2*x^2*r[x] + \[Mu]^-1*
      ke^2*x^2*\[Theta][x] == 0};

(*Define the model*)
model[\[Mu]_, \[Lambda]_, ke_, ko_] := 
  ParametricNDSolveValue[{eqns[\[Mu], \[Lambda], ke, ko], 
    r[0.75] == 0.1625, \[Theta][0.75] == 0, 
    r[187.5] == 0, \[Theta][187.5] == 0}, {r[x], \[Theta][x]}, {x, 
    0.75, 187.5}, {\[Mu], \[Lambda], ke, ko}];

(*Fit the data with initial guess values*)
initialGuess = {{\[Mu], 15}, {\[Lambda], 50}, {ke, 0.01}, {ko, 
    0.01}};
fit = NonlinearModelFit[rYValues, 
   model[\[Mu], \[Lambda], ke, ko][[1]][x], initialGuess, x, 
   MaxIterations -> 10000];
fit2 = NonlinearModelFit[tYValues, 
   model[\[Mu], \[Lambda], ke, ko][[2]][x], initialGuess, x, 
   MaxIterations -> 10000];

(*Plot the fitted functions*)enter code here
Show[Plot[fit[x], {x, 0.75, 187.5}, PlotStyle -> Red, 
  PlotLabel -> "Fitted r(x)", AxesLabel -> {"x", "r(x)"}], 
 ListPlot[rData, PlotStyle -> Blue]]

Show[Plot[fit2[x], {x, 0.75, 187.5}, PlotStyle -> Red, 
  PlotLabel -> "Fitted \[Theta](x)", 
  AxesLabel -> {"x", "\[Theta](x)"}], 
 ListPlot[tData, PlotStyle -> Blue]]

I am trying to fit the two data sets using a coupled differential equations and fitting parameters. However, Getting an error like that: "The function value is not a list of real numbers with dimensions and 0.75 is not a valid variable."

How to fix it? Any help would be appreciated.

$\endgroup$
4
  • 7
    $\begingroup$ Please provide your data, this will increase your chance to get helpful answers! $\endgroup$ Mar 2 at 10:29
  • $\begingroup$ Please see my data files in my latest answer. $\endgroup$
    – random
    Mar 3 at 8:45
  • $\begingroup$ model[\[Mu], \[Lambda], ke, ko][[1]][x] evaluates to 1[x] what is wrong syntax. $\endgroup$ Mar 3 at 19:40
  • $\begingroup$ Please elaborate this a bit.Letme know how to overcome this $\endgroup$
    – random
    Mar 3 at 22:09

1 Answer 1

0
$\begingroup$

Not a complete answer, but an extended comment with corrected code

Here I try to prepare the solution for rData . As mentioned by @DanialHuber your model definition has syntax error

Try

funr = ParametricNDSolveValue[{eqns[\[Mu], \[Lambda], ke, ko], 
r[0.75] == 0.1625, \[Theta][0.75] == 0,r[187.5] == 0, \[Theta][187.5] == 0}, 
r  , {x, 0.75, 187.5}, {\[Mu], \[Lambda], ke, ko}];

initialGuess = {{\[Mu], 15}, {\[Lambda], 50}, {ke, 0.01}, {ko, 0.01}};
fit = NonlinearModelFit[rData, funr[\[Mu], \[Lambda], ke, ko]  [x],initialGuess, x, Method -> "NMinimize"]

Evaluation of fit doesn't finish and gives an error

"ParametricNDSolveValue::bvluc: The equations derived from the boundary conditions are numerically ill-conditioned. The boundary conditions may not be sufficient to uniquely define a solution. If a solution is computed, it may match the boundary conditions poorly."

Please give some hints about your model, especialy about the boundary conditions that appear too restrictive!

addendum

Here an approach with generalized boundary conditions (two additional parameters!)

funr = ParametricNDSolveValue[{eqns[\[Mu], \[Lambda], ke, ko], 
r[0.75] == 0.1625, \[Theta][0.75] == 0, 
r'[.75] == rs0, \[Theta]'[.75] == \[Theta]s0}, 
r, {x, 0.75, 187.5}, {\[Mu], \[Lambda], ke, ko, rs0, \[Theta]s0}];

initialGuess = {{\[Mu], 15}, {\[Lambda], 50}, {ke, 0.01}, {ko, 0.01}};
fit = NonlinearModelFit[
rData, {funr[\[Mu], \[Lambda], ke, ko, rs0, \[Theta]s0][x], \[Mu] > 0, \[Lambda] > 0, ke > 0, ko > 0}, {\[Mu], \[Lambda], ke, ko, rs0, \[Theta]s0}, x, Method -> "NMinimize"]; // AbsoluteTiming

fit runs without error but evaluation doesn't finish...

$\endgroup$
8
  • $\begingroup$ We can relax the boundary condition. It's a circular system. There is a inner boundary condition for r and theta are: r(x_in) = 0.1625 and ϕ(x_in) = 0. At the outer boundary we assume r(x_out) = 0 and ϕ(x_out) = 0. The experimental data needs to fit with the solutions of the above coupled differential equation for r and theta. Hope it will help you to solve this $\endgroup$
    – random
    Mar 4 at 10:21
  • $\begingroup$ @random Why do you expect the model fits your data well? $\endgroup$ Mar 4 at 10:38
  • $\begingroup$ I do not expect that. but i am getting some error. I want to solve this error first $\endgroup$
    – random
    Mar 4 at 12:01
  • $\begingroup$ My answer shows you runnable code without syntax errors(hopefully) $\endgroup$ Mar 4 at 12:07
  • $\begingroup$ initialGuess = {{[Mu], 15}, {[Lambda], 50}, {ke, 0.01}, {ko, 0.01}}; fit = NonlinearModelFit[rData, funr[[Mu], [Lambda], ke, ko][x], initialGuess, x, Method -> "NMinimize"] fit2 = NonlinearModelFit[tData, funr[[Mu], [Lambda], ke, ko][x], initialGuess, x, Method -> "NMinimize"]; Changed this portion (as you suggested) but getting error again. $\endgroup$
    – random
    Mar 4 at 12:51

Your Answer

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

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