MATLAB Answers

0

creating algorithm for specific computation

Asked by sermet on 19 Sep 2018
Latest activity Commented on by Guillaume
on 19 Sep 2018
I need to perform below computation using algorithm approach.
t=100; % constant et=1:1:4; % the numbers of "et" are variable.
result=(((t-et(2))*(t-et(3))*(t-et(4)))/((et(1)-et(2))*(et(1)-et(3))*(et(1)-et(4))))+(((t-et(1))*(t-et(3))*(t-et(4)))/((et(2)-et(1))*(et(2)-et(3))*(et(2)-et(4))));
How can I create algorithm to perform above computation automatically with respect to the array number of et?

  0 Comments

Sign in to comment.

1 Answer

Answer by Guillaume
on 19 Sep 2018
 Accepted Answer

If I understood correctly:
numerators = t - repmat(et(:), 1, numel(et)); %calculate t-et(1), t-et(2), etc.
numerators(logical(eye(size(numerators)))) = 1; %replace t-et by 1 on diagonal
numerators = prod(numerators);
denominators = et - et';
denominators(logical(eye(size(denominators)))) = 1;
denominators = prod(denominators);
result = sum(numerators ./ denominators);
However, the result of this is always going to be 1. So
result = 1;
may be simpler.

  2 Comments

I got "Matrix dimensions must agree" error for denominators = et - et'; line
That would be because you're using an ancient version of matlab (< R2016b). In older versions:
denominators = bsxfun(@minus, et, et');

Sign in to comment.