Definiing two functions (mathematica)

In summary: For example:g[s_,u_]:=RandomReal[{-Sqrt[(s^2-u^2)^(1/2)],Sqrt[(s^2-u^2)^(1/2)]}] In summary, two functions have been defined for a molecule with speed s. The first function, f[s], has a horizontal velocity between -s and s, while the second function, g[s,u], has a vertical velocity between +Sqrt[s^2-u^2] and -Sqrt[s^2-u^2]. To use the values of s and u from the first function in the second function, one can simply enter g[s,f[s]]. However, care must be taken to ensure that the arguments
  • #1
rbnphlp
54
0
I need to define two functions for a molecule with speed s.
a)it has a horizontal velocity between -s and s
b) v , the vertical velocity between +Sqrt[s^2-u^2] and -Sqrt[s^2-u^2]

This is what I have done
for a)
Code:
f[s_]:=RandomReal[{-s,s}]
for b)
Code:
g[s_,u_]:=RandomReal[{-Sqrt[s^2-u^2],+Sqrt[s^2-u^2]}]

now , If I enter f[2] I get u, so I have s and u from first code. How do I use these values in the second code (without manually inputting them)?

Thanks a bunch
 
Physics news on Phys.org
  • #2
You mean just

g[s,f] ?
 
  • #3
jackmell said:
You mean just

g[s,f] ?


Yes ,thats what I want ..But how do I put into mathematica ?

If just insert what you have written I get

RandomReal::unifr: "The endpoints specified the uniform distribution range are not real valued

I also Tried,

Code:
g[s_] := RandomReal[{-Sqrt[s^2 - f[s]^2], +Sqrt[s^2 - f[s]^2]}]
However , what it does in this case is , finds f for -Sqrt[s^2 - f^2] and +Sqrt[s^2 - f^2] seperately , so I get different f in that case..

Any ideas?
 
  • #4
I believe that's because the arguments to RandomReal have to be real. But for example if I entered:

g[2,f[3]]

the argument passed to Sqrt results in a complex number. Either need to make sure s^2-u^2 is real or either use RandomComplex.
 
  • #5


Your first function, f, randomly generates a number between -s and s, which can be considered as the horizontal velocity of the molecule. Your second function, g[s,u], takes in two parameters (s and u) and randomly generates a number between +Sqrt[s^2-u^2] and -Sqrt[s^2-u^2], which can be considered as the vertical velocity of the molecule.

To use the values from f in g[s,u], you can modify the code for g[s,u] to take in a single parameter, s, and use that value in the calculation for the vertical velocity:

g[s_]:=RandomReal[{-Sqrt[s^2-f^2],+Sqrt[s^2-f^2]}]

This way, when you enter g[2], it will use the value from f[2] as the horizontal velocity and generate a random vertical velocity within the specified range.
 

1. What is a function in mathematics?

A function in mathematics is a relation between a set of inputs and a set of possible outputs. It assigns exactly one output for each input, and the output value depends on the input value. Functions are commonly represented in the form of f(x), where x is the input and f(x) is the output.

2. What is the purpose of defining two functions in Mathematica?

Defining two functions in Mathematica allows you to perform calculations and manipulate data using those functions. It also helps you to organize your code and make it more readable and reusable.

3. How do I define a function in Mathematica?

To define a function in Mathematica, you can use the syntax "functionName[input_] := expression". This creates a function named "functionName" that takes in an input and evaluates the expression to produce an output.

4. Can I define multiple functions in one line of code in Mathematica?

Yes, you can define multiple functions in one line of code in Mathematica by separating each function definition with a semicolon (;). For example: "f[x_] := x^2; g[y_] := y + 1;"

5. How do I call a defined function in Mathematica?

To call a defined function in Mathematica, you can simply type the function name followed by the input value in square brackets. For example, if you have defined a function named "square" that squares a given number, you can call it by typing "square[4]" and the output will be 16.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
849
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top