clearvars format long disp('Solution to exercise problem'); fprintf('%s\n','Demonstration of tangents at a point'); % fprintf('%s\n','for the equation y=f(x)=x^2'); % f1=inline('x^2'); % str0=('Plot of y=f(x)=x^2'); % fprintf('%s\n','for the equation y=f(x)=10x'); % f1=inline('10*x'); % str0=('Plot of y=f(x)=10x'); % fprintf('%s\n','for the equation y=f(x)=1'); % f1=inline('1'); % str0=('Plot of y=f(x)=1'); % fprintf('%s\n','for the equation y=f(x)=x^2+x+1'); % f1=inline('x^2+x+1'); % str0=('Plot of y=f(x)=x^2+x+1'); fprintf('%s\n','for the equation y=f(x)=x^3-5x^2+6x+3'); f1=inline('x^3-5*x^2+6*x+3'); str0=('Plot of y=f(x)=x^3-5x^2+6x+3'); fprintf('%s\n\n','where slope m (at point a) = ( f(a+h) - f(a) ) / h'); x_value = input('At what point a is tangent desired (enter value from 0 to 12)?'); h=1; smallest_h=1/2048; while h>smallest_h slope=( f1(x_value+h)-f1(x_value) ) / h; fprintf('for h=%f : then ( f(%f+%f)-f(%f) )/ %f) = ( %f-%f)/ %f) = %f\n',... h,x_value,h,x_value,h,f1(x_value+h),f1(x_value),h,slope); h=h/2; end grid on xlabel('x coordinate','FontSize',10,'Color','k') ylabel('y coordinate','FontSize',10,'Color','b') set(gca,'YLim',[0 100],'YTick',0:5:100,'YGrid','on') set(gca,'XLim',[0 10],'XTick',0:1:10,'XGrid','on') hold on % axis equal; % quiver(0,0,3,0,'g','LineWidth',1.5); % quiver(0,0,0,3,'g','LineWidth',1.5); X1=0:.05:12; imax=length(X1); % X2=X1.^2; % X2=10*X1; % X2=X1./X1; % X2=X1.^2+X1+1; X2=X1.^3-5*X1.^2+6*X1+3; plot(X1,X2,'-r','LineWidth',2) str1=(''); str2=(''); str3=(''); % str3=sprintf('singular value of A=%f',s(1)); str=sprintf('%s\n',str0); % str=sprintf('%s\n %s\n %s\n %s\n',str0, str1, str2, str3); title(str); %print title with line breaks %print -djpeg lesson1_exercise5.jpg;