Error: variable might be used before it is defined.

답변 (2개)

Ruchika
Ruchika 2023년 8월 11일
이동: Image Analyst 2023년 8월 11일

1 개 추천

The error message "variable might be used before it is defined" typically occurs when you're trying to use a variable before it has been assigned a value or initialized. This error can often be resolved by ensuring that you define or initialize your variables properly before using them in your MATLAB code. In this code, 'yy3' has been used to initialise 'yy2' before being initialised itself. You want to have an initial value for 'yy3' before using it to resolve this error.
VBBV
VBBV 2023년 8월 11일
편집: VBBV 2023년 8월 11일
Change this line
yy2 = Pr*(y(4)*y(2)-y(1)*y(5)-Df*yy3-Nb*y(5)*y(7)-Nt*y(5)^2)
to
yy2 = Pr*(y(4)*y(2)-y(1)*y(5)-Df*y(3)-Nb*y(5)*y(7)-Nt*y(5)^2)
% ^^

댓글 수: 6

VBBV
VBBV 2023년 8월 11일
yy3 has been used even before its defined a finite value. Matlab doesnt recognize any undefined variables in a program and throws error as soon as code attempts to call that variable,
@VBBV yy3 cannot be replaced by y(3), they are totally different.
Then give yy3 a value before you use it in that line of code.
Torsten
Torsten 2023년 8월 11일
편집: Torsten 2023년 8월 11일
Insert the expression for yy2 in the equation for yy3. Then this equation will only depend on yy3 and you can solve for it.
Fareeha
Fareeha 2023년 8월 11일
편집: Fareeha 2023년 8월 11일
@Torsten I tried it and get this error
Error using bvparguments (line 108)
Error in calling BVP5C(ODEFUN,BCFUN,SOLINIT):
The derivative function ODEFUN should return a column vector of length 8.
Error in bvp5c (line 137)
bvparguments(solver_name,ode,bc,solinit,options);
Error in SAD_Sol_bvp4c (line 17)
sol = bvp5c(@bvp2D, @bc2D, sol1);
If you have an initial vector of length 8, you must return a dydt vector of length 8.

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

카테고리

도움말 센터File Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2023년 8월 11일

댓글:

2023년 8월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by