I would code it differently, but there are two changes you would need to make in your code:
f1 = []; % Initialise ‘f1’ As Empty
for x1=0:.1:pi
f=cos(x1); % Use a Different Variable Here
f1=[f1 f]; % Concatanate the New Value With Previous
end
Reproduce your ‘x1’ vector in your for loop statement to plot your function.