Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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,

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]

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, 

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?

share|improve this question
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! – Dr. belisarius Dec 17 '13 at 17:30
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))]; 
             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

share|improve this answer
so this works, thanks. i guess its not possible outside Manipulate? – superklo Dec 17 '13 at 13:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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