% Farm example with chickens and pigs. We know that at the farm there are
% 95 heads and 250 legs. By general biological knowledge we have:
% -Pigs have four legs and chickens have two legs each.
% -Both pigs and chickens have one head each.
%
% How many pigs and how many chickens are there at the farm?
% Let's solve it using matrices and plot the result.
%
% Samuli Siltanen October 2015
% Number of heads
h = 95;
% Number of legs
l = 250;
% Construct system matrix
A = [[1 1];[4 2]];
% Solve the problem
x = inv(A)*[h;l];
NumberOfPigs = x(1)
NumberOfChicken = x(2)
% Plot the result.
t = [-10 100]; % Evaluation points
line1 = -t+95;
line2 = -2*t+125;
figure(1)
clf
plot(t,line1) % First line
hold on
plot(t,line2) % Second line
plot(x(1),x(2),'r.','markersize',20)
set(gca,'xtick',[(min(t)-20):30:(max(t)+20)],'fontsize',22)
axis equal