Effacer les filtres
Effacer les filtres

how can I fix the code for Segregation Model and population?

4 vues (au cours des 30 derniers jours)
Wenchen Liu
Wenchen Liu le 7 Déc 2021
Réponse apportée : Vatsal le 27 Fév 2024
I have a code like this:
% model
function schell(grid_size,percent_empty,percent_p1,percent_p2)
grid_size = 50; percent_empty = 30; percent_p1 = 30; percent_p2 = 50;
n = grid_size;
n2 = n^2;
grids = zeros(n); % a grid that occupied by a being from population 1 or 2.
emp = round((percent_empty/100)*n2);
p1 = round((percent_p1/100)*n2);
p2 = round((percent_p2/100)*n2);
tot = emp + p1 + p2;
rand_loc = randperm(n2);
init_emp = rand_loc(1:emp);
init_p1 = rand_loc(emp+1:tot);
init_p2 = rand_loc(tot+1:end);
figure, imagesc(grids);
% frame = getframe;
% writeVideo(writerObj,frame);
but it says "Error using model
Error: File: model.m Line: 1 Column: 10
Class name and filename must match."
How can I fix this? I named this file as "matlab_project".
In this code, I want to include an m * n grid where the "squares" are either unoccupied, or occupied by a being from populations 1 or 2. Initially p1% of the squares are occupied by population 1 occupants, and p2% by population 2, where p1 + p2 < 100. This also have to relate to Segregation Model and population.

Réponses (1)

Vatsal
Vatsal le 27 Fév 2024
Hi,
The error message is occurring because the function name "schell" does not match the filename "matlab_project.m". In MATLAB, it is necessary for the function name to be the same as the filename for correct recognition and usage.
To fix this, you can either:
  1. Rename the function to "matlab_project" to match the filename.
  2. Rename the file to "schell.m" to match the function name.
Here is the code with the function renamed to "matlab_project":
function matlab_project(grid_size,percent_empty,percent_p1,percent_p2)
grid_size = 50; percent_empty = 30; percent_p1 = 30; percent_p2 = 50;
n = grid_size;
n2 = n^2;
grids = zeros(n); % a grid that occupied by a being from population 1 or 2.
emp = round((percent_empty/100)*n2);
p1 = round((percent_p1/100)*n2);
p2 = round((percent_p2/100)*n2);
tot = emp + p1 + p2;
rand_loc = randperm(n2);
init_emp = rand_loc(1:emp);
init_p1 = rand_loc(emp+1:tot);
init_p2 = rand_loc(tot+1:end);
figure, imagesc(grids);
% frame = getframe;
% writeVideo(writerObj,frame);
end
I hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by