Differentiating inside a matlab function

3 views (last 30 days)
David
David on 28 Feb 2025
Commented: Sam Chak on 28 Feb 2025
I want to Differentate 2 equations inside a function but for some reason it breaks when i try to use diff.
this is the longer form of writing out what i want to do
syms a1
syms q1(t)
syms t
x1 = a1*cos(q1);
y1 = a1*sin(q1);
xdot1 = diff(x1,t)
v1sq = (xdot1^2 + ydot1^2) %velocitys
But given i have quite a few of velocitys to calculate i wanted to use a function to do rather than creating loads of xdot variables and writing it all out by hand.
first i tried to do it with this function
function vSq=FindVelocity(x_pos,y_pos) %creating a function to find velocity
x_posdot=diff(x_pos,t);
y_posdot=diff(y_pos,t)
vSq=x_posdot^2 + y_posdot^2
end
However that didnt work so after some research i tried this
function vSq=FindVelocity(x_pos,y_pos) %creating a function to find velocity
arguments (Input)
x_pos (1,1) symfun
y_pos (1,1) symfun
end
sym t
x_posdot=diff(x_pos,t);
y_posdot=diff(y_pos,t)
vSq=x_posdot^2 + y_posdot^2
end
However i still got an error saying "Unrecognized function or variable 't' "
Is what im tyring to do possible in matlab or will i just have to type it all out by hand
  1 Comment
Sam Chak
Sam Chak on 28 Feb 2025
If you want to find the resultant "velocitys" when the vector and the vector are perpendicular, you need to take the square root of the sum of the squares of the vectors as follows:
This is usually covered in the Pythagorean theorem (geometry), vectors, and projectile motion in introductory university physics.

Sign in to comment.

Answers (2)

John D'Errico
John D'Errico on 28 Feb 2025
Edited: John D'Errico on 28 Feb 2025
Oh, you were so close. Actually, you did not even need to tell diff what variable to differentiate with respect to.
syms x(t)
diff(x)
ans(t) = 
diff understands that x is a function only of the variable t.
function vSq=FindVelocity(x_pos,y_pos) %creating a function to find velocity
arguments (Input)
x_pos (1,1) symfun
y_pos (1,1) symfun
end
x_posdot=diff(x_pos);
y_posdot=diff(y_pos);
vSq=x_posdot^2 + y_posdot^2;
end
Now, try it.
syms x(t) y(t)
vSq=FindVelocity(x,y)
vSq(t) = 
Ok, now maybe you are worried that you needed to learn what variables x actually depends upon? So if you really wanted to use diff as you did, symvar would have told you what variable x_pos and y_pos depends upon.
symvar(x)
ans = 
t
And, yes, I remember having gotten hung up on this myself once.
  1 Comment
Sam Chak
Sam Chak on 28 Feb 2025
Actually, after seeing @Cris LaPierre's answer, I intended to suggest that the OP add the simplify() function at the end of the FindVelocitySquared() function to reduce the equation if possible. However, it turned out to be less than ideal for Case 2. Perhaps I made an error in my approach.
syms x(t) y(t) a1
%% Case 1: cos²(t) + sin²(t) = 1
x(t) = a1*cos(t);
y(t) = a1*sin(t);
vSq1 = FindVelocitySquared(x, y)
vSq1(t) = 
%% Case 2: sech²(t) + tanh²(t) = 1
x(t) = a1*sech(t);
y(t) = a1*tanh(t);
vSq2 = FindVelocitySquared(x, y)
vSq2(t) = 
function vSq = FindVelocitySquared(x_pos,y_pos) %creating a function to find velocity
arguments (Input)
x_pos (1,1) symfun
y_pos (1,1) symfun
end
x_posdot= diff(x_pos);
y_posdot= diff(y_pos);
vSq = x_posdot^2 + y_posdot^2;
vSq = simplify(vSq, 100); % rewriting the equation in a simpler form
end

Sign in to comment.


Cris LaPierre
Cris LaPierre on 28 Feb 2025
Edited: Cris LaPierre on 28 Feb 2025
I think you just need to declare your symbolic variable inside your function - or pass it it as an input to the function. Also, I think you want to use syms instead of sym.
syms a1
syms q1(t)
syms t
x1 = a1*cos(q1);
y1 = a1*sin(q1);
xdot1 = diff(x1,t)
xdot1(t) = 
ydot1 = diff(y1,t)
ydot1(t) = 
v1sq = (xdot1^2 + ydot1^2) %velocitys
v1sq(t) = 
% Call v1 of function
vSq1=FindVelocity1(x1,y1,t)
y_posdot(t) = 
vSq(t) = 
vSq1(t) = 
% Call v2 of function
vSq2=FindVelocity2(x1,y1)
y_posdot(t) = 
vSq(t) = 
vSq2(t) = 
function vSq=FindVelocity1(x_pos,y_pos,t) %creating a function to find velocity
x_posdot=diff(x_pos,t);
y_posdot=diff(y_pos,t)
vSq=x_posdot^2 + y_posdot^2
end
function vSq=FindVelocity2(x_pos,y_pos) %creating a function to find velocity
arguments (Input)
x_pos (1,1) symfun
y_pos (1,1) symfun
end
syms t;
x_posdot=diff(x_pos,t);
y_posdot=diff(y_pos,t)
vSq=x_posdot^2 + y_posdot^2
end

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!