Differentiating inside a matlab function

4 次查看(过去 30 天)
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 个评论
Sam Chak
Sam Chak 2025-2-28
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.

请先登录,再进行评论。

回答(2 个)

John D'Errico
John D'Errico 2025-2-28
编辑:John D'Errico 2025-2-28
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 个评论
Sam Chak
Sam Chak 2025-2-28
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

请先登录,再进行评论。


Cris LaPierre
Cris LaPierre 2025-2-28
编辑:Cris LaPierre 2025-2-28
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

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by