MATLAB Answers


How can I make derivatives with several variables?

Asked by Oscarin
on 5 Sep 2018
Latest activity Edited by Oscarin
on 23 Oct 2018
Hello I have a problem, I tryed to derive a equation like "a1^2*a2^2" but I got "ans = 2*a1*a2^2", This result is correct but If we take as variable only "a1" but I need to take as varible "a1" and "a2". I want to get this becase is a multiplication:
I have used diff () this is my code:
what is the correct way to say matlab which are my variables?
Thank you very much I wait for your answer.


Sign in to comment.




2 Answers

Answer by Walter Roberson
on 15 Sep 2018
 Accepted Answer

You can use gradient() to find the diff(), and you could sum() the result.


Could you remind me again how the variables you use in your code map to d_2 d_2_dot theta_1 theta_1_dot ?
when I use gradient (), I get a vector, [1,1] is the partial derivative of a variable, [2,1] is the partial derivative of another variable, this depend on the number of variables and GDL (Degrees of freedom) in this case GDL is 2 then we check the case whith "if GDL == 2 " therefore I get each position of vector and multiply for "w" if joint is rotate or cylindrical ("radio buttoms", art1 with value (1 or 3) y art2 with value (1 or 3)) and if joint is prismatic I multiply the position of vector by "y" (art1 with value (2), art2 with value (2)).
this is the simplified code for one case "GDL == 2" when I multiply variables.
if true
syms m g rt w1 w2 w3 y1 y2 y3
global Px Py Pz gdl
aux1 = Px + Py + Pz;
C = symvar(aux1)
D = setdiff(C,B)
F = setdiff(C,D)
aux2 = gradient(aux1,F)
if gdl == 2
art1 = str2double(get(handles.arti1,'String'));
art2 = str2double(get(handles.arti2,'String'));
if art1 == 2
aux5 = aux2(2,1)*y1;
aux5 = aux2(2,1)*w1;
if art2 == 2
aux6 = aux2(1,1)*y2;
aux6 = aux2(1,1)*w2;
aux2 = [aux5; aux6];
vel = sum(aux2);
vel = expand(vel);
thanks my friend, I find the error you were right, I derivate the vector then square it, finally I use sum() and simplify then I get the result, sorry for a lot of question, this is my proyect to get my degree but I know about robotics but not about matlab and I am learning.

Sign in to comment.

Answer by D_coder on 15 Sep 2018
Edited by D_coder on 15 Sep 2018

You can try this
a = a1^2*a2^2;
diff(a,a1) + diff(a,a2);
ans =
2*a1^2*a2 + 2*a1*a2^2

  1 Comment

thaks for your answer but this is more difficult because I am developing a graphic interphase and I export the data and I need to derivate de ecuation but I need to derive with a lot of variable I show a example:
In this case the last Graphic Interphase exported the data and I need to derive with variables "d1", "d2" and "t1". That's difficult because the ecuation it's different each time.
I have a list of possible variables but not all of them always appear because some of them based on the analysis become 0

Sign in to comment.