現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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 で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)