simulation of the target

34 ビュー (過去 30 日間)
bouchra turki
bouchra turki 2024 年 11 月 19 日 16:43
Hello everyone,
I'm working on a simulation of the following target using MATLAB. Fortunately, I succeeded in generating the profile, but there is one thing left that I'm still trying to achieve. I need to have a smooth transition between the circles and the flat section, as shown in the picture.
Please, if you could provide me with any information, I would be very grateful.
Thank you in advance.

回答 (2 件)

Madheswaran
Madheswaran 2024 年 11 月 20 日 11:28
To achieve a seamless transition between the 'rectangular' and 'circular' sections of your plot, consider plotting an arc to bridge them. The following code, inspired by a MATLAB Central discussion, demonstrates how to create such an arc: https://mathworks.com/matlabcentral/answers/367126-plot-an-arc-on-a-2d-grid-by-given-radius-and-end-points
Once the arcs are constructed, you can utilize the polyshape function to connect the top and bottom arcs, allowing you to fill the area between them. An added benefit of using polyshape is its ability to translate a polygon for reuse on other sides.
Here is the implementation for the described approach:
function [x, y] = bottomArc(startPoint, endPoint, radius)
d = norm(endPoint-startPoint);
C = (endPoint+startPoint)/2+sqrt(radius^2-d^2/4)/d*[0,-1;1,0]*(endPoint-startPoint);
a = atan2(startPoint(2)-C(2),startPoint(1)-C(1));
b = atan2(endPoint(2)-C(2),endPoint(1)-C(1));
b = mod(b-a,2*pi)+a; % Ensure that arc moves counterclockwise
t = linspace(a,b,1000);
x = C(1)+radius*cos(t);
y = C(2)+radius*sin(t);
end
function [x, y] = topArc(startPoint, endPoint, radius)
d = norm(endPoint-startPoint);
C = (endPoint+startPoint)/2+sqrt(radius^2-d^2/4)/d*[0,-1;1,0]*(endPoint-startPoint);
a = atan2(startPoint(2)-C(2),startPoint(1)-C(1));
b = atan2(endPoint(2)-C(2),endPoint(1)-C(1));
b = mod(b-a,2*pi)+a; % Ensure that arc moves counterclockwise
t = linspace(a,b,1000);
x = C(1)+radius*cos(t);
y = C(2)+radius*sin(t);
end
figure;
rectangle("Position", [3, -1, 20, 2], "FaceColor", [0, 0, 1], "EdgeColor", "none");
hold on
rectangle("Position", [0, -2, 4, 4], "FaceColor", [0, 0, 1], "Curvature", [1 1], "EdgeColor", "none");
rectangle("Position", [21, -2, 4, 4], "FaceColor", [0, 0, 1], "Curvature", [1 1], "EdgeColor", "none");
[xtop, ytop] = topArc([2.8;1.8], [8; 1], 15);
[xbot, ybot] = bottomArc([8; -1], [2.8;-1.8], 15);
x_arc = [xtop xbot];
y_arc = [ytop ybot];
left = polyshape(x_arc, y_arc);
right = translate(left, 21, 0);
right = rotate(right, 180, [23, 0]);
plot(left, "FaceColor", [0 0 1], "FaceAlpha", 1, "EdgeColor", "none");
plot(right, "FaceColor", [0 0 1], "FaceAlpha", 1, "EdgeColor", "none");
axis equal
xlim([-5 30])
ylim([-2.5 2.5])
xlabel('X (\mum)')
ylabel('Y (\mum)')
grid on;
You can modify the starting point, ending point and radius to change the look and feel of the arcs.
For more information, refer to the following MathWorks documentations:
  1. https://mathworks.com/help/matlab/ref/polyshape.html
  2. https://mathworks.com/help/matlab/ref/polyshape.plot.html
Hope this solution is helpful to you!

bouchra turki
bouchra turki 33分 前
編集済み: bouchra turki 10分 前
Thank you so much, I really appreciate your help.
The following code allows me to calculate the target density in the circles and in the flat section (rectangle), but I also have the density in the arcs. I added a for condition to my script to account for the arcs (I wrote the functions in separate .m files). unfortunetaly I couldn't obtain the desired profile.
P.S: Using the code you provided, we can only add the arcs to my original target, the goaI is to have the entire profil.
Thank you .
% Define parameters
targ_rod_thickness = 0.6; % Thickness of the flat section in microns
targ_length = 25; % Total length of the target in microns
targ_ball_radius = 2; % Radius of the spherical balls in microns
critical_density = 1.73e27; % Critical density per m3
targ_dens = 30 * critical_density; % Density value for Hydrogen target
curvature = 1; % The expo region
% ############### Grid resolution ###############
dx = 0.05; % Resolution
x = -5:dx:targ_length + 5; % Positive x-coordinates
y = -4 * targ_rod_thickness:dx: 4 * targ_rod_thickness; % y-coordinates for thickness range
[X, Y] = meshgrid(x, y); % A 2D grid of coordinates
% ############# Initialize density matrix #############
density = zeros(size(X));
% Density for Flat Section (Central Rod)
for i = 1:numel(X)
if (Y(i) >= -targ_rod_thickness) && (Y(i) <= targ_rod_thickness) && ...
(X(i) >= targ_ball_radius) && (X(i) <= targ_length - targ_ball_radius)
density(i) = targ_dens; % Set density for the rod region
else
density(i) = 0; % Otherwise, keep density as 0
end
end
% ################### Density for Circular Ends ####################
% % Right end circle
ball_right_condition = ((X - (targ_length - targ_ball_radius)).^2 + Y.^2 <= targ_ball_radius^2);
density(ball_right_condition) = targ_dens;
% % Left end circle
ball_left_condition = ((X - targ_ball_radius).^2 + Y.^2 <= targ_ball_radius^2);
density(ball_left_condition) = targ_dens;
% ################### Density for the Arc Transition #####################
% Define arc parameters
[xtop, ytop] = topArc([2.8;1.8], [8; 1], 15); % Top arc
[xbot, ybot] = bottomArc([8; -1], [2.8; -1.8], 15); % Bottom arc
% Arc density for Top and Bottom Arcs
for i = 1:numel(X)
for j = 1:numel(Y)
% Distance to top arc
dist_top = sqrt((X(i) - xtop).^2 + (Y(i) - ytop).^2);
% Distance to bottom arc
dist_bot = sqrt((X(i) - xbot).^2 + (Y(i) - ybot).^2);
% If the point is close to the arcs (within a small threshold), apply density
if min(dist_top) < 0.1 || min(dist_bot) < 0.1
density(i, j) = targ_dens; % Assign density for arc regions
end
end
end
% ################### Plot the Target #####################
figure;
contourf(X, Y, density, 'LineColor', 'none'); % Filled contour plot
colormap([1 1 1; 0 0 1]); % White for 0, blue for density
axis equal;
xlabel('X (µm)');
ylabel('Y (µm)');
title('Gas Jet Target with Density on Arcs');

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by