BIL 113E Int. to Sci. & Eng. Comp. (MATLAB) March 16, 2018 Answer the questions in the spaces provided. If you run out of room for an answer, continue on the back of the page. Name Surname: Student Number: 1. (a) The function given below can be used to encrypt a string. How would you call it in Matlab? function [ encryptStr ] = encrypt( originalStr, offset ) %encrypt - this function performs a simple encryption of a given string. %Each character is converted by the given offset in the ASCII table. (b) You wish to write a script that will analyze the following function: y= 2x2 cos(x) exp 0.1x Write the m-file, fun_x.m, required to define this as a MATLAB function. 2. We have made a function triangleArea(xvec,yvec) that can be used to find the area of a triangle. The help for the function is shown below. >> help triangleArea triangleArea (xvec, yvec) returns the area of a triangle. Argument is xvec, a vector containing the three x-coordinates of the triangle, and yvec, the three y-coordinates of the triangle. You have a triangle with the coordinates given by the figure below. Write a call to the function with correct parameters. 3. If matrix A is defined in MATLAB as A = [1,4,3,2 ; -3,4,-5,1 ; 5,6,1,2 ; -2,-3,5,6] What is the output of the commands? (a) A(1:3 , 2:3) BIL 113E Int. to Sci. & Eng. Comp. (MATLAB), Page 2 of 4 March 16, 2018 (b) A(1,:) (c) A(:,3) (d) C = A(2:3,:) 4. A student produced the following script, eqnsolver.m, in response to a programming assignment to create a script that would solve a set of linear equations given an augmented coefficient matrix file. 1. % script to solve linear set of equations 2. % A*x = b 3. % requires augmented coefficient matrix file 4. 5. header( ) 6. 7. % get augmented coefficient matrix, decompose 8. % to coefficient matrix, Amat, and right hand side, rhs 9. disp(’ ’) 10. fname = input(’Augmented coefficient matrix file name? ==> ’); 11. augmat = load(fname); 12. [Nrow, Ncol] = size(fname); 13. Amat = augmat(:,1:Nrow); 14. rhs = augmat(:,Ncol); 15. 16. % solve system 17. soln = Amat/rhs; 18. 19. % display results 20. disp(’ ’) 21. disp(The solution vector is) 22. disp(soln) However, the following messages appeared when the student attempted to run the script using a properly configured augmented coefficient matrix , called test.dat, as a test case. Your job is to (1) find the offending line of code and (2) show a fix to the error in the spaces provided below the message. (a) ??? Error: File: C:\temp\eqnsolver.m Line: 5 Column: 13 Expected a variable, function, or constant, found ")". (b) Augmented coefficient matrix file name? ==> test.dat ??? Undefined variable ’test’ or class ’test.dat’. BIL 113E Int. to Sci. & Eng. Comp. (MATLAB), Page 3 of 4 March 16, 2018 (c) ??? Index exceeds matrix dimensions. Error in ==> C:\temp\eqnsolver.m On line 14 ==> rhs = augmat(:,Ncol); (d) ??? Error using ==> / Matrix dimensions must agree. Error in ==> C:\temp\eqnsolver.m On line 17 ==> soln = Amat/rhs; (e) ??? Error: File: C:\temp\eqnsolver.m Line: 21 Column: 14 ")" expected, "identifier" found. (f) What will be displayed if help eqnsolver is entered at the command window prompt? Hint: A sample augmented matrix is defined as below a b e A= c d f which represents the following sets of equation. ax + by = e cx + dy = f 5. Given the following matrix, show the results generated by the MATLAB command. R = [1.22 3.78 2.41] fprintf(’R = %3.1f \n’, R) 6. Write the MATLAB commands to generate a table of conversions from knots to m/s. The range of knots should be from 0 to 30 in increments of 5. Note 1 knot = 0.5144 m/s. 7. Write a MATLAB program to prompt the user for a time constant tc and a max time tmax, then generate a plot of v = exp(−t/tc). Hint: The time interval is between 0 and tmax. BIL 113E Int. to Sci. & Eng. Comp. (MATLAB), Page 4 of 4 March 16, 2018 8. Consider the following MATLAB script. What are the values for a, b and c after it is run? a=3.2; b=10; c=2; if (c>(a+b)) a=b-c; else a=b^2; end b=a+(c^2); a b c 9. Use one if-statement to rewrite the following nested if-statement if w < x if w > y w = x*y end if end if 10. Given vectors x=[-1, 2, 3, -2], y=[0.2, 3.1, 0, -3] and z=[3, 0, 1, 0.1], provide answers to the following operations. (a) x < y > z (b) x + ~ y > z (c) x == y ~ =z Hint: ~ means logical not. An element of the output array is set to 1 if A contains a zero value element at that same array location. Otherwise, that element is set to 0.