How can I fix error in fzero (line 241)

When I use the following fzero function in Matlab R2012a:
for jj=1:358;
mat=cv_o2{jj};
pr=CC_m{jj};
x = [-5 100];
fun = @(x)mat-(std(power(pr,x),1,3)./mean(power(pr,x),3))
b=fzero(fun,x);
BB{jj}=b;
end
end
I get the following error message:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 241)
if any(~isfinite([fa fb])) || any(~isreal([fa fb]))
I do not understand what the matter is. Could someone write me how can I fix this error?

Answers (1)

Hi,
fzero works for scalar valued functions. Looking at your function I guess it's not scalar valued (i.e., if you call your function fun for one value, the value should not be a vector/matrix but a scalar ("number").
Titus

7 Comments

Thank you. I tried to solve this problem with the following way:
for jj=1:360;
for i=1:464;
for j=1:201;
mat=cv_o2{jj};
pr=CC_m{jj};
if mat(i,j) == 0;
b(i,j)=0;
elseif mat(i,j) > 0;
b(i,j)=fzero(@(x)mat(i,j)-( nanstd(pr(i,j).^x,1,3)./nanmean(pr(i,j).^x,3)),0.1);
BB{jj}=b;
end
end
end
end
But I got the error:
Error using fzero (line 289)
FZERO cannot continue because user supplied function_handle ==> @(x)mat(i,j)-(nanstd(pr(i,j).^x,1,3)./nanmean(pr(i,j).^x,3)) failed with the error below.
Too many input arguments.
How can solve this function with fzero?
Hi,
what is pr?
  • Is it a matrix? Then I don't understand nanmean(pr(i,j).^x,3), because pr(i,j) would be a number, how should one calculate a mean ...
  • Or is it a cell array (and pr{i,j} is a 3D matrix)? In this case you need to use pr{i,j} instead of pr(i,j) for both inside nanstd and nanmean ...
Titus
Hi, Yes, pr is a 3D martix. You are right that I do not have use (i,j) because pr(i,j) is a scalar. But I tried solve it by:
for jj=1:358;
for i=1:464;
for j=1:201;
mat=cv_o2{jj};
pr=CC_m{jj};
myfun = @(x,mat,pr) mat(i,j)-( nanstd(pr(i,j).^x)./nanmean(pr(i,j).^x));
fun = @(x) myfun(x,mat,pr);
if mat(i,j) == 0;
b(i,j)=0;
elseif mat(i,j) > 0;
b(i,j)=fzero(fun,-1);
BB{jj}=b;
end
end
end
end
But the error is:
Error using fzero (line 309)
Function value at starting guess must be finite and real.
I do not have idea what should I do.
All pr(i,j) are greater than zero ? Otherwise there will be a problem to calculate pr(i,j)^x.
Best wishes
Torsten.
I tried to solve it with adding elseif:
for jj=1:358;
for i=1:464;
for j=1:201;
mat=cv_o2{jj};
pr=CC_m{jj};
myfun = @(x,mat,pr) mat(i,j)-( nanstd(pr(i,j).^x)./nanmean(pr(i,j).^x));
fun = @(x) myfun(x,mat,pr);
if mat(i,j) == 0;
b(i,j)=0;
elseif pr(i,j) ==0;
b(i,j) =0;
elseif pr(i,j) > 0 && mat(i,j) > 0;
b(i,j)=fzero(fun,0.1);
BB{jj}=b(i,j);
end
end
end
end
But I got same error.
Hi,
but if pr is a 3D matrix, should't it then be pr(i,j,:)?
Titus
pr(i,j) is a scalar. Thus taking nanstd(pr(i,j)^x) gives Inf since you divide by 0.
Best wishes
Torsten.

Sign in to comment.

Categories

Commented:

on 4 Sep 2015

Community Treasure Hunt

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

Start Hunting!