problem of solving the PDE

i define the function, boundary and initial condition of the heat equation in a standard form ,when i try to plot the solution with x direction, it gives me an error that x =linspace () is not inside any function. Do i need to create a function file for each function in order to put the x range inside?

답변 (1개)

VBBV
VBBV 2024년 3월 20일
편집: VBBV 2024년 3월 20일

1 개 추천

Cut all the lines from 1 till 16 and place it after line 26.
Function declarations must be done at the end of the code in your script file (with properly termination using keyword end ... in your case its done correctly)

댓글 수: 5

VBBV
VBBV 2024년 3월 20일
편집: VBBV 2024년 3월 20일
E.g. put all the functions at the end of code in yuur script as below, then run the code again
x = linspace(0,1,25);
y = linspace(0,1,25);
m = 1;
% ... yourcode
% ... yourcode
view([150 25])
function [c,f,s] = heatcyl(x,t,u,dudx) % similarly for other functions
end
suen
suen 2024년 3월 21일
Thank you very much, but I have a question. Usually, we need to define the function first, which is letting the computer understand what inputs I have before solving the main problem. However, why does the arrangement order have to be reversed in this situation?
Torsten
Torsten 2024년 3월 21일
편집: Torsten 2024년 3월 21일
It's just a MATLAB convention that the functions must follow the script part. And in my opinion it's reasonable because the script part is executed first and usually calls the functions.
I don't know where you find that
Usually, we need to define the function first, which is letting the computer understand what inputs I have before solving the main problem.
The inputs are not given in the functions, but in the script part (main program) that calls the functions.
suen
suen 2024년 3월 21일
thank you so much
VBBV
VBBV 2024년 3월 21일
편집: VBBV 2024년 3월 21일
@suen If your code begins with keyword function in line 1 , then by MATLAB convention the file is considered as function file for which it expects inputs and/or outputs. Since the inputs to function are present later part of your code and defined outside the function body, MATLAB throws such error.
Note that each function must end using keyword end except when there is only one function defined inside the script file.
As mentioned by @Torsten its just MATLAB convention.

댓글을 달려면 로그인하십시오.

제품

질문:

2024년 3월 20일

편집:

2024년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by