how to solve this error: FSOLVE requires all values returned by functions to be of data type double?

2 visualizaciones (últimos 30 días)
C=1;
n=20 ;
Cp=C+0.33;
alpha=0.01;
f=@(y) normpdf(y+3.*(Cp-C).*sqrt(n))+normpdf(y-3.*(Cp-C).*sqrt(n));
G=@(c0,y) chi2cdf(((n-1).*(3.*Cp.*sqrt(n)-y).^2)./(9.*n.*(c0.^2)),(n-1));
%Gf=@(c0,y) G(c0,y)*f(y);
PV=@(c0) double(integral(G(c0,y)*f(y),0,(3*Cp*sqrt(n)))-alpha);
x=fsolve(@(c0)PV,0);

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Ag. de 2020
PV=@(c0) double(integral(G(c0,y)*f(y),0,(3*Cp*sqrt(n)))-alpha);
PV is a function handle.
x=fsolve(@(c0)PV,0);
@(c0)PV is a function handle to an anonymous function that accepts a single parameter, ignores the parameter, and returns the function handle PV. Not PV evaluated at something, just the function handle itself. But fsolve() cannot deal with a function handle being returned.
To repair this, use one of these two:
x = fsolve(@(c0)PV(c0), 0);
or
x = fsolve(PV, 0); %PV is already a function handle
However... your PV needs an improvement. It should be
PV=@(c0) integral( @(y) G(c0,y).*f(y), 0, (3*Cp*sqrt(n)))-alpha;
You do not need the double() as integral() already returns double in most cases. You need the .* instead of * because both G and f return vectors when given vector inputs, and integral() will be generating vector inputs.

Más respuestas (0)

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by