How can I get the plot correctly?
1 回表示 (過去 30 日間)
古いコメントを表示
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
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)
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!