"Function definitions are not permitted in this context." ? bisection
Show older comments
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
Answers (3)
Walter Roberson
on 23 Sep 2017
0 votes
You have to write that code into a file named TEval.m
Geoff Hayes
on 23 Sep 2017
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
on 23 Sep 2017
0 votes
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.
Categories
Find more on Debugging and Analysis 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!