Differentiating inside a matlab function

조회 수: 3 (최근 30일)
David
David 2025년 2월 28일
댓글: Sam Chak 2025년 2월 28일
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