FSOLVE requires all values returned by functions to be of data type double. showing while solving an algebraic equation in matlab ??

I want to solve the eqs for u_s and find out the value of u_s but i am getting error while solving the equation using fsolve. Can you provide some solution for this problem ?? Thank you
type untitled03.m
clc clear close all a = deg2rad(45); m = 0.683; We = 277; Re = 75; b = (-0.13*m.^3+0.263*m.^2 +0.039*m +0.330)/tan(a); s_b = 0.0042; p = pi/2; R = 1.7440; t = deg2rad(0); syms q x = b*cos(t)*sin(a)^2; x01 = 4*((1-cos(t)^2*cos(a)^2)); x02 = (b*sin(t)*sin(a))^2; q_j = -((x -(x01-x02)^0.5)/(x01/4)); d = (b^2*sin(a)^2 + q^2*(1-cos(t)^2*cos(a)^2) + 2*b*q*cos(t)*sin(a)^2)^0.5; u_j = 2*(m-1)*(4*d^2-1) + m; F = int(q*u_j, q, 0, q_j); G = int(q*u_j^3,q,0,q_j); eqs = @(u_s)((4*sin(a).^3*F.^4*(R-q_j)*(R.^5-q_j.^5))/(15*q_j.^7*R.^5*Re))+... ((((R.^3-q_j.^3)*F.^3*sin(a).^3)/(9*q_j.^3*R.^3))-((R-q_j)*F*((4/q_j)+sin(a))))*u_s+... (R-q_j)*F*sin(a)*u_s.^3+... (R.^2 +2*R*sqrt(pi*s_b))+(16*F.^3*sin(a)*(R-q_j).^2)/(3*q_j.^3*R*Re)*u_s^2; sol = fsolve(eqs,0.5);

 Accepted Answer

F and G are int() calls. Even if they evaluate to closed forms that were rational numbers, they have symbolic results. When you create an anonymous function that includes F and G, any numeric coefficients will be converted to symbolic at the point F or G is encountered, so the anonymous function will return something of data type sym even in the best possible case. But fsolve requires single or double. fsolve refuses to even try to convert sym to double.
Instead of constructing that anonymous function you should use matlabFunction()

7 Comments

Thank you for your suggestions. I tried matlabfunction but it showing another error "Operator '.^' is not supported for operands of type 'function_handle'. if i am use ^ instead .^ still it showing error "Error in ^".
One thing also after putting all values F is the numeric type then why is it showing data type error??
Yes, Sure
type untitled3.m
clc clear close all a = deg2rad(45); m = 0.683; We = 277; Re = 75; b = (-0.13*m^3+0.263*m^2 +0.039*m +0.330)/tan(a); s_b = 0.0042; p = pi/2; R = 1.7440; t = deg2rad(0); syms q x = b*cos(t)*sin(a)^2; x01 = 4*((1-cos(t)^2*cos(a)^2)); x02 = (b*sin(t)*sin(a))^2; q_j = -((x -(x01-x02)^0.5)/(x01/4)); d = (b^2*sin(a)^2 + q^2*(1-cos(t)^2*cos(a)^2) + 2*b*q*cos(t)*sin(a)^2)^0.5; u_j = 2*(m-1)*(4*d^2-1) + m; F = int(q*u_j, q, 0, q_j); G = int(q*u_j^3,q,0,q_j); F = matlabFunction(F); G = matlabFunction(G); eqs = @(u_s)((4*sin(a)^3*F^4*(R-q_j)*(R^5-q_j^5))/(15*q_j^7*R^5*Re))+... ((((R^3-q_j^3)*F^3*sin(a)^3)/(9*q_j^3*R^3))-((R-q_j)*F*((4/q_j)+sin(a))))*u_s+... (R-q_j)*F*sin(a)*u_s^3+... (R^2 +2*R*sqrt(pi*s_b))+(16*F^3*sin(a)*(R-q_j)^2)/(3*q_j^3*R*Re)*u_s^2; sol = fsolve(eqs,0.5);
@Surendra Ratnu, Use double() can solve the problem.
a = deg2rad(45); m = 0.683; We = 277; Re = 75;
b = (-0.13*m^3+0.263*m^2 +0.039*m +0.330)/tan(a);
s_b = 0.0042; p = pi/2; R = 1.7440; t = deg2rad(0);
syms q
x = b*cos(t)*sin(a)^2; x01 = 4*((1-cos(t)^2*cos(a)^2)); x02 = (b*sin(t)*sin(a))^2;
q_j = -((x -(x01-x02)^0.5)/(x01/4));
d = (b^2*sin(a)^2 + q^2*(1-cos(t)^2*cos(a)^2) + 2*b*q*cos(t)*sin(a)^2)^0.5;
u_j = 2*(m-1)*(4*d^2-1) + m;
F = int(q*u_j, q, 0, q_j);
G = int(q*u_j^3,q,0,q_j);
F = double(F)
F = -12.3408
G = double(G)
G = -495.0286
eqs = @(u_s)((4*sin(a)^3*F^4*(R-q_j)*(R^5-q_j^5))/(15*q_j^7*R^5*Re))+...
((((R^3-q_j^3)*F^3*sin(a)^3)/(9*q_j^3*R^3))-((R-q_j)*F*((4/q_j)+sin(a))))*u_s+...
(R-q_j)*F*sin(a)*u_s^3+...
(R^2 +2*R*sqrt(pi*s_b))+(16*F^3*sin(a)*(R-q_j)^2)/(3*q_j^3*R*Re)*u_s^2;
sol = fsolve(eqs,0.5)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = 0.3475
The matlabFunction is to be applied on eqs -
%Constants
a = deg2rad(45); m = 0.683; We = 277; Re = 75;
b = (-0.13*m.^3+0.263*m.^2 +0.039*m +0.330)/tan(a);
s_b = 0.0042; p = pi/2; R = 1.7440; t = deg2rad(0);
syms q
x = b*cos(t)*sin(a)^2; x01 = 4*((1-cos(t)^2*cos(a)^2)); x02 = (b*sin(t)*sin(a))^2;
q_j = -((x -(x01-x02)^0.5)/(x01/4));
d = (b^2*sin(a)^2 + q^2*(1-cos(t)^2*cos(a)^2) + 2*b*q*cos(t)*sin(a)^2)^0.5;
u_j = 2*(m-1)*(4*d^2-1) + m;
F = int(q*u_j, q, 0, q_j);
G = int(q*u_j^3,q,0,q_j);
%Define eqs as a symbolic expression of symbolic variable u_s
syms u_s
eqs = ((4*sin(a).^3*F.^4*(R-q_j)*(R.^5-q_j.^5))/(15*q_j.^7*R.^5*Re))+...
((((R.^3-q_j.^3)*F.^3*sin(a).^3)/(9*q_j.^3*R.^3))-((R-q_j)*F*((4/q_j)+sin(a))))*u_s+...
(R-q_j)*F*sin(a)*u_s.^3+...
(R.^2 +2*R*sqrt(pi*s_b))+(16*F.^3*sin(a)*(R-q_j).^2)/(3*q_j.^3*R*Re)*u_s^2;
%Convert the symbolic expression to a function handle
eqn = matlabFunction(eqs, 'Vars', u_s)
eqn = function_handle with value:
@(u_s)u_s.*(sqrt(2.0).*6.020431799008798-1.899208852577259e+1)+sqrt(2.0).*1.146934754833616e-1-sqrt(2.0).*u_s.^2.*1.172403304923791+sqrt(2.0).*u_s.^3.*3.989296385175077+3.442196065609545
%call fsolve() on the function handle with an initial point
sol = fsolve(eqn,0.5)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = 0.3475
Hello @Surendra Ratnu, if this answer solved your problem, please consider accepting the answer.

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Tags

Community Treasure Hunt

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

Start Hunting!