fsolve problem with syms var

3 次查看(过去 30 天)
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 个评论
shahin hashemi
shahin hashemi 2017-12-23
x0=[0;0.1;0];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@consnt6,x0,options)
shahin hashemi
shahin hashemi 2017-12-23
my final F is 3*1 matrix that only contain x1 x2 x3

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-12-23
编辑:Walter Roberson 2017-12-23
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 个评论
Walter Roberson
Walter Roberson 2017-12-24
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 2017-12-24
thank you mr walter finaly my problem solved with your help
happy new year with best wishes

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2017-12-24
编辑:Matt J 2017-12-24
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 个评论
shahin hashemi
shahin hashemi 2017-12-24
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 ?

请先登录,再进行评论。

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by