"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

Answers (3)

You have to write that code into a file named TEval.m
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
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

Asked:

on 23 Sep 2017

Answered:

on 23 Sep 2017

Community Treasure Hunt

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

Start Hunting!