%=====CME434 Fall 06: Solution to HW2 by TSD 09/07/06 %FIDI_NAME = input('Input Data File Name: ', 's'); %Get typed name of FIDI %FIDO_NAME = input('Output Data File Name: ', 's'); %Get typed name of FIDO status = fclose('all') %Make sure all files are closed FIDI_NAME = 'HW2data.txt' %Name of FIDI FIDO_NAME = 'HW2output.txt' %Name of FIDO %----- Read Input Data File into Row Vectors A & B ----- FIDI = fopen(FIDI_NAME, 'r'); %Open input file disp(blanks(1)); %Write blanks to screen nn = str2num(fgets(FIDI)) %Read nn A=zeros(1,nn); %Initialize A 1 x nn B=zeros(1,nn); %Initialize B 1 x nn for x=1:nn %Read A, each element on newline A(x) = str2num(fgets(FIDI)); end A %Display A B = str2num(fgets(FIDI)) %Read & display B, all elements on one line status = fclose('all') %Close all files %----- Create Vector C ----- x=1:16 C=exp(-0.05*x) %Calculate 16 elements of C %----- Make Output File ----- FIDO = fopen(FIDO_NAME, 'w'); fprintf(FIDO,'%g\r\n',16); fprintf(FIDO,'%f\r\n',C); status = fclose('all') %----- Make Graph ----- ylabel('f(x)') %Label vert-axis xlabel('x') %Label horizontal-axis grid on %Show grid on graph title('HW2 by tdranger') %Specify Title hold on %Accept multiple plots plot(A,':') %Plot A w/ dots plot(B,'--') %plot B w/ dashes plot(C) %Plot C solid by default hold off %Done with plotting %print %Print Graph on paper