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

2 ビュー (過去 30 日間)
Wenchen Liu
Wenchen Liu 2021 年 12 月 7 日
回答済み: Vatsal 2024 年 2 月 27 日
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.

回答 (1 件)

Vatsal
Vatsal 2024 年 2 月 27 日
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!

カテゴリ

Help Center および File ExchangeFilter Banks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by