Question

In: Computer Science

Write a function in Matlab that takes as input the number n and a symmetric tridiagonal...

Write a function in Matlab that takes as input the number n and a symmetric tridiagonal matrix given as two vectors: n×1 vector v representing the main diagonal and (n−1)×1 vector w representing the upper diagonal. Have this function output the Cholesky factor of the matrix as a vector for the main diagonal and a vector for the upper diagonal and output the number of flops and, separately, the number of square roots used as well. Use only basic programming.

(a) Write out or print out your function.

(b) Run the case with v =2*ones(10,1), w = -ones(9,1) and write out or print out all your results.

(c) Rune the case with v =2*ones(100,1), w =-ones(99,1) and write out or print out your results just for the number of flops and square roots used. How many times more flops are used than in the previous case?

Solutions

Expert Solution

Part a) The Matlab function Cholesky_factor.m

function[V,W,flp,sqt]= Cholesky_factor(n,v,w)
    flp = 0;sqt = 0; % initially flp and sqt are zeros
    V(1) = sqrt(v(1));% First element C11
    sqt = 1; % found one sqrt above
    for i = 2:n % for the computation of elements 2 to n-1
        W(i-1) = w(i-1)/V(i-1); % one devision involved
        flp = flp +1; % for the devision
        V(i) = sqrt(v(i)-W(i-1)^2);
       flp = flp+1; % one substraction
       sqt = sqt+1; % one sqrt involved
    end
end

Part b)

>> v =2*ones(10,1);
>> w = -ones(9,1);
>> n =10;
>> [V,W,flp,sqt]= Cholesky_factor(n,v,w)

V =

    1.4142    1.2247    1.1547    1.1180    1.0954    1.0801    1.0690    1.0607    1.0541    1.0488


W =

   -0.7071   -0.8165   -0.8660   -0.8944   -0.9129   -0.9258   -0.9354   -0.9428   -0.9487


flp =

    18


sqt =

    10

>> A=diag(V)+diag(W,-1);
>> A*A'

ans =

    2.0000   -1.0000         0         0         0         0         0         0         0         0
   -1.0000    2.0000   -1.0000         0         0         0         0         0         0         0
         0   -1.0000    2.0000   -1.0000         0         0         0         0         0         0
         0         0   -1.0000    2.0000   -1.0000         0         0         0         0         0
         0         0         0   -1.0000    2.0000   -1.0000         0         0         0         0
         0         0         0         0   -1.0000    2.0000   -1.0000         0         0         0
         0         0         0         0         0   -1.0000    2.0000   -1.0000         0         0
         0         0         0         0         0         0   -1.0000    2.0000   -1.0000         0
         0         0         0         0         0         0         0   -1.0000    2.0000   -1.0000
         0         0         0         0         0         0         0         0   -1.0000    2.0000

Part c)

>> v =2*ones(100,1);
>> w =-ones(99,1);
>> n =100;
>> [V,W,flp,sqt]= Cholesky_factor(n,v,w);

flp =

   198


sqt =

   100

198-18 = 180 flops are more


Related Solutions

A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix...
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns a string containing the number word for the whole numbers 0 - 999. For example:  numberWords(234) would return 'two hundred thirty-four' If the input value is not a whole number between 0 - 999 then the function should return a string equivalent to 'ERROR'.
Write a MATLAB function that accepts input vector x and returns the number of local maximums...
Write a MATLAB function that accepts input vector x and returns the number of local maximums of x with value between xmin and xmax. Ask user to input values xmin and xmax at the beginning of the procedure. Use vector x (the vector x from that file consists of 1000 numbers ranging from 0.0044 to 0.67).
Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input...
Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input 3 positive integers. 2. Of the two integers a and b, the function returns the integer that has the most factors fact. 3. If both integers a and b have the same amount of factors fact, the function will return the larger integer. Test your function with the following: >> result=moreFactors(24,32,3) result = 24 (24 = 3^1 · 2^3 , 32 = 2^5 )...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT