How do I write the function C=solve(A)?
Ältere Kommentare anzeigen
function C=solve(A)
solves the equation Ax = b by using each of the three methods described below and gives the output vectors x1, x2, x3 for each of methods (1)-(3) respectively. These outputs should be placed into the matrix C = [x1 x2 x3]. An n n × matrix A will be the input. Vector b = randi (10, n, 1) has to be placed in the code and displayed (do not put semicolon after typing b = randi (10, n, 1) in your function).
Use the command format long (to display number in exponent format with 15 digit mantissas) within your function.
**The function C=solve(A) should also return vector N = [ n1; n2; n3] with n1 norm = (x1 – x2), n2 norm = (x2 – x3), n3 norm = (x3 – x1). Each entry of the vector N is the 2-norm (a square root of the sum of squares of the entries) of the vector of the difference between two solutions. The vector N gives an idea how different are the solutions obtained by the different methods.
(1) There is a special operator in MATLAB, \ ,called backslash that usually gives an excellent result. To use it, store A and b and type A\b. Backslash is the best of the methods. It works fast and minimizes a roundoff error. It also checks the condition number on the coefficient matrix. If the condition number is large, it will warn you by printing message: “Matrix is close to singular or badly scaled. Results may be inaccurate.”
(2) The other method is using inv which also checks the condition number, but calculating 1 A− requires a lot more arithmetic than backslash.
(3) It’s definitely not wise to use rref command to solve real word problems. This function was written to help students to learn linear algebra, so its algorithm is not accurate, and it will not warn you if your system is one of those for which it is hard to get an accurate solution. However, for the matrices with integer entries you could use the command rref( [A b]) to get the reduced echelon form of the augmented matrix and then read the solution from the last column.
Antworten (0)
Kategorien
Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!