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);
채택된 답변
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
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?
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
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
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
