Info

# If statement is not correctly operating

1 view (last 30 days)
aroj bhattarai on 22 Oct 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
Dear Matlab experts,
I have a simple if statement to execute, however is not responding how I am expecting in a relaxation function. In the attached matlab code, I want my code to respond in two steps:
1. Firstly, initialize some internal variables to ZEROS during first elements of X-data vector, i.e., lambda(1,1) and lambda(1,2): The value of these variables remains ZEROS until a subsequent if statement is fulfilled.
for r = 1
if (lambda(r,1) == lambda(1,1)) && (lambda(r,2) == lambda(1,2))
Dmat = 0.0;
Dmat_ant = 0.0;
Thetam = 0.0;
Thetam_ant = 0.0;
end
end
2. Secondly, I need to compute a function value PK2 which is dependent on an internal variable Dmat = [0, 1] and is calculated fulfilling a if statement:
if Taum_t < Taum_max
PK2(:,1) = PK2_peak1;
PK2(:,2) = PK2_peak2;
PK2(:,3) = PK2_peak3;
else
Taum_max = Taum_t;
Amat = (gdm./S0dm.^2 - 0.5).^(-1);
Dmat = 1 - (S0dm./Taum_t).* exp(Amat.*(1 - (Taum_t./S0dm)));
delDmat = Dmat - Dmat_ant;
Thetam = Thetam_ant + WbarISO.* delDmat;
Dmat_ant = Dmat;
Thetam_ant = Thetam;
PK2(:,1) = (1 - Dmat).*PK2_peak1;
PK2(:,2) = (1 - Dmat).*PK2_peak2;
PK2(:,3) = (1 - Dmat).*PK2_peak3;
end
It means, Dmat is calculated only when Taum_t >= Taum_max, for other cases, Dmat = 0. It applies the same for Dmat_ant, Thetam and Thetam_ant.
Running the existing matlab code gives a continusouly increasing PK2 value which does not fit to the Y-data. In addition, commenting the if statement
%if Taum_t < Taum_max
% PK2(:,1) = PK2_peak1;
% PK2(:,2) = PK2_peak2;
% PK2(:,3) = PK2_peak3;
%else
gives the fitted curve towards X-data vs Y-data plot which is again not correct because Dmat, Dmat_ant, Thetam and Thetam_ant are not initialized to ZEROS at the beginning of the calculation.
Thank you Sindar for the explanation, for loop over lambda and the followed if statement gave me what I was looking for. The unclear variables you mentioned were all user-defined scalars except . I could have asked you the simple formulation instead of confusing long paragraphs.