MATLAB Answers

0

Struggling with equation and FOR loops

Asked by Serena Solanki on 8 Nov 2017
Latest activity Commented on by Guillaume
on 8 Nov 2017
Hi I am trying to code this equation into MATLAB, I have tried using for loops but have had no success
The code is as follows:
r=[1 2 3 4 5]
Ai=0.6895
%Ai+r=N
N=[0.3256 0.2254 0.1300 0.07684]
%delta =(1/r)*log(Ai/N)]
I want to solve the equation delta and find delta values for each of the Ai/N values. So I would like to achieve 1/1*log(Ai/0.3256), 1/2*log(Ai/0.2254) etc
Hope you can help

  0 Comments

Sign in to comment.

1 Answer

Answer by Torsten
on 8 Nov 2017
Edited by Torsten
on 8 Nov 2017

Ai=0.6895;
r=[1 2 3 4 5];
N=[0.3256 0.2254 0.1300 0.07684];
for i=1:numel(r)
for j = 1:numel(N)
delta(i,j)=1/r(i)*log(Ai/N(j));
end
end
Best wishes
Torsten.

  2 Comments

Hi Torsten
Thank you very much for this- it has worked perfectly :)
Well, if that's the desired result, and if using matlab R2016b or later, simply:
delta = 1./r'.*log(Ai./N);
And if using matlab < R2016b:
delta = bsxfun(@times, 1./r', log(Ai./N));
No need for loops in any case.

Sign in to comment.