Answered step by step
Verified Expert Solution
Question
1 Approved Answer
WARNING: Most newer computers have more than one processor ( core ) . By default, Matlab will use all the processors available by assigning one
WARNING: Most newer computers have more than one processor core By
default, Matlab will use all the processors available by assigning one thread to
each available processor. This procedure will mess up our timings, so you should
tell Matlab to use only a single thread. To do this, you should start up Matlab with
the command line
matlab singleCompThread
If you do not know how to start up Matlab from the command line, use the
following command at the beginning of your session.
maxNumCompThreads;
Matlab will warn you that this command will disappear in the future, but it should
work fine now.
Exercise : The Matlab command invA computes an approximation for the
inverse of a matrix A Do not worry for now just how it does it but be
assured that it uses one of the most efficient and reliable methods available.
Matlab provides the commands tic and toc to measure computational time.
The tic command starts the stopwatch, and the toc command stops it either
printing the elapsed time or returning its value as in the
expression elapsedTimetoc;. The times are in seconds.
Note: tic and toc measure elapsed time. When a computer is doing more
than one thing, the cputime function can be used to measure how much
computer time is being used for this task alone.
Copy the following code to a Matlab function mfile
named exerm and modify it to produce information for the table
below. Be sure to add comments and your name and the date. Include
the file with your summary.
function elapsedTimeexern
elapsedTimeexern
comments
your name and the date
if modn
errorPlease use only odd values for n;
end
A magicn; only odd n yield invertible matrices
b onesn; the right side vector doesn't change the
time
tic;
Ainv compute the inverse matrix
xSolution compute the solution
elapsedTimetoc;
You have seen in lecture that the time required to invert
an matrix should be proportional to Fill in the following
table, where the column entitled ratio should contain the ratio of the
time for n divided by the time for the preceeding value of n
Note: timings are not precise! Furthermore, the first time a function
is used results in Matlab reading the mfile and compiling it and
that takes considerable time. The last row of this table may take
several minutes!
Remark: Compute the first line of the table twice and use the second
value. The first time a function is called involves substantial overhead
that later calls do not require.
Time to compute inverse matrices
n time ratio
Are these solution times roughly proportional to
Exercise : Matlab provides a special operator, the backslash operator,
that is designed to solve a linear system without computing the inverse. It is
used in the following way, for a matrix A and column vector b
xAb;
It may help you to remember this operator if you think of the A as being
underneath the slash. The effect of this operator is to find the solution of
the system of equations Axb
The backslash looks strange, but there is a method to this madness. You
might wonder why you can't just write xbA This would put the column
vector bto the left of the matrix A and that is the wrong order of operations.
Copy exerm to exerm and replace the inverse matrix computation
and solution with an expression using the Matlab command, and
fill in the following table
Time to compute solutions
n time ratio
Are these solution times roughly proportional to
Compare the times for the inverse and solution and fill in the
following table
Comparison of times
n time for inversetime for solution
Theory shows that computation of a matrix inverse should take
approximately three times as long as computation of the solution. Are
your results consistent with theory?
You should be getting the message: You should never compute an inverse
when all you want to do is solve a system.
Warning: the symbol in Matlab will work when the matrix A is not square. In
fact, sometimes it will work when A actually is a vector. The results are not usually
what you expect and no error message is given. Be careful of this potential for error
when using A similar warning is true for the division symbol because
Matlab will try to "nterpret' it if you use it with matrices or vectors and you will
get answers that are not what you intend.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started