function [rsk, ret] = Demo4_task_optim_jobs(covMat, expRet, muVec, numTasks)
%Demo4_task_optim_scheduled_job Find the frontier for a stock portfolio with the given
%mean and covariance.
% [rsk, ret] = Demo4_task_optim_scheduled_job(covMat, expRet, muVec) returns the
% risk-return relationship in the stock portfolio that is optimal in the
% mean-variance sense. covMat and expRet are the covariance and mean returns
% of a collection of stocks and muVec is a vector of desired returns.
% Copyright 2008 The MathWorks, Inc.
% Divide the vector |muVec| into |numTasks| segments.
[muSplit, numTasks] = pctdemo_helper_split_vector(muVec, numTasks);
%% Create and Submit the Job
% Let us create the optimization job and the tasks in the job. We let
% task |i| perform the optimization for all the values found in
% |muSplit{i}|.
job = createJob();
for i = 1:numTasks
createTask(job, @Demo4_task_optim, 2, {covMat, expRet, muSplit{i}});
end
%% Submit the job and wait for it to finish
submit(job);
waitForState(job, 'finished');
%% Retrieve the Results
% Let us obtain the job results, verify that all the tasks finished successfully
% and then destroy the job. We throw an error if we could not obtain any
% results, but display a warning if we got only some of the results.
jobResults = getAllOutputArguments(job);
%%
% We combine the results from the individual tasks into two vectors, the risks
% and the returns, and we verify that we obtained all the results that we were
% expecting.
rsk = [jobResults{:, 1}];
ret = [jobResults{:, 2}];
%%
% We have now finished all the verifications, so we can destroy the job.
destroy(job);
end