Answered step by step
Verified Expert Solution
Link Copied!

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(1);
Matlab will warn you that this command will disappear in the future, but it should
work fine now.
Exercise 4: The Matlab command inv(A) 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 elapsedTime=toc;. 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.)
1. Copy the following code to a Matlab function m-file
named exer4.m 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.
2. function elapsedTime=exer4(n)
3.% elapsedTime=exer4(n)
4.% comments
5.
6.% your name and the date
7.
8. if mod(n,2)==0
9. error('Please use only odd values for n');
10. end
11.
12. A = magic(n); % only odd n yield invertible matrices
13. b = ones(n,1); % the right side vector doesn't change the
time
14. tic;
15. Ainv =???% compute the inverse matrix
16. xSolution =???% compute the solution
17. elapsedTime=toc;
18.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 m-file 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.
19. Time to compute inverse matrices
20. n time ratio
21.161_________
22.321__________________
23.641__________________
24.1281__________________
25.2561__________________
26.5121__________________
27.10241__________________
28.Are these solution times roughly proportional to ?
Exercise 5: 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.
x=A\b;
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 A*x=b.
The backslash looks strange, but there is a method to this madness. You
might wonder why you can't just write x=b/A. This would put the column
vector bto the left of the matrix A and that is the wrong order of operations.
1. Copy exer4.m to exer5.m and replace the inverse matrix computation
and solution with an expression using the Matlab ``\'' command, and
fill in the following table
2. Time to compute solutions
3. n time ratio
4.161_________
5.321__________________
6.641__________________
7.1281__________________
8.2561__________________
9.5121__________________
10.10241__________________
11.Are these solution times roughly proportional to ?
12.Compare the times for the inverse and solution and fill in the
following table
13. Comparison of times
14. n (time for inverse)/(time for solution)
15.161_________
16.321_________
17.641_________
18.1281_________
19.2561_________
20.5121_________
21.10241_________
22.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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions