% PS2_1.m This is the m-file answer for Problem set 2 question 1 % This is a fairly simple problem; and is more or less an exercise in plotting clear close all clc ans1=['Start by running the goldmean m-file. Note the output to your screen']; ans2=['is the four column matrix R. As given in the homework problem, the']; ans3=['first column is the iteration number, the second is Phi calculated']; ans4=['by raising n to each successive power, the third column is Phi']; ans5=['calculated using the recursive technique, the fourth column is the']; ans6=['relative error. If you scroll up a bit and examine columns 2 and 3,']; ans7=['you can see the potential problem with the recursion technique and']; ans8=['how tiny errors, if cumulative, can cause problems. ']; goldmean disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5),disp(ans6),disp(ans7),disp(ans8) pause clc ans1=['Have you noticed how matlab has multiplied everything by 1e4?']; ans2=['Isn''t this aggravating? To see the numbers in a more reasonable']; ans3=['format use the command format short g and then type R again, or type']; ans4=['help format for other formatting options. ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4) pause clc format short g R ans1=['That''s a little better. The differences are easier to spot']; ans2=['Now suppose we want to see these differences graphically?']; ans3=['Let''s plot the iteration power vs. the absolute value of the']; ans4=['relative error using the command plot(R(:,1),abs(R(:,4)))']; disp(ans1),disp(ans2),disp(ans3),disp(ans4) pause clc t=input('How many seconds would you like to view the figure for?'); clc plot(R(:,1),abs(R(:,4))) title('Iteration Power vs. Absolute value of the relative error') xlabel('Iteration Power') ylabel('Absolute value of the Relative error') text(7,1e4,'Notice the large discrepancy between the two') text(7,.9e4,'different methods in obtaining the value of Phi') text(7,.8e4,'at around iteration 44?') figure(1) pause(t) delete(figure(1)) q=input('Do you want to see it again? 1=yes 2=no '); if q==1 plot(R(:,1),abs(R(:,4))) title('Iteration Power vs. Absolute value of the relative error') xlabel('Iteration Power') ylabel('Absolute value of the Relative error') text(7,1e4,'Notice the large discrepancy between the two') text(7,.9e4,'different methods in obtaining the value of Phi') text(7,.8e4,'at around iteration 44?') figure(1) pause(t) delete(figure(1)) end ans1=['End of problem 1']; disp(ans1)