Function that integrates another function, with units

  • Context: Mathematica 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Function Units
Click For Summary
SUMMARY

The discussion focuses on defining functions in Mathematica that integrate velocity as a function of time while maintaining proper unit handling. The initial function, fnVq[t_], is defined as 2 m/s² * t, and the integration of this function over specified time intervals yields correct results. The issue arises when defining fnX[tx_], which initially does not account for units correctly. The solution involves redefining fnX to integrate with proper unit specifications, allowing for correct outputs such as 36 m for both fnX[6 s] and fnX[6000 ms].

PREREQUISITES
  • Understanding of Mathematica's Quantity feature
  • Familiarity with integration in Mathematica
  • Knowledge of unit conversions in physics
  • Basic programming skills in Mathematica
NEXT STEPS
  • Learn about Mathematica's Quantity and Unit functions
  • Explore advanced integration techniques in Mathematica
  • Research unit handling in mathematical functions
  • Study examples of physics simulations using Mathematica
USEFUL FOR

Mathematica users, physicists, and engineers interested in integrating functions with unit considerations and ensuring accurate outputs in computational models.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
780
Velocity as a function of time, defined with units attached (Quantity feature of Mathematica):
Code:
fnVq[t_ ]:= 2   m/s^2   * t 
fnVq[5 s]
Integrate[fnVq[tt],{tt,0 s, 2000 ms}]
10m/s
4m
When we printed above the value and integral, we got the correct results with proper units.

Now I'm trying to define another function:
Code:
fnX[tx_]:=Integrate[fnVq[tt  ], {tt ,0 ,tx   }] 
fnX[6 ] 
36m/(s)^2

How can I fix the above so it outputs 36 m ? Also, I would like to print say fnX[ 2000 ms ] and have it interpret the milliseconds correctly and output the correct value with correct units.
 
Physics news on Phys.org
Swamp Thing said:
Velocity as a function of time, defined with units attached (Quantity feature of Mathematica):
Code:
fnVq[t_ ]:= 2   m/s^2   * t 
fnVq[5 s]
Integrate[fnVq[tt],{tt,0 s, 2000 ms}]
10m/s
4m
When we printed above the value and integral, we got the correct results with proper units.

Now I'm trying to define another function:
Code:
fnX[tx_]:=Integrate[fnVq[tt  ], {tt ,0 ,tx   }] 
fnX[6 ] 
36m/(s)^2

How can I fix the above so it outputs 36 m ? Also, I would like to print say fnX[ 2000 ms ] and have it interpret the milliseconds correctly and output the correct value with correct units.

Observe that in the first example, you do not integrate between 0 and 2000, but between 0 s and 2000 ms. In the second example, fnX[6] computes Integrate[fnVq[tt ], {tt ,0 ,6 }] which is not what you want; you want Integrate[fnVq[tt ], {tt ,0 s,6 s }]. Can you see how to fix the definition of fnX and what argument you must pass to it in order to get that result?
 
Thanks, this worked:
Code:
fnVq[t_]:=2   m/(s)^2  *t
fnX[x_]:= Integrate[fnVq[tt],{tt,0  s,x}]

fnX[6 s]
fnX[6000 ms]

Out[31] = 36 m
Out[32] = 36 m
 
Last edited:

Similar threads

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