1
$\begingroup$

Let me explain my question by a toy example: Suppose I have a simple Gaussian function as follows:

test[x_] = 4*Exp[-(x)^2];

If I its data to a new Gaussian function I do:

data = Table[{x, test[x]}, {x, 0, 10, 10/100}];

fit = NonlinearModelFit[data, {a1*Exp[-b1*(x)^2]}, {a1, b1}, x, 
   Method -> NMinimize];
fit[x]
(*3.99999994699 E^(-0.999999974954 x^2)*)

where I see it recovers the original function very good. Now I want to fit just a part of data, for example instead of {0 to 10} I want to pick data of {5 to 10} from original function and then fit them (so I think the center of gaussian is not 0 any more and it shifts +5 namely in exponent I have to write $(x+5)^2$ instead $x^2$, however I don't know what is the best way to implement it). How can I do this? Does Mathematica has a built-in function or option to do this?

$\endgroup$
5
  • $\begingroup$ Why do you think the center of the gaussian would shift, if you use only part of the data? $\endgroup$ Apr 8, 2022 at 16:15
  • $\begingroup$ My mean was the center of fit function must be on x+5 to simulate that range of data of the original function, namely I must use $(x+5)^2$ in fit and also produce data from 5 to 10 $\endgroup$
    – Wisdom
    Apr 8, 2022 at 16:26
  • 1
    $\begingroup$ If you make a fit with only part of the data, the center and width will be approx. the same. $\endgroup$ Apr 8, 2022 at 18:16
  • $\begingroup$ So how can I do a fitting by using just a part of data? $\endgroup$
    – Wisdom
    Apr 8, 2022 at 18:18
  • $\begingroup$ Are you looking to just generate data using the same function from 5 to 10: data = Table[{x, test[x]}, {x, 5, 10, 10/100}]; ? $\endgroup$
    – Nate
    Apr 8, 2022 at 18:52

1 Answer 1

1
$\begingroup$

Edit: in this edited version I will try to be more concrete after the comments of the author of the OP. All detailed steps remain as they were presented in the first version at the end


Breaking down the main point of the OP assuming that there is no miscommunication.

In the OP the author provided us with the following data

test[x_] = 4*Exp[-(x)^2];
data = Table[{x, test[x]}, {x, 0, 10, 1/10}];

The task at hand is to choose the subset of the above data -already generated- in the region {x,5,10} and their corresponding test[x] values. This can be done programmatically in the following manner

data[[5*10 + 1 ;; All]]

tab1

So, as we see we sorted out the subset of the original data for which we have $x \geq 5$ and their corresponding test[x] values. Since we are done with this, we can this new set of data like so:

fit = NonlinearModelFit[
   data[[5*10 + 1 ;; All]], {a1*Exp[-b1*(x)^2]}, {a1, b1}, x, 
   Method -> NMinimize];
fit[x]

fit

We plot the specific set of data used for the fit and the fitted function we obtained for comparion

p1 = ListPlot[data[[5*10 + 1 ;; All]], 
   PlotStyle -> PointSize /@ {Large}, PlotRange -> {All, All}];
p2 = Plot[
   2.6960551596217814` E^(-0.9843743450389705` x^2), {x, 5, 10}, 
   PlotStyle -> {Thick, Dashed, Red}, PlotRange -> {All, All}];
Show[p1, p2, ImageSize -> Large]

ps


Original answer: step-by-step explanation


Comment: I think that the main point of this question is how to single out specific sublists from a mother list.

Also, I do not understand the reason of writing 10/100 and not 1/10, so I am changing this to 1/10

So, from the OP

test[x_] = 4*Exp[-(x)^2];

Now we generate the data

data = Table[{x, test[x]}, {x, 0, 10, 1/10}];

How to single out the ones that {x,5,10} from the above list? I think that this is the question.

The following

data[[5*10 + 1 ;; All]]

takes care of that. You get

tab1

Another check for the validity of the above. If you want to single out data with x=5 or higher you could have started by generating only those. I understand that this might not be useful for practical purposes, but serves well for a check.

lst = Table[{x, test[x]}, {x, 5, 10, 1/10}];

and then

data[[5*10 + 1 ;; All]] - lst

check

Finally, doing the fit is easy now

fit = NonlinearModelFit[
   data[[5*10 + 1 ;; All]], {a1*Exp[-b1*(x)^2]}, {a1, b1}, x, 
   Method -> NMinimize];
fit[x]

fit

Checking what we did

p1 = ListPlot[data[[5*10 + 1 ;; All]], 
   PlotStyle -> PointSize /@ {Large}, PlotRange -> {All, All}];
p2 = Plot[
   2.6960551596217814` E^(-0.9843743450389705` x^2), {x, 5, 10}, 
   PlotStyle -> {Thick, Dashed, Red}, PlotRange -> {All, All}];
Show[p1, p2, ImageSize -> Large]

ps

$\endgroup$
9
  • $\begingroup$ Thanks, but you got my mean conversely! I exactly need to fit {x,5,10} data and as I understand you have done such a thing in your finally check, right? $\endgroup$
    – Wisdom
    Apr 9, 2022 at 3:21
  • $\begingroup$ @Wisdom So, I started by showing you how you can split the full data to the ones that start from 5 and higher programatically, then I did the fit for you and then I plotted the fit and the data. I don't understand what you mean when you say conversely to be honest. I thought that by breaking it down, it would help you understand the commands better. $\endgroup$
    – bmf
    Apr 9, 2022 at 3:23
  • $\begingroup$ @Wisdom if my answer and my comment did not clarify the situation, I am happy to try and re-organize my thoughts in an edited version. Please let me know :) $\endgroup$
    – bmf
    Apr 9, 2022 at 3:40
  • $\begingroup$ @Wisdom please see the updated version of my answer. From the data you generated, I sorted out only the ones for which we have $x \geq 5$ and their associated test[x] values and performed the Fit using this subset of the original data. I sorted out your data as there is no need to regenerate the Table as was suggested in the comment section by another user. Have a look and please let me know if it's clear now. If it's not clear, can you please leave a comment explaining which part is obscure to you? Many thanks :-) $\endgroup$
    – bmf
    Apr 9, 2022 at 3:58
  • 1
    $\begingroup$ @Wisdom you can always check when you sort the set of mother data. For instance, this generates Table[{x, test[x]}, {x, 10, 20, 1/10}] the data you mentioned in the last comment, so you can compare this with data[[10*10 + 1 ;; All]] which is the set of sorted data :) $\endgroup$
    – bmf
    Apr 10, 2022 at 4:23

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.