Dear Community :
I’m learning doing the integration.
I know how to import data and integration.
But I do not how to set the variables after importing it.
The screencapture is as below.
Please advise me
Thanks in advance
Lee
Hi. I’m not sure what you would like to do with Mathematica. What do you want this code to do?
Import will give you a list of numbers, not a function. If you would like to make the list of numbers into a function, please try using the Interpolation function.
If you are new to Mathematica, you may want to read an overview of how to use it. You can do this by looking at the Mathematica Virtual Book.
I would like to know how to do a definite integral of sigma(E) over an defined region.
You may take a look at this funciton after you import the data:
Let’s make up some data close to yours:
data = Table[{x, 700 x Exp[-x] + 50 RandomReal[]}, {x, 0, 9, .1}];
When you have just list of points you have three choices for integration:
Let’s first get all three on the same figure.
(* interpolation *)
f = Interpolation[data];
(* model fit *)
model = a x Exp[-b x];
fit = FindFit[data, model, {a, b}, x]
modelf = Function[{t}, Evaluate[model /. fit]];
Out[] = {a -> 678.552, b -> 0.892996}
Show[
Plot[{f[x], modelf[x]}, {x, 0, 9}],
ListPlot[data, PlotStyle -> Red]
, PlotRange -> All]
Now we can compute the integrals - which all are pretty close to each other. You should pick a method more appropriate to your case. The 1st one - integration of data - is the simplest. You just have to sum all the Y-values and multiply them by the step between the points - in our case it is 0.1 .
(* integrate data *)
.1 Total[data[[All, 2]]]
Out[]= 1911.23
(* integrate interpolation *)
NIntegrate[f[x], {x, 0, 9}]
Out[]= 1901.01
(* integrate model fit *)
NIntegrate[modelf[x], {x, 0, 9}]
Out[]= 1756.51
Thank you so much for the instruction.
I understand much more how to do the interpolation and integral now.
But I still do not know how to set the variables after importing data, how to correspond the importing data to variables
Please advise me.
The data O8p18Ft.xls.
(2.5, 2.74)
(2.6, 52 )
(2.7, 6.6 )
…
…
(20, ) is attached in the link below
Try this. Ammend the file path and the need to flatten/Partition as you need. The attached picture is using your data from your dropbox, renamed.
dtsa = Import["test2.xls", {"Data"}]; dtsa =
Partition[Flatten[dtsa], 2]; r1 = Min[dtsa[[All, 1]]]; r2 =
Max[dtsa[[All, 1]]]; f = Interpolation[dtsa];
model = a x Exp[-b x];
fit = FindFit[dtsa, model, {a, b}, x]
modelf = Function[{t}, Evaluate[model /. fit]];
Print[Show[Plot[{f[x], modelf[x]}, {x, r1, r2}],
ListPlot[dtsa, PlotStyle -> Red], PlotRange -> All,
ImageSize -> {800, 800}]]; .1 Total[dtsa[[All, 2]]]
NIntegrate[f[x], {x, r1, r2}]
NIntegrate[modelf[x], {x, r1, r2}]