If statement is not correctly operating
Info
This question is closed. Reopen it to edit or answer.
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 23 Oct 2020
why do you have a for loop over a single element? (for r = 1)
per isakson
on 23 Oct 2020
With r==1 the expression
(lambda(r,1) == lambda(1,1)) && (lambda(r,2) == lambda(1,2))
is always true. However, == doesn't go well together with double because of floating point errors.
aroj bhattarai
on 23 Oct 2020
Sindar
on 24 Oct 2020
From what I can see, it's not an issue with the if Taum_t < Taum_max statement. My suggestion is to go into debug mode and step through one line at a time, keeping track of the variable values.
Also, you seem to have completely missed both our points about the r-loop, so I'll rephrase
- Why have a loop at all when you only do one iteration?
- Why loop through more iterations when the first one should do everything you need, and later iterations either do nothing or repeat the same action?
- Checking equality for doubles is risky. There are other strategies which are safer
- Why are you even checking for equality when you know the items are equal?
This has the intended effect, no loops no ifs:
Dmat = 0.0;
Dmat_ant = 0.0;
Thetam = 0.0;
Thetam_ant = 0.0;
aroj bhattarai
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?
aroj bhattarai
on 28 Oct 2020
Answers (0)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!