"Function definitions are not permitted in this context." ? bisection
이전 댓글 표시
I am really new to MATLAB. I'm trying to write/test a bisection program that determines T as a function of C. I have initial guesses and will test some cases in the command window, but my code doesn't execute. I'm getting this error on the first line: "Function definitions are not permitted in this context." I'm not really sure if it's a file type error or a syntax error and how to fix it. Some help would be appreciated.
Thanks!
function [TofC] = TEval(osf)
% temp absolute
U = 40+273.15;
L = 0+273.15;
% disp if no bracketing for a guess
if 0 < fTabs(L,osf)*fTabs(U,osf)
error('brackets')
end
end
r = L;
%iterate 1 to 10
for i=1:10
rInitial=r;
r = 0.5*(L+U);
% middle val
if r~=0, z = 100 * abs((r-rInitial)/r);
end
%check specifies test values from text
check=fTabs(r,osf)*fTabs(L,osf);
if check < 0
U = r;
elseif check > 0
r = L;
else
z = 0;
end
end
TofC = -273.15 + r;
function [k] = fTabs(Tabs,osf)
k = -139.3441 + ((1.5757e5)/Tabs) - ((6.64231e7)/Tabs^2);
k = k - ((8.62195e11)/Tabs^4) + ((1.2438e10)/Tabs^3);
k = k - log(osf);
end
답변 (3개)
Walter Roberson
2017년 9월 23일
0 개 추천
You have to write that code into a file named TEval.m
Geoff Hayes
2017년 9월 23일
Amanda - you seem to have an extra end after the first if statement
if 0 < fTabs(L,osf)*fTabs(U,osf)
error('brackets')
end
end
Remove one of these and that will almost fix the problem. Since you have two functions, one which uses the other, with the second one "nested" within the first, then you need to close the first function with an end too
function [TofC] = TEval(osf)
% function code
function [k] = fTabs(Tabs,osf)
% nested function code
end
end % <---- need this end
Star Strider
2017년 9월 23일
0 개 추천
You most likely need to save your functions to their own individual ‘.m’ files. Then you can call them as you would any other function. See the documentation on Function Basics (link) for your MATLAB version.
카테고리
도움말 센터 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!