How can I change a variable and collect an vector of outputs?

2 Ansichten (letzte 30 Tage)
Hello! I'm trying to figure out a way that I can vary V, and recieve an array of dc values that I can plot. Something about the nature of how dc is being obtained here is really throwing me off.
I've tried messing around with functions, whiles, and fors but I can't seem to make it work how I want it to. Any help is greatly appriciated!
Here's my code so far:
% process Parameters
V = 10 % S/L interface velocity in microns/sec
% Material Parameters
c0 = 0.2; % Alloy composition
k = 0.28; % Partition Coefficient (conc solute in solid/ liquid)
D = 10; % Diffusivity in microns^2/sec
dc_tol = 1.01*c0; % Defines how close to c0 is acceptable
% Mesh Parameters
n = 500; % number of nodes
L = 5; % Length of nodes in microns
dx = L / n; % defining node spacing
x = linspace(0, L, n); % Mesh
% Initialise concentration field
c_field = ones(1, n)*c0; % Creates a field vector of intial concentrration
c_field_update = c_field;
% Initialise dc Field
dc_field = zeros(1, 100);
% Boundary Conditions
c_field(1) = c0 / k;
c_field(n) = c0;
% Solver controls
tol = 1e-3; % Sets the tolerance for stopping the iteration
err = 1; % sets starting value to check against tol
F0 = 0.5*dx*D/V; % Finite Difference Coefficient
% Solver
while err > tol
% Apply Boundary Conditions
c_field_update(1) = c_field(1);
c_field_update(n) = c_field(n);
% Finite Difference Scheme
for i = 2:(n-1)
c_field_update(i) = 0.5*(1+F0)*c_field(i+1)+0.5*(1-F0)*c_field(i-1);
end
% Compute Error
errVector = abs((c_field_update - c_field)./ c_field_update);
err = max(errVector);
% Update Concentration Field
c_field = c_field_update;
end
% Determine Solute Boundary Layer
% The ismembertol function gives number of nodes into the mesh for the first value that meets the dc_tol within a certain toleranmce, idx_tol)
idx_tol = 0.001*dc_tol;
[~, idx] = ismembertol(dc_tol, c_field, idx_tol);
dc = idx*dx % solute boundary layer thickness in microns

Akzeptierte Antwort

Star Strider
Star Strider am 6 Mär. 2025
One approach is to define a vector of ‘V’ values and then iterate.
Try something like this —
% process Parameters
V = 10 % S/L interface velocity in microns/sec
V = 10
Vv = (2:2:20).';
for k1 = 1:numel(Vv)
V = Vv(k1);
% Material Parameters
c0 = 0.2; % Alloy composition
k = 0.28; % Partition Coefficient (conc solute in solid/ liquid)
D = 10; % Diffusivity in microns^2/sec
dc_tol = 1.01*c0; % Defines how close to c0 is acceptable
% Mesh Parameters
n = 500; % number of nodes
L = 5; % Length of nodes in microns
dx = L / n; % defining node spacing
x = linspace(0, L, n); % Mesh
% Initialise concentration field
c_field = ones(1, n)*c0; % Creates a field vector of intial concentrration
c_field_update = c_field;
% Initialise dc Field
dc_field = zeros(1, 100);
% Boundary Conditions
c_field(1) = c0 / k;
c_field(n) = c0;
% Solver controls
tol = 1e-3; % Sets the tolerance for stopping the iteration
err = 1; % sets starting value to check against tol
F0 = 0.5*dx*D/V; % Finite Difference Coefficient
% Solver
while err > tol
% Apply Boundary Conditions
c_field_update(1) = c_field(1);
c_field_update(n) = c_field(n);
% Finite Difference Scheme
for i = 2:(n-1)
c_field_update(i) = 0.5*(1+F0)*c_field(i+1)+0.5*(1-F0)*c_field(i-1);
end
% Compute Error
errVector = abs((c_field_update - c_field)./ c_field_update);
err = max(errVector);
% Update Concentration Field
c_field = c_field_update;
end
% Determine Solute Boundary Layer
% The ismembertol function gives number of nodes into the mesh for the first value that meets the dc_tol within a certain toleranmce, idx_tol)
idx_tol = 0.001*dc_tol;
[~, idx] = ismembertol(dc_tol, c_field, idx_tol);
dc = idx*dx; % solute boundary layer thickness in microns
dcv(k1,:) = dc;
end % ‘k’ Loop
Result = table(Vv,dcv)
Result = 10x2 table
Vv dcv __ ____ 2 0.52 4 0.64 6 0.69 8 0.71 10 0.73 12 0.74 14 0.75 16 0.76 18 0.76 20 0.77
figure
plot(Vv, dcv)
grid
xlabel('V')
ylabel('dc')
.
  2 Kommentare
Charlie Rideout
Charlie Rideout am 6 Mär. 2025
This makes a lot of sense! Yes absolutely, thank you! For some reason I was stuck on the idea of varying V as a variable... I need to get back into the habit of treating everything as a matrix or vector. Cheers!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Torsten
Torsten am 6 Mär. 2025
Bearbeitet: Torsten am 6 Mär. 2025
V_array = 0.5:0.1:20;
% Material Parameters
c0 = 0.2; % Alloy composition
k = 0.28; % Partition Coefficient (conc solute in solid/ liquid)
D = 10; % Diffusivity in microns^2/sec
dc_tol = 1.01*c0; % Defines how close to c0 is acceptable
% Mesh Parameters
n = 500; % number of nodes
L = 5; % Length of nodes in microns
dx = L / n; % defining node spacing
x = linspace(0, L, n); % Mesh
% Initialise concentration field
c_field = ones(1, n)*c0; % Creates a field vector of intial concentrration
% Boundary Conditions
c_field(1) = c0 / k;
c_field(n) = c0;
c_field_update = c_field;
% Solver controls
tol = 1e-8; % Sets the tolerance for stopping the iteration
for j = 1:numel(V_array)
V = V_array(j);
F0 = 0.5*dx*D/V; % Finite Difference Coefficient
err = 1; % sets starting value to check against tol
% Solver
while err > tol
% Finite Difference Scheme
for i = 2:(n-1)
c_field_update(i) = 0.5*(1+F0)*c_field(i+1)+0.5*(1-F0)*c_field(i-1);
end
% Compute Error
errVector = abs((c_field_update - c_field)./ c_field_update);
err = max(errVector);
% Update Concentration Field
c_field = c_field_update;
end
% Determine Solute Boundary Layer
% The ismembertol function gives number of nodes into the mesh for the first value that meets the dc_tol within a certain toleranmce, idx_tol)
idx_tol = 0.001*dc_tol;
[~, idx] = ismembertol(dc_tol, c_field, idx_tol);
dc(j) = idx*dx; % solute boundary layer thickness in microns
end
plot(V_array,dc)

Kategorien

Mehr zu Historical Contests finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by