Definiing two functions (mathematica)

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

Discussion Overview

The discussion revolves around defining two functions in Mathematica to model the velocities of a molecule, specifically focusing on horizontal and vertical velocities. Participants explore how to correctly implement these functions and handle variable inputs without manual intervention.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant defines a function for horizontal velocity, f[s_]:=RandomReal[{-s,s}], and another for vertical velocity, g[s_,u_]:=RandomReal[{-Sqrt[s^2-u^2],+Sqrt[s^2-u^2]}].
  • There is a query about how to use the output of f[s] as an input for g[s,u] without manual input.
  • Another participant suggests using g[s,f[s]] as a potential solution.
  • A participant reports an error when trying to implement this, indicating that the endpoints for RandomReal must be real values, and mentions that using g[2,f[3]] leads to complex numbers.
  • There is a suggestion that ensuring s^2-u^2 remains real or using RandomComplex might be necessary to avoid errors.

Areas of Agreement / Disagreement

Participants generally agree on the need to ensure that the arguments for RandomReal are real values, but there is no consensus on the best method to implement the functions without encountering errors.

Contextual Notes

Participants note that the arguments passed to RandomReal must be real, and there are concerns about the potential for complex numbers when calculating square roots. The discussion does not resolve how to effectively manage these issues in Mathematica.

rbnphlp
Messages
53
Reaction score
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
You mean just

g[s,f] ?
 
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] separately , so I get different f in that case..

Any ideas?
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K