% Exercise 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear x=2.5e-3 y=2.9e3 z=x/y format long z format short % answer % x = % 0.0025 % y = % 2900 % z = % 8.6207e-07 % z = % 8.620689655172414e-07 % input('press return to continue: ') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Exercise 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear t=0:0.2:5; y=exp(-sin(t)); plot(t,y,'r') hold on z=exp(-cos(t)); plot(t,z,'b+') set(gca,'FontName','Helvetica','FontSize',16) title('two functions (y and z)') xlabel('time') ylabel('function value') hold off input('press return to continue: ') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Exercise 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear [root1,root2]=quadratic(1,-3,2); disp('roots of quadratic are: ') disp([root1 root2]) [root1,root2]=quadratic(1,3,3); disp('roots of quadratic are: ') disp([root1 root2]) % roots of quadratic are: % 2 1 % % roots of quadratic are: % -1.5000 + 0.8660i -1.5000 - 0.8660i input('press return to continue: ') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % quadratic.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [root1,root2]=quadratic(a,b,c) % % a,b,c are input arguments % root1, root2 are returned by the function % and contain the roots of the quadratic % a x^2 + b x + c % temp = sqrt(b^2-4*a*c ); root1 = (-b + temp)/(2*a); root2 = (-b - temp)/(2*a); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Exercise 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % plot a circle, radius 3, centre (1,2) % theta=0:0.01:2*pi; radius=3; xcen=1; ycen=2; x=xcen+radius*cos(theta); y=ycen+radius*sin(theta); plot(x,y) axis equal % preserves correct shape %%%%%%%%%%%%%%%%% % % (a) plot the ellipse x^2/4+y^2/9=1 % theta=0:0.01:2*pi; x=2*cos(theta); y=3*sin(theta); plot(x,y) axis equal % (b) filled ellipse patch(x,y,'r') hold on plot(x,y,'r') % makes boundary of ellipse same colour as interior % (c) a square centred on the origin, side 1 x=[0.5 0.5 -0.5 -0.5 0.5]; y=[-0.5 0.5 0.5 -0.5 -0.5]; plot(x,y) axis equal % (d) regular hexagon, centred on the origin cx=1/2; cy=sqrt(3)/2; x=[cx 0 -cx -1 -cx 0 cx 1 cx]; y=[cy cy cy 0 -cy -cy -cy 0 cy]; plot(x,y) axis equal axis([-1.2 1.2 -1.2 1.2]) %leaves clear area around the figure %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Exercise 5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear t=[0:0.1:3]; % a vector, elements 0, 0.1, 0.2, ... constant = pi temp1 = size(t) % temp1 gives the size of the vector t temp2 = ones(temp1) % temp2 is a vector of ones, with the same % dimension as t temp3 = pi*temp2 % the vector has all elements with value pi plot(t,temp3) % in two lines: % t=[0:0.1:3]; % plot(t,pi*ones(size(t))) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Exercise 6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear f1 = factorial(5) f2 = factorial(1) f3 = factorial(0) f4 = factorial(-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % factorial.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function y = factorial(n) % % function returns y = n! % for integer n >= 0 % if n< 0 disp ( ' error in factorial.m: n is less than zero') y = []; elseif n == 0 y = 1; else y = 1; for j = 1:n % loop can run from 2 i.e. for j=2:n y = j*y; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Note that in fact there is a factorial function built in to MATLAB that is about 3 times faster than the code given above (it uses the in-built routine prod.m) It is also possible to code the factorial function recursively, but this is about 10 times slower than the version given above. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Exercise 7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % fibonacci.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function f = fibonacci(n) % % function returns the nth number in the Fibonacci sequence % if n == 0 f = 0; else y(1) = 1; y(2) = 1; for j=3:n y(j) = y(j-1)+y(j-2); end f = y(n); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Using the format long switch we find: fibonacci(39)/fibonacci(38) = 1.61803398874990 fibonacci(40)/fibonacci(39) = 1.61803398874989 fibonacci(41)/fibonacci(40) = 1.61803398874989 (1+sqrt(5))/2 = 1.61803398874989 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%