How can I get the plot correctly?

조회 수: 1 (최근 30일)
Louis Liu
Louis Liu 2017년 8월 23일
편집: Louis Liu 2017년 8월 29일
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

채택된 답변

Robert
Robert 2017년 8월 23일
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
Louis Liu
Louis Liu 2017년 8월 23일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!