270 likes | 400 Views
Problem set. Problem 1. function pos=mystrfind(s1,s2) %Two strings, s1 and s2 %pos denotes the starting position of the first occurrence of s1 in s2. http://134.208.26.61/course/MathProgramming/Lecture6/Lecture6.files/frame.htm. Page 17. Problem 2. function C=new_sudoku()
E N D
Problem 1 function pos=mystrfind(s1,s2) %Two strings, s1 and s2 %pos denotes the starting position of the first occurrence of s1 in s2 http://134.208.26.61/course/MathProgramming/Lecture6/Lecture6.files/frame.htm Page 17
Problem 2 function C=new_sudoku() % Create a 4x4 zero matrix, C % Randomly select four entries of C and fill them with four integers 1,2,3,4 respectively http://134.208.26.61/course/MathProgramming/Lecture7/Lecture7.files/frame.htm Page 2
Problem 3 function [errs]=row_validity(S) % S denotes a 4x4 Sudoku matrix filled with integers belonging {1,2,3,4} % errs counts the number of invalid rows % A row is valid only if it contains four different elements Lecture 7, page 6
Problem 4 function [errs]=column_validity(S) % S denotes a 4x4 Sudoku matrix filled with integers belonging {1,2,3,4} % errs counts the number of invalid column % A column is said valid only if it contains four different elements Lecture 7, page 7
Problem 4 function [errs]=block_validity(S) % S denotes a 4x4 Sudoku matrix filled with integers belonging {1,2,3,4} % errs counts the number of invalid blocks % A block is valid only if it contains four different elements Lecture 7, page 8
Determinant • A recursive rule for determination of the determinant of a square matrix Lecture 7, page 27
Problem 5 function B=Get_submatrix(A,i) % A is a nxn square matrix % i denotes a positive integer % B is a (n-1)x(n-1) square matrix formed by removing the first row and the ith column from A Lecture 7, page 30
Problem 6 function v=mydet(A) % A is a square matrix % v denotes the determinant of matrix A Lecture 7, page 33
Problem 7 function s=fib(n) % s denotes the output of a recursive function f(n) % f(0) = f(1)= 1 % f(n)=f(n-1)+f(n-2) if n > 1 http://134.208.26.61/course/MathProgramming/Lecture8/Lecture8.files/frame.htm Page 9
Stack • Three fields of a stack • A string: S • Size: n • Top position: top
Problem 8 Stack initialization function stk=stack_ini(n,s) % s is a string. n denotes a positive integer % stk represents a stack % It consists of three fields. % Set stk.S to s % Set stk.n to n % Set stk.top to zero Page 28
Problem 9 function stk=push(stk,c) % c stores a character % stk denotes a stack % Update three fields of stk to emulate pushing c to stk Page 29
Problem 10 function [stk,c]=pop(stk) % stk represents a stack % c is character popped from stk % Update three fields of stk to emulate popping an element from stk Page 31
Hanoi tower • Hanoi tower problem • Three stacks, A, B and C • Stack A contains n objects, from bottom to top respectively numbered with ‘n’ to ‘1’ • Move all objects from stack A to stack C • Valid move http://134.208.26.61/course/MathProgramming/Lecture9/Lecture9.files/frame.htm Page 15
Problem 11 function Hanoi(n) % Generate sequential moves for solving the Hanoi tower problem % n denotes the number of objects that need to be moved http://134.208.26.61/course/MathProgramming/Lecture10/Lecture10.files/frame.htm Page 2
Problem 12 function C=multiply(A,B) %Scalar codes for multiplication of two matrices A and B Page 11
Hyper-plane fitting Page 20
Problem 13 function [a,b]=Ce(x,y) % scalar codes for solving the normal equations Page 29
Problem 14 function D=mydistance(X) % Scalar codes for calculation of distances between n high-d points % X is a matrix with n rows, each representing a point in Rd % D is a matrix. D(i,j) stores the distance between points X(i,:) and X(j,:) Page 37
Problem 15 function D=mydistance(X) % vector codes for calculation of distances between n high-d points % X is a matrix with n rows, each representing a point in Rd % D is a matrix. D(i,j) stores the distance between points X(i,:) and X(j,:) http://134.208.26.61/course/MathProgramming/Lecture11/Lecture11.files/frame.htm Page 6
Problem 16 function [x,y]=fkin(p1,p2,l1,l2) % Forward kinematics of the two-link robot % p1 and p2 denote the two joint angles % (x,y) denotes the tool position Page 28
Problem 17 function [p1,p2]=inverse_kin(x,y,l1,l2) % Inverse kinematics of the two-link robot % p1 and p2 denote the two joint angles % (x,y) denotes the tool position Page 29
Problem 18 function y=eval_MLP(x,r,a,b,M) % MLP evaluation % x denotes a scalar input % y denotes the output of a network with parameters r,a and b % M denotes the number of joined hidden units http://134.208.26.61/course/MathProgramming/Lecture12/Lecture12a.files/frame.htm Page 11
Problem 19 function E=mean_square_error2(x,y,a,b,r) % mean square error Page 14
Problem 20 function demo_diff() % input a string to specify a scalar function % plot the specified function and its derivative Page 25
Reconstruction of a 2d function Pages 13-21