%% LAB 3 FUNCTIONS and FILES %% A function is created as in the following example % % function [x,y]=func(a,b) % % Simple function % x=a^b; % y=cos(b); % % a and b are the parameters (input data) of the function and x and y are % the output data. % Usually a function is stored in a separate m-file with the same name as % the function. % The first comment of the function is displayed if one types "help func" %-------------------------------------------------------------------------- %% Write the function in an m-file and execute it for a=1.5 and b=3.6 %-------------------------------------------------------------------------- %% Write a function that implements the expression y=sin(x²). Make sure % that it works also when x is a vector. %-------------------------------------------------------------------------- %% Write a function polyn that evaluates the polynomial % p(x)=c(1)+c(2)*x+c(3)*x^2+...+c(n+1)*x^n % The function should have two parameters: a vector c with the coefficients % and a scalar x. The output is the value of the polynomial for x. Make % sure that it functions also for x being a vector. Use your function to % plot the polynmial p(x)=3-2x+x² over the interval [0,2]. %-------------------------------------------------------------------------- %% Write a function that computes the square root of a non-negative number % a. Use the iteration % x(1)=1, x(n+1)==.5*(x(n)+a/x(n)), n=1,2,... % Terminate when |x(n)^2 - a| < tol. % Your function should have two parameters, a and tol. Check your function % using sqrt(a). %-------------------------------------------------------------------------- %% FILES % The functions "save" and "load" can be used to handle data stored on % disk. (help save, help load) % %-------------------------------------------------------------------------- %% Create a random 5x5 matrix with normally distributed entries (help randn), % and compute its SVD. Store the SVD in a file svda, clear the workspace % and check that it is empty of variables (help who, help clear). Load the % file and check that the variables are back in the workspace. %-------------------------------------------------------------------------- %% END