# how can i rewrite an equation base on variables

1 view (last 30 days)
shahin hashemi on 9 Dec 2017
Commented: Walter Roberson on 22 Dec 2017
dear all
i have 3*1 matrix like matrix E below
syms qd td ed q teta
E =[ ed*cos(q)*sin(teta) - cos(teta)*(qd*cos(q)*sin(teta) + td*cos(teta)*sin(q)) - td*sin(q)*sin(teta)^2 ; cos(teta)*(td*cos(teta)*sin(q) - qd*sin(q)*sin(teta)) + ed*sin(q)*sin(teta) + td*cos(q)*sin(teta)^2 ; ed*cos(teta) + cos(q)*sin(teta)*(qd*cos(q)*sin(teta) + td*cos(teta)*sin(q)) - sin(q)*sin(teta)*(td*cos(teta)*sin(q) - qd*sin(q)*sin(teta))]
that i want to rewrite it in [3*3]*[qd;td;ed]=[E] this form
is there any Command that can help me to calculate this [3*3] matrix by matlab
[ -cos(q)*cos(teta)*sin(teta), - cos(teta)^2*sin(q) - sin(q)*sin(teta)^2, cos(q)*sin(teta)]
[ -cos(teta)*sin(q)*sin(teta), sin(q)*cos(teta)^2 + cos(q)*sin(teta)^2, sin(q)*sin(teta)]
[ cos(q)^2*sin(teta)^2 + sin(q)^2*sin(teta)^2, cos(q)*cos(teta)*sin(q)*sin(teta) - cos(teta)*sin(q)^2*sin(teta), cos(teta)]
i really apreciated if you could help me

Birdman on 9 Dec 2017
Try the following code:
syms qd td ed q teta
E =[ ed*cos(q)*sin(teta) - cos(teta)*(qd*cos(q)*sin(teta) + td*cos(teta)*sin(q)) - td*sin(q)*sin(teta)^2 ; cos(teta)*(td*cos(teta)*sin(q) - qd*sin(q)*sin(teta)) + ed*sin(q)*sin(teta) + td*cos(q)*sin(teta)^2 ; ed*cos(teta) + cos(q)*sin(teta)*(qd*cos(q)*sin(teta) + td*cos(teta)*sin(q)) - sin(q)*sin(teta)*(td*cos(teta)*sin(q) - qd*sin(q)*sin(teta))]
Ecollected=collect(E,[qd,td,ed])
for i=1:size(E,1)
E3by3form(i,:)=coeffs(Ecollected(i),[qd,td,ed])
end
Let me know the results. The answer you want is stored in E3by3form.

Walter Roberson on 22 Dec 2017
You simply cannot do that.
Calculate the symbolic form first, and use matlabFunction() on it to get the function handle to pass to fsolve()
shahin hashemi on 22 Dec 2017
dear mr walter raberson thank you for your attention
function wk = consnt7(x)
L=3e-3;
N=1;
for j=2:N+1
for i=j-1
Rl(:,:,j)=[cos(x(3*i-2))*cos(x(3*i-1))*cos(x(3*i))-sin(x(3*i-2))*sin(x(3*i)) -cos(x(3*i-2))*cos(x(3*i-1))*sin(x(3*i))-sin(x(3*i-2))*cos(x(3*i)) cos(x(3*i-2))*sin(x(3*i-1));sin(x(3*i-2))*cos(x(3*i-1))*cos(x(3*i))+cos(x(3*i-2))*sin(x(3*i)) -sin(x(3*i-2))*cos(x(3*i-1))*sin(x(3*i))+cos(x(3*i-2))*cos(x(3*i)) sin(x(3*i-2))*sin(x(3*i-1));-sin(x(3*i-1))*cos(x(3*i)) sin(x(3*i-1))*sin(x(3*i)) cos(x(3*i-1))]; end
end
R(:,:,2)=Rl(:,:,2);
R(:,:,1)=[1 0 0;0 1 0;0 0 1];
for i=3:N+1
R(:,:,i)=R(:,:,i-1)*Rl(:,:,i);
end
for j=2:N+1
for i=j-1
wl(:,:,j)=[-sin(x(3*i-2))*x(6*i-1)+cos(x(3*i-2))*sin(x(3*i-1))*x(6*i);cos(x(3*i-2))*x(6*i-1)+sin(x(3*i-2))*sin(x(3*i-1))*x(6*i);x(6*i-2)+cos(x(3*i-1))*x(6*i)];
w(:,:,2)=wl(:,:,2);
end
end
for i=3:N+1
w(:,:,i)= w(:,:,i-1)+R(:,:,i-1)*wl(:,:,i);
end
for i=2:N+1
wc(:,:,i)=collect(w(:,:,i),[x(4),x(5),x(6)]);%,xd(7),xd(8),xd(9),xd(10),xd(11),xd(12) ,xd(13),xd(14),xd(15),xd(16),xd(17),xd(18),xd(19),xd(20),xd(21),xd(22),xd(23),xd(24)]);
end
for i=2:N+1
for j=1:size(w(:,:,i),1)
[wtemp,vtemp]=coeffs(wc(j,:,i),[x(4),x(5),x(6)]);%,xd(7),xd(8)]);%,xd(9),xd(10),xd(11),xd(12),xd(13),xd(14),xd(15),xd(16),xd(17),xd(18),xd(19),xd(20),xd(21),xd(22),xd(23),xd(24)]);
[~,idx]=ismember(vtemp,[x(4),x(5),x(6)]);%,xd(7),xd(8)]);%,xd(9),xd(10),xd(11),xd(12),xd(13),xd(14),xd(15),xd(16),xd(17),xd(18),xd(19),xd(20),xd(21),xd(22),xd(23),xd(24)]);
wk(j,idx,i)=wtemp
end
end
this is a part of my cod and as you can see i use fucntions and then i run this :
x0=[0;0.1;0;0.01;0.001;0.0001];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@consnt7,x0,options)
but i face this error :
Undefined function 'collect' for input arguments of type 'double'.
Walter Roberson on 22 Dec 2017
That cannot be solved.
x = sym('x',[1 6]);
consnt7(x)
ans(:,:,1) =
[ 0, 0, 0]
[ 0, 0, 0]
[ 0, 0, 0]
ans(:,:,2) =
[ 0, -sin(x1), cos(x1)*sin(x2)]
[ 0, cos(x1), sin(x1)*sin(x2)]
[ 1, 0, cos(x2)]
The first pane of the 3D answers can be solved, as it is already all 0. However, in the second pane, ans(:,:,2), there is no x1 such that -sin(x1) == 0 at the same time that cos(x1) == 0 -- and even if there were, the 1 in the bottom left corner, position ans(3,1,2), can never be equal to 0, so there is no possible input vector such that the result of the function is all zeros.

### Community Treasure Hunt

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

Start Hunting!