f

#### Cramer's Rule - linear equations

```I have three linear equations with 3 unknowns, say for example:

3x + 4y + 6z = 1;
x - 2y + 7z = 10;
2x + 3y - 9z = 15;

How do i solve this using matlab, is there any easiest way
to apply Cramer's Rule to Solve these equations ???

Any help would be appreciated !!
Thanks !
Ashwini

```
 0
vd.ashwini (373)
5/13/2008 6:50:18 AM
comp.soft-sys.matlab 211266 articles. 19 followers. lunamoonmoon (257) is leader.

7 Replies
1176 Views

Similar Articles

[PageSpeed] 11

```On May 13, 6:50=A0pm, "Ashwini Deshpande" <vd.ashw...@mathworks.com>
wrote:
> I have three linear equations with 3 unknowns, say for example:
>
> 3x + 4y + 6z =3D 1;
> x - 2y + 7z =3D 10;
> 2x + 3y - 9z =3D 15;
>
> How do i solve this using matlab, is there any easiest way
> to apply Cramer's Rule to Solve these equations ???
>
> Any help would be appreciated !!
> Thanks !
> Ashwini

If you're using Matlab, why would you even contemplate using Cramer's
Rule?
You would only use that if you wanted to solve it by hand - or you
needed to do it for homework.

If you genuinely want to solve the equations in Matlab, try: help
mldivide

```
 0
mulgor (3007)
5/13/2008 8:06:33 AM
```"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in
message <g0bdna\$rjm\$1@fred.mathworks.com>...
> I have three linear equations with 3 unknowns, say for
example:
>
> 3x + 4y + 6z = 1;
> x - 2y + 7z = 10;
> 2x + 3y - 9z = 15;
>
> How do i solve this using matlab, is there any easiest way
> to apply Cramer's Rule to Solve these equations ???
>
> Any help would be appreciated !!
> Thanks !
> Ashwini
>

Cramer's Rule:  It may be slow, but it's inaccurate!

James Tursa

```
 0
5/13/2008 8:26:04 AM
```NZTideMan wrote:
> On May 13, 6:50 pm, "Ashwini Deshpande" <vd.ashw...@mathworks.com>
> wrote:
>> I have three linear equations with 3 unknowns, say for example:
>>
>> 3x + 4y + 6z = 1;
>> x - 2y + 7z = 10;
>> 2x + 3y - 9z = 15;
>>
>> How do i solve this using matlab, is there any easiest way
>> to apply Cramer's Rule to Solve these equations ???
>>
>> Any help would be appreciated !!
>> Thanks !
>> Ashwini
>
> If you're using Matlab, why would you even contemplate using Cramer's
> Rule?
> You would only use that if you wanted to solve it by hand - or you
> needed to do it for homework.
>
> If you genuinely want to solve the equations in Matlab, try: help
> mldivide

Perhaps the OP meant "Is there any easier way than to apply Cramer's Rule?"
```
 0
dbell5608 (63)
5/14/2008 12:00:33 AM
```"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message
<g0bdna\$rjm\$1@fred.mathworks.com>...
> I have three linear equations with 3 unknowns, say for example:
>
> 3x + 4y + 6z = 1;
> x - 2y + 7z = 10;
> 2x + 3y - 9z = 15;
>
> How do i solve this using matlab, is there any easiest way
> to apply Cramer's Rule to Solve these equations ???
>
> Any help would be appreciated !!
> Thanks !
> Ashwini
-------------
It is true that using Cramer's Rule in matlab would in general constitute an
inefficient method of solving linear equations, when there are so many
superior algorithms available.  However, it must be said in defense of
Cramer's Rule that it remains a very useful tool in mathematics, both in
understanding the theory in linear algebra and also for doing certain symbolic
manipulations.  I personally have to resort to its use quite often in deriving
various formulas and the like.  It is only in the area of actual numerical
computation that other methods become preferable.

Roger Stafford

```
 0
5/14/2008 12:54:01 AM
```On May 14, 12:54=A0pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Ashwini Deshpande" <vd.ashw...@mathworks.com> wrote in message
>
> <g0bdna\$rj...@fred.mathworks.com>...> I have three linear equations with 3=
unknowns, say for example:
>
> > 3x + 4y + 6z =3D 1;
> > x - 2y + 7z =3D 10;
> > 2x + 3y - 9z =3D 15;
>
> > How do i solve this using matlab, is there any easiest way
> > to apply Cramer's Rule to Solve these equations ???
>
> > Any help would be appreciated !!
> > Thanks !
> > Ashwini
>
> -------------
> =A0 It is true that using Cramer's Rule in matlab would in general constit=
ute an
> inefficient method of solving linear equations, when there are so many
> superior algorithms available. =A0However, it must be said in defense of
> Cramer's Rule that it remains a very useful tool in mathematics, both in
> understanding the theory in linear algebra and also for doing certain symb=
olic
> manipulations. =A0I personally have to resort to its use quite often in de=
riving
> various formulas and the like. =A0It is only in the area of actual numeric=
al
> computation that other methods become preferable.
>
> Roger Stafford

I agree entirely with you Roger, but the OP quoted a numerical
example, not a symbolic one.

How large a matrix would you use Cramer's Rule on?  Back when I learnt
submitted as a background job) I could handle 3x3 no trouble and 4x4
with a bit of effort, but these days 2x2 would be my limit before
finding a better way.
```
 0
mulgor (3007)
5/14/2008 7:21:45 AM
```"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in
message <g0bdna\$rjm\$1@fred.mathworks.com>...
> I have three linear equations with 3 unknowns, say for
example:
>
> 3x + 4y + 6z = 1;
> x - 2y + 7z = 10;
> 2x + 3y - 9z = 15;
>
> How do i solve this using matlab, is there any easiest way
> to apply Cramer's Rule to Solve these equations ???
>
> Any help would be appreciated !!
> Thanks !
> Ashwini
>

If you want to learn why Cramer's Rule is not a very good
method numerically and why it is avoided, particularly for
large problems, consider the code below and try it for
inputs of 100, 200, 300, 400, 500, 600. The intermediate
numbers get huge (eventually blows up), the timings get very
long, and it is not as accurate as the built in MATLAB \
operator. Like I said, it's slow, but it's inaccurate.

James Tursa

------------------------------

function callcramer(m)
disp(' ');
A = rand(m,m);
b = rand(m,1);
disp('Timing backslash:');
tic
x = A\b;  % Backslash operator for solving A*x = b
toc
xc = cramersrule(A,b);
disp(['norm of backslash residual     = '
num2str(norm(A*x-b),4)]);
disp(['norm of Cramer''s Rule residual = '
num2str(norm(A*xc-b),4)]);
disp(' ');
return
end

function x = cramersrule(A,b)  % Demo of Cramer's Rule for
solving A*x = b
disp('Timing Cramer''s Rule:');
tic
[m n] = size(b);
z = zeros(m,1);
Ai = A;
for k=1:m
Ai(:,k) = b;
z(k) = det(Ai);
Ai(:,k) = A(:,k);
end
detA = det(A);
x = z / detA;
toc
disp(['Max abs(det(Ai)) = ' num2str(max(abs(z)),4)]);
disp(['abs(det(A))      = ' num2str(abs(detA),4)]);
return
end

```
 0
5/14/2008 7:53:01 AM
```"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in
message <g0e5ot\$96\$1@fred.mathworks.com>...
> "Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in
> message <g0bdna\$rjm\$1@fred.mathworks.com>...
> > I have three linear equations with 3 unknowns, say for
> example:
> >
> > 3x + 4y + 6z = 1;
> > x - 2y + 7z = 10;
> > 2x + 3y - 9z = 15;
> >
> > How do i solve this using matlab, is there any easiest way
> > to apply Cramer's Rule to Solve these equations ???
> >
> > Any help would be appreciated !!
> > Thanks !
> > Ashwini
> >
>
> If you want to learn why Cramer's Rule is not a very good
> method numerically and why it is avoided, particularly for
> large problems, consider the code below and try it for
> inputs of 100, 200, 300, 400, 500, 600. The intermediate
> numbers get huge (eventually blows up), the timings get very
> long, and it is not as accurate as the built in MATLAB \
> operator. Like I said, it's slow, but it's inaccurate.
>
> James Tursa
>
> ------------------------------
>
> function callcramer(m)
> disp(' ');
> A = rand(m,m);
> b = rand(m,1);
> disp('Timing backslash:');
> tic
> x = A\b;  % Backslash operator for solving A*x = b
> toc
> xc = cramersrule(A,b);
> disp(['norm of backslash residual     = '
> num2str(norm(A*x-b),4)]);
> disp(['norm of Cramer''s Rule residual = '
> num2str(norm(A*xc-b),4)]);
> disp(' ');
> return
> end
>
> function x = cramersrule(A,b)  % Demo of Cramer's Rule for
> solving A*x = b
> disp('Timing Cramer''s Rule:');
> tic
> [m n] = size(b);
> z = zeros(m,1);
> Ai = A;
> for k=1:m
>     Ai(:,k) = b;
>     z(k) = det(Ai);
>     Ai(:,k) = A(:,k);
> end
> detA = det(A);
> x = z / detA;
> toc
> disp(['Max abs(det(Ai)) = ' num2str(max(abs(z)),4)]);
> disp(['abs(det(A))      = ' num2str(abs(detA),4)]);
> return
> end
>

Thanks one and all ...
I solved my problem ..
All the replies were useful...

Ashwini

```
 0
vd.ashwini (373)
5/15/2008 5:41:02 AM