Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

If statement is not correctly operating

1 Ansicht (letzte 30 Tage)
aroj bhattarai
aroj bhattarai am 22 Okt. 2020
Geschlossen: MATLAB Answer Bot am 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.
Can anyone please help me to fix the issue? Thank you very much in advance.
  7 Kommentare
Sindar
Sindar am 27 Okt. 2020
Bearbeitet: Sindar am 27 Okt. 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
aroj bhattarai am 28 Okt. 2020
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.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by