You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Why do i receive this error? (line 11)
7 views (last 30 days)
Show older comments
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 Comments
Zakhar
on 3 Jan 2025
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
on 3 Jan 2025
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
on 3 Jan 2025
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
on 3 Jan 2025
It is fine (and normal) for function u0 to be defined at the end of the code.
Torsten
on 3 Jan 2025
Edited: Torsten
on 3 Jan 2025
Then post the complete code (as plain ascii text) so that we can test it .
What MATLAB release do you use ? In earlier releases, functions (like u0) have to be defined in separate .m-files and cannot be appended to the script part.
Zakhar
on 3 Jan 2025
Edited: Torsten
on 3 Jan 2025
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
on 3 Jan 2025
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
on 3 Jan 2025
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
on 3 Jan 2025
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.
Answers (1)
Sameer
on 3 Jan 2025
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 Comment
Zakhar
on 3 Jan 2025
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
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)