Matrix won't display while inside "if" statement

% This script uses the function, respower, to test several pre-computed % test cases of equivalent resistance and power rating, and it compares the % functional values with actual values.
A = [4 5 3];
B = [200 3 40];
Req = (1/((1/4)+(1/5)+(1/3)));
Peq = 34;
total = respower(A,B);
if (total ~= [Req,Peq])
fprintf('Test values used for resistors: %d ',A);
fprintf('\n')
fprintf('Test values used for power ratings: %d ',B);
fprintf('\n');
fprintf('The actual result for equivalent resistance (left) and equivalent power rating (right) is: %f',[Req,Peq]);
fprintf('\n');
fprintf('The expected result for equivalent resistance in ohms (left) and equivalent power rating in watts (right) is: %f',total);
fprintf('\n');
end
The respower function works correctly. This test is for when the pre-computed equivalent resistance and power rating (test values) does not agree with the function. For some reason, I get absolutely no output from this code. Any help would be greatly appreciated.

 Accepted Answer

The problem is comparing a scalar and a vector. If you want to test ‘total’ against ‘Req’ or ‘Peq’ as a vector, use any.
For example:
A = 1;
B = 2;
C = 1;
if C ~= [A,B]
fprintf('Test #1 Successful!\n\n')
end
if any(C ~= [A,B])
fprintf('Test #2 Successful!\n\n')
end
Does this do what you want?

2 Comments

Yes. You, sir, are a life saver. Thank you!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!