How to make the modfunc function work ?
2 vues (au cours des 30 derniers jours)
I have problem with the modfunc function. I get this erroe message: function out = modfunc(Population, Year)
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.
Also note that I am writing in live script and I wrote the modfunc function in a different live script. Additionally, the plots work, but the the line does not appear.
My code is as follows:
% Fit an exponential function to the data
function out = modfunc(Year,Population)
out = Population(1)*exp(Population(2)*Year);
% Plot the data and the fitted function on a semi-log scale
title('World Population with Exponential Fit');
legend('Data', 'Exponential Fit');
Rik le 19 Fév 2023
A function should exist in an m file. If you want to put it in a script file, it needs to have a closing end keyword, and no other code (except for other function) may exist after it. The error you posted indicates you have not met these requirements.