필터 지우기
필터 지우기

To call a function results in an error (fzero)

조회 수: 2 (최근 30일)
Haojie
Haojie 2012년 7월 11일
I am really new to matlab. When I wrote two functions like this:
funw.m
function q = funw(t)
h=0;
r=1/7;
q = quadl(@(x) sqrt(t^(2*r)-x.^(2*r)),h,t)...
-quadl (@(x) sqrt(x.^(2*r)-t^(2*r)),t,(h+2));
return
VentRate.m
function w = VentRate
global elev;
global h;
w = fzero(@funw,(h+elev)/2);
return
I try to use VentRate to call funw, by typing VentRate in the command window, but it will show some error. However, if i directly write w = fzero(@funw,(h+elev)/2) in the command window, it will run without error. So can anyone tell me what is wrong with the second function? I appreciate your help.
Error:
??? Error using ==> fzero at 422 Second argument must be a scalar or vector of length 2.
Error in ==> VentRate at 4 w = fzero(@funw,(h+elev)/2);

채택된 답변

Thomas
Thomas 2012년 7월 11일
편집: Thomas 2012년 7월 11일
You need to define elev and h in the function you are calling initally.
you 'h' value comes from the funw function which will not get called unless h is defined in the VentRate function and the fzero needs 'h' and 'elev' to be present and scalars.
.m
function w = VentRate
h=0;
elev=2;
w = fzero(@funw,(h+elev)/2);
return
funw.m
function q = funw(t)
r=1/7;
q = quadl(@(x) sqrt(t^(2*r)-x.^(2*r)),h,t)...
-quadl (@(x) sqrt(x.^(2*r)-t^(2*r)),t,(h+2));
return
I cant currently check your function further for Mathematical errors but these functional errors should solve your problem..
  댓글 수: 1
Haojie
Haojie 2012년 7월 11일
Thank you very much. It is the variable's problem. Problem solved.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by