function amath3132hw1 % Instructor's solution for amath 3132 hw 1 disp('----------My Information-------------') Name = 'Jahrul'; Surname = 'Alam'; ID = 123456789; fprintf(' Professor: %s %s (ID: %d) \n', Name, Surname, ID); disp(' ------------------------------------------------ ') disp(' ') disp('Hit enter to proceed') pause clear all; close all; disp(' ---------- Solution to Question #1 ---------- ') disp(' ') disp('Q.1(a)') disp('The algorithm is available in the lecture note') disp(' ') fprintf('Q.1(b): convert to binary using conver_decimal\n') fprintf('Q.1(c):\n') res=convert_decimal(6789); fprintf('6789 to binary %s\n',res) fprintf('Q.1(d):\n') res=convert_decimal(191); fprintf('191 to binary %s\n',res) res=convert_decimal(396); fprintf('396 to binary %s\n',res) res=convert_decimal(3196); fprintf('3196 to binary %s\n',res) res=convert_decimal(9164); fprintf('9164 to binary %s\n',res) disp(' ') disp(' ------------------------------------------------ ') disp(' ') disp('Hit enter to proceed to next question') pause disp(' ---------- Solution to Question #2 ---------- ') disp(' ') fprintf('2(b): truncation error for x=0.5\n') x=0.5; fprintf('n=1, truncation error=%d\n',lon(x,1)) fprintf('n=2, truncation error=%d\n',lon(x,2)) fprintf('n=3, truncation error=%d\n',lon(x,3)) fprintf('n=4, truncation error=%d\n',lon(x,4)) disp(' ') fprintf('2(c): relative truncation error for x=0.5\n') x=0.5; fprintf('n=1, rel. truncation error=%d\n',lon(x,1)/log(1.0+x)) fprintf('digits=%d\n',floor(-log10(lon(x,1)/log(1.0+x)/0.5))) fprintf('n=2, rel. truncation error=%d\n',lon(x,2)/log(1.0+x)) fprintf('digits=%d\n',floor(-log10(lon(x,2)/log(1.0+x)/0.5))) fprintf('n=3, rel. truncation error=%d\n',lon(x,3)/log(1.0+x)) fprintf('digits=%d\n',floor(-log10(lon(x,3)/log(1.0+x)/0.5))) fprintf('n=4, rel. truncation error=%d\n',lon(x,4)/log(1.0+x)) fprintf('digits=%d\n',floor(-log10(lon(x,4)/log(1.0+x)/0.5))) fprintf('2(d): plot relative truncation error\n') x=linspace(0,1,32); err(1:length(x),1:4)=0; for i=1:length(x) err(i,1)=lon(x(i),1)/log(1.0+x(i)); err(i,2)=lon(x(i),2)/log(1.0+x(i)); err(i,3)=lon(x(i),3)/log(1.0+x(i)); err(i,4)=lon(x(i),4)/log(1.0+x(i)); end plot(x,err(:,1),'o-',x,err(:,2),'x-',x,err(:,3),'s-',x,err(:,4),'*') set(gca,'fontsize',18) xlabel('x');ylabel('error'); legend('n=1','n=2','n=3','n=4',2) disp(' ') disp('Hit enter to proceed to next question') pause disp(' ---------- Solution to Question #3 ---------- ') disp(' ') t=linspace(0,2*pi); % x versus t x=3*cos(2*t); plot_xy(t,x,1,'o-') xlabel('t');ylabel('x') disp('Hit enter to proceed to next plot') pause % y versus t y=5*sin(3*t); plot_xy(t,y,2,'x--') xlabel('t');ylabel('y') disp('Hit enter to proceed to next plot') pause % x versus y plot_xy(x,y,3,'d:') xlabel('x');ylabel('y') disp('Hit enter to proceed to next plot') pause xbound=[-3 3]; ybound=[-3,3]; plot_surf(xbound,ybound) disp('Hit enter to proceed to finish') pause function result=convert_decimal(n) %this function converts a given decimal number n to a binary %number. %Version 1.0 %The following algorithm uses less knowledge of matlab, and hence %requires more programming work. res(1:128)=0; i=1; p=n; res(i)=mod(p,2); while(p >0) i=i+1; if(mod(p,2)==1) p=(p-1)/2; else p=p/2; end res(i)=mod(p,2); end res=res(1:i-1); res=fliplr(res); result='000000000000000000000000000000000'; for(i=1:length(res)) result(i)=num2str(res(i)); end result=result(1:length(res)); function result=lon(x,n) sum=0; for i=1:n sum = sum + (x^i/i * (-1)^(i+1)); end err=(log(1.0+x)-sum);%/log(1.0+x); result=abs(err); function plot_xy(x,y,fign, opt) if(nargin<4) opt='o-'; end figure(fign);clf plot(x,y,opt) set(gca,'fontsize',18); grid on; axis tight; function plot_surf(xbound,ybound) x=linspace(xbound(1),xbound(2)); y=linspace(ybound(1),ybound(2)); [x,y]=meshgrid(x,y); z=(x.^2-y.^2).*exp(-x.^2-y.^2); surf(x,y,z) set(gca,'fontsize',16) shading interp; title('Example for surface plot') xlabel('x') ylabel('y') zlabel('z')