FSOLVE requires all values returned by functions to be of data type double. showing while solving an algebraic equation in matlab ??
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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
Walter Roberson
on 8 Dec 2023
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??
Can you share the modified code and the error(s) you got?
Surendra Ratnu
on 8 Dec 2023
Edited: Sam Chak
on 8 Dec 2023
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);
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
Dyuman Joshi
on 8 Dec 2023
Edited: Dyuman Joshi
on 5 Feb 2024
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
It works. Thank you for your suggestions
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
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: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)