I am given a set of points (p1,q1) (p2,q2) ... (p20,q20)
which satisfy the function q = 1/(ap + b)^2
except that one of these does not satisfy the given relation. The values of a
and b
are not given to me. All I have with me is two inputs p
and q
as arrays. I need to find the index of the point which does not satisfy the given relation.
The way I proceeded to solve is to find the values of a
and b
using the first two pairs (p1,q1)
and (p2,q2)
and check if the remaining points satisfy the function for the solved values of a
and b
. The results will be stored in a logical matrix. I wish to make use of the logical matrix to pick out the odd pair, but unable to proceed further.
Specifically, the challenge is to make use of vectorization in MATLAB to find the odd point, instead of resorting to for-loops. I think that I will have to first search for the only logical zero in any of the row. In that case, the column index of that zero will fetch me the odd point. But, if there are more than one zeros in all 4 rows, then the odd point is either of the first two pairs. I need help in translating this to efficient code in MATLAB.
Please note that vectors p
and q
have been named as x
and y
in the below code.
function [res, sol] = findThePair(x, y)
N = length(x);
syms a b
vars = [a,b];
eqns = [y(1) - 1/(a*x(1) + b)^2 == 0; y(2) - 1/(a*x(2) + b)^2];
[solA, solB] = solve(eqns,vars);
sol = [double(solA) double(solB)]; %solution of a & b (total 4 possibilites)
xTest = x(3:end); % performing check on remaining points
yTest = y(3:end);
res = zeros(4, N-2); % logical matrix to store the results of equality check
for i = 1:4
A = sol(i,1); B = sol(i, 2);
res(i, :) = [yTest == 1./(A*xTest + B).^2]; % perform equality check on remaining points
end