2) Create a Matlab function called tridfun.nthat solves the tridiagonal system o
ID: 3888944 • Letter: 2
Question
2) Create a Matlab function called tridfun.nthat solves the tridiagonal system of equations Au = k. It is important to understand tha h ll be both a main program tridmain.m and the function tridfun.n that the main program calls. To provide an example, suppose that A is the tridiagonal matrix 3 5 0 0 A=10573 and k is the vector 13 27 43 41 Then your main program tridnain. should closely resemble clear; n-4 a-[3 4 7 81 b-[5 6 3] c [1 5 3] k [13 27 43 41] u tridfun (n,a,b,c,k) Here the comrnand n-4 gives the size of the problem (A is 4 x 4, k is 4 × 1), vector a contains the diagonal entries of A, vector b contains the superdiagonal entries of A, and vector c contains the subdiagonal entries of A. Note that your code should not erplicitly form the matriz A. If the code in your function tridfun.m works correctly, the vector u should be calculated asExplanation / Answer
% First we are creating the function tridfun and we will save it as a script file with name tridfun.m
function [y] = tridfun(n, a, b, c, k)
% Adding a zero in the beginning of b vector to make it equal to size 1Xn
b = [0,b(1:end)];
% Adding a zero in the end of c vector to make it equal to size 1Xn
c=[c(1:end),0];
% Taking transpose of a, b and c to convert them into column vectors
a = a';
b = b';
c = c';
% Now, convert these columns into a sparse matrix
X = spdiags([c a b], -1:1, n, n);
% Finally, use backslash operator to get the final result and store it in vector y
y = k/X;
end
% Save the above function into tridfun.m file
% Now let's create tridmain.m file and call this function from there
n=4;
a=[3,4,7,8];
b=[5,6,3];
c=[1,5,3];
k=[13,27,43,41];
u = tridfun(n,a,b,c,k)
% output of the function will be available in the variable u
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.