dear all
i write cod that i want to solve it with fsolve commend
at the end, my equations just contain double parameters but in the middle of my function i must consider some symbolic parameters that i remove them after some calculations
but when i run my function i got error : FSOLVE requires all values returned by functions to be of data type double and point the line i define my symbolic variable but i mention again that my final equation doesnt contain any of those symbolic var
is there any way to my problem

4 comentarios

shahin hashemi
shahin hashemi el 23 de Dic. de 2017
Editada: shahin hashemi el 23 de Dic. de 2017
function F = consnt7(x)
m=1.669e-3;
jx=5.743e-14;
E=2.1e11;
O=12.5e-3;
jz=1.149e-13;
G=8e10;
L=3e-3;
g=9.81;
N=1;
T1=5;
T2=0;
T3=0;
xd = sym('xd', [1 3*N], 'real');
r(:,:,1)=[1*O;0;0;];
r(:,:,2)=[(-1/2)*O;(sqrt(3))*O/2;0];
r(:,:,3)=[(-1/2)*O;-(sqrt(3))*O/2;0];
for j=2:N+1
for i=j-1
pl(:,:,j)=L*[(cos(x(3*i-2))*(1-cos(x(3*i-1))))/x(3*i-1) (sin(x(3*i-2))*(1-cos(x(3*i-1))))/x(3*i-1) sin(x(3*i-1))/x(3*i-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))];
p(:,:,2)=pl(:,:,2);
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);
p(:,:,i)=p(:,:,i-1)+R(:,:,i-1)*pl(:,:,i);
end
for j=2:N+1
for i=j-1
wl(:,:,j)=[-sin(x(3*i-2))*xd(3*i-1)+cos(x(3*i-2))*sin(x(3*i-1))*xd(3*i);cos(x(3*i-2))*xd(3*i-1)+sin(x(3*i-2))*sin(x(3*i-1))*xd(3*i);xd(3*i-2)+cos(x(3*i-1))*xd(3*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),[xd(1),xd(2),xd(3)]);%,xd(4),xd(5),xd(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),[xd(1),xd(2),xd(3)]);%,xd(4),xd(5),xd(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,[xd(1),xd(2),xd(3)]);%,xd(4),xd(5),xd(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
for j=2:N+1
for i=j-1
pd(:,:,j)=L*[(-sin(x(3*i-2))*((1-cos(x(3*i-1)))/x(3*i-1))*xd(3*i-2))+(cos(x(3*i-2))*((sin(x(3*i-1))*x(3*i-1))-(1-cos(x(3*i-1)))/(x(3*i-1)^2))*xd(3*i-1));(cos(x(3*i-2))*((1-cos(x(3*i-1)))/x(3*i-1))*xd(3*i-2))+(sin(x(3*i-2))*((sin(x(3*i-1))*x(3*i-1))-(1-cos(x(3*i-1)))/(x(3*i-1)^2))*xd(3*i-1));(((cos(x(3*i-1))*x(3*i-1))-sin(x(3*i-1)))/(x(3*i-1)^2))*xd(3*i-1)];
v(:,:,2)=pd(:,:,2);
end
end
for i=3:N+1
v(:,:,i)=v(:,:,i-1)+cross(w(:,:,i-1),R(:,:,i-1)*pl(:,:,i))+R(:,:,i-1)*pd(:,:,i);
end
for i=2:N+1
vc(:,:,i)=collect(v(:,:,i),[xd(1),xd(2),xd(3)]);%),xd(4),xd(5),xd(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(v(:,:,i),1)
[vwtemp,vvtemp]=coeffs(vc(j,:,i),[xd(1),xd(2),xd(3)]);%),xd(4),xd(5),xd(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(vvtemp,[xd(1),xd(2),xd(3)]);%,xd(4),xd(5),xd(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)]);
vk(j,idx,i)=vwtemp;
end
vk(:,3*(i-1),i)=[0;0;0];
end
for j=N+1
for i=j-1
Mb(:,:,j)=E*jx*((x(3*i-1))/L)*R(:,:,j-1)*[-sin(x(3*i-2));cos(x(3*i-2));0];
Mt(:,:,j)=-G*jz*(x(3*i-2)+x(3*i))*R(:,1,i)/L;
Me(:,:,j)=Mt(:,:,i)-Mb(:,:,i);
end
end
for j=2:N
for i=j-1
Mb(:,:,j)=E*jx*((x(3*i-1))/L)*R(:,:,j-1)*[-sin(x(3*i-2));cos(x(3*i-2));0];
end
end
for j=2:N
for i=j-1
Mt(:,:,j)=-G*jz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,:,i)=Fc(:,1,i)+Fc(:,2,i)+Fc(:,3,i);
end
for i=2:N+1
Ma(:,:,i)=cross(r(:,:,1),Fc(:,1,i))+cross(r(:,:,2),Fc(:,2,i))+cross(r(:,:,3),Fc(:,3,i));
end
for i=2:N+1
feq(:,:,i)=fa(:,:,i) +Fg(:,:,i);
Meq(:,:,i)=Ma(:,:,i)+Me(:,:,i);
end
for j=1:(3*N)
for i=2:N+1
F(j,:,:)= dot(Meq(:,:,i),wk(:,j,i))+dot(feq(:,:,i),vk(:,j,i));
end
end
shahin hashemi
shahin hashemi el 23 de Dic. de 2017
this is my cod and as its is obvious my main variable is x but i must define xd but i remove them after some calculations with "collect" and "coeffs" commend
x0=[0;0.1;0];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@consnt6,x0,options)
shahin hashemi
shahin hashemi el 23 de Dic. de 2017
my final F is 3*1 matrix that only contain x1 x2 x3

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Dic. de 2017
Editada: Walter Roberson el 23 de Dic. de 2017

0 votos

It is not possible to do what you are asking to do.
I already told you what you need to do to solve this: you need to run your code symbolically, and then use matlabFunction to convert the resulting symbolic expressions to function handles.

5 comentarios

thank you mr walter for your solutions
but i dont understand what do you mean by "I already told you" and i really be apreciated if you could gave me an example
i search the net and i face with smth like this :
function y = example373677
y = fsolve(@mysubfun, 3);
function z = mysubfun(q)
x = sym('x');
r = x.^3-2*x-5;
z = double(subs(r, q));
i that what you mean ?
x = sym('x', [1 3], 'real');
fun_sym = consnt7(x);
fun = matlabFunction(fun_sym, 'vars', {x.'});
x0=[0;0.1;0];
options = optimoptions('fsolve','Display','iter');
z = fsolve(fun, x0, options)
Note: there are a number of different solutions even with x(1) = 0 and x(2) = 0
dear mr walter
did you run the cod with :
x = sym('x', [1 3], 'real');
fun_sym = consnt7(x);
fun = matlabFunction(fun_sym, 'vars', {x.'});
x0=[0;0.1;0];
options = optimoptions('fsolve','Display','iter');
z = fsolve(fun, x0, options)
and if you run could you plz tell me where shold i put them ?
Yes, I ran that code, and got back numeric values in z.
This code should replace your current
x0=[0;0.1;0];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@consnt6,x0,options)
shahin hashemi
shahin hashemi el 24 de Dic. de 2017
thank you mr walter finaly my problem solved with your help
happy new year with best wishes

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 24 de Dic. de 2017
Editada: Matt J el 24 de Dic. de 2017

0 votos

but i mention again that my final equation doesnt contain any of those symbolic var
But your final F is generated from calculations with these variables, so they inherit their symbolic type. At no point in your code, do you do anything to convert from symbolic to double. When I run your function on some sample input, I obtain
>> class(consnt7([1,1,1]))
ans =
'sym'
So, as you can see, your output is still of type sym.
but i remove them after some calculations with "collect" and "coeffs" commend
Nope. The output of these commands are still of type sym. I explained to you here (and you thanked me emphatically) that the commmand double() was the way to convert from sym to double.

1 comentario

thank you mr matt for your answer
and i should say i do what you say befor and i can run cod for N=1
function F = consnt4(x)
m=1.669e-3;
jx=5.743e-14;
E=2.1e11;
O=12.5e-3;
jz=1.149e-13;
G=8e10;
L=3e-3;
g=9.81;
N=3;
T1=2;
T2=1;
T3=0.5;
r1=[1*O;0;0;];
r2=[(-1/2)*O;(sqrt(3))*O/2;0];
r3=[(-1/2)*O;-(sqrt(3))*O/2;0];
pl=L*[(cos(x(1))*(1-cos(x(2))))/x(2) (sin(x(1))*(1-cos(x(2))))/x(2) sin(x(2))/x(2)]';
wk=[0 -sin(x(1)) cos(x(1))*sin(x(2));0 cos(x(1)) sin(x(1))*sin(x(2));1 0 cos(x(2))];
vk=L*[-sin(x(1))*((1-cos(x(2)))/x(2)) (cos(x(1))/x(2)^2)*(sin(x(2))*x(2)-(1-cos(x(2)))) 0;cos(x(1))*((1-cos(x(2)))/x(2)) (sin(x(1))/(x(2)^2))*((sin(x(2))*x(2))-(1-cos(x(1)))) 0;0 (cos(x(2))*x(2)-sin(x(2)))/(x(2)^2) 0];
R=[cos(x(1))*cos(x(2))*cos(x(3))-sin(x(1))*sin(x(3)) -cos(x(1))*cos(x(2))*sin(x(3))-sin(x(1))*cos(x(3)) cos(x(1))*sin(x(2));sin(x(1))*cos(x(2))*cos(x(3))+cos(x(1))*sin(x(3)) -sin(x(1))*cos(x(2))*sin(x(3))+cos(x(1))*cos(x(3)) sin(x(1))*sin(x(2));-sin(x(2))*cos(x(3)) sin(x(2))*sin(x(3)) cos(x(2))];
Mb=E*jx*((x(2))/L)*[1 0 0;0 1 0;0 0 1]*[-sin(x(1));cos(x(1));0];
Mt=-G*jz*(x(1)+x(3))*R(:,3)/L;
Me=Mb+Mt;
Fg=-m*g*[1;0;0];
ph(:,1)=[pl+R*r1-r1];
ph(:,2)=[pl+R*r2-r2];
ph(:,3)=[pl+R*r3-r3];
ap=[sqrt(ph(1,1)^2+ph(2,1)^2+ph(3,1)^2);sqrt(ph(1,2)^2+ph(2,2)^2+ph(3,2)^2);sqrt(ph(1,3)^2+ph(2,3)^2+ph(3,3)^2)];
f=[ph(:,1)/ap(1,1) ph(:,2)/ap(2,1) ph(:,3)/ap(3,1)];
Fc=[-T1*f(:,1) -T2*f(:,2) -T3*f(:,3)];
fa=Fc(:,1)+Fc(:,2)+Fc(:,3);
Ma=cross(r1,Fc(:,1))+cross(r2,Fc(:,2))+cross(r3,Fc(:,3));
feq=fa +Fg;
Meq=Ma+ Me;
F(1,:)= [dot(Meq,wk(:,1))+dot(feq,vk(:,1))];
F(2,:)= [dot(Meq,wk(:,2))+dot(feq,vk(:,2))];
F(3,:)= [dot(Meq,wk(:,3))+dot(feq,vk(:,3))];
and for fsolve :
x0=[0;0.1;0];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@consnt4,x0,options)
and now i try for N>1 that cause to appear xd
and now when i try your solution i mean using double(sub()) again i face with same error and i think i cant use syms in my main function that call by f solve and i should do it in smth like sub functions ?

Iniciar sesión para comentar.

Productos

Etiquetas

Preguntada:

el 23 de Dic. de 2017

Comentada:

el 24 de Dic. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by