% Control3_1.m
% Solution to problem 1 of control 3. May 14, 2021.
% JMS, May.2021

% Set data
clear
R0 = [1.5;2.0;1.0];  % resistances at B=0 (Ohm)
mu = [5.0;7.8;0.1];  % magnetoresistance coefficients (Ohm^(1/2)/T)
V = [0.1,0.2];       % source voltages (V)
I = 0.15;            % source intensity (Amp)
Bmax = 0.3;          % max. magnetic field (T)
nB = 101;            % number of magnetic field values

% Loop on magnetic fields
B = linspace(0,Bmax,nB);
for iB = 1:nB
  R = R0.*(1+mu.^2*B(iB)^2);                % resistances for this B
  A = [R(1),0,-R(3); 0,R(2),-R(3); 1,1,1];  % so that A*I=b
  b = [V(1);V(2);I];
  ii(:,iB) = A\b;                           % intensities for this B
  Vf(iB) = R(3)*ii(3,iB);                   % voltage across R(3) and I source
  P(:,iB) = R.*ii(:,iB).^2;                 % power dissipated at each R
end

% Plot various powers
figure(11)
Pdis = sum(P);                         % power of I source, as a function of B
Psum = Vf*I+V(1)*ii(1,:)+V(2)*ii(2,:); % total power of all sources
plot(B,P,B,Pdis,B,Psum,'--','LineWidth',1.5);
legend('P_1','P_2','P_3','P_{dis}','P_{sum}')
xlabel('B (T)')
ylabel('P (W)')
set(gca,'FontSize',15)
grid on
