undefined function 'f'

조회 수: 2 (최근 30일)
Titas Chattopadhyay
Titas Chattopadhyay 2020년 3월 31일
댓글: Ameer Hamza 2020년 3월 31일
tmesh = linspace(0,pi/2,5);
solinit = bvpinit(tmesh, @f);
sol = bvp4c(@odefun, @bcfun, solinit);
plot(sol.x, sol.y, '-o');
FUNCTION
function dzdt = odefun(t,z)
dzdt = zeros(2,1);
dzdt = [z(2)+z(1)];
end
% boundary conditions
function res = bcfun(za, zb)
res = [za(1) zb(pi/2)-2]
end
% Initial guess
function g = f(t)
g = [sin(t) cos(t)];
end
error unrecognized function or variable 'f'
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 3월 31일
If you stored those three functions together in one file, and had the script in another file, then the script would only be able to see the first function inside the file of functions. Only the first function in a file of functions is visible to the outside normally; the rest are private implementations only for use by that function (typically)
Titas Chattopadhyay
Titas Chattopadhyay 2020년 3월 31일
So, I have to make 3 function files for func ,bcs and ics. Right?

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 31일
편집: Ameer Hamza 2020년 3월 31일
Either write all the functions in one file like this. (Also note that I have made few changes in your code.)
tmesh = linspace(0,pi/2,5);
solinit = bvpinit(tmesh, @f);
sol = bvp4c(@odefun, @bcfun, solinit);
plot(sol.x, sol.y, '-o');
function dzdt = odefun(t,z)
dzdt = zeros(2,1);
dzdt = [z(2); z(1)]; % <----- This should be a 2x1 vector
end
% boundary conditions
function res = bcfun(za, zb)
res = [za(1); zb(2)-2]; % <---- boundary conditions are written like this, zb(pi/2) is invalid
end
% Initial guess
function g = f(t)
g = [sin(t) cos(t)];
end
Or create 4 files like this.
main_script:
tmesh = linspace(0,pi/2,5);
solinit = bvpinit(tmesh, @f);
sol = bvp4c(@odefun, @bcfun, solinit);
plot(sol.x, sol.y, '-o');
odefun.m:
function dzdt = odefun(t,z)
dzdt = zeros(2,1);
dzdt = [z(2); z(1)];
end
bcfun.m:
% boundary conditions
function res = bcfun(za, zb)
res = [za(1); zb(2)-2];
end
f.m:
% Initial guess
function g = f(t)
g = [sin(t) cos(t)];
end
  댓글 수: 2
Titas Chattopadhyay
Titas Chattopadhyay 2020년 3월 31일
Thank you so much.
Ameer Hamza
Ameer Hamza 2020년 3월 31일
Glad to be of help.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by