-2

I get this:

"This statement is not inside any function. (It follows the END that terminates the definition of the function "plot_2_components".)"

I've tried entering plot before "end" but no plots showed up. Assuming the the variables have values assigned to them.

function [U,A] = beer_game_orbit(a,b,t,Q,x0,n, k)
    %create orbit in 15 x K matrix using a total of N matrix 

    A = zeros(15,n); 

    A(:,1) = x0; 
    for i =2:n
       %compute but do not store yet, iterate over first column
       A(:,i) = beer_game_function(a,b,t,Q,A(:,i-1)); 


    end

    U = A(:,n-k+1:n);

end

function [] = plot_2_components(k1,k2,U)
    %k1 and k2 are the components who we can to graph against each other 
    %U is the output of beer_game_orbit 

    m = size(U(k1,:),2); 

    figure(1); 
    clf; 

    for i = 1:m

        plot(U(k1,i),U(k2,i),'k*'); 
        hold on 

    end


    if(k1 == 1)
        str1 = 'FI'; 
    elseif k1 == 2
        str1 = 'FB'; 
    elseif k1 == 3
        str1 = 'FPD2'; 
    elseif k1 == 4
        str1 = 'FPD1'; 

    end

    if(k2 == 1)
        str2 = 'FI'; 
    elseif k2 == 2
        str2 = 'FB'; 
    elseif k2 == 3
        str2 = 'FPD2'; 
    elseif k2 == 4
        str2 = 'FPD1'; 
    elseif k2 == 5

    end

    title(['Plot of ' str1 ' vs ' str2]);
end

Run [U] = beer_game_orbit(a,b,t,Q,x0,n,k)
plot_2_components(1,4,U)

Thank you!

Jet
  • 1
  • 1

1 Answers1

4

It looks like you're trying to create a script file that contains local functions (NOTE: only supported in R2016b or later). If that's true, then you have to move your local functions after the code you want to run in your script. Your file should look something like this:

[U] = beer_game_orbit(a,b,t,Q,x0,n,k);  % Code to run
plot_2_components(1,4,U);               % Code to run

function [U,A] = beer_game_orbit(a,b,t,Q,x0,n, k)  % Local function

  % Code for beer_game_orbit...

end

function [] = plot_2_components(k1,k2,U)  % Local function

  % Code for plot_2_components...

end

If your file starts with a function, then it will be treated like a function file and not a script. The first function is the main function (which is what is called from outside the file) while any subsequent functions are local functions (which can only be called from within the file). Function files can't define any code outside of a function definition, hence the error you're getting.

gnovice
  • 125,304
  • 15
  • 256
  • 359