How can I make derivatives with several variables?

37 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Sep 2018
You can use gradient() to find the diff(), and you could sum() the result.
  35 Comments
Oscarin
Oscarin on 17 Oct 2018
Edited: Oscarin on 17 Oct 2018
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;
else
aux5 = aux2(2,1)*w1;
end
if art2 == 2
aux6 = aux2(1,1)*y2;
else
aux6 = aux2(1,1)*w2;
end
aux2 = [aux5; aux6];
vel = sum(aux2);
vel = expand(vel);
end
Oscarin
Oscarin on 23 Oct 2018
Edited: Oscarin on 23 Oct 2018
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.
Thanks

Sign in to comment.

More Answers (1)

D_coder
D_coder on 15 Sep 2018
Edited: 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
Oscarin
Oscarin on 15 Sep 2018
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
Thanks

Sign in to comment.

Categories

Find more on Multibody Modeling in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!