MATLAB keeps returning that the statement is not inside any function, and has a parse error, even though both are clearly defined already. How do I fix this?

3 views (last 30 days)
Matlab keeps returning to me that the statement is not inside any function, and when I run/print the function that was clearly defined above, it says that there is a parse error (in this case, the main() function that was called), and that the function main() might be unused (which is wrong because I definitely called it under the end of the function). Or is this a problem BECAUSE I ran the code after the function end? But I've already tried otherwise as well and it still doesn't work. Please help!!
% define the run simulation function
function run_simulation()
% define the time vector
t = linspace(0,5*365,100);
% call rate of change of costs of operation function
rate_of_change_costs_of_operation(t);
end
% Define the sales rate function
function sales_rate = sales(t)
if t < 365*2 % sales rate for first two years
sales_rate = 5;
else % sales rate for all subsequent years
sales_rate = 7;
end
end
% Define the operation costs function
function operation_costs = costs(t)
% Define the parameters
daily_operation_cost = 1000;
% Calculate the operation costs at time t
operation_costs = cumsum(daily_operation_cost)*t;
end
% Define the rate of change of costs of operation function
function rate_of_change_costs_of_operation(t)
% Calculate the total number of sales using numerical integration
daily_customer_arrival_rate = 10;
daily_sales_rate = sales(t);
total_sales = trapz(t, daily_customer_arrival_rate .* sales(t) .* daily_sales_rate);
% Calculate the total costs of operation using numerical integration
total_costs = trapz(t, costs(t));
% Calculate the rate of change of sales using partial differential equations
% Define the PDE system
dSdt = @(t,S) daily_customer_arrival_rate .* total_sales - S .* daily_sales_rate;
dSdt_bc = @(Sleft,Sright) [Sleft; Sright];
% Solve the PDE system
x = linspace(0,5*365,100); % spatial mesh
sol = pdepe(0,dSdt,dSdt_bc,zeros(size(x)),x,t); % solve PDE system
% Extract the solution and calculate the rate of change of sales
S = sol(:,:,1);
dSdt_num = gradient(S(:,1), x(2)-x(1)); % numerical derivative
% Calculate the rate of change of operation costs using partial differential equations
% Define the PDE system
dCdt = @(t,C) daily_operation_cost - C .* interp1(x, gradient(S(:,1), x(2)-x(1)), t);
dCdt_bc = @(Cleft,Cright) [Cleft; Cright];
% Solve the PDE system
x = linspace(0,5*365,100); % spatial mesh
C0 = zeros(size(x)); % initial condition
sol = pdepe(0,dCdt,dCdt_bc,C0,x,t); % solve PDE system
% Extract the solution and plot the results
C = sol(:,:,1);
plot(x,C);
xlabel('Time (days)');
ylabel('Costs of Operation');
title('Rate of Change of Costs of Operation');
end % end of rate_of_change_costs_of_operation function
function main()
run_simulation();
end
main();
this is the error that returned to me:
>> rate_of_change_of_costs_of_operation
Error: File: rate_of_change_of_costs_of_operation.m Line: 74 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "main".)

Answers (1)

Cris LaPierre
Cris LaPierre on 2 Apr 2023
Edited: Cris LaPierre on 2 Apr 2023
In a MATLB script, all functions must be defined at the bottom (or below) the script code (see here).
Move main() to the first line of your script.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!