이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Why do i receive this error? (line 11)
조회 수: 5 (최근 30일)
이전 댓글 표시
Zakhar
2025년 1월 3일
So, i have a complete listing for MATLab, basically it should be a finished program, but it seems theres an error in this code. Since im really new to MATLab i cannot understand what i did wrong. 

댓글 수: 17
Torsten
2025년 1월 3일
u0 is undefined (at least in the code you posted).
And please don't include graphics with your code, but the code itself (as plain ascii text).
Zakhar
2025년 1월 3일
Sorry about picture, im just new to this program and site.
About u0, could you explain, please? Sorry again for the picture, but its original code that i have in a textbook, it's a screenshot. So, according to the book, they want to determine the initial temperature distribution as a two-dimensional matrix by this action, and u0 appears just like that, i wrote the code just like it shows in the book.

Walter Roberson
2025년 1월 3일
The error message you partially show would only occur if function u0 is defined, but it is defined in the form
function y = u0(something_here, something_else_here)
but there is at least one path through the u0 code that does not assign to y
Zakhar
2025년 1월 3일
I looked at the code presented in the book closely and noticed, that thay actually define this function, but later in the code. Below, in the "Answers" section, i wrote the full code, and as i can see, right in the end, they define it. I guess it should be defined before u0 is actually presented in the code? The problem is that i cannot include this line correctly in the code.
And also i have another question, in the book they just write function "etc etc..." and then goes next part of the code, theres no "end" command for "function". But MATLab asks me to include this, am i doing something wrong?
Walter Roberson
2025년 1월 3일
It is fine (and normal) for function u0 to be defined at the end of the code.
Zakhar
2025년 1월 3일
Then i don't know whats the problem again. It is presented and defined in the code, but it still does not allow me to run this code
Zakhar
2025년 1월 3일
편집: Torsten
2025년 1월 3일
Heres the code:
heat_dim1p1

function heat_dim1p1
global a b r0 hg
T=1;
a=1;
b=1;
r0=0.25;
hg=10;
koef=1;
Nt=151;
tau=T/(Nt-1);
N=41;
M=41;
h1=a/(N-1);
h2=b/(M-1);
x1=0:h1:a;
x2=0:h2:b;
for n=1:N
for m=1:M
ind(n,m)=u0(x1(n),x2(m));
end
end
subplot(1,2,1);
surf(x2,x1,ind);
for n=1:N
for m=1:M
y(1,n,m)=u0(x1(n),x2(m));
end
end
for t=1:Nt
for m=1:M
y(t,1,m)=0;
y(t,N,m)=0;
end
end
for t=1:Nt
for n=1:N
y(t,n,1)=0;
y(t,n,m)=0;
end
end
for t=1:(Nt-1)
p1=(tau*koef)/(2*h1^2);
p2=(tau*koef)/(2*h2^2);
for n=1:N
w(n,1)=y(t,n,1);
w(n,M)=y(t,n,M);
end
for m=2:(M-1)
alpha(2)=0;
beta(2)=y(t,1,m);
for n=2:(N-1)
alpha(n+1)=p1/(1+p1*(2-alpha(n)));
beta(n+1)=(y(t,n,m)+p1*(y(t,n-1,m)-...
2*y(t,n,m)+y(t,n+1,m)+beta(n)))/...
(1+p1*(2-alpha(n)));
end
w(N,m)=y(t,N,m);
for n=N:-1:2
w(n-1,m)=alpha(n)*w(n,m)+beta(n);
end
end
for n=2:(N-1)
alpha(2)=0;
beta(2)=y(t+1,n,1);
for m=2:(M-1)
alpha(m+1)=p2/(1+p2*(2-alpha(m)));
beta(m+1)=(w(n,m)+p2*(w(n,m-1)-...
2*w(n,m)+w(n,m+1)+beta(m)))/...
(1+p2*(2-alpha(m)));
end
for m=M:-1:2
y(t+1,n,m-1)=alpha(m)*y(t+1,n,m)+beta(m);
end
end
end
for n=1:N
for m=1:M
z(n,m)=y(Nt,n,m);
end
end
subplot(1,2,2);
surf(x2,x1,z);
end
function y=u0(x1,x2)
global a b r0 hg
y=0;
r=sqrt((x1-0.5*a)^2+(x2-0.5*b)^2);
if r<r0
y=(hg/r0)*(r0-r);
end
end
Hope i understood you right. I tried this on MATLAB Online, maybe that was the issue in the first place.
Walter Roberson
2025년 1월 3일
function heat_dim1p1 end
function y=u0(x1,x2) end
The first of those two lines defines heat_dim1p1 as a function which expects no parameters, and returns no values, and which returns immediately.
The second of those two lines defines u0 as a function which expects up to two parameters, and which normally returns one output location. It returns as soon as it starts, leaving y unset. It would work if it were called without a output position, but will fail if called in a context that expects an output.
Walter Roberson
2025년 1월 3일
function y=u0(x1,x2)
global a b r0 hg
y=0;
r=sqrt((x1-0.5*a)^2+(x2-0.5*b)^2);
if r<r0 y=(hg/r0)*(r0-r) end
would probably work, if it were in a file u0.m by itself, or if it were part of another .m file and the other .m file started with "function".
If the function definition for u0 is part of a script file, then the code needs another "end" statement after the above.
Zakhar
2025년 1월 3일
And, what should i do to make everything correct? I kinda understood the point, but still dont know what im supposed to do to make things work.
Zakhar
2025년 1월 3일
Oh i see it now. I wasnt familiar with coding so, when the program asked me to put end after function command, i did just that. Actually, i fixed old version of my code and it worked just fine as well. Thanks everyone.
답변 (1개)
Sameer
2025년 1월 3일
Hi @Zakhar
The error you're encountering is due to the function "u0" not being defined in your code. MATLAB is unable to find "u0", which is causing the error on line 11.
Make sure you have a function named "u0" defined either within this script or in a separate file that is accessible to your script. The function should accept two inputs and return a value.
If "u0" is meant to be a function handle, ensure it is defined as such before it is used.
For example:
u0 = @(x, y) some_expression; % Define u0 as an anonymous function
Hope this helps!
댓글 수: 1
Zakhar
2025년 1월 3일
I understand the problem now, but another problem is that i dont know what u0 i supposed to mean. Like i said in comment above, this is a code from a textbook, and function u0 appears just like that, and wasnt mentioned in the code before. By this line they want to determine the initial temperature distribution as a two-dimensional matrix, but i do not understand this action code-wise. Heres a full code, if it helps. The original code is meant to calculate two-dimensional equation of heat transfer. As far as i know, u0 - is a temperature function, that depends on values t, x1 and x2, i just do not understand how to include that in the code correctly. Would really appreciate some help here.
function heat_dim1p1
end
global a b r0 hg
T=1; a=1; b=1; r0=0.25; hg=10; koef=1;
Nt=151; tau=T/(Nt-1);
N=41; M=41;
h1=a/(N-1); h2=b/(M-1);
x1=0:h1:a; x2=0:h2:b;
for n=1:N
for m=1:M
ind(n,m)=u0(x1(n),x2(m));
end
end
subplot(1,2,1); surf(x2,x1,ind);
for n=1:N
for m=1:M
y(1,n,m)=u0(x1(n),x2(m));
end
end
for t=1:Nt
for m=1:M
y(t,1,m)=0; y(t,N,m)=0;
end
end
for t=1:Nt
for n=1:N
y(t,n,1)=0; y(t,n,m)=0;
end
end
for t=1:(Nt-1)
p1=(tau*koef)/(2*h1^2);
p2=(tau*koef)/(2*h2^2);
for n=1:N
w(n,1)=y(t,n,1); w(n,M)=y(t,n,M);
end
for m=2:(M-1)
alpha(2)=0; beta(2)=y(t,1,m);
for n=2:(N-1)
alpha(n+1)=p1/(1+p1*(2-alpha(n)));
beta(n+1)=(y(t,n,m)+p1*(y(t,n-1,m)-...
2*y(t,n,m)+y(t,n+1,m)+beta(n)))/...
(1+p1*(2-alpha(n)));
end
w(N,m)=y(t,N,m);
for n=N:-1:2
w(n-1,m)=alpha(n)*w(n,m)+beta(n);
end
end
for n=2:(N-1)
alpha(2)=0; beta(2)=y(t+1,n,1);
for m=2:(M-1)
alpha(m+1)=p2/(1+p2*(2-alpha(m)));
beta(m+1)=(w(n,m)+p2*(w(n,m-1)-...
2*w(n,m)+w(n,m+1)+beta(m)))/...
(1+p2*(2-alpha(m)));
end
for m=M:-1:2
y(t+1,n,m-1)=alpha(m)*y(t+1,n,m)+beta(m);
end
end
end
for n=1:N
for m=1:M
z(n,m)=y(Nt,n,m)
end
end
subplot(1,2,2); surf(x2,x1,z);
function y=u0(x1,x2)
end
global a b r0 hg
y=0;
r=sqrt((x1-0.5*a)^2+(x2-0.5*b)^2);
if r<r0
y=(hg/r0)*(r0-r)
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
