Problem #1. Set n 200 and generate an n × n matrix and two vectors in Rn (all ha
ID: 3587097 • Letter: P
Question
Problem #1. Set n 200 and generate an n × n matrix and two vectors in Rn (all having integer entries) via the following commands A = floor (10*rand(n)); b = sum(A,2); z = ones (n ,1); Since this matrix and these vectors are large, you want semicolons here to suppress the output. (a) The exact solution to Ax -b should be the vector z. Explain why. You can solve the linear system in MATLAB via either the backslash operation or by forming A-1b. What is the difference? Write a script to both time and compute the errors for each approach.1 Include the commands above and use tic toc as follows. tic; x = A; t1 = toc ; err1 max (abs (x-z)); disp(['time for backslash solve: ',num2str(t1)]) disp(['error for backslash solve: ',num2str(erri)]) tic; y inv (A) *b; t2 toc ; err2 = max (abs (y-z)); disp(['time for inverse A solve: ',num2str (t2)1) disp(['error for inverse A solve: ',num2str (err2)]) Which is faster and which has better error? Can you explain the results? (b) Repeat using n = 500 and n = 1000. Include your script, the output of your script, and comments on what you observe.Explanation / Answer
Solution:
Consider the data set n = 200 and generate a n x n matrix and two vectors in Rn
both having integer entries using MATLAB as follows:
>> n=200;
>> A=floor(10*rand(n));
>> b=sum(A')';
>> z=ones(n.1);
a)
(a) The exact solution of the system Ax = b should be the vector z because in the rows of the matrix b.
we add the elements of the corresponding rows of matrix A and so the solution is 11 x I matrix with all entries as 1.
Now, measure the elapsed time by using the following MATLAB commands:
Input: >> tic,x=A1b;toc
Output: Elapsed time is 0.783085 seconds.
Input: >> tic,y=inv(A)*b,toc
Output: Elapsed time is 0.084168 seconds.
From the time calculation, we observe that the method using "I" operation for the system
Ax = b is faster than the method of computing the inverse.
Now, compare the accuracy of the two methods by using the following code in Matlab:
Input: >> max(abs(x-z))
Output: ans = 3.3273e-13
Input: >>max(abs(y-z))
Output: ans = 2.0428e-12
From the above calculation, we can see that the accurate solutions are same.
(b)
For n = 500
>> n=500;
>> A=floor(10trand(n));
>> b=sum(A')';
>> z=ones(n.1);
We can measure the elapsed time by using the following Matlab command:
Input: >>tic,x=A1b;toc
Output: Elapsed time is 0.033196 seconds.
Input: >> tic,y=inv(A)*b;toc
Output: Elapsed time is 0.061206 seconds.
From the elapsed time calculations, observed that the method using "1" operation for the system Ax = b is faster than the method of computing the inverse.
Now. we compare the accuracy of the two methods by using the following code in Matlab:
Input: >> max(abs(x-z))
Output: ans =
6.2839e-13
Input: >> max(abs(y-z))
Output: ans =
3.4674e-12
From the above calculation. observed that the method using "" operation give more accurate solution than the method of computing the inverse
For n =1000
>> n=1000;
>> A=floor(10*rand(n));
>> b=sum(A')';
>> z=ones(n,1);
We can measure the elapsed time by using the following Matlab command:
Input: >> tic,x=A;toc
Output: Elapsed time is 0.143390 seconds.
Input: >> tic,y=inv(A)*b;toc
Output: Elapsed time is 0.293533 seconds.
From the elapsed time calculation, observed that the method using "' operation for the system Ax = b is faster than the method of computing the inverse.
Now. compare the accuracy of the two methods by using the following code in Matlab:
Input:
>> max(abs(x-z))
Output:
ans =
5.2336e-12
Input:
>> max(abs(y-z))
Output: ans =
3.5584e-11
From the above calculation. observe that the method using "" operation give more accurate solution than the method of computing the inverse
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.