FZERO cannot continue because user-supplied function_handle, A and B must be floating-point scalars.

My code is:
p=0.51:0.001:1.013;
fun1 = @(x) x.^2.*exp(-x)./(1-exp(-10));
fun2 = @(x,u) fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]).^2.*exp(-x)./(1-exp(-10));
fun3 = @(u) integral(@(x) fun1(x),0,0.5./(20-p)^0.5./(2.*u))+integral(@(x) fun2(x,u),0.5./(20-p)^0.5./(2.*u),10,'arrayvalued', true);
fun11 = @(x) x.*exp(-x)./(1-exp(-10));
fun21 = @(x,u) fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]).*exp(-x)./(1-exp(-10));
fun31 = @(u) integral(@(x) fun11(x),0,0.5./(20-p)^0.5./(2.*u))+integral(@(x) fun21(x,u),0.5./(20-p)^0.5./(2.*u),10,'arrayvalued', true);
fun4 = @(x,u) (20-x+fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10])-p)^0.5.*exp(-x)./(1-exp(-10));
fun44= @(u) integral(@(x) (20-p).^0.5.*exp(-x)./(1-exp(-10)),0,0.5./(20-p)^0.5./(2.*u))+integral(@(x) fun4(x,u),0.5./(20-p)^0.5./(2.*u),10,'arrayvalued', true);
fun51 =@(p) p - feval(fun31,fzero(@(u) fun3(u)-0.9973,[0.02,1]));
fun52 =@(p) feval(fun44,fzero(@(u) fun3(u)-0.9973,[0.02,1])) - integral(@(x) (20-x).^0.5.*exp(-x)./(1-exp(-10)),0,10);
plot3(p,fun51(p),fun52(p))
format long
Here is the error I get:
FZERO cannot continue because user-supplied function_handle ==> @(u)fun3(u)-0.9973 failed with the error below.
One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power.
I am sorry that my function is a little bit messy. But I am worried about that you may miss some information if I simplify it. Any help is appreciated, thanks!

 Accepted Answer

Make a loop over the elements of p.
"fzero" cannot determine zeros of vector-valued functions (like in fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]) in fun2).

4 Comments

I put a loop in my code as follows:
for p=0.51:0.02:1.01;
fun1 = @(x) x.^2.*exp(-x)./(1-exp(-10));
fun2 = @(x,u) fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]).^2.*exp(-x)./(1-exp(-10));
fun3 = @(u) integral(@(x) fun1(x),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun2(x,u),0.5./(20-p)^0.5./(2.*u),10,'arrayvalued', true);
fun11 = @(x) x.*exp(-x)./(1-exp(-10));
fun21 = @(x,u) fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]).*exp(-x)./(1-exp(-10));
fun31 = @(u) integral(@(x) fun11(x),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun21(x,u),0.5./(20-p).^0.5./(2.*u),10,'arrayvalued', true);
fun4 = @(x,u) (20-x+fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10])-p).^0.5.*exp(-x)./(1-exp(-10));
fun44= @(u) integral(@(x) (20-p).^0.5.*exp(-x)./(1-exp(-10)),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun4(x,u),0.5./(20-p).^0.5./(2.*u),10,'arrayvalued', true);
plot3(p,p - feval(fun31,fzero(@(u) fun3(u)-0.9973,[0.02,1])),feval(fun44,fzero(@(u) fun3(u)-0.9973,[0.02,1])) - integral(@(x) (20-x).^0.5.*exp(-x)./(1-exp(-10)),0,10),'bo')
format long
end
The good news is the error disappears. But just single point shows in the figure. Any one could help for this code?
format long
pvals = 0.51:0.02:1.01;
nump = length(pvals);
y = zeros(1, nump);
z = zeros(1, nump);
for pidx = 1 : nump
p = pvals(pidx);
fun1 = @(x) x.^2.*exp(-x)./(1-exp(-10));
fun2 = @(x,u) fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]).^2.*exp(-x)./(1-exp(-10));
fun3 = @(u) integral(@(x) fun1(x),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun2(x,u),0.5./(20-p)^0.5./(2.*u),10,'arrayvalued', true);
fun11 = @(x) x.*exp(-x)./(1-exp(-10));
fun21 = @(x,u) fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]).*exp(-x)./(1-exp(-10));
fun31 = @(u) integral(@(x) fun11(x),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun21(x,u),0.5./(20-p).^0.5./(2.*u),10,'arrayvalued', true);
fun4 = @(x,u) (20-x+fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10])-p).^0.5.*exp(-x)./(1-exp(-10));
fun44= @(u) integral(@(x) (20-p).^0.5.*exp(-x)./(1-exp(-10)),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun4(x,u),0.5./(20-p).^0.5./(2.*u),10,'arrayvalued', true);
y(pidx) = p - feval(fun31,fzero(@(u) fun3(u)-0.9973,[0.02,1]));
z(pidx) = feval(fun44,fzero(@(u) fun3(u)-0.9973,[0.02,1])) - integral(@(x) (20-x).^0.5.*exp(-x)./(1-exp(-10)),0,10);
end
plot3(pvals, y, z, 'bo')
Thank you so much! How can I accept your this comment?
Just accept Torsten's answer, since he provided the key about looping over p.

Sign in to comment.

More Answers (2)

Put .^ instead of ^

2 Comments

Thanks. I change it, now the error becomes:
FZERO cannot continue because user-supplied function_handle ==> @(u)fun3(u)-0.9973 failed with the error below.
A and B must be floating-point scalars.
Error in CaseB>@(p)p-feval(fun31,fzero(@(u)fun3(u)-0.9973,[0.02,1]))
Error in CaseB (line 29)
plot3(p,fun51(p),fun52(p))
Now the code is:
p=0.51:0.001:1.013;
fun1 = @(x) x.^2.*exp(-x)./(1-exp(-10));
fun2 = @(x,u) fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]).^2.*exp(-x)./(1-exp(-10));
fun3 = @(u) integral(@(x) fun1(x),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun2(x,u),0.5./(20-p)^0.5./(2.*u),10,'arrayvalued', true);
fun11 = @(x) x.*exp(-x)./(1-exp(-10));
fun21 = @(x,u) fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10]).*exp(-x)./(1-exp(-10));
fun31 = @(u) integral(@(x) fun11(x),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun21(x,u),0.5./(20-p).^0.5./(2.*u),10,'arrayvalued', true);
fun4 = @(x,u) (20-x+fzero(@(y) 2.*u.*y.*(20-p-x+y).^0.5-0.5,[0,10])-p).^0.5.*exp(-x)./(1-exp(-10));
fun44= @(u) integral(@(x) (20-p).^0.5.*exp(-x)./(1-exp(-10)),0,0.5./(20-p).^0.5./(2.*u))+integral(@(x) fun4(x,u),0.5./(20-p).^0.5./(2.*u),10,'arrayvalued', true);
fun51 =@(p) p - feval(fun31,fzero(@(u) fun3(u)-0.9973,[0.02,1]));
fun52 =@(p) feval(fun44,fzero(@(u) fun3(u)-0.9973,[0.02,1])) - integral(@(x) (20-x).^0.5.*exp(-x)./(1-exp(-10)),0,10);
plot3(p,fun51(p),fun52(p))
format long
Error is:
FZERO cannot continue because user-supplied function_handle ==> @(u)fun3(u)-0.9973 failed with the error below.
A and B must be floating-point scalars.
Any help is really appreciated, thanks!

Sign in to comment.

AUXILIOOO COMO RESUELVO ESTO?
Error using fzero>localFirstFcnEvalError FZERO cannot continue because user-supplied function_handle ==> @(t)((R*t)/(Vmol-b))-((psi*((R^2*Tc^2*(((1+(0.48+(1.574*w)-(0.176*w^2))*(1-sqrt(t/Tc))))^2))/Pc))/((Vmol+(epsilon*b))*(V+(sigma*b))))-P failed with the error below. Error: File: V.m Line: 49 Column: 9 Using 'V' as both the name of a variable and the name of a script is not supported.

1 Comment

Using 'V' as both the name of a variable and the name of a script is not supported.
El mensaje de error es obvio. Cambie el nombre de la variable "V" en su código o asigne a su secuencia de comandos un nombre diferente.
Y dado que el identificador de su función se llamará con vectores para t, debe usar la multiplicación y división por componentes (.* y ./) en lugar de la multiplicación normal en los casos en que sea necesario.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Asked:

on 10 Jan 2019

Edited:

on 31 Mar 2023

Community Treasure Hunt

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

Start Hunting!