Adding many functions in mathematica

  • Context: Mathematica 
  • Thread starter Thread starter wil3
  • Start date Start date
  • Tags Tags
    Functions Mathematica
Click For Summary
SUMMARY

The discussion focuses on resolving issues with plotting functions in Mathematica, specifically Gaussian and Lorentzian functions. The original code used the Apply function incorrectly, preventing the plotting of the sums. The solution involves redefining the functions using Total instead of Apply, allowing for successful plotting with the command Plot[{spec1[x], spec2[x]}, {x, -10, 10}]. Users are encouraged to familiarize themselves with the Apply operator for better function handling in Mathematica.

PREREQUISITES
  • Understanding of Mathematica syntax and function definitions
  • Familiarity with Gaussian and Lorentzian functions
  • Knowledge of plotting functions in Mathematica
  • Basic understanding of the Apply operator in Mathematica
NEXT STEPS
  • Learn about function definitions in Mathematica, specifically using := versus =
  • Explore the Apply function and its applications in Mathematica
  • Study the Total function for summing lists in Mathematica
  • Investigate advanced plotting techniques in Mathematica, including options for customizing plots
USEFUL FOR

Mathematica users, data analysts, and anyone interested in mathematical modeling and visualization using Mathematica.

wil3
Messages
177
Reaction score
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
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}]
 
that works for me. I probably should get better acquainted with that "Apply" operator. Thanks for your help.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K