FSOLVE requires all values returned by user functions to be of data type double

5 views (last 30 days)
syms x;
for i=1:8
xe=[0.07142
0.10701
0.14246
0.1778
0.21306
0.24857
0.28524
0.32688];
p1xe(i,1)=2.*asin(2.*xe(i,1))+4.*xe(i,1).*sqrt(1-(4.*xe(i,1).^2));
p1xc(i,1)=2.*asin(2.*x)+4.*x.*sqrt(1-(4.*x.^2));
p2xc(i,1)=sqrt(1-(4.*x.^2));
b(i,1)=1/(2.*x.*(1+(p1xc(i,1)/(16.*x.*p2xc(i,1))))); %b(i,1) haman R/H mib(i,1)ashad.
a0(i,1)=1;
a1(i,1)=(1/2).*b(i,1);
a2(i,1)=(1/8).*((b(i,1).^2)+4);
a3(i,1)=(1/16).*((b(i,1).^3)-(4.*b(i,1)));
a4(i,1)=(5/128).*((b(i,1).^4)-(8/5).*b(i,1).^2+(16/5));
a5(i,1)=(7/256).*(b(i,1).^5-(8/7).*b(i,1).^3-(16/7).*b(i,1));
a6(i,1)=(21/1024).*((b(i,1).^6)-(20/21).*b(i,1).^4-(16/21).*b(i,1).^2+(64/21));
a7(i,1)=(231/14336).*((b(i,1).^7)-(28/33).*b(i,1).^5-(112/231).*b(i,1).^3-(448/231).*b(i,1));
p3xc(i,1)=((a0(i,1).*x)-(a1(i,1).*(x.^2))-((4/3).*a2(i,1).*(x.^3))-((8/4).*a3(i,1).*(x.^4))...
-((16/5).*a4(i,1).*(x.^5))-((32/6).*a5(i,1).*(x.^6))-((64/7).*a6(i,1).*(x.^7))-((128/8).*a7(i,1).*(x.^8)));
%Function handle:
f=@(x)(p1xc(i,1).^(5/2)/(32.*p2xc(i,1).^(1/2).*p3xc(i,1))).*(x+(p1xc(i,1)/(16.*p2xc(i,1)))).^(-0.5)-p1xe(i,1);
xc(i,1) = fsolve(f,0.07)
i get this error: FSOLVE requires all values returned by user functions to be of data type double.
.how can i solve it?

Accepted Answer

Matt J
Matt J on 5 Jul 2020
Edited: Matt J on 5 Jul 2020
The result I get from this is,
xc =
0.1000
0.1500
0.2000
0.2500
0.3000
0.3500
0.4000
0.4500
Seems like a bit of a simple progression for such a complicated compilation of equations...
function xc=runit
xc=nan(8,1); %pre-allocate
for i=1:8
xc(i) = fsolve(@myfunction,0.07);
end
function f=myfunction(x) %NESTED FUNCTION
xe=[0.07142
0.10701
0.14246
0.1778
0.21306
0.24857
0.28524
0.32688];
p1xe(i,1)=2.*asin(2.*xe(i,1))+4.*xe(i,1).*sqrt(1-(4.*xe(i,1).^2));
p1xc(i,1)=2.*asin(2.*x)+4.*x.*sqrt(1-(4.*x.^2));
p2xc(i,1)=sqrt(1-(4.*x.^2));
b(i,1)=1/(2.*x.*(1+(p1xc(i,1)/(16.*x.*p2xc(i,1))))); %b(i,1) haman R/H mib(i,1)ashad.
a0(i,1)=1;
a1(i,1)=(1/2).*b(i,1);
a2(i,1)=(1/8).*((b(i,1).^2)+4);
a3(i,1)=(1/16).*((b(i,1).^3)-(4.*b(i,1)));
a4(i,1)=(5/128).*((b(i,1).^4)-(8/5).*b(i,1).^2+(16/5));
a5(i,1)=(7/256).*(b(i,1).^5-(8/7).*b(i,1).^3-(16/7).*b(i,1));
a6(i,1)=(21/1024).*((b(i,1).^6)-(20/21).*b(i,1).^4-(16/21).*b(i,1).^2+(64/21));
a7(i,1)=(231/14336).*((b(i,1).^7)-(28/33).*b(i,1).^5-(112/231).*b(i,1).^3-(448/231).*b(i,1));
p3xc(i,1)=((a0(i,1).*x)-(a1(i,1).*(x.^2))-((4/3).*a2(i,1).*(x.^3))-((8/4).*a3(i,1).*(x.^4))...
-((16/5).*a4(i,1).*(x.^5))-((32/6).*a5(i,1).*(x.^6))-((64/7).*a6(i,1).*(x.^7))-((128/8).*a7(i,1).*(x.^8)));
%Function handle:
f=(p1xc(i,1).^(5/2)/(32.*p2xc(i,1).^(1/2).*p3xc(i,1))).*(x+(p1xc(i,1)/(16.*p2xc(i,1)))).^(-0.5)-p1xe(i,1);
end
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!