How to work around dynamic declaration of variables?

3 次查看(过去 30 天)
Good Day to All:
I am tasked with simulating the discrete mean value property of a shape/mesh in thermal equilibrium. While I understand the problem and the method by hand, I am a novice in MatLab. So far I've been able to create the polygon, create a mesh of only the the points on the edge and in the polygon, and I've been able to associate the temperature value along the boundies of the polygon (which is the given in the procedure). Now I must create a matrix with points shown here. I could directly declare my syms (t0, t1, t2, ... tn) however I made my program so I'd only change the interval of the grid to solve meshes a, b, and c (variable u).
Tl;Dr
My problem is that i now need to create the equation at every interior point of the mesh with its neighboring points which may be 1, 2, 0, ot a variable (t0, t1, t2, ...). While I understand that a for-loop would be the way to go, I'm stuck in answering the declaration of variables problem. Do I go ahead and dynamically declare them depending on the no of interior points I have?
I apologize in advance if my codes screaming novice, or if copy-pasting the whole code was unecessary but I'd like to get a complete answer w/o any misundestanding so I figured for wholeness I'd send what I have so far.
syms t
temp1 = 1;
temp2 = 2;
temp3 = 0;
close all
u=0.5;
[Xq,Yq] = meshgrid(0:u:5, 0:u:5); % create a mesh with intervale u
x = [1, 1, 3, 3, 1]; % boundaries of trapezoid
y = [1, 4, 2, 1, 1];
axis equal; %set axis
hold on
plot(x, y, 'Color', [0 0 0], 'LineWidth', 2); % plots grid and shape
[in,on] = inpolygon(Xq, Yq, x, y); % separates points within in, on, out the shape
plot(Xq(in), Yq(in), '*', 'Color', [0 1 1]); % includes all the points inside & on the edge of the shape
%plot(Xq(on), Yq(on), '*', 'Color', [0 1 1]); % only points on shape
%create another matrix with the format (x, y, value at (x,y))
outline = [Xq(on), Yq(on)];
%disp('This is outline: ');disp(outline);
borderTemp = zeros(1, size(outline, 1));
for i = 1:size(outline, 1) %if the y value of the ordered pair is 1 then =temp1
if outline(i, 2) == 1 %if the x value of the ordered pair is 0 then =tem2
borderTemp(1, i) = temp1;
elseif outline(i, 1) == 1
borderTemp(1, i) = temp2;
end
end
for i = 1:size(outline) %the remaining slots in the borderTemp matrix is =temp3
if borderTemp(1, i) == 0
borderTemp(1, i) = temp3;
end
end
borderTemp1 = string(borderTemp.');
text(Xq(on)+0.1, Yq(on)+0.1, borderTemp1); %inserts the border temp values to the
borderTemp = borderTemp.';
insidePts = [Xq(in&~on), Yq(in&~on)];
%disp('This is the inside points');disp(insidePts);
insideMat = t*ones(size(insidePts, 1), 1); %make column vector with the names of the variables
numberingVar = (0:size(insidePts, 1)-1).';
str1 = string(numberingVar); str2 = string(insideMat); %instead of creating a lot of variables
textVar = append(str2, str1); %(t0, t1, t2, t3 ... tn). I'm going to have an column vector
%disp(textVar); %consisting of only t's for the solution process and I added this
text(Xq(in&~on), Yq(in&~on), textVar, 'FontSize', 7) %portion only to show the t0, t1, t2, t3 ...
%now we have the points of interest (insidePts) we have to find the average
%termperature based on the sum of its neighboring points/4.
%first we create two matrices (one for edge points and one for interior points)
%the format of the matrice is x, y, value at (x, y)
edgeMat = [outline borderTemp];
disp('Edge Points:')
Edge Points:
disp(" x y value @ (x, y)");
x y value @ (x, y)
disp(edgeMat);
1.0000 1.0000 1.0000 1.0000 1.5000 2.0000 1.0000 2.0000 2.0000 1.0000 2.5000 2.0000 1.0000 3.0000 2.0000 1.0000 3.5000 2.0000 1.0000 4.0000 2.0000 1.5000 1.0000 1.0000 1.5000 3.5000 0 2.0000 1.0000 1.0000 2.0000 3.0000 0 2.5000 1.0000 1.0000 2.5000 2.5000 0 3.0000 1.0000 1.0000 3.0000 1.5000 0 3.0000 2.0000 0
inMat = [insidePts textVar ];
disp('Inside Points:')
Inside Points:
disp(" x y value @ (x, y)");
x y value @ (x, y)
disp(inMat);
"1.5" "1.5" "t0" "1.5" "2" "t1" "1.5" "2.5" "t2" "1.5" "3" "t3" "2" "1.5" "t4" "2" "2" "t5" "2" "2.5" "t6" "2.5" "1.5" "t7" "2.5" "2" "t8"

回答(1 个)

Sameer
Sameer 2024-4-26
Hi Lorenzo
From my understanding, you want to simulate the discrete mean value property in thermal equilibrium for different meshes by dynamically generating variables for each interior point's temperature and formulating equations that relate these temperatures to the average temperatures of their neighboring points.
To simulate the discrete mean value property of a shape/mesh in thermal equilibrium, one can dynamically generate variables for each interior point of a mesh and create equations that relate each point's temperature to the average temperature of its neighbors. This approach is particularly useful when the goal is to adapt a program for different meshes by simply changing the grid interval (u).
Here's a general guide on dynamically declaring variables and formulating the equations in MATLAB, leveraging the symbolic toolbox.
Dynamic Variable Creation
Symbolic variables for each interior point can be generated dynamically based on the number of interior points. This avoids the need for manual declaration of each variable.
% Assuming the number of interior points is determined
numInteriorPoints = size(insidePts, 1);
% Dynamically generate symbolic variables
syms t[numInteriorPoints] % Creates t1, t2, ..., tn dynamically
Formulating Equations
For each interior point, an equation needs to be created that sets the point's temperature to the average temperature of its neighbors. This involves identifying the neighbors for each point and then creating the corresponding equation.
Identifying Neighbors and Creating Equations
The process of identifying neighbors and creating equations involves iterating over each interior point, determining its neighbors based on their coordinates, and then formulating an equation that represents the mean value property.
Identify Neighbors: The identification of neighbors is based on their proximity to the current interior point, often determined by the grid interval (u).
Create Equations: An equation is created for each interior point, setting its temperature equal to the average temperature of its neighbors. Neighbors at the boundary have known temperatures, while interior neighbors are represented by symbolic variables.
Here is a conceptual approach to identifying neighbors and creating equations. The specifics will vary based on the grid layout and data structure used to store point information.
% Assuming a loop over all interior points
pointIndex = 1; % Example index for demonstration
point = insidePts(pointIndex, :); % Current point
% Based on the grid structure need to write Logic to identify neighbors
% Creating an equation for the current point
% Assuming 4 neighbors (North, South, East, West) for simplicity
equation = t(pointIndex) == (tN + tS + tE + tW) / 4; % Use actual references to neighbors
Solving the Equations
Once all equations are formulated, they can be solved simultaneously to find the temperature at each interior point. This step typically involves using MATLAB's solve function, providing it with the vector of equations and the symbolic variables representing the temperatures.
Please refer to the link below for more information regarding Symbolic Math Toolbox:
I hope this helps!
Sameer

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by