Why do i receive this error? (line 11)
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
Torsten
on 3 Jan 2025
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
on 3 Jan 2025
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
Walter Roberson
on 3 Jan 2025
It is fine (and normal) for function u0 to be defined at the end of the code.
Zakhar
on 3 Jan 2025
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.
Torsten
on 3 Jan 2025
Works for me (see above).
Zakhar
on 3 Jan 2025
Zakhar
on 3 Jan 2025
Torsten
on 3 Jan 2025
Two "end" commands were at the wrong place. Do you see where ?
Zakhar
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:
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
Categories
Find more on Loops and Conditional Statements 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!
