필터 지우기
필터 지우기

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

조회 수: 1 (최근 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 CenterFile Exchange에서 State-Space Control Design and Estimation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by