How do I define the are of checkboard
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, I have a problem for with area of integration in Monte Carlo method.
The script for the integrated 2D function I used and worked is
% square
res = (abs(x)<= a ) && (abs(y)<= a );
% circle
res = ( (x^2 + y^2) <= a^2 );
where res being the map or area where I want to integrate, a = 1 for these purposes and x and y are the variables of f(x,y).
Now the map I want looks like the one on the picture. I've tried cycles and writing all the conditions for x and y but nothing works. Can you please help ?
0 件のコメント
回答 (1 件)
Parag
2025 年 1 月 31 日
It looks like you are trying to define an integration area in MATLAB using a Monte Carlo method. The image shows a checkerboard-like pattern within a square region. To define such a region in MATLAB, you need to apply a condition that alternates between included and excluded areas.
Here’s how you can define the checkerboard pattern in MATLAB:
% Define grid resolution
n = 100;
x = linspace(-1,1,n);
y = linspace(-1,1,n);
[X, Y] = meshgrid(x, y);
% Define checkerboard pattern (3x3 grid)
a = 2/3; % Size of each square in the 3x3 grid
res = mod(floor((X + 1) / a) + floor((Y + 1) / a), 2) == 1;
% Plot the region
figure;
imagesc(x, y, res);
colormap(gray);
axis equal;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!