Mathematica Function that integrates another function, with units

  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Function Units
AI Thread Summary
The discussion focuses on defining functions in Mathematica to calculate displacement based on a velocity function that incorporates units. Initially, the user defined a velocity function, fnVq, which correctly outputs results with proper units. However, when attempting to define a displacement function, fnX, the output was incorrect because the integration limits did not account for units. The solution involved modifying the fnX function to ensure it integrates from 0 seconds to the specified time in seconds, allowing it to correctly interpret inputs like 6 seconds or 2000 milliseconds. The corrected function now outputs the expected displacement of 36 meters for both inputs.
Swamp Thing
Insights Author
Messages
1,028
Reaction score
763
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
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
6
Views
4K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
1
Views
5K
Back
Top