Question

In: Advanced Math

Exercise 3 Part 1. Solving a system Ax = b **Create a function in MATLAB that...

Exercise 3
Part 1. Solving a system Ax = b **Create a function in MATLAB that begins with: function [C,N]=solvesys(A) [~,n]=size(A); b=fix(10*rand(n,1)) format long We are using format long to display a number in exponent format with 15 digit mantissas. If later on, you will need to switch back to the default format, type and run format The input is an matrix A. If A is invertible, the outputs are the matrix C, whose 3 columns x1, x2, x3 are the solutions obtained by the three methods described above, respectively, and N is the vector whose 3 entries are the 2-norms of the vectors of the differences between each two solutions.

**First, check if A is invertible. If A is not invertible, the function returns an empty matrix C=[ ] and an empty vector N=[ ]. Also, in this case, there are two possibilities for the system Ax = b: either “the system is inconsistent” or “the solution is not unique”. Use the command rank to check it on the two possible cases and program the two corresponding output messages. After that, terminate the program.

If A is invertible, the function solves the equation Ax = b using the three methods described above and gives the output vectors x1, x2, x3 for each of the methods (1)-(3), respectively. The vectors have to be the columns of the matrix C. Thus, we assign: C=[x1, x2, x3 ];

**The function [C,N]=solvesys(A) also returns a column vector N=[n1;n2;n3]; where n1=norm(x1-x2); n2=norm(x2-x3); n3=norm(x3-x1); The entries of the vector N are the 2-norms of the vectors of the differences between each two distinct solutions. Each entry is calculated by using a built-in MATLAB function norm, which is a square root of the sum of squares of the entries of the vector. The vector N gives an idea how “different” are the solutions obtained by various methods.

**Type the function solvesys in your Live Script.

**Run the function [C,N]=solvesys(A) for the following choices of the matrix A:

(a) A = magic(6); (b) A = magic(7); (c) A = eye(4); % Write a comment on the output for part (c) by comparing the solution with the vector b. (d) A = randi(20,4), (e) A = magic(3); (f) A = hilb(7)

Part 2. Condition numbers **Find the condition numbers of the matrices A=magic(7) and A=hilb(7) c1=cond(magic(7)) c2=cond(hilb(7))

% Compare c1 and c2 with number 1 and explain in your dairy file why the norms of the differences between the solutions for the coefficient matrix in part (f) are so big compared with the ones for the matrix in part (b).

**Explore the sensitivity of a badly conditioned matrix hilb(7): Input: A=hilb(7); Run the following: b=ones(7,1); x=A\b; b1=b+0.01; y=A\b1; norm(x-y) c3=rcond(A) %Using the output c3, which is the reciprocal condition number, explain why the system with the coefficient matrix hilb(7) is sensitive to perturbations.

**Re-run the code above for: A=magic(7);

%Comment on sensitivity to perturbations of magic(7) compared with hilb(7) by analyzing the corresponding outputs for the norm(x-y) and c3.

Solutions

Expert Solution

MATLAB CODE

1) file solvesys.m

function [C,N] = solvesys(A)
format long
%Storing the dimension of A in n and m
[n,m]=size(A);
%Calculating daterminant of A
magnitude=det(A);
%Calculating b
N=fix(10*rand(n,1))
%condition
if magnitude==0
disp('The system is either incosistent or the solution is not unique');
C=[0;0;0]
N=[0;0;0]
else
%Calculating x1,x2,x3

C=A\N
end
%calculating n1,n2,n3
n1=norm(C(1)-C(2));
n2=norm(C(2)-C(3));
n3=norm(C(3)-C(1));
N=[n1; n2; n3]
end

2) File tests.m to call solvesys function

clc;
clear all;
%declaring the values of matrix A
disp('For A=magic(6)');
A=magic(6);
solvesys(A)
disp('For A=eye(6)');
solvesys(eye(5))
disp('For A=randi(20,4,4)')
solvesys(randi(20,4,4))
disp('For A=magic(3)')
solvesys(magic(3))
disp('For A=hilb(6)');
format rat, A=hilb(6);
solvesys(A)

OUTPUT:

Thanks!


Related Solutions

Make the program(such as matlab) for solving the linear system Ax = b using the Naive-Gaussian...
Make the program(such as matlab) for solving the linear system Ax = b using the Naive-Gaussian elimination & also the Gaussian elimination with partial pivoting.
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
2. (Solving linear systems) Consider the linear system Ax = b with A =[14 9 14...
2. (Solving linear systems) Consider the linear system Ax = b with A =[14 9 14 6 -10;-11 -11 5 8 6;15 -2 -14 8 -15;14 13 11 -3 -7;0 9 13 5 -14], and . b = [-4;8;6;0;10]. a) Verify that the linear system has a unique solution. Hint: use rref, rank, det, or any other Matlab method. Briefly explain the answer please. You'll now solve Ax = b in three different ways. Store the three different solutions in...
Write a function to solve a system of linear equations of the form Ax= b using...
Write a function to solve a system of linear equations of the form Ax= b using the iterative Gauss-Seidel method. You are free to use any basic MATLAB operation to implement the algorithm (i.e. you may use any combination of loops, indexing, math, etc.), but avoid “built-in” solution methods — you would not be allowed to use the GaussSeidel function if such a function existed. The function must also test for a number of possible issues. If an issue is...
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). B) use...
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). B) use Gauss-siedel iteration. C)use SOR with w=1.25, w=1.5, w=1.75,w=1.9, and optimal value if given. * A=[4,8,0,0;8,18,2,0;0,2,5,1.5;0,0,1.5,1.75] , B=[8;18;0.5;-1.75]. , (optimal w is 1.634.)
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following...
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following problem using problem specifications: Plot x vs y when y=sin(x), y=cos(x), y=sin (2*x), and y=2*sin(x) when x = 1:0.1:10. Use 2 by 2 subplot, sin(x) is in location 1, cos(x) is in location 2, sin(2*x) is in location 3 and 2*sin(x) is in location 4. The plot should have: (1) x label = ‘x value’, y label = ‘y value’, legend ‘y=sin(x)’,’ y=cos(x)’,’ y=sin...
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). A)use Jacobi...
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). A)use Jacobi iteration. B) use Gauss-siedel iteration. 1) make sure to use SOR with w=1.25, w=1.5, w=1.75,w=1.9, and optimal value if given. * A=[1,-2,0,0;-2,5,-1,0;0,-1,2,-0.5;0,0,-0.5,1.25]] , B=[-3;5;2;3.5]. , (optimal w is 1.5431.)
If an SPL ( LINEAR EQUATION SYSTEM ) is known: Ax = b. A is a...
If an SPL ( LINEAR EQUATION SYSTEM ) is known: Ax = b. A is a matrix sized m × n and b is a vector sized m × 1, with the component values of matrix A and vector b known. The x vector is n × 1 and the component values are unknown. Explain how the possible solution of SPL Ax = b. i want answer for the question , and what he mean by (the component values of...
Part 2: MATLAB Exercise 1 (50 pts) Write a program that tells the user if a...
Part 2: MATLAB Exercise 1 (50 pts) Write a program that tells the user if a city of their choice is in a tropical, temperate, or polar region. Use the cities.txt file provided in the Files section of this class. Your program should read the data in that file, find the city given by the user, and return a text message with the information. If the city is not found, your program should write a message accordingly. To decide which...
1. Consider the cubic function f ( x ) = ax^3 + bx^2 + cx +...
1. Consider the cubic function f ( x ) = ax^3 + bx^2 + cx + d where a ≠ 0. Show that f can have zero, one, or two critical numbers and give an example of each case. 2. Use Rolle's Theorem to prove that if f ′ ( x ) = 0 for all xin an interval ( a , b ), then f is constant on ( a , b ). 3.True or False. The product of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT