How can I get the plot correctly?

1 Ansicht (letzte 30 Tage)
Louis Liu
Louis Liu am 23 Aug. 2017
Bearbeitet: Louis Liu am 29 Aug. 2017
Hello,
I write some code like this:
vx = 30:10:300;
vy = 2:0.04:3;
[xx,yy] = meshgrid(vx,vy);
zz = get_critical_value(yy,xx,0.05);
surf(xx,yy,zz)
Unfortunately, Matlab returns some feedback messages below:
======================================================
FZERO cannot continue because user-supplied function_handle ==>
@(czero)integral(@(y)fun(y,czero),0,3*Cp*sqrt(n))-alpha failed with the error below.
Inner matrix dimensions must agree.
Error in get_critical_value (line 15)
czero_sol = fzero(@(czero) integral(@(y)fun(y,czero),0,3*Cp*sqrt(n))-alpha,czero_guess);
======================================================
What's wrong with my program? How can I solve this situation to get the right plot? Thanks!
*The code of get_critical_value:
function czero_sol = get_critical_value(C,n,alpha)
if n<100
Cp = C+0.33;
else
Cp = C+0.12;
end
fun = @(y,czero)chi2cdf((n-1)*(3*Cp*sqrt(n)-y).^2/(9*n*czero^2),n-1).*(normpdf(y+3*(Cp-C)*sqrt(n))+normpdf(y-3*(Cp-C)*sqrt(n)));
czero_guess = 1;
czero_sol = fzero(@(czero) integral(@(y)fun(y,czero),0,3*Cp*sqrt(n))-alpha,czero_guess);
end

Akzeptierte Antwort

Robert
Robert am 23 Aug. 2017
Your function get_critical_value is not build to handle array inputs. It is always nice if you can vectorize your function to take advantage of MATLAB's fast array operations but in your case, integral and fzero will make this difficult.
Instead of vectorizing, you could perform the operation one-at-a-time using arrayfun
vx = 30:10:300;
vy = 2:0.04:3;
[xx, yy] = meshgrid(vx, vy);
zz = arrayfun(@(y, x) get_critical_value(y, x, 0.05), yy, xx);
surf(xx, yy, zz)
  1 Kommentar
Louis Liu
Louis Liu am 23 Aug. 2017
Thanks! But I think I have to study arrayfun() first. It's my first time to know this function. Hope I could truly understand whole your answer...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!