Why do i receive this error? (line 11)

5 ビュー (過去 30 日間)
Zakhar
Zakhar 2025 年 1 月 3 日
編集済み: Torsten 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 件のコメント
Zakhar
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.
Torsten
Torsten 2025 年 1 月 3 日
編集済み: Torsten 2025 年 1 月 3 日
Before you continue, you should pass MATLAB Onramp - an introductory course free of costs to learn the basics of the new software:

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

回答 (1 件)

Sameer
Sameer 2025 年 1 月 3 日
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
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 ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by