%% reads set of coefficients a,b,c from data file
d = load('data_for_quadratic_eq_solver.dat'); % data file is converted to matrix d

[Nrows, Ncols] = size(d); 

% number of quadratic equations coincides with number of rows in data table d
% since we solving quadratic equation we expect two roots
% so solution matrix will have Nrows x 2 dimensions
solutions=zeros(Nrows,2); 

%% We iterate over all rows and find root of every equation
for i=1:Nrows
	a=d(i,1); % a coefficient is in 1st column
	b=d(i,2); % b coefficient is in 2nd column
	c=d(i,3); % c coefficient is in 3rd column
	[x1,x2] = quad_eq_solver(a,b,c);
	solutions(i,1)=x1;
	solutions(i,2)=x2;
end

%% We form joined a matrix which has combined problem and solution.
% In first 3 column it has coefficients a,b,c
% and in columns 4,5 solutions of the quadratic equation.
% This requires to join/concatenating 2 matrices/arrays along 2nd dimension, i.e. columns
results=cat(2, d, solutions); 
% other possible ways
% results = d; results(:,[4,5]) = solutions; % 1st alternative
% results = d; results(:,4)=solutions(:,1); results(:,5)=solutions(:,2); % 2nd alternative

%% Saving results for future use or evaluation
% I use 'txt' extension so we can easily see result in a text editor
% but it can be anything you like.
fname='results_of_the_batch_solver.txt';
save(fname, '-ascii', 'results'); % note the uses of ''

