Need Help Re: 'Function definitions are not permitted in this context'
Show older comments
I've searched around but couldn't find a solution to my problem. Any help appreciated. I have my base code which I will include below, and a separate script with my 'least_squares' function in it (also included below). However i keep getting the error message 'Function definitions are not permitted in this context' when I try to run my code. Apologies for the length of this question.
Base Code:
clear
load('indoor_path_loss_data.res.txt')
xi = indoor_path_loss_data(:,1);
yi = indoor_path_loss_data(:,2);
N = 1000;
Nplot = 10000;
[a_hat,b_hat] = least_squares(xi,yi,N);
dx=(xi(N)-xi(1))/Nplot;
for ct = 1:Nplot
xplot(ct) = xi(1) + (ct-0.5)*dx;
yplot(ct) = a_hat + b_hat*xplot(ct);
end
plot(xi,yi,'*',xplot,yplot)
xlabel('x')
ylabel('y')
legend('y_i', 'y_a_p_p')
title('A linear approximation to given data')
function [a_hat,b_hat] = least_squares(xi,yi,N)
% Form sum(xi)^2/N
term4 = 0;
for ii = 1:N
term4 = term4 + x(ii);
end
term4 = term4^2/N;
% Form the sum of xi and yi
term5 = 0;
term6 = 0;
for ii = 1:N
term5 = term5 + y(ii);
term6 = term6 + x(ii);
end
% Form b_hat
b_hat_top = term1 - term2;
b_hat_bot = term3 - term4;
b_hat = b_hat_top/b_hat_bot;
% Form a_hat
a_hat = 1/N*term5- b_hat/N*term6;
Separate Script:
function [a_hat,b_hat] = least_squares(xi,yi,N)
% determine a_hat and b_hat for a linear approximation by the least squares method to
% data y(x).
%Form the sum of xi*yi
term1 = 0;
for ii = 1:N
term1 = term1 + (x(ii)*y(ii));
end
%Form the sum of xi and the sum of yi
%Form the product of the sums and divide by N
term2a = 0;
term2b = 0;
for ii = 1:N
term2a = term2a + x(ii);
term2b = term2b + y(ii);
end
term2 = term2a*term2b/N;
% Form the sum of xi*xi
term3 = 0;
for ii = 1:N
term3 = term3 + x(ii)*x(ii);
end
% Form sum(xi)^2/N
term4 = 0;
for ii = 1:N
term4 = term4 + x(ii);
end
term4 = term4^2/N;
% Form the sum of xi and yi
term5 = 0;
term6 = 0;
for ii = 1:N
term5 = term5 + y(ii);
term6 = term6 + x(ii);
end
% Form b_hat
b_hat_top = term1 - term2;
b_hat_bot = term3 - term4;
b_hat = b_hat_top/b_hat_bot;
% Form a_hat
a_hat = 1/N*term5- b_hat/N*term6;
4 Comments
James Tursa
on 24 Mar 2015
Edited: James Tursa
on 24 Mar 2015
You don't need to insert blank lines between all of your code lines to get them to appear on separate lines. In the future, simply highlight the code and then press the "{ } Code" button.
Image Analyst
on 24 Mar 2015
And you do know that there is a built in "polyfit" function that does least squares, don't you. A demo is attached. If you define leastsquares in a separate m-file, then don't also define it in your main script m-file.
Star Strider
on 24 Mar 2015
Even easier than polyfit:
B = [ones(size(xi(:)) xi(:)]\yi(:);
intercept = B(1);
slope = B(2);
Image Analyst
on 24 Mar 2015
I guess it's a matter of opinion which 3 lines are easier to remember.
coefficients = polyfit(x, y, 1);
intercept = coefficients(2);
slope = coefficients(1);
Answers (2)
In MATLAB it is not possible to have both scripts and functions in one file (Mfile), although you can have multiple functions on one file. If you turn your script (Base Code) into a function, it will work. Replace the clear on the first line of the script to turn it into a function:
function my_fun
Alternatively you can keep the script and save the function least_squares to another file.
The documentation describes the difference between a script and a function: the terms are not interchangeable so be careful that you do not call a function a script. Both are saved in Mfiles, so this is the most general term to use.
Star Strider
on 24 Mar 2015
0 votes
Copy your ‘least_squares’ function code to a new tab in the MATLAB editor, then save it as: least_squares.m. Then delete it from your main script.
Categories
Find more on Robotics 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!