2
$\begingroup$

Suppose i have some function

G[n_,B_,d_] = ....

to fit this function i need to collect the data first. However at first i dont understand how to format the data what i have done is:

 data = Table[G[n_,B_,d_],{n,100},{d,5},{B,0.01,1,0.01}]

Now suppose i want to fit the following function:

 F[n_,B_,d_,a_,b_,c_,d_] = a + b Tanh[c*B^(e*d)*n^(f)]

I tried to compute this by:

 FindFit[data, a + b Tanh[c*B^(e*d)*n^(f)],{a,b,c,e,f},{n,B,d}]

getting the error that my data is not rectangular, furthermore i think i am formatting in the wrong way anyone got a tip for this?

$\endgroup$

2 Answers 2

5
$\begingroup$
ClearAll[model];
model[n_, B_, d_, a_, b_, c_] := a + b Tanh[B^d c n]

data = With[
   {
    a = 1,
    b = 2,
    c = 3
    },
   Flatten[
    Table[
     N[{n, d, B, model[n, B, d, a, b, c] + RandomReal[0.1]}]
     , {n, 100}
     , {B, 0.01, 1, 0.01}
     , {d, 5}
     ], 2]
   ];

This is how your data should look like. A single list with elements {ni,Bi,di, measuredvalue}

Short[data]
(* {{1.,1.,0.01,1.13965},<<49998>>,{100.,5.,1.,3.06851}} *)

FindFit[data, model[n, B, d, a, b, c], {a, b, c}, {n, d, B}]
(* {a -> 1.04984, b -> 1.99999, c -> 2.99911} *)

A good hint is to read the documentation for FindFit

enter image description here

$\endgroup$
1
  • $\begingroup$ thanks i just jumped to the examples never saw that tab :D $\endgroup$
    – Kees Til
    Jul 10, 2018 at 21:05
1
$\begingroup$

I think your "data" is wrong format. You might want to try this

data=Table[{n,b,d,G[n,B,d]},{n,100},{d,5},{B,0.01,1,0.01}]

Example

Because I could able to fit multi-dimensional data

param = Table[RandomReal[], {i, 1, 6}];
vari = {x^2, y^2, z^2, x y, x z, y z};
G[x_, y_, z_] := Evaluate[param.vari]
estim = {a1, a2, a3, a4, a5, a6};
data = Flatten[
   Table[{x, y, z, G[x, y, z]}, {x, -1, 1, 0.1}, {y, -1, 1, 
     0.1}, {z, -1, 1, 0.1}], 2];
nlm = NonlinearModelFit[data, estim.vari, estim, {x, y, z}];

and i got

{estim, param, nlm["BestFitParameters"][[All, 2]]} // TableForm

   a1          a2          a3          a4           a5         a6
0.460446    0.259803    0.346077    0.767826    0.0984281   0.685461
0.460446    0.259803    0.346077    0.767826    0.0984281   0.685461
$\endgroup$
3
  • $\begingroup$ tried this i get this error: FindFit::fitd: First argument {<<1>>} in FindFit is not a list or a rectangular array. $\endgroup$
    – Kees Til
    Jul 10, 2018 at 20:39
  • $\begingroup$ my command is FindFit[data, a + b Tanh[cB^(ed)*n^(f)], {a, b, c, e, f}, {n, d, B}] $\endgroup$
    – Kees Til
    Jul 10, 2018 at 20:40
  • 1
    $\begingroup$ I think @rhermans answers the question! :D $\endgroup$
    – Saesun Kim
    Jul 10, 2018 at 21:00

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.