error from command window as the codes involves local functions
Mostra commenti meno recenti
some one plz help me ,am getting problems in call functions to plot the gaph codes. here are codes
clc;
clear;
close all;
% FOOTBALL POISSON PROBABILITY MODEL
% Togo vs France
lambda_T = 0.37;
lambda_F = 2.65;
match_time = 90;
% Goal-scoring rates per minute
rate_T = lambda_T / match_time;
rate_F = lambda_F / match_time;
% Reproducible simulated goal times
rng(7);
% Generate independent Poisson goal times
goals_T = goalTimes(rate_T, match_time);
goals_F = goalTimes(rate_F, match_time);
% Time points
time = 0:90;
% Probability arrays
P_Draw = zeros(size(time));
P_Togo = zeros(size(time));
P_France = zeros(size(time));
K = 12;
% Calculate probabilities at every minute
for n = 1:length(time)
t = time(n);
% Current score
score_T = sum(goals_T <= t);
score_F = sum(goals_F <= t);
% Expected goals remaining
mu_T = lambda_T * (90-t)/90;
mu_F = lambda_F * (90-t)/90;
% Future Poisson probabilities
PT = poissonVector(mu_T,K);
PF = poissonVector(mu_F,K);
% Calculate final outcome probabilities
for i = 0:K
for j = 0:K
probability = PT(i+1)*PF(j+1);
final_T = score_T+i;
final_F = score_F+j;
if final_T > final_F
P_Togo(n) = P_Togo(n)+probability;
elseif final_T == final_F
P_Draw(n) = P_Draw(n)+probability;
else
P_France(n) = P_France(n)+probability;
end
end
end
end
% Plot graphs in the same layout as reference image
figure('Color','w','Position',[200 100 850 900]);
subplot(3,1,1)
plot(time,P_Draw,'k','LineWidth',1.5);
title('P (Draw)');
ylabel('Probability');
xlabel('Time (Minutes)');
xlim([0 90]);
ylim([0 1]);
yticks(0:0.2:1);
grid on;
subplot(3,1,2)
plot(time,P_Togo,'k','LineWidth',1.5);
title('P (Togo Win)');
ylabel('Probability');
xlabel('Time (Minutes)');
xlim([0 90]);
ylim([0 1]);
yticks(0:0.2:1);
grid on;
subplot(3,1,3)
plot(time,P_France,'k','LineWidth',1.5);
title('P (France Win)');
ylabel('Probability');
xlabel('Time (Minutes)');
xlim([0 90]);
ylim([0 1]);
yticks(0:0.2:1);
grid on;
sgtitle('Evolution of Football Match Probabilities');
% Display simulated goal times
disp('Togo goal times (minutes):');
disp(goals_T);
disp('France goal times (minutes):');
disp(goals_F);
% Initial probabilities
fprintf('Initial Togo win: %.4f\n',P_Togo(1));
fprintf('Initial Draw: %.4f\n',P_Draw(1));
fprintf('Initial France win: %.4f\n',P_France(1));
% Local functions
function times = goalTimes(rate,duration)
times = [];
t = 0;
while true
t = t-log(rand)/rate;
if t > duration
break;
end
times(end+1) = t;
end
end
function p = poissonVector(mu,K)
p = zeros(1,K+1);
p(1) = exp(-mu);
for k = 1:K
p(k+1) = p(k)*mu/k;
end
end
1 Commento
Steven Lord
il 6 Nov 2026 alle 16:08
Please show us the full and exact text of the error message, all the text displayed in red in the Command Window (and if there are any warning messages displayed in orange, please show us those too.) The exact text may be useful and/or necessary to determine what's going on and how to avoid the warning and/or error.
I suspect you're copying and pasting this code into the Command Window, including the local function definitions. That's not going to work. If you're using a release that's less than (I think) ten years old, you can have script files containing local functions. Just write this code in a script file and run it by typing the script name.
Risposte (1)
The code gives no error message when executed here (see above).
Could you explain in more detail what you mean by "am getting problems in call functions to plot the gaph codes" ?
If you use a MATLAB release >= R2016b, save the complete code in a .m - file, load this file into the editor and click the "Run" button.
If you use a MATLAB release < 2016b, save the two functions in a .m - file with name "goalTimes.m" and the part of your code without the functions (the script part) as two separate files. Then load the script part into the editor and click the Run button.
Categorie
Scopri di più su Axis Labels in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
