1
$\begingroup$

I want to fit a gaussian profile by setting some values dynamically, but FindFit returns an error and i can't see the problem.

with a given gaussian:

gauss = Table[{x, 
E^(-((x - 10)^2/(2 3^2))) + RandomReal[{-0.05, 0.05}]}, {x, 0, 20,
 0.05}];

I set the model to:

gaussmodel = b Exp[-((x - d)^2/(2 g^2))];

With fitparameters set non-dynamically everything is fine:

fit = FindFit[gauss, gaussmodel, {{b, 1}, {d, 10}, {g, 3}}, x]
(*{b -> 1.00439, d -> 10.0109, g -> 2.9804}*)

Now I want to set the startingvalues as following:

InputField[Dynamic[startvalueb], Number]
InputField[Dynamic[startvalued], Number]
InputField[Dynamic[startvalueg], Number]
Checkbox[Dynamic[bool1]]

xx0 = {Dynamic[
   Which[bool1 == True, {b, Dynamic[startvalueb]}, bool1 == False, 
    Null]], Dynamic[
   Which[bool1 == True, {d, Dynamic[startvalued]}, bool1 == False, 
    Null]], Dynamic[
   Which[bool1 == True, {g, Dynamic[startvalueg]}, bool1 == False, 
    Null]]}

witch returns just the form I need, but maybe not??!? Anyway

fi2 = FindFit[gauss, gaussmodel, xx0, x]

returns the following error:

FindFit::vloc:The variable {b,1} cannot be localized so that it can be assigned to numerical values.>>

What can I do to fix this?

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) 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. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Dec 17 '13 at 17:30
1
$\begingroup$
gauss = Table[{x, E^(-((x - 10)^2/(2 3^2))) + RandomReal[{-0.05, 0.05}]}, {x, 0, 20, 0.05}];
gaussmodel = b Exp[-((x - d)^2/(2 g^2))]; 
Manipulate[If[bool,
             FindFit[gauss, gaussmodel, {{b, bb}, {d, dd}, {g, gg}}, x],
             FindFit[gauss, gaussmodel, {b, d, g}, x]],
          {bool, {True, False}}, {bb, 1}, {dd, 1}, {gg, 1}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ so this works, thanks. i guess its not possible outside Manipulate? $\endgroup$ – superklo Dec 17 '13 at 13:44

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.