Info
This question is closed. Reopen it to edit or answer.
If statement is not correctly operating
1 view (last 30 days)
Show older comments
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:
- 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.
Can anyone please help me to fix the issue? Thank you very much in advance.
7 Comments
Sindar
on 27 Oct 2020
Edited: Sindar
on 27 Oct 2020
For this particular point, you can easily loop through:
lambda(:,1) = [1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2];
% loop over rows of lambda
for ind=1:size(lambda,1)
if lambda(ind,1) <= 1.6
Dfib(ind) = 0.0;
else
% unclear what variables are vectors
% Dfib(ind) = 1 - (S0d./Tau_t).* exp(Afib.*(1 - (Tau_t./S0d)));
Dfib(ind) = 1 - (S0d(ind)./Tau_t(ind)).* exp(Afib(ind).*(1 - (Tau_t(ind)./S0d(ind))));
end
end
or without the loop:
Dfib=zeros(size(lambda,1),1);
idx = (lambda(:,1) <= 1.6);
Dfib(idx) = 1 - (S0d(idx)./Tau_t(idx)).* exp(Afib(idx).*(1 - (Tau_t(idx)./S0d(idx))));
For your original code, the question is probably what portions of the code need to be in the loop. Does it run through the whole code for each element of lambda? Or, does it operate on a vector of Dfib?
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!