# how to work with a variable as a function of time?

45 views (last 30 days)
Commented: Walter Roberson on 14 Jan 2023
I know you can set a variable as a function of other variable and differentiate it by doing the following:
syms theta(t)
diff(sin(theta),t)
ans(t) =
However, I still have three questions:
• Why is it being shown as a partial derivative even though I set the variable as a single-variable function?
• Is there any way to change the notation so that it shows up like this:
• Can I work with the differentiated variable? (assign a solution to it, etc...)

Walter Roberson on 14 Jan 2023
Changing the notation:
syms theta(t)
dtheta = diff(theta,t)
dtheta(t) =
derivative = diff(sin(theta),t)
derivative(t) =
syms theta_dot(t)
subs(derivative, dtheta, theta_dot)
ans(t) =
However, MATLAB has lost any connection between diff(theta,t) and theta_dot . If you were to construct
syms theta_ddot(t)
d2theta = diff(dtheta,t)
d2theta(t) =
eqn = 2*d2theta - 5*dtheta == 1
eqn(t) =
pretty_eqn = subs(eqn, {d2theta, dtheta}, {theta_dot, theta_ddot})
pretty_eqn(t) =
Here, pretty_eqn might look nice, but dsolve() will not know that the two functions are related
Edited: Imanol Fernandez de arroyabe Zabala on 14 Jan 2023
Thanks!
I asked this because I'm doing a physics project and I need to work with angular positions and accelerations. Do you know if I can then use the solve function in order to get the values for the differentiated theta? (angular velocity and acceleration that is). And let me clarify: I wouldn't want the program to solve the differential equation, but rather to treat the differentiated theta as a new variable and for the program to give me a symbolic answer.
Walter Roberson on 14 Jan 2023
solve() will not solve with respect to a function. You would need to substitute a variable for the function and then solve() for the variable.
syms theta(t)
dtheta = diff(theta,t)
dtheta(t) =
derivative = diff(sin(theta),t)
derivative(t) =
syms theta_dot theta_ddot
d2theta = diff(dtheta,t)
d2theta(t) =
eqn = 2*d2theta - 5*dtheta == 1
eqn(t) =
pretty_eqn = subs(eqn, {d2theta, dtheta}, {theta_dot, theta_ddot})
pretty_eqn(t) =
sol = solve(pretty_eqn, theta_ddot)
sol =
%pretty, but of limited value, you have to switch back to get anywhere
dsolve(d2theta == subs(sol, theta_dot, dtheta))
ans =

John D'Errico on 14 Jan 2023
Edited: John D'Errico on 14 Jan 2023
Why MATLAB shows a partial derivative there, and not an ordinary derivative?
syms theta(t)
dTdt = diff(theta,t)
dTdt(t) =
Technically, a partial derivative is just a superset of ordinary differentiation. While it tends to imply that theta is a function of other variables too, it costs nothing to write only the one operator there, and it saves them some extra work in coding, since then the display is simpler.
Is there a way to use the over-dot notation? No. That is not an option. Again, it would just add complexity, creating the possibility of bugs, and increase development time. There are way more important things I would want to see them invest their available person-years on than giving the user multiple ways to show exactly the same thing.
Can you assign something to a differentiated variable? Not really in any useful way, at least not if you want to use it as a container, but also have MATLAB know where it came from. For example, we see that dTdt initially contains the derivative of theta. But suppose you now decide to do this:
dTdt(t) = 3*t
dTdt(t) =
As you can see, MATLAB now sees dTdt is just a simple function of t, but it no longer thinks or remembers that it was also the derivative of theta. That connection to theta has been lost.
Can you use the derivative in an ODE, so maybe what you are asking? Yes, but I don't think that was at all your question.
ODE = diff(theta) == 3*t
ODE(t) =
dsolve(ODE,theta(0) == 2)
ans =
But that is not really assigning something to the derivative expression.
So I'm not really sure what you are hoping to do in your third question.

### Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

R2022b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!