Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a user-dened function named symmetric Pascal(n) that computes values in

ID: 3564858 • Letter: D

Question

Develop a user-dened function named symmetric Pascal(n) that computes values in an nn symmetric Pascal matrix. Test your newly developed function using n = 4 and report the output when n = 10. The only MATLAB built-in function you may use is zeros() to initialize your matrix. You may not use the factorial() built-in function for this exercise. Instead, build a user-dened function userDefinedFactorial() which models factorial() and is called in symmetric Pascal(n). HINT: Inserting elements into all entries of a matrix is usually done with nested for-loops

Develop a user-dened function named symmetric Pascal(n) that computes values in an n½n symmetric Pascal matrix. Test your newly developed function using n = 4 and report the output when n = 10. The only MATLAB½ built-in function you may use is zeros() to initialize your matrix. You may not use the factorial() built-in function for this exercise. Instead, build a user-dened function userDefinedFactorial() which models factorial() and is called in symmetric Pascal(n). HINT: Inserting elements into all entries of a matrix is usually done with nested for-loops

Explanation / Answer

Well the conventional way is to do it with while or for loops, however since matlab works so quick with matrices and vectors I decided to show you another approach. Treat i and j as matrices, and perform matrix operations on them. The variable 'i' will be a matrix of all the row numbers, and the variable 'j' will be the column numbers. The factorial function will perform the element by element factorial, so its a pretty simple problem. The program is below.

%Matlab Code:

%Prompt user for matrix size 'n'
n = input('Enter size of pascal matrix. ');

%Initialize vector from 1 to n. Use to compute i,j
a = 1:n;

%Create j (The column numbers)
j = ones(n,1)*a;

%Flip j to create i (The row numbers)
i = j';

%Calculate Pij = (i + j - 2)!/(i - 1)!(j - 1)!
p = factorial(i + j - 2)./(factorial(i - 1).*factorial(j - 1));

%Echo p
p