4

I am Beginner in Matlab, i would like to plot system concentration vs time plot at a certain time interval following is the code that i have written

%Input function of 9 samples with activity and time calibrated with Well %counter value approx : 1.856 from all 9 input values of 3 patients

function c_o = Sample_function(td,t_max,A,B)

   t   =(0 : 100  :5000); % time of the sample post injection in mins
   c   =(0 : 2275.3 :113765);

   A_max= max(c);   %Max value of Concentration (Peak of the curve)

   if (t >=0 && t <= td)
      c_o(t)=0;
   else if(td <=t && t<=t_max)
      c_o(t)= A_max*(t-td);
   else if(t >= t_max)
      c_o(t)=(A(1)*exp(-B(1)*(t-t_max)))+(A(2)*exp(-B(2)*(t- t_max)))+...
             (A(3)*exp(-B(3)*(t-t_max)));
   end

   fprintf('plotting Data ...\n');
   hold on;
   figure;
   plot(c_o);
   xlabel('Activity of the sample Ba/ml ');
   ylabel('time of the sample in minutes');
   title (' Input function: Activity sample VS time ');
   pause;
   end

I am getting following error

Operands to the || and && operators must be convertible to logical scalar values.

Error in Sample_function (line 18)
if (t >=0 && t <= td)

Kindly .Let me know if my logic is incorrect

1
  • See what I meant in my comment to your previous question? In this case, other people have taken the time to edit your question and format/indent your code properly, so that everyone can read it. For subsequent questions, please consider doing the formatting yourself to save other people's time. Oh and thanks for caring about my students :-) Commented Feb 16, 2014 at 3:21

3 Answers 3

2

Your t is not a single value to compare with 0 so it cannot evaluate to true or false.

Sign up to request clarification or add additional context in comments.

Comments

1

You want to do this with logical indexing

c_o = zeros(size(t));

c_o(t>=0 & t<=td) = 0; % this line is actually redundant and unnecessary since we initialized the vector to zeros
c_o(t>td & t<=t_max) = A_max*(t(t>td & t<=t_max)-td);
c_o(t>t_max) = (A(1)*exp(-B(1)*(t(t>t_max)-t_max)))+(A(2)*exp(-B(2)*(t(t>t_max)- t_max)))...
      +  (A(3)*exp(-B(3)*(t(t>t_max)-t_max)));

You could also make this a little prettier (and easier to read) by assigning the logical indexes to variables:

reg1 = (t>=0 & t<=td);
reg2 = (t>td & t<=t_max);
reg3 = (t>t_max);

Then, for instance, the second assignment becomes the much more readable:

c_o(reg2) = A_max*(t(reg2)-td);

Comments

1

t is written as a array of numbers. So, it can't be compared with a scalar value ex. 0. Try it in a for loop

for i=1:length(t)
   if (t(i) >=0 && t(i) <= td)
      c_o(t(i))=0;
   else if(td <=t(i) && t(i)<=t_max)
      c_o(t(i)))= A_max*(t(i)-td);
   else if(t(i) >= t_max)
      c_o(t)=(A(1)*exp(-B(1)*(t(i)-t_max)))+(A(2)*exp(-B(2)*(t(i)- t_max)))...
      +  (A(3)*exp(-B(3)*(t(i)-t_max)));

   end
end 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.