Adding many functions in mathematica

In summary, the conversation discusses a code fragment involving the use of the sigma and mu variables to define gaussian and lorentz functions. The problem is that the spec1 and spec2 functions, which should appear as sums of multiple terms, cannot be plotted as a function of x. The solution provided is to use the Total function to sum the terms and plot the result.
  • #1
wil3
179
1
Hello. Can someone tell me what I can do to the following code fragment:

sigma = {2,4,6,8}
mu = {1,3,5,7}
gauss[x_, mu_, sigma_] = (1/(2*\[Pi] * sigma^2))*
E^-((x - mu)^2/(2*sigma^2))
lorentz[x_, m_, gamma_] = 1/(\[Pi]*gamma*(1 + ((x - m)/gamma)^2))
spec1 = Apply[Plus, gauss[x, mu, sigma]]
spec2 = Apply[Plus, lorentz[x, mu, sigma]]

spec1 and spec 2 correctly appear as sums of multiple terms, but I cannot plot either as a function of x. Where have I gone wrong?

thanks for any help
 
Physics news on Phys.org
  • #2
The following quick fix will work:

sigma = {2, 4, 6, 8};
mu = {1, 3, 5, 7};

gauss[x_, mu_, sigma_] := E^(-(x - mu)^2/(2*sigma^2))/(2*Pi*sigma^2)
lorentz[x_, m_, gamma_] := 1/(Pi*gamma*(1 + ((x - m)/gamma)^2))

spec1[x_] := Total[gauss[x, mu, sigma]]
spec2[x_] := Total[lorentz[x, mu, sigma]]

Plot[{spec1[x], spec2[x]}, {x, -10, 10}]
 
  • #3
that works for me. I probably should get better acquainted with that "Apply" operator. Thanks for your help.
 

1. How do I add multiple functions in Mathematica?

To add multiple functions in Mathematica, you can use the "+" operator between the functions. For example, if you want to add the functions f(x) and g(x), you can write "f[x]+g[x]" in Mathematica.

2. Can I add more than two functions at a time in Mathematica?

Yes, you can add as many functions as you want in Mathematica. You can use the "+" operator between each function, or you can enclose all the functions in a list and use the "Total" function to add them together. For example, if you have functions f(x), g(x) and h(x), you can write "Total[{f[x], g[x], h[x]}]" in Mathematica to add them.

3. Is there a specific order in which I should add the functions?

No, there is no specific order in which you should add the functions. The order of addition does not affect the result, as long as all the functions are included in the addition.

4. Can I add functions with different numbers of variables?

Yes, you can add functions with different numbers of variables in Mathematica. However, you need to make sure that the variables are defined and have the same name in all the functions. If the variables have different names, you can use the "Function" command to rename them before adding the functions.

5. How can I add functions with conditions in Mathematica?

To add functions with conditions in Mathematica, you can use the "Piecewise" function. This function allows you to specify different functions for different intervals or conditions. You can then use the "+" operator to add the different pieces of the function together.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
892
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
823
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
700
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
882
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
5
Views
942
Back
Top