How to determine index for gradient?
Hello,
I’m trying to determine the index for a change in gradient along the radial direction from x=0 to x=1, which is expected to be negative. My code which runs but doesn’t give correct result. Kindly have a look at the same and suggest me the possible correction(s) please.
data.variable.gradpressure = 10000 x 100 matrix
data.variable.x = 1 x 100 vector
Moreover the matchflag is also false when I try to match the columns which are both equal.
The code is as below:
% Determine the index for negative gradient from x=0 to x=1
clear grad_width;
global grad_width;
global data;
match_flags = false(size(data.variable.gradpressure,1),1); % Logical vector for matching rows
% Check if any row of pressure equals x
for i = 1:size(data.variable.x, 1)
if isequal(data.variable.gradpressure(:,i)’, data.variable.x(:,i))
match_flags(i) = true;
end
end
%disp(match_flags)
threshold_fraction = 0.1;
% Preallocate result arrays
nTimes = length(data.variable.t);
grad_foot_idx = zeros(nTimes,1);
grad_head_idx = zeros(nTimes,1);
grad_width = zeros(nTimes,1);
for i = 1:nTimes
gradp = data.variable.gradpressure(i,:); % 1D vector at time i
% Avoid division by zero
valid = gradp(1:end-1) ~= 0;
% Compute gradient change (like second derivative)
change_gp = zeros(1, length(gradp)-1);
change_gp(valid) = diff(gradp)./gradp(1:end-1); % Central diff approx
% Find steepest drop (most negative change)
[max_grad1, max_grad1_idx] = min(abs(change_gp)); % Min of |second derivative|
grad_threshold = threshold_fraction * abs(max_grad1);
% Search left (foot)
left_idx = max_grad1_idx;
while left_idx > 1 && abs(change_gp(left_idx)) > grad_threshold
left_idx = left_idx – 1;
end
% Search right (head)
right_idx = max_grad1_idx;
while right_idx < length(change_gp) && abs(change_gp(right_idx)) > grad_threshold
right_idx = right_idx + 1;
end
% Store
grad_foot_idx(i) = left_idx;
grad_head_idx(i) = right_idx;
grad_width(i) = data.variable.x(right_idx) – data.variable.x(left_idx);
end
% Display last result (or modify to plot or analyze all)
disp(grad_width(end));Hello,
I’m trying to determine the index for a change in gradient along the radial direction from x=0 to x=1, which is expected to be negative. My code which runs but doesn’t give correct result. Kindly have a look at the same and suggest me the possible correction(s) please.
data.variable.gradpressure = 10000 x 100 matrix
data.variable.x = 1 x 100 vector
Moreover the matchflag is also false when I try to match the columns which are both equal.
The code is as below:
% Determine the index for negative gradient from x=0 to x=1
clear grad_width;
global grad_width;
global data;
match_flags = false(size(data.variable.gradpressure,1),1); % Logical vector for matching rows
% Check if any row of pressure equals x
for i = 1:size(data.variable.x, 1)
if isequal(data.variable.gradpressure(:,i)’, data.variable.x(:,i))
match_flags(i) = true;
end
end
%disp(match_flags)
threshold_fraction = 0.1;
% Preallocate result arrays
nTimes = length(data.variable.t);
grad_foot_idx = zeros(nTimes,1);
grad_head_idx = zeros(nTimes,1);
grad_width = zeros(nTimes,1);
for i = 1:nTimes
gradp = data.variable.gradpressure(i,:); % 1D vector at time i
% Avoid division by zero
valid = gradp(1:end-1) ~= 0;
% Compute gradient change (like second derivative)
change_gp = zeros(1, length(gradp)-1);
change_gp(valid) = diff(gradp)./gradp(1:end-1); % Central diff approx
% Find steepest drop (most negative change)
[max_grad1, max_grad1_idx] = min(abs(change_gp)); % Min of |second derivative|
grad_threshold = threshold_fraction * abs(max_grad1);
% Search left (foot)
left_idx = max_grad1_idx;
while left_idx > 1 && abs(change_gp(left_idx)) > grad_threshold
left_idx = left_idx – 1;
end
% Search right (head)
right_idx = max_grad1_idx;
while right_idx < length(change_gp) && abs(change_gp(right_idx)) > grad_threshold
right_idx = right_idx + 1;
end
% Store
grad_foot_idx(i) = left_idx;
grad_head_idx(i) = right_idx;
grad_width(i) = data.variable.x(right_idx) – data.variable.x(left_idx);
end
% Display last result (or modify to plot or analyze all)
disp(grad_width(end)); Hello,
I’m trying to determine the index for a change in gradient along the radial direction from x=0 to x=1, which is expected to be negative. My code which runs but doesn’t give correct result. Kindly have a look at the same and suggest me the possible correction(s) please.
data.variable.gradpressure = 10000 x 100 matrix
data.variable.x = 1 x 100 vector
Moreover the matchflag is also false when I try to match the columns which are both equal.
The code is as below:
% Determine the index for negative gradient from x=0 to x=1
clear grad_width;
global grad_width;
global data;
match_flags = false(size(data.variable.gradpressure,1),1); % Logical vector for matching rows
% Check if any row of pressure equals x
for i = 1:size(data.variable.x, 1)
if isequal(data.variable.gradpressure(:,i)’, data.variable.x(:,i))
match_flags(i) = true;
end
end
%disp(match_flags)
threshold_fraction = 0.1;
% Preallocate result arrays
nTimes = length(data.variable.t);
grad_foot_idx = zeros(nTimes,1);
grad_head_idx = zeros(nTimes,1);
grad_width = zeros(nTimes,1);
for i = 1:nTimes
gradp = data.variable.gradpressure(i,:); % 1D vector at time i
% Avoid division by zero
valid = gradp(1:end-1) ~= 0;
% Compute gradient change (like second derivative)
change_gp = zeros(1, length(gradp)-1);
change_gp(valid) = diff(gradp)./gradp(1:end-1); % Central diff approx
% Find steepest drop (most negative change)
[max_grad1, max_grad1_idx] = min(abs(change_gp)); % Min of |second derivative|
grad_threshold = threshold_fraction * abs(max_grad1);
% Search left (foot)
left_idx = max_grad1_idx;
while left_idx > 1 && abs(change_gp(left_idx)) > grad_threshold
left_idx = left_idx – 1;
end
% Search right (head)
right_idx = max_grad1_idx;
while right_idx < length(change_gp) && abs(change_gp(right_idx)) > grad_threshold
right_idx = right_idx + 1;
end
% Store
grad_foot_idx(i) = left_idx;
grad_head_idx(i) = right_idx;
grad_width(i) = data.variable.x(right_idx) – data.variable.x(left_idx);
end
% Display last result (or modify to plot or analyze all)
disp(grad_width(end)); gradient MATLAB Answers — New Questions