Why do i receive this error? (line 11)

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

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).
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.
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
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?
It is fine (and normal) for function u0 to be defined at the end of the code.
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
Torsten
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.
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.
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.
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.
Works for me (see above).
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.
Torsten
Torsten on 3 Jan 2025
Edited: Torsten on 3 Jan 2025
Just copy the code I used and load it in your personal MATLAB version.
@Torsten Wow, thank you so much, but what was the issue? At quick look i dont see much difference except it visually just looks longer.
Two "end" commands were at the wrong place. Do you see where ?
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 on 3 Jan 2025
Edited: Torsten on 3 Jan 2025
Before you continue, you should pass MATLAB Onramp - an introductory course free of costs to learn the basics of the new software:

Sign in to comment.

Answers (1)

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

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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 3 Jan 2025

Edited:

on 3 Jan 2025

Community Treasure Hunt

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

Start Hunting!