I want to use integral&fzero to solve the equation but I could't debug by myself

1 回表示 (過去 30 日間)
Louis Liu
Louis Liu 2017 年 8 月 25 日
コメント済み: Louis Liu 2017 年 8 月 26 日
Hello, I have written some code below but I could't debug by myself. Does anyone can give me some advice? Thanks!
n = 100; Cpk_hat = 1.5; r = 0.95; zzz = 1.00;
fun = @(t,C)chi2cdf(((n-1)*((3*C+abs(zzz)*sqrt(n))-t).^2/(9*n*Cpk_hat.^2)),n-1)*...
(normpdf(t+zzz*sqrt(n))+normpdf(t-zzz*sqrt(n)));
C_guess = 1;
fzero(@(C) integral(@(t) fun(t,C),0,((3*C+abs(zzz))*sqrt(n)))-(1-r),C_guess);
error message:
Error using fzero (line 306)
FZERO cannot continue because user-supplied function_handle ==>
@(C)integral(@(t)fun(t,C),0,((3*C+abs(strange_hat))*sqrt(n)))-(1-r) failed with the error
below.
Inner matrix dimensions must agree.
  2 件のコメント
Adam
Adam 2017 年 8 月 25 日
Simplest debugging option would be to move your complicated function into its own file, split into variables across multiple lines rather than all on one line, then just create a simple function handle to that to pass in. Then you will be able to debug it with the 'stop on errors' option from the breakpoints menu.
At a glance all your inputs are scalar so I can't see simply where your error would be.
Louis Liu
Louis Liu 2017 年 8 月 25 日
Thanks! I will try it!

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

採用された回答

Matt J
Matt J 2017 年 8 月 25 日
Define fun() with element-wise operators
fun=@(t,C)chi2cdf(((n-1).*((3.*C+abs(zzz).*sqrt(n))-t).^2./(9.*n.*Cpk_hat.^2)),n-1).*(normpdf(t+zzz.*sqrt(n))+normpdf(t-zzz.*sqrt(n)))
  4 件のコメント
Louis Liu
Louis Liu 2017 年 8 月 26 日
@Matt J Thanks! I will check it again!
Louis Liu
Louis Liu 2017 年 8 月 26 日
@Matt J Hello, I have found the mistake and get the right value 1.3147 now. Thank for your help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!