MATLAB Answers

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

  0 件のコメント

サインインしてコメントする。

採用された回答

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 件)

タグ

Community Treasure Hunt

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

Start Hunting!