This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Example Project
1. Deﬁne a Matlab function X=cramer(A,b) which solves the system A*X=b. You
may assume that A is a square (n × n) matrix, and that b is a column vector in
Rn .
2. Use your program to solve the system A*X=b with A=magic(7) and b=[1;2;3;4;5;6;7]. Solution
1. Cramer’s rule is a method for solving a nonsingular n × n system of linear equations
Ax = b. The method is as follows: For j = 1, 2, . . . , n deﬁne an n × n matrix
Cj by replacing the j th column of A by b. Thus if A = [a1 , a2 , . . . , an ], then
C2 = [a1 , b, a3 , . . . , an ]. The value of xj in the (unique) solution to Ax = b is then
the determinant of Cj divided by the determinant of A. This algorithm can be
implemented as follows.
function X=cramer(A,b)
%
% This function solves the system A*X=b using Cramer’s rule.
% A must be an n x n matrix, and b must be an n x 1 vector.
%
[m,n]=size(A);
d=det(A);
X=zeros(n,1);
for j=1:n,
% The matrix C is obtained by replacing
C=A;
% the jth column of A with b. The jth
C(:,j)=b;
% entry in the solution vector is equal
X(j,1)=det(C)/d;
% to det(C)/det(A).
end
2. We now apply our program to the given data.
>> A=magic(7)
A=
30
38
46
5
13
21
22 39
47
6
14
15
23
31 48
7
8
16
24
32
40 >> b=[1 2 3 4 5 6 7]’ 1
9
17
25
33
41
49 10
18
26
34
42
43
2 19
27
35
36
44
3
11 28
29
37
45
4
12
20 b=
1
2
3
4
5
6
7
>> cramer(A,b)
ans =
0.0050
0.0050
0.0050
0.1300
0.0050
0.0050
0.0050 ...
View
Full
Document
This note was uploaded on 12/27/2011 for the course MAS 3114 taught by Professor Olson during the Fall '08 term at University of Florida.
 Fall '08
 Olson

Click to edit the document details