ans(t) =
Differentiating inside a matlab function
이전 댓글 표시
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
2025년 2월 28일
Hi @David
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개)
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)
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)
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)
And, yes, I remember having gotten hung up on this myself once.
댓글 수: 1
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)
%% Case 2: sech²(t) + tanh²(t) = 1
x(t) = a1*sech(t);
y(t) = a1*tanh(t);
vSq2 = FindVelocitySquared(x, y)
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
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)
ydot1 = diff(y1,t)
v1sq = (xdot1^2 + ydot1^2) %velocitys
% Call v1 of function
vSq1=FindVelocity1(x1,y1,t)
% Call v2 of function
vSq2=FindVelocity2(x1,y1)
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
카테고리
도움말 센터 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


