- You will see updates in your activity feed.
- You may receive emails, depending on your notification preferences.

- 4 questions asked
- 0 answers
- 0 accepted answers
- reputation: 0

- 4 questions asked
- 0 answers
- 0 accepted answers
- reputation: 0

Accepted Answer by Walter Roberson

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

1 view (last 30 days)

1 view (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.

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

Answer by Walter Roberson
on 15 Sep 2018

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

Accepted Answer

thaks for your answer but this is 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

Walter Roberson
on 16 Sep 2018

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

gradient() automatically takes the derivative with respect to each of the unbound variables in the expression. Since you are adding them all together and symbolic addition is commutative, you do not even need to know what order of variables it used.

You can use symvar() to detect which variables were used in an expression, if you need to.

when I tryed with gradient () matlab took all varibles.

Are there some way to say to matlab which of my simbolic variables takes like varibles?

For example in a ecuation I have "d1", "t1" and "d2", I need to derivate at one time but I need to derivate only about "t1" and "d2", I want "d1" like a constant.

Is this possible?

thanks

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

Ok, I am learning but I don't understand how can I do derivatives, I try this.

Thanks.

Walter Roberson
on 17 Sep 2018

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

setdiff(B, A)

That's great! I solve one problem but I have the last one problem, I need to get a derivative with notation like the following:

I have a position vector and I need to derive and get vector speed, but I need to appear it notation for the first derivative, second derivative, etc.

how can I do it?

thanks.

Walter Roberson
on 19 Sep 2018

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

You can use pretty(), or you can use LiveScript .

I try to use pretty () but matlab show a error.

but this is the problema I fill in with this ecuation.

I want to get the first ecuation on next picture. But If it were possible to obtain it with the notation of the second equation, it would be better

Walter Roberson
on 20 Sep 2018

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

BL = latex(B)

But then you would have to display the latex stored in BL somehow.

You cannot get the graphical format you want to store into a variable. That format is a graphical format that is not how MATLAB represents symbolic expressions internally. You can render into latex like I show, but the result is a character vector that cannot be operated on symbolically.

Hello.

How can I get the shorter from a ecuation?

I try to use "simplify" but I don't get the shorter form.

I found "simple" but I don't know how can I use it.

I get this error.

Thank you very much

Walter Roberson
on 14 Oct 2018

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

What is in your vel variable, and what shorter form are you expecting?

If you are hoping for trig identities to be used to create a simpler expression, then use rewrite() and simplify()

I try to simplifly this ecuation but when I use "simplify" Matlab give me the same result.

I simplified in my notebook, I got this result.

But I used trigonometric identities to simplify the ecuation

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

Nope. You have made a mistake somewhere, or else your notebook is wrong.

>> vel2

vel2 =

(y2*sin(t1) - y2*cos(t1) + d2*w1*cos(t1) + d2*w1*sin(t1))^2

>> subs(vel2,[w1,y2,d2],[3,5,7])

ans =

(16*cos(t1) + 26*sin(t1))^2

>> subs(d2^2*w1^2+y2^2,[w1,y2,d2],[3,5,7])

ans =

466

These are not equivalent.

I want to simplify with variables not with numbers, this is my notebook.

In picture we can see "P" is a position vector, we need to derive to get speed vector "V" and we need to raise the square vector and simplify.

It is worth mentioning that the variables are replaced as follows between Matlab code and notebook

thanks

Walter Roberson
on 15 Oct 2018

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

"I want to simplify with variables not with numbers, this is my notebook."

If you substitute specific numeric values into both forms, and you do not get the same result, then one of the two forms must be wrong.

ok I am goin to check my notebook but could you help me how to displayed some ecuation in Latex form in an axes?

I have looked for information and I have found this code but Matlab doesn't do anything

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

s = ['$$', latex(vel2), '$$'];

text(0.2, 0.5, s, 'Interpreter', 'latex')

looks okay to me.

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

It is a bit difficult to interpret your notebook, as you go from column vector to v^2 equation without intermediate form showing how v was derived.

Your v^2 appears to be calculated by squaring the individual components of the column vector and summing those.

Your vel2 term, on the other hand, is calculated by summing the elements in the column vector and squaring the result. Thus, vel2 has cross-product terms in it where v^2 does not.

I can't show ecuation, I modified the code like this. In where part of yur code I can send the result to axes1?.

if true

s = ['$$', latex(vel), '$$'];

axes(handles.axes1)

cla

text(0.2, 0.5, s, 'Interpreter', 'latex')

end

I was cheking my notebook and I found two things that I forget tell you, firstly "t1" and "d2" are variables in time, "d1" is variable but for derivate is constant, don't worry for that I already fix the code with "if" for that. Second thing I have "P" then I derive and I get "V", in this vector I square the terms and then add them after I simplify the ecuation with trigonometric identities and I get the result as you can see in the notebook.

this is my code.

if true

aux2 = [aux5; aux6; aux7];

vel = sum(aux2);

vel = expand(vel);

s = ['$$', latex(vel), '$$'];

axes(handles.axes1)

cla

text(0.2, 0.5, s, 'Interpreter', 'latex')

vel2 = vel^2

vel2 = simplify (vel2)

end

Walter Roberson
on 16 Oct 2018

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

if true

function Resolver4_Callback(hObject, eventdata, handles)

syms m g rt w1 w2 w3 y1 y2 y3

global Px Py Pz gdl

aux1 = Px + Py + Pz;

A =(get(handles.tabla1,'Data'));

if gdl == 1

B = cell2sym( [A(1,1)]);

aux3 = B*1;

end

if gdl == 2

B = cell2sym( [A(1,1), A(2,1)]);

aux3 = B(1,1) * B(1,2)

end

if gdl == 3

B = cell2sym ([A(1,1), A(2,1), A(3,1)])

aux3 = B(1,1) * B(1,2) * B(1,3);

end

if aux3 == 0

errordlg('Ingrese las variables correspondientes por articulación.', 'Error');

else

C = symvar(aux1)

D = setdiff(C,B)

F = setdiff(C,D)

aux2 = gradient(aux1,F)

if gdl == 1

art1 = str2double(get(handles.arti1,'String'));

if art1 == 2

vel = aux2*y1;

else

vel = aux2*w1;

end

vel = expand(vel);

Mc = arrayfun(@char, vel, 'uniform', 0).';

maxlen = max(cellfun(@length, Mc(:)));

EL1 = sprintf('%%%ds, ', maxlen);

fmt = repmat([repmat(EL1, 1, size(vel,2)-1), '%s\n'], 1, size(vel,1));

outstr = regexp(sprintf(fmt, Mc{:}),'\n', 'split');

set(handles.velocidad, 'String', outstr, 'fontname', 'fixed');

end

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);

Mc = arrayfun(@char, vel, 'uniform', 0).';

maxlen = max(cellfun(@length, Mc(:)));

EL1 = sprintf('%%%ds, ', maxlen);

fmt = repmat([repmat(EL1, 1, size(vel,2)-1), '%s\n'], 1, size(vel,1));

outstr = regexp(sprintf(fmt, Mc{:}),'\n', 'split');

set(handles.velocidad, 'String', outstr, 'fontname', 'fixed');

end

if gdl == 3

art1 = str2double(get(handles.arti1,'String'));

art2 = str2double(get(handles.arti2,'String'));

art3 = str2double(get(handles.arti3,'String'));

if art1 == 2

aux5 = aux2(3,1)*y1;

else

aux5 = aux2(3,1)*w1;

end

if art2 == 2

aux6 = aux2(1,1)*y2;

else

aux6 = aux2(1,1)*w2;

end

if art3 == 2

aux7 = aux2(2,1)*y3;

else

aux7 = aux2(2,1)*w3;

end

aux2 = [aux5; aux6; aux7];

vel = sum(aux2);

vel = expand(vel);

Mc = arrayfun(@char, vel, 'uniform', 0).';

maxlen = max(cellfun(@length, Mc(:)));

EL1 = sprintf('%%%ds, ', maxlen);

fmt = repmat([repmat(EL1, 1, size(vel,2)-1), '%s\n'], 1, size(vel,1));

outstr = regexp(sprintf(fmt, Mc{:}),'\n', 'split');

set(handles.velocidad, 'String', outstr, 'fontname', 'fixed');

s = ['$$', latex(vel), '$$'];

axes(handles.axes1)

cla

text(0.2, 0.5, s, 'Interpreter', 'latex')

end

vel2 = vel^2

vel2 = simplify (vel2) end

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

"Second thing I have "P" then I derive and I get "V", in this vector I square the terms and then add them after I simplify the ecuation with trigonometric identities"

You do not do that anywhere in your posted code.

You have vel2 = vel^2 which is squaring whatever terms are already in vel, but you do not add anything to vel2 afterwards so you cannot be referring to vel2 when you say "and add them after"

You have

aux2 = [aux5; aux6];

vel = sum(aux2);

which calculates some terms and then adds them. When you then square vel later, that will be squaring the sum of terms, not squaring the terms and then summing the squares.

Your notebook is as if you square individual terms and then sum them squares. That is not what you appear to have implemented.

It is too much information, "Px", "Py" and "Pz" come from another GUI with another analysis, "art1" and "art2" come from the options of the "radio buttom", the table is created according to GLD, if GLD is 1 it only creates a cell, if it is 2 it creates a vector [2,1] if it is 3 it creates a vector [3,1]. That's why the "if"

But all that is already done, really what I need is to show the result in a better way, that is why I intend to do it with the axis and Latex. I also need to reduce the equation.

For the equation, the "P" of the notebook that is [Px; Py; Pz] to optimize time what I do is add them, then derive the vector "P" with respect to the variables entered in the table1 the rest will be constant as is the case of "d1" to get "V" that would be "vel", then I raise to the square and I have vel2 but I need to simplify it to the maximum since after I replace vel2 in the second law of newton and I generate the equation of LaGrange and I derive partially to obtain the final result.

All this is a robotic analysis but I try to ask directly the problem as easy as possible I adapt the rest.

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

"to optimize time what I do is add them, then derive the vector "P" [...] then I raise to the square"

Yes, that is what the code does. However, that is not the calculation you made in the notebook. The notebook raises each to the square and then adds them.

Suppose the two entries were sin(t1) and cos(t1) . Then your notebook would do sin(t1)^2 + cos(t1)^2 and then uses the trig identity to reduce that to 1 . But your code does sin(t1)+cos(t1) and squares that, (sin(t1) + cos(t1))^2 . Trig identities would simplify that to sin(2*t1) + 1, not the 1 your notebook would expect.

I understand the point, but I get the same vector "V".

code:

notebok:

we need to change "w" and "y".

Itis the same vector but when when I trying to square and simplify I do not get the same result and matlab does not simplify just put parentheses and squares.

The problem is that matlab does not simplify just put parentheses and squares.

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

"I understand the point, but I get the same vector "V"."

You get the same vector under what conditions?

"The problem is that matlab does not simplify just put parentheses and squares."

No, that is not the problem.

syms d_2 theta_1_dot theta_1 d_2_dot

V1 = -d_2*theta_1_dot * cos(theta_1) - d_2_dot*sin(theta_1)

V2 = -d_2*theta_1_dot * sin(theta_1) + d_2_dot * cos(theta_1)

VV = V1^2 + V2^2

simplify(VV)

The answer is d_2^2*theta_1_dot^2 + d_2_dot^2 . Which means that simplify() is able to do the trig simplifications when it is given appropriate input to do so.

Ok I understand, how can I find my error?

what do you think about it?

could be data type? or could be operations?¨

thanks

- 30 questions asked
- 41,061 answers
- 15,684 accepted answers
- reputation: 81,291

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

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.

- 24 questions asked
- 2 answers
- 0 accepted answers
- reputation: 3

You can try this

a = a1^2*a2^2;

diff(a,a1) + diff(a,a2);

ans =

2*a1^2*a2 + 2*a1*a2^2

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.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply TodayUnable to complete the action because of changes made to the page. Reload the page to see its updated state.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

## 0 Comments

Sign in to comment.